Bootloaders

The traditional methods of programming a PIC microcontroller are well known. You either plug it in a programmer, program it in-circuit (ICSP) with a high-voltage programmer, or program it in-circuit with low voltage (LV-ICSP). All of these, except maybe the last one, are very impractical in prototyping, R&D and learning environments. They require either removing the chip from the circuit, switching at least 2 lines (Vcc and Vpp for ICSP) or loosing a pin for LV-ICSP.Modern microicontrollers have lots of programming space (Generally, one can never have enough!). And most of them have two very useful features – RESET instruction, and the ability to write to their own program memory under software control. We can all use these to our benefit and implement a bootloader. It is a small resident program, located either in the beginning or the end of the user program memory. Every time the PIC is rebooted, this program takes control and waits for a predefined time (2 seconds in case of Colt) to hear from a sister application on a PC, thru the serial port. If there is no response on the USART, the bootloader releases control back to the user firmware and the controller continues working as required and programmed by the user. But if the sister application on a PC responds to the bootloader’s calls, something beautifully elegant happens – new firmware gets downloaded and programmed into the controller memory. After that a RESET command is issued and after waiting for another 2 seconds, the PIC gets to work executing your new code. This means, that all you have to do is write the code, compile it, press “Program” on the PC application and press the reset button on the controller. Your new code will be downloaded and executed – no switching, just 3-4 seconds of waiting. This is bootloading. It is simple and will work equally well on the workbench and over 10000 kilometers across the world. You just need to include a RESET instruction in your code, which will allow you to reset the board remotely.
The RCi1000 and Colt bootloader
Colt is located in the official PIC18F4620 boot sector – the first 2kb of the user memory. It spans up to address 0x800. This means you have to relocate your code to start from this address. In JAL this is accomplished with a pragma bootloader loader18 command. In Flow Code you have to add a -rb 0x800 to the Linker / Assembler parameters in the Compiler Options. There is one more important thing. Colt uses the last location of the microcontroller’s EEPROM to store a special value. This means, that your code should never write to this location. Normally the sister application on the PC will write a value of “25” every time you bootload.

People have asked me which bootloader HEX file to use for the RCi1000. If you want to use a bootloader, you can order the board preloaded, but sometimes user code might corrupt the bootloader code. In this case, you can use any of the 2kb Boot Block HEX files (bootload2k_e.hex, bootloadint_e.hex, bootload2kuart2.hex) , but you have to make the following adjustments in your programming software (we use ICSP and the great PICPgm Programmer) – select a HSPLL (High Speed oscillator with PLL x4 multiplication) setting for the oscillator (this will multiply the 10MHz XTAL frequency to 40MHz), disable the WRTB bit (making it FALSE or “0” will actually enable the writing protection of the boot sector), disable the Watchdog by WDTEN bit, disable LVP and write by hand a value of “25” to the last byte of the EEPROM. This will complete the configuration of your microcontroller.  Later on you can skip writing to the configuration when using Colt – you just have to deselect the Program Config option. Select Reload before Program and Reset after Program and you are good to go! Happy bootloading!