Programming microcontroller using a bootloader

This post is about programming microcontroller using a bootloader firmware.

During the programming process of a microcontroller the application firmware has to be transferred, sooner or later, to the memory of the chip. This is normally done with a programmer device that burns the machine code to the program memory.

An optional way to program the application firmware onto the device is using a bootloader. The bootloader is a short program used to burn the firmware to the microcontroller without any programmer device, as it writes any data onto the flash memory of the device, provided to the serial interface. The process of burning the provided data to the program memory is controlled by the bootloader itself.


Usually the bootloader is located at the beginning of the memory area or at the very end of it and is the first program that will be executed after powering on the device. If the bootloader firmware detects the programming intention, it will take over the control and transfer the coding to the device. Otherwise it will hand over to the application firmware, jumping to the address which marks the entry point of the firmware. This entry point has to be defined in the coding of the bootloader. If the bootloader is located at the beginning of the program memory, the address for the interrupt routines has to be shifted as well in the same manner.

Obviously the bootloader firmware has to be transferred previously to the program memory of the chip before it can be used to program the application firmware onto the device. This has to be done with a conventional programmer as already mentioned above. You will find a variety of bootloader software for all kind of microcontroller in the internet.

Another issue which has to be considered is that the bootloader firmware and the application firmware are sharing the same memory space on the chip and therefore we have to ensure to not overwrite the bootloader while transferring the application coding to the program memory.

Programming PIC18 devices with bootloader AN1310 and HI-TECH C compiler


Recently I bought a microcontroller development board which is equipped with a PIC18F23K22 and the bootloader AN1310 provided by Microchip (see figure). The bootloader AN1310 provides an incremental bootloading option which will allow us to change only that memory blocks which have been modified since the last write operation.

To program the firmware I am using HI-TECH C Compiler. The development board will be connected to the PC via USB wire. For this purpose the board is equipped with the USB chip FT232R which provides the USB serial interface to communicate with the UART interface of the chip.

As the bootloader, installed on the chip of my development board, is located at the beginning of the program memory area, the starting address for the application firmware has to be shifted to an address outside of the memory region where the bootloader resides. In my case it is the memory position 0x400 which marks the beginning of the application. The target address for the interrupt routines will be shifted accordingly as the bootloader firmware will occupy the interrupt address as well. Where we normally expect the address for the interrupt routines we will find a pointer to the shifted interrupt address.


As the compiler creates an image with the machine code for the application, which always will be placed at address 0 we have to add an offset value to that address to ensure that the program will be placed in the memory starting with address 0x400. In addition we have to ensure as well the usable ROM range, excluding the memory areas where the bootloader is located to not overwrite that coding. For this purpose the HI-TECH C compiler provides the option CODEOFFSET which defines the entry point for the application coding and ROM which defines the usable ROM space on the chip. The image shows the build options to be adjusted in MPLAB IDE for the C Compiler. You will find these settings under project -> build options in MPLAB IDE.

Using the commandline you have to add the following options to the compiler frontend.


Regardless of which frontend is being used these settings will place the coding starting at address 0x400 and will use the default ROM range excluding the memory space between 0 and 3FF where we expect the bootloader firmware.


After connecting the development board to the USB port of the PC we can change into bootloader mode to be able to write the application to the memory of the chip. As shown in the figure the application firmware is being placed starting with address 0x400 (within the red square). The memory area before 0x400 is used by the bootloader and shouldn’t be overwritten. Actually the bootloader frontend marks that area in light blue.