16F87x BOOT LOADER/DOWNLOADER Copyright (C)2000 HI-TECH Software.

Freely distributable.

Included in this package is an implementation of a boot loader for the 16F87x, a downloading program which it communicates with and all of the associated source code.

Introduction

The Microchip 16F87x PIC processors are able to write to their own program memory while executing from it. By taking advantage of this fact, a bootloader program can be designed to accept new code via the serial port (or other method) and re-program itself.

On startup the bootloader code is the first to execute. By default, it checks the RB0 push-button to see if it is on. If it is, it goes into download mode waiting for commands via the serial port. If it is not on, it checks to see if there has been a user program installed and if so, executes it.

How it Works

So as to maximize the amount of available program memory, the bootloader has been designed to occupy less than 256 words. Another reason for doing this is because this is how much space Microchip's ICD debug code requires and by positioning the bootloader code at the same location, the -ICD compiler switch can be used when compiling downloadable programs. Using this switch ensures that the program being downloaded won't overlap the program locations that the bootloader may have used.

Since the bootloader code must execute before any user program, it uses the first 4 program locations to jump to its code. Programs that have been compiled for download will also try to use these locations. To avoid a conflict, the bootloader code will relocate the first 4 user program words to a different location. It is assumed that the downloaded user program has been compiled with HI-TECH PIC C and therefore these first four instructions simply implement a long jump. Below is a typical memory map with the bootloader installed.

Microchip PIC 16F877 memory map with the bootloader installed Address 0 ---------------------------- | Jump to bootloader | <--- Bootloader uses 1st 4 words (0-3H) 4 |--------------------------| | | | | | | | | | | | | | Program space | | available for | <--- Available space for user programs | downloaded programs | | | | | | | | | | | | | | | 1F00 |--------------------------| | Jump to user program | <--- 1st 4 user program words (1F00-1F03H) 1F04 |--------------------------| | Program ID | <--- Identifying word (1F04H) 1F05 |--------------------------| | | | Bootloader code | <--- The bootloader code (1F05-1FFFH) | | 1FFF ---------------------------- Also used is a program ID. This is needed so that the bootloader can check to see if a user program has been downloaded yet. If it has been set to a specific value, then it knows that it is safe to run the user program. If it is not set, it assumes that no user program has successfully been downloaded and therefore will not attempt to run the program on startup.

Any program locatations of the downloaded program that correspond to locations in the bootloader, will be ignored. This ensures that program data being received cannot overwrite any bootloader code.

Compiling Programs for Download

Except for the fact that there are 256 words less space available, nothing special has to be done in the design of downloadable programs. The only thing that must be done is to make sure the program doesn't use any program space where the bootloader resides. By default, the bootloader is positioned at 0x1F00 - 0x1FFF. To avoid these locations use the compiler switch -ICD (available from version 7.85). Included with the source code are HPDPIC projects pre-configured with the appropriate linker options for other 16F87x processors. If you need to position the bootloader elsewhere, then you will have to modify the CODE and CONST ranges in the linker options (see the tutorial section of the user manual for information about changing linker options). The "intentry" psect should be set to to the address where the bootloader code starts. The "LoaderStart" define in the bootloader code should also be changed to the start address of where you would like the bootloader to go.

Downloading Programs

Included are two versions of the command line downloading program. These are: NTLOAD.EXE and DOWNLDR.EXE. NTLOAD is designed to work with Windows NT, and DOWNLDR is a generic version for other operating systems with DOS command lines. The command to the downloader takes the following form: DOWNLDR options hex_file Or, NTLOAD options hex_file Where 'options' is zero or more of the following options and 'hex_file' is the name file the HEX file you wish to download. Options: -Sbaud_rate Specify the baud rate for communications. Eg: -S9600 -PCOMn Specify the com port to use. Eg: -PCOM1 -DEBUG_MODE=n Show debug information. The value for n can be 1 or 2. If set to 1 it will show information about the hex file being downloaded: addresses,etc... Set to 2 it will show all comm information being sent to/from the bootloader. The following is a typical command line used to download a program: DOWNLDR -S9600 -PCOM1 file.hex
Converted to HTML by Philippe Corbes (Email:philippe.corbes@laposte.net) 01/12/2002.