I have been looking with great interest at boot loaders design in recent months. It all started with the “Graphics, Touch, Sound and USB” book I published last year, where I used Mikromedia boards as my primary hardware platform to illustrate how easy it was to adapt the Microchip Library of Applications (MLA) to fit a custom design. Those boards all come pre-loaded with a clever USB boot loader (based on the USB HID class). The fact that MikroElektronika provided a Windows-only application, motivated me to learn more about it and write a platform agnostic version of the Loader application (using Python) for all my Linux and Mac readers!
Later came a new 8-bit project. An Intelligent Power Supply module featuring the PIC16F178x family. Half way through the design of a basic firmware framework, I realised that without an I2C boot loader this project would have been very user unfriendly, so I set off to write one. In the process, I learned a lot about writing boot loaders using the XC8 compiler (more on this in a future post). Most importantly, I learned a lot about the challenge to make a boot loader fit in the smallest code-protection unit (512 words) available on a PIC16F micro controller.
Writing an I2C boot loader (in C) that uses less than 512 instructions is relatively hard. But if you know a little about USB, you might assume that writing a USB boot loader that fits in the same space is simply impossible.
So my interest was peaked when a friend pointed me to an Open Source project posted on GitHub by Matt Sarnoff:
- A USB – CDC boot loader for the PIC16F145x micro controller family that uses less than 512 words of memory: https://github.com/74hc595/PIC16F1-USB-Bootloader
Matt squeezed the USB stack code down using some tough assembly on the little PIC micro and wrote an equally short and elegant script in Python to provide a PC application (across all platforms). When in “boot mode” the micro appears on the USB bus as a CDC (serial port) device so that .HEX files can be passed to the micro controller ( with MPLAB XC8 use the –offset:200 option when compiling your apps for later use with the loader). I checked out the project on my Mac (running Yosemite) and used the USB Low Pin Count demo board (DM164127-2) to try my code. I am glad to report that it worked at first try!
This project inspired a few developers and shortly after, Peter Lawrence posted on GitHub his own variant offering a new twist to this game: a more secure USB DFU class bootloader:
- A USB – DFU boot loader for the PIC16F145x micro controller family using still less than 512 words of memory(!) https://github.com/majbthrd/PIC16F1-USB-DFU-Bootloader
Peter’s approach has the benefit of a stronger CRC protection/verification of the code being loaded but also implements the Device Firmware Update (DFU) specific USB class, which makes the little boot loader compatible with standard tools such as dfu_util . The only drawback is that now the original .hex file obtained by the standard MCHP toolchain has to be converted to a .dfu file before use with the loader application. Not a big deal, as Peter provides a convenient tool that performs such translation. I checked out this project as well, and I am glad to report that it worked flawlessly on my Mac running OS X Yosemite.
Thanks Matt, thanks Peter!