BOOTLOAD is a bootloader designed for the PIC16F874/877 microcontroller made by Microchip Inc. A bootloader is a program whose purpose is to load and run other programs. In a computer this task is performed by an operating system, but in a microcontroller such complexity is usually unnecessary, and often impossible.
BOOTLOAD permanently occupies 800 bytes of the processor's read-only memory space, but once user code begins execution, there are no other restrictions on the processor's resources.
BOOTLOAD will load data to the processor EEPROM if the data is sent to "pseudo-addresses" in the range 0x2100-0x21FF, provided that code protection for EEPROM is disabled.
The PIC series components are true single-chip processors, or components which execute code stored in internal memory. Normally, a processor of this type is programmed "off-line" in a special fixture which often requires voltages which are not needed for normal operation. In a development environment this process is cumbersome and presents risks in the form of bent leads and static discharges, and it precludes soldered-in components. Alternatively, the components may be programmable via a special interface involving a cable to a computer and some amount of interface circuitry. This is an improvement over fixture-based programming, but it still requires added hardware which becomes redundant once new software has been loaded. The PIC16F874/877 devices, however, have the ability to "program themselves" by allowing the user to change (under software control) the internal flash memory in which code is stored. The bootloader is the program which makes this happen.
The process of loading new code requires communication between the processor and the computer on which the code is stored. BOOTLOAD accomplishes this via a serial port. If the finished product requires a serial port also, no extra hardware is needed. If the serial port is not needed after programming, the level-shifting components needed to turn RS-232 (from the computer) to logic levels (at the processor) can be built in an off-board pod which would connect to the processor via a 3-pin plug.
BOOTLOAD is programmed, by a conventional method, into the top words of processor memory, and into the lowest 3 words. Once programmed, the code should not be changed. When execution begins, a jump to the main body of the program occurs. BOOTLOAD then makes a decision about whether valid user code has been loaded. If it determines that valid code has been installed, execution vectors to the user code's starting address after 5 seconds of inactivity on the serial port, and BOOTLOAD plays no further role. If no valid code has been loaded, BOOTLOAD maintains control indefinitely while waiting for new software to arrive. If characters arrive on the serial port while BOOTLOAD has control, BOOTLOAD will (assuming the arriving data is correctly formatted) place it in flash memory at the appropriate addresses. When the user's code is entirely loaded, and if the new code passes all tests, BOOTLOAD will hand control over to the newly-loaded program.
It should be noted that software cannot change the processorís configuration word, or "fuse data". If fixture-based programming is to be done only once, then the configuration word must be programmed at the same time that BOOTLOAD itself is installed. On the other hand, Microchip Inc makes a strong recommendation to load configuration data along with user code in its "EEPROM Memory Programming Specification", Document DS39025B. This recommendation is based on the expectation that the chip will be programmed in a fixture where the configuration word can be set at the same time as installation of the user (i.e. final) code. If the "user code" will be programmed via software after the fixture-based programming stage (when BOOTLOAD is loaded to the chip), then the configuration word may not match the requirements of the user code. There is no solution to this problem, given the design of the processor. For this reason, decisions regarding the type of setup needed in the final product must be made at the time when BOOTLOAD, not the user code, is installed. BOOTLOAD includes the following fuse settings:
If changes in the configuration word are needed, then BOOTLOAD must be modified, recompiled and reloaded, or the chip must be reprogrammed with the correct fuse settings after BOOTLOAD has been installed, but without changing BOOTLOAD.
Normally, BOOTLOAD will not allow itself to be overwritten, but if user code (rather than BOOTLOAD itself) writes to flash memory, BOOTLOAD is vulnerable to damage.
The following tests are applied to data sent to BOOTLOAD:
8-bit hex code is similar to "Intel hex code", originally used for 8-bit processors, but modified to allow the same format to be used for processors such as the PIC series where the memory word is larger than 8 bits. For every memory word in the user's program, two bytes are taken from the file. The order of storage in the file is low byte first, which is opposite to the order in which a 16-bit number would be written as text. Because the PIC components have 14-bit-wide code memory, the two highest bits of each word will always be 0. BOOTLOAD requires each line from the hex file to be sent exactly as stored, as ASCII characters in the same sequence.
Every line of code stored in the incoming hex file must start with a colon, must end with a correct checksum at the correct point in the line, and must contain no characters (except the leading colon) outside the range 0-9 and A-F or a-f. Once a valid line has been completed, all characters intervening before the next colon are ignored. Carriage-return and line-feed characters are redundant and can be suppressed by the computer.
A companion program must run on the computer to load data from the hex file to the serial port. The serial port should run at 19200 baud, 8 data bits, no parity and 1 or more stop bits. For every valid line of code received, the processor will return a checksum to the computer which is the POSITIVE 8-BIT SUM of all data actually written to memory, treating high and low bytes of memory words exactly the same. It should be noted that this is not the same as the checksum which terminates that line of the hex file! The arrival of the checksum is the signal to the computer to send the next line from the file. In the event that BOOTLOAD encounters an invalid line of code, no checksum is sent and this should be a signal to the computer that an error has occurred. A suitable timeout should be used to determine whether a line has failed. The normal delay between the computer's sending the last character from a line of hex code and the arrival of the checksum is approximately 3 milliseconds per word of data programmed, plus 0.5 milliseconds each for the final character itself and the checksum. Since the entire timeout should never elapse, there is no reason to risk using one with an extremely short duration: a substantial fraction of a second, say 0.25, is recommended. When the "Start at 0" instruction is received, the processor responds with a special code which denotes successful loading of the entire program (0x55) or failure (any other value). Immediately following the 0x55 code, execution of the new software begins.
BOOTLOAD contains code to operate an indicator light connected to pin RC0 of the processor (pin 15 for 40-pin DIP, 11 for 28-pin DIP/SO, 16 for PLCC, 32 for QFP). A light-emitting diode (LED) in series with a suitable resistor is recommended, wired so that current will flow through the LED and resistor from the processor to ground. The LED displays the following aspects:
Note that once execution of user code begins, the LED will be under control of the user program and may be on, off or flashing.