Boot Loader
Boot Loader
The PC software attempts to communicate with the PIC16 microcontrollers, however, have no software
bootloader using the bootload baud rate. If successful, Reset instruction, so the application jumps to the boot-
the PC receives the bootloader firmware revision and loader start-up vector at address, 0h. To avoid leaving
the PIC device information, shown earlier in Figure 4. unremovable return addresses on the call stack, jump-
ing to address 0h must be done only from the
application’s “main()” function.
Software Bootloader Re-Entry
The preceding procedure for manually executing the Hardware Bootloader Re-Entry
re-entry sequence can be cumbersome when making
many incremental application firmware changes during The software re-entry procedure is useful for getting
development. An easier alternative is to use the simple started, but is not recommended for robust operation.
software re-entry mechanism, given in the example Should the application code have bugs, the application
application firmware project. firmware could lock up and prevent automatic
bootloader re-entry for the next code change.
That mechanism is shown in Example 1.
Additionally, an actual framing error, triggered during
normal application serial communications, could
EXAMPLE 1: SOFTWARE BOOTLOADER
inadvertently cause unintended re-entry into Boot-
RE-ENTRY
loader mode. Then, the application could not be
while(1) restarted without user intervention.
{
if(PIR1bits.RCIF) For a more robust, hardware-based bootloader re-
{ entry, the serial port RTS signal can be wired to control
if(RCSTAbits.FERR && the PIC device’s MCLR Reset signal. This allows the
!PORTCbits.RC7) host PC software to automatically assert Break and
{ Reset signals, as described in “Manual Break and
// receiving BREAK Reset for Re-Entry”.
// state, soft reboot
// into Bootloader mode.
Reset();
}
}
(...)
}
10 k
J1 C2 0.1F
+
R1
1 5 U1
C2-
RXD 2 6 11
GND R3 VDD
TXD 3 7 RTS 13 1
MCLR#
4 8 D1
470
5 9 C5 0.1F
8 R2
26 RC7/RX
DB9
470
GND
11 25
RC6/TX
10
12
V SS
MAX3232 GND ®
PIC Device
J1 C2
+
R1
1 5
C2- U1
RXD 2 6 11
GND VDD
TXD 3 7 RTS 13 1 MCLR#
4 8
5 9 C5 0.1F
8
R2
26 RC7/RX
DB9
470
470
GND
R2
11 25 RC6/TX
10
12
V SS
®
Q1 PIC Device
2N7002
High-Speed Baud Rates The PIC device also will have limited steps between
available baud rates, dependent on the FOSC clock
The bootloader is capable of operating at baud rates of source used and the PIC device’s baud rate formula
up to 3 Mbps. Reliably achieving high-speed baud (see Table 3).
rates requires some considerations in the hardware
design. TABLE 3: PIC® DEVICE BAUD RATE
Traditionally, PCs’ serial ports only operate at standard FORMULAS
baud rates of up to 115.2 kbps. Manufacturers of USB- BRG16 BRGH PIC Baud Rate
to-serial converters may claim higher baud rates, but
some incorporate level translator circuits that limit 0 1 FOSC/[16 (BRG + 1)]
performance to baud rates of 500 kbps or less. 1 1 FOSC/[4 (BRG + 1)]
More reliable, high-speed operation may be provided
The bootloader firmware source code automatically
by a bare logic level, USB-to-serial converter circuit
uses the BRG16 = 1, BRGH = 1 mode on microcon-
wired directly to the PIC device without RS-232 level
troller devices that support it. This is the most flexible
translators.
mode for hitting the widest range of possible baud
Another hardware design consideration is matching the rates.
PIC device's clock source frequency to the baud rates
To avoid miscommunication, the PIC device and serial
available on the host serial port. At high speeds, USB-
port baud rates ideally should match within 3%. If a
to-serial converters may have limited steps between
CRC error is detected during communications with the
available baud rates.
bootloader firmware, the host PC application will halt
As an example, some of the currently available baud and display an error status.
rates for two USB-to-serial converters are shown in
Table 2. (For more information, consult Prolific Tech-
nology Inc. (PL2303), Future Technology Devices
International Limited (FT232BM) or your converter’s
data sheet.)
Memory
at the beginning of every packet. Once the baud rate is
successfully captured, it is locked in until the
EE Data Command
Interpreter bootloader command loop receives an invalid or
Memory
unexpected request.
Configuration Locking the baud rate allows high baud rate data to be
Registers received without losing data during slow auto-baud
Control calculations. When an invalid or unexpected request is
received, the auto-baud routine is run again.
When a valid request packet has been received, the When switching between baud rates, the bootloader
command interpreter evaluates the command number firmware can get locked at the wrong baud rate and the
in the packet to determine what operation needs to be incoming data stream may not trigger the auto-baud
done (such as Erase, Write, Read or Verify). The routine to be run again. In such situations, a MCLR
request is fulfilled and a response is returned through Reset is required to force the auto-baud routine to run.
the USART to either Acknowledge completion of the When RTS is wired to control MCLR, such situations
task, or on Read operations, to send back device can be handled automatically by the host PC
memory data. application asserting a MCLR Reset.
The host PC application is not allowed to send more
than one packet at a time. Each packet must be
Acknowledged before the next can be sent. This
maintains flow control.
For detailed bootloader protocol documentation, see
Appendix A: “Bootloader Protocol” on page 19.
Write Protection
To ensure that bootloader firmware is always available
for updating the device, it is wise to write-protect the
Application Firmware bootloader’s block of Flash memory space (the boot
block). This can be accomplished through software or
hardware.
Most devices, however, only provide write protection High Priority Interrupt Vector 0008 h
from the beginning of the Flash memory space. For Low Priority Interrupt Vector 0018 h
these devices, it will be necessary to locate the boot-
loader at address, 0h, in Flash memory. To do that, use
the #define BOOTLOADER_ADDRESS 0 option in the Bootloader Firmware
bootconfig.inc file.
Application Reset Vector 0400 h
When located at address, 0h, the bootloader occupies
the hardware Reset and interrupt vector addresses. Application Interrupt Vector 0408 h
For this layout to work, application firmware must Application Interrupt Vector 0418 h
provide remapped Reset and interrupt vectors. The
bootloader firmware will “pass-through” hardware
Reset and interrupt events to the application firmware
at the remapped addresses (see Figure 11).
Note: Some PIC devices provide configuration
Application Firmware
options that can write-protect application
firmware’s program memory regions.
Write-protected regions will not be
modifiable by the bootloader.
If the pre-existing contents of write- Write-Protected,
protected regions do not match the new “Boot Block” Area
data in the application firmware’s hex file,
write protection may prevent the boot- The bootconfig.inc file defines where the
loader write operation from completing bootloader firmware looks for the following application
successfully. vectors: AppVector, AppHighIntVector and AppLowInt
Vector. To optimize usage of Flash memory for a specific
application, these addresses can be adjusted. For now,
however, leave the addresses at their default values to
maintain compatibility with the example application
firmware projects.
There are no assembly language helper files, project build options or customized linker scripts for remapping application
vectors in Example 3. Everything is handled by the code.
At first glance, the C code for this project looks pretty The code for this project is normal application code.
similar to normal application code. However, some There is no need for an assembly language helper file,
slight changes are made: as required for PIC16 devices. Instead, only build
• An assembly language helper file (isr.as) is added options need to be configured.
to handle Reset and interrupt vector remapping.
1. Select Project>Build Options...>Project and go
This file should be copied into user’s projects and to the Linker tab menu.
can be customized with an alternative to the “ORG
The “Codeoffset” field has been set to 400.
0x400” Reset vector address. The 0x400 address
should match the bootloader AppVector setting. This address must be equal to the bootloader
• The Codeoffset option is reset to 404. defined AppVector address in order for the
HI-TECH C compiler to generate Reset and
The Codeoffset hex number must always be equal interrupt vector code further into Flash memory
to the application Reset vector plus four space than the default of address, 0h.
(AppVector + 4). This causes the HI-TECH C com-
piler to generate Reset and interrupt vector code 2. Go to the Global tab menu and review the ROM
further into Flash memory space than the default ranges field.
of address 0. This option has been set to prevent the
To verify this, open the project in MPLAB IDE, HI-TECH C compiler from using Flash memory
select Project>Build Options...>Project and go to space from address, 0h, through the end of the
the Linker tab menu. bootloader firmware.
• To prevent the HI-TECH C compiler from using Note: If the size of the bootloader firmware and
the RAM at address, 7Eh and 7Fh, the RAM write-protected boot block permits, the
ranges option is set to default,-7E-7F end address can be adjusted to free more
The bootloader uses access bank RAM at 7Eh and Flash memory for the application code.
7Fh to pass the PCLATH and WREG registers, For now, leave the end address as:
respectively, to the assembly language helper default,-0-3FF.
code in the file, isr.as. The address numbers
used must match the PCLATH_TEMP and On PIC18 devices, there is no need to exclude RAM
W_TEMP address definitions in the bootloader ranges. The PIC18 bootloader does not use any RAM
firmware source code. to save context data when passing through control to
your application’s interrupt vector code.
To verify this, open the project in MPLAB IDE,
select Project>Build Options...>Project and go to
the Global tab menu.
Note: Enhanced architecture PIC16F devices pro-
vide automatic hardware to save and restore
ISR context during interrupt handling. Thus,
it is possible to omit the “RAM ranges”
setting on enhanced core PIC16 devices.
• The ROM ranges option is set to prevent the
HI-TECH C compiler from using Flash memory
space from address, 0h, through the end of the
bootloader firmware.
Depending on the size of the bootloader firmware
and write-protected boot block, the end address to
be excluded may be changed to free more Flash
memory for application code.
To verify this, go to the same Global tab menu.
Software used for building the host PC software DEVELOPING THE WRITE PLAN
includes:
In the Write Plan process:
• Qt SDK 4.x – Lesser General Public License soft-
1. Regions of Flash memory space are added to
ware, used unmodified and dynamically linked
the Write List, aligned to the Flash write block
• QextSerialPort 1.2 – Public domain software, heavily boundaries. Any blocks of memory that are
modified for this application found to contain empty data (such as NOPs) are
• SQLite 3.6.x – Public domain software, used excluded from being added to the Write List.
unmodified 2. The Write List is copied to create the Erase List,
Using only open source and public domain develop- except that memory address regions are aligned
ment tools/libraries facilitates porting the software to to Flash erase block boundaries.
other operating system platforms, such as Linux®, or Using Erase and Write Lists makes it easy to re-
Macintosh®. As of this writing, the software is known to order the memory regions to be erased or
compile and work on Linux. No testing has been done written first, for fail-safe operation in case of
for Macintosh. interruption during a write operation.
On “J” family devices, where Configuration bits
Write Planning
are stored at the end of Flash memory, the Flash
The original bootloader application, documented in erase block with Configuration bits is scheduled
AN851, used a simple algorithm for writing new to be erased last. The Flash write block contain-
firmware: erase all memory and write all memory with ing the new Configuration bits is scheduled to be
new data. written immediately afterwards, as the first write
transaction.
With large memory devices, such as the PIC18F87J11,
erasing all of Flash memory can take almost four Scheduling Configuration bits’ erase/write
seconds, followed by the transferring of 128 Kbytes of minimizes the time that Configuration bits could
new Flash data at 115.2 kbps, which can take another be left blank on the device.
11 seconds or more, at lower baud rates. 3. Flash memory is erased in descending order by
When developing new application firmware, typically the erase block address.
the target PIC microcontroller has far more Flash This ensures that being interrupted will force the
memory than what is needed by application firmware. bootloader to stay in Bootloader mode on the
As a result, the earlier bootloader’s simple algorithm next Reset, rather than attempt starting partially
can become noticeably inefficient. erased application firmware.
This serial bootloader incorporates a different algo-
rithm, “write planning”. The application firmware hex file
is analyzed to produce two lists of memory regions:
Erase and Write. These lists form a “Write Plan” that
the software uses to issue erase and write commands
to the bootloader firmware kernel. For an example
Write Plan, see Table 4.
1. Add a single DEVICES record, using an SQL 3. Regenerate the binary devices.db database
insert statement like that shown in Example 4. file, used at run time by the host PC software:
• Go to the following command prompt path:
EXAMPLE 4: SQL INSERT STATEMENT
insert into DEVICES values ( C:\Microchip Solutions\Serial
161, -- Device ID (in decimal) Bootloader AN1310 vX.XX\Device Database
4, -- Family ID (2 for PIC16, 4 for
PIC18) • Run the following command:
'PIC18F8722',
2, -- Bytes Per Word (FLASH) sqlite3.exe -init devices.sql -batch
64, -- Write FLASH Block Size ..\devices.db .quit
64, -- Erase FLASH Page Size
'0x0', -- Start address of FLASH The updated devices.db file must be kept in the
'0x20000', -- End address of FLASH same folder as the host PC serial bootloader
'0xF00000', -- Start address of EEPROM executable file.
(use 0 if no EEPROM on your device)
'0xF00400', -- End address of EEPROM
'0x200000', -- Start address of User ID
Modifying the DEVICES.INC Include File
(use 0 if no User ID space on your device) To update the DEVICES.INC file for a new device:
'0x200008', -- End address of User ID
'0x300000', -- Start address of Config 1. In MPLAB IDE, open the bootloader firmware
Bits project file appropriate for the new device.
'0x30000E', -- End address of Config 2. Open the DEVICES.INC file.
Bits
'0x3FFFFE', -- Start address of Device
3. Add a new #ifdef block for the new device, as
Id shown in Example 6.
'0x400000', -- End address of Device Id The device numbers in the DEVICES.INC file
'0xFFE0', -- Device Id Mask (so that must match those used in the SQL database
Revision ID bits are ignored) script (Example 5).
'0x0', -- Start address of GPR
'0xF60' -- End address of GPR (tells the EXAMPLE 6: INCLUDE FILE ADDITION
PC software how big packets can be #ifdef __18F8722
-- without overflowing the #define DEVICEID .161
device buffer RAM) #define WRITE_FLASH_BLOCKSIZE .64
); #define ERASE_FLASH_BLOCKSIZE .64
#define END_FLASH 0x20000
2. If the new device uses configuration fuses, add #define END_GPR 0xF60
records to the CONFIGWORDS table for each #endif
Configuration Word, as shown in Example 5.
EXAMPLE 5: CONFIGWORDS ADDITIONS Simplifying Device Data Collection
insert into CONFIGWORDS values ( The device and family numbers required in the preced-
161, -- Device ID ing examples can be obtained from the devices’ data
4, -- Family ID
sheets, programming specifications and reference
'CONFIG1H', -- Config Name
3145729, -- Address
manuals. The data can be acquired more easily, how-
'0x37', -- Default Value ever, by using Microchip’s XML “essential device
'0xCF' -- Implemented Bits characteristics” files.
); These XML files, used to generate the MPLAB
processor-specific header files, MPLAB configuration
insert into CONFIGWORDS values (
screens and other items, are in a compressed file
161, -- Device ID
4, -- Family ID
stored on the host PC during the MPLAB IDE installa-
'CONFIG2L', -- Config Name tion. A “Device Database” utility program can generate
3145730, -- Address the SQL and include information for new devices.
'0xFF', -- Default Value
'0x1F' -- Implemented Bits
);
(...)
PIC18 Response
<STX> <BOOTBYTESL><BOOTBYTESH> <VERSIONL><VERSIONH> <COMMANDMASKH> <COMMANDMASKL:FAMILYID>
<STARTBOOTL><STARTBOOTH><STARTBOOTU><0x00> <CRCL><CRCH> <ETX>
PIC16 Response
<STX> <BOOTBYTESL><BOOTBYTESH> <VERSIONL><VERSIONH> <COMMANDMASKH> <COMMANDMASKL:FAMILYID>
<STARTBOOTL><STARTBOOTH><STARTBOOTU><0x00> <DEVICEIDL><DEVICEIDH> <CRCL><CRCH> <ETX>
Details
CRC for the request packet is, conveniently, always 0x0000 for this packet.
• VERSION defines the version number of the bootloader firmware.
• BOOTBYTES specifies the boot block size, in bytes.
• STARTBOOT specifies the starting Flash memory address for the boot block.
Together, STARTBOOT and BOOTBYTES are used in the host PC software to color the bootloader Flash memory
region turquoise.
• COMMANDMASK is currently unused on PIC18.
For PIC16, COMMANDMASKH will contain 0x01 if the device implements the erase Flash command. Otherwise, it
contains 0x00, signifying that the PIC16 device does automatic erases during writes (for example, PIC16F88X family).
• FAMILYID is a four-bit number in the least significant nibble, indicating what kind of microcontroller is in use.
Currently used family IDs are: 2 – PIC16, 4 – PIC18.
• DEVICEID is only transmitted for PIC16 devices. It is a compile-time value indicating exactly which PIC16FXXX
part number is being used.
PIC18 parts do not need to transmit the DEVICEID here because the host PC application can use the read Flash
memory command, below, to read the device ID from configuration memory at address 0x3FFFFE.
The host PC application uses the Family ID and Device ID to look up the correct device information in the essential
device characteristics database.
Response
<STX> [DATA…] <CRCL><CRCH> <ETX>
Details
• ADDRESS is the beginning Flash memory address from which to start reading.
• BYTES specifies the number of bytes to read.
Response
<STX>[<CRC1L><CRC1H>…<CRCnL><CRCnH>]<ETX>
Details
• ADDRESS is the beginning Flash memory address from which to start reading.
• BLOCKS specifies that number of 16-bit CRC words to generate. Each 16-bit CRC is generated on erase Flash
block size number of bytes.
Note that the response packet of this command does not provide a CRC of the data payload, which is a little different
from most of the packets. This allows the bootloader firmware to avoid having to calculate two different CRCs at
the same time, reducing memory use and processing time.
Response
<STX> <0x03> <CRCL><CRCH> <ETX>
Details
• ADDRESS is the last Flash memory address to start erasing from. Erases are performed in descending address
order, which is backwards from normal operations.
This feature can help the bootloader to fail more safely in some situations.
• PAGES specifies the number of erase Flash block size pages of Flash memory.
Response
<STX> <0x04> <CRCL><CRCH> <ETX>
Details
• ADDRESS is the Flash memory address to start writing to. Writes are performed in ascending address order.
• BLOCKS specifies the number of write Flash block size chunks of Flash memory to write.
Flash memory cells must be erased before writing can be performed on the same cells again.
Response
<STX> [DATA…] <CRCL><CRCH> <ETX>
Details
• ADDRESS is the beginning EEPROM address from which to start reading.
• BYTES specifies the number of bytes to read.
Microcontrollers that do not have EEPROM immediately send the response packet with a dummy payload of 0x05.
Write EEPROM
Request
<STX> <0x06> <ADDRESSL><ADDRESSH><0x00><0x00> <BYTESL><BYTESH> [<DATA>…] <CRCL><CRCH> <ETX>
Response
<STX> <0x06> <CRCL><CRCH> <ETX>
Details
• ADDRESS is the EEPROM address to start writing to. Writes are performed in ascending address order.
• BYTES specifies the number of bytes to write to EEPROM.
EEPROM does not require erasing before rewriting, thus, there is no erase command.
Microcontrollers that do not have EEPROM immediately send the response packet without performing any action.
Response
<STX> <0x07> <CRCL><CRCH> <ETX>
Details
• ADDRESS is the Configuration address to start writing to. Writes are performed in ascending address order.
• BYTES specifies the number of bytes to write.
Config bits do not require erasing before rewriting, so there is no erase command.
Response
None.
Details
This command makes the bootloader jump to the application firmware Reset vector. No response is returned because
the bootloader is no longer active once application firmware is started.
• Microchip believes that its family of products is one of the most secure families of its kind on the market today, when used in the
intended manner and under normal conditions.
• There are dishonest and possibly illegal methods used to breach the code protection feature. All of these methods, to our
knowledge, require using the Microchip products in a manner outside the operating specifications contained in Microchip’s Data
Sheets. Most likely, the person doing so is engaged in theft of intellectual property.
• Microchip is willing to work with the customer who is concerned about the integrity of their code.
• Neither Microchip nor any other semiconductor manufacturer can guarantee the security of their code. Code protection does not
mean that we are guaranteeing the product as “unbreakable.”
Code protection is constantly evolving. We at Microchip are committed to continuously improving the code protection features of our
products. Attempts to break Microchip’s code protection feature may be a violation of the Digital Millennium Copyright Act. If such acts
allow unauthorized access to your software or other copyrighted work, you may have a right to sue for relief under that Act.
01/05/10