Monday, February 9, 2015
picobootSTK500: an Arduino compatible atmega bootloader in 216 bytes
picoboot was the first bootloader I wrote. Although it hasn't been beat for size, the fact that it uses a proprietary protocol is a big drawback. I soon found out that despite Avrdude being open source, there is no open and public process for contributing to it. So I published a version of avrdude with picoboot support, but since I had emailed Joerg with my patches instead of posting to the avrdude mailing list, support for picoboot in the official avrdude release was not going to happen.
With Arduino Pro Mini boards available from China for only $2, there is no longer a significant cost advantage of using individual DIP AVR MCUs. Many of these boards come with the stock Arduino bootloader, which is 2KB. Optiboot was what I flashed to my pro mini boards to replace the stock bootloader. Since optiboot doesn't wait long after reset for a flash download, and my PL-2303hx USB-TTL modules don't break out the DTR line, I developed a zero-wire auto-reset for the boards. But since I couldn't get my patch added to avrdude, it would be hard for other people to use it.
My solution was to write a tiny bootloader that, like Optiboot, is compatible with the Arduino bootloader already supported by avrdude. Like picoboot, I decided to write it in AVR assembler. Optiboot is small enough to fit into the 512-byte minimum boot size of a mega328, but too big for the 256-byte minimum bootsize of a mega168. Another design goal was for the bootloader to wait indefinitely, to avoid having to juggle the timing of pressing the reset button and uploading code to flash. I didn't want to use the watchdog reset the way Optiboot does, since it makes it impossible for application code to detect a watchdog reset. Lastly, if space allowed, I wanted to support eeprom writing (Optiboot only supports flash writes).
After a few months of on-and-off development, I have a working release. There's no eeprom write support yet, but at 216 bytes of code, I still have 40 bytes to use for eeprom support and still have it fit in 256 bytes. Besides being the smallest arduino-compatible bootloader, it has a unique reset-handling functionality. Pressing the reset button once will go into bootloader mode, waiting with the LED dimly lit. Pressing the reset button a second time will do a normal reset. Due to delayed re-enabling of the read-while-write (RWW) section, it should be slightly faster than Optiboot. Like my original picoboot bootloader, a basic blink program is included with picobootSTK500.
I have included two pre-built versions in the repository; one for the mega328 and another for the mega168. I only have mega328's for testing, so I'd appreciate someone with a mega168 testing it out and dropping a note in the comments. Lastly I'd like to thank Optiboot maintainer Bill Westfield for his help. He is one of those pleasant programmers where his abilities exceed his ego - something I may yet achieve in the future. :-)