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
positioned 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 positioned at the end of
memory. The reset vector will still be needed, so
this will mean shifting the downloaded program's
reset vector elsewhere.
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
elsewhere. Figure B shows how the reset vector
now points to the bootloader code and the
downloaded program's reset vector will be
positioned to an address just before the
bootloader.
• On 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 addresses
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
at www.htsoft.com/support/forums.php
Design Tip: Give Your Project the Boot!
Page 2
The diagram above shows the memory map of a
program with and without the bootloader installed.
HI-TECH Software Booth Number: 411
350 leading companies showcasing cutting edge hardware,
software, tools, and the full spectrum of systems components.
Stop by HI-TECH’s booth in the exhibits area to learn more about
our industrial-strength software development tools and C compilers.
For more information on this event, go to www.esconline.com/sf
How it Works: