Could one chip program a bootloader into a 2nd?

So I take it that it isn't easy to program a usb bootloader into a blank chip without exactly the right hardware.

Would it be possible to set up a sketch on a good chip and wire it to a socket so you could put a blank chip in and have the good chip simulate a PC with a parallel programmer?

On the surface it seems like it should be possible. The sketch would probably have to contain a copy of the bootloader somehow.

It would be very nice if it worked. You could even make up a programming shield so you wouldn't have to dedicate an Arduino to it. When you needed to program a blank chip, just upload the sketch to an Arduino, turn it off, plug in the programming shield with a blank chip in the socket, power up and wait for it to finish.

Would this be feasible?

It's feasible, but you couldn't use the Arduino bootloader to do it. The Arduino bootloader uses program-write instructions to allow the chip to program its own flash (LPM and SPM). If you want a chip to program another chip, you need to use the proper SPI commands while the other chip is held reset. What you'd want is a sketch that makes your mega168 emulate an ICSP such as AVR ISP, and then connect that mega168 to another's SPI and reset lines.

For example, the Orangutan USB programmer is just a USB-to-serial chip and an ATmega168 with firmware that lets it emulate an AVR ISP (v2), so programming software such as that used by the Arduino IDE will recognize it as an ICSP and use it to program. It decodes the incoming avrispv2 serial packets and converts those into the appropriate SPI command packets, as detailed in the mega168 datasheet.

  • Ben

So the ATmega168 is programmed serially even for the bootloader? I guess I figured it was parallel for some reason.

Also, I wasn't envisioning a full programmer that could program anything. I was just thinking about something that could burn the USB bootloader. So if I understand correctly it could be done in a similar way to the Orangutan USB programmer, but it would not connect to a computer. It would just emulate an AVR ISP that is particularly dumb in that it always sends the same SPI command packets every time it is turned on. I.E. the command packets that would constitute burning a USB bootloader. And obviously it would handle the reset line for the target ATmega168 correctly.

So the next obvious question is would this be any easier or cheaper than buying a commercial programmer cable and getting it to work on your desktop computer. It might not be. I have read a few threads from people trying to get their homebrew programming cable to work on this desktop OS or that, so that made me wonder if it would be simpler to eliminate the desktop computer from the equation. At least for burning bootloaders. Obviously you would still need to be able to get the sketch loaded into the host ATmega168 to start with, but that seems like a much easier task.

So if it could be boiled down to a shield and a sketch, then you could buy cheap blank ATmega168's from anywhere you like and pop them in to this one at a time and result in chips with bootloaders.

So the ATmega168 is programmed serially even for the bootloader? I guess I figured it was parallel for some reason.

Yeah, the method by which a chip programs itself from the bootloader is different from the method by which a chip is programmed by something else.

Also, I wasn't envisioning a full programmer that could program anything. I was just thinking about something that could burn the USB bootloader. So if I understand correctly it could be done in a similar way to the Orangutan USB programmer, but it would not connect to a computer. It would just emulate an AVR ISP that is particularly dumb in that it always sends the same SPI command packets every time it is turned on. I.E. the command packets that would constitute burning a USB bootloader. And obviously it would handle the reset line for the target ATmega168 correctly.

This would be much easier than writing a full programmer, because you wouldn't have to worry about receiving and decoding the AVR ISP serial packets that the computer would send you. I guess I would recommend that you burn the Arduino bootloader onto a chip and then use that bootloader to load a sketch that is designed to program a target via SPI with the bytes it gets by reading its own bootloader flash section. The mega168 datasheet section on serial programming would give you all the information you need to do this. I recommend you have a few mega168s on hand for testing purposes as it becomes exceedingly easy to incapacitate them if you program them incorrectly (if the wrong fuses get accidentally set, it can be very hard to get them back in working order).

So the next obvious question is would this be any easier or cheaper than buying a commercial programmer cable and getting it to work on your desktop computer.

In my experience it has never really been difficult to get a commercial programmer working. The cost for the device would probably be cheaper since it would basically just be a single mega168, although the shield you propose would probably have a relatively large PCB, but it would also be a lot less functional than a commercial programmer (for example, the Orangutan programmer I linked before can program most AVRs with pretty much anything, including setting fuses and lock bits, and it can also serve as a simple USB-to-serial adapter). So I imagine it would be a case of paying less to get less (but perhaps something that is easier to use). At any rate, it could be a fun project for you.

  • Ben

Would it be possible to set up a sketch on a good chip and wire it to a socket so you could put a blank chip in and have the good chip simulate a PC with a parallel programmer?

Yes. Start by looking at: Arduino Playground - Boot-Cloner

You could also look at the links in the "As an 'In System Programmer'" section of: Arduino (The site is probably still down so check the Yahoo cache--it seems to have dropped out of the Google cache but the Yahoo cache seems difficult to link directly.)

--Phil

Thanks for all the info! This sounds like it would be a fun and educational project. I'll add it to my list.

If anyone else wants to try it and gets to it faster than me, then by all means steal this idea and let us know if it works. :slight_smile:

From what I read on the Boot Cloner playground page, you can almost write anything on the target chip, as long as it can be stored in the Flash Memory (16k). In addition, it can write on any Atmel chip that has the ISP functionnality, you "just" have to change some variables & maybe tweak a bit with the registers adresses & some functions.
Maybe a dumb question : as ICSP is an "evolution" of ISP, don't you think you can program another chip than an Atmel (a PIC16Fxx for exemple) using the same kind of technique ?
Another one : with an external flash memory, isn't it possible to write larger programs on the target chips ? Using a first sketch to write the flash (maybe with some kind of USB transfer) then another one to read the flash and burn it onto the target ? Maybe without even using the first sketch and directly writing from a Serial transfert ?
That's just questions, I don't think I'm able right now to do those kind of fancy tricks ^^