Chinese Version

Give Your Project the Boot!

With flash memory becoming more readily available in microcontrollers, the possibility of software updates in applications can now be more easily realized. This article is a brief outline on how to develop a bootloader for a Microchip PIC processor (16F87x). The same procedure, however, could be applied to many other types of processors (8051, ARM, MSP430, etc.).

Before any coding starts, you should first think about how things are going to be positioned in program memory. Since the bootloader needs control after reset, it will need to use the reset vector. Since it would be nice to make it easy to build bootloader compatible programs, it is best if the bootloader is positioned entirely at the start of memory, or, all at the end of memory. On mid-range PICs, the regular interrupt vector is located at address 0x4. If the bootloader was going to be posistioned entirely at the start of memory, it would then have to redirect the interrupt vector to the downloaded program. Since this would add complexity to the code and latency to the vector, the bootloader will be posistioned at the end of memory. The reset vector will still be needed, so this will mean shifting the downloaded program's reset vector elsewhere.

The diagram above shows the memory map of a program with and without the bootloader installed. In Figure A, typically the reset vector contains instructions to jump into the main program. As the bootloader needs the reset vector, you must move the downloaded program's reset vector now points to the bootloader code and the downloaded to an address just before the bootlaoder.

How it works:

  • One reset the bootloader will take control and prompt the user to send a hex file. It will wait here (with count down) for a configurable number of seconds.
  • If no hex file is sent, the bootloader will assume that no update is required and it will jump to the redirected reset vector which in turn will run the previously downloaded program. If no program has ever been downloaded, the redirected reset vector will simply contain a jump to the beginning of the bootloader and the whole process will start again.
  • If a HEX file is sent to the bootloader within the count down period, it will start interpreting the data and writing it to program memory. The bootloader will look for addresses less than 0x4 (reset vector) and will instead write this elsewhere. It will also ignore addressed conflicting with those used by the bootloader, thus protecting itself from being overwritten.

This bootloader is suitable for the following processors: 16F870, 16F871, 16F873, 16F873A, 16F874, 16F874A, 16F876, 16F876A, 16F877, 16F877A. All source code, documentation and a sample download program is included with the latest version of the compiler. There is also further information on HI-TECH Software's online forums.


 

The 10th Floor, Quantum Plaza, No. 27, Zhichun Road, Haidian District, Beijing, 100083, P.R. China
Tel: +86-10-82357579, 82357580, 82357576

Fax: +86-10-82357574

info@mcu-world.com