An Avr or Pic bootloader program

Introduction

This page is related to a software module included in quite all my Avr or Pic 18f projects. This module supplies the On-chip bootloading facility. So this function reads / ansvers to the programming host commands sent by pgrm on the serial interface of the processor.

With this module, you can program the memory flash and eeprom of your processor without removing it from your application board.

Common Features

The bootloader is considered as resident and its code should be restrained to the BOOTLOADER section with no calls tp code functions in the loaded program.

Avr specific

The function Program lock byte, Read lock byte are not enabled by default. You need to define BOOT_EXTENDED. The function Read signature bytes, Read calibration bytes are enabled, but due to the lack of implementation in some devices like atmega8 the result has no meaning.

Locating the boot loader in Avrs

To be effective the bootloader must be placed in a special section called boot block for which the address is device dependant. So you must explicitely give instruction to the linker where to place le bootloader. This is done in the Makefile. In this example for an atmega8515, the code is located at 0x1E00 that is 0X200 below the last flash address in the device. You can also play with the size of the boot block, see datasheet for that.


MMCU = atmega8515
BOOTAD = 0x1E00
...

LDFLAGS = -g  -mmcu=$(MMCU) -Wl,-Map,$(PROJECT).map \
          -Wl,--section-start=.bootloader=$(BOOTAD)
...

In avr-gcc, the address is expressed in bytes, in Atmel datasheets there are expressed in word address, so you should take care, the result in not the same !

Pic specific

The bootloader is written in assembler and has to be located in the boot section.

The PGRM functions for the fuses bits in Avr now deal with Pic configuration bits. And the functions for the lock bits now deal with Pic User id location bits.

The boot section also include the SDCC crt0iz.c startup routines : ram initialization and variable initialization. Provision is given to find the cinit variable in the my_cinit variable at a fixed place. So you don't need to reflash the boot section. However you need to link with the boot code.

Locating the boot loader in Pics 18f

The bootloader is located in the boot block at the bottom of the flash memory (low addresses). The reset and interrupt vectors are redirected to fixed locations at the start of application code. This location is device dependant and should be defined (if not already done) in the file bootdef.h

There are some functions included in bootloader section for read/write eeprom that can be accessed from the application.

Calling the bootloader

After a reset, the processor start the application which implements, in its main loop, a mini command interpreter for debug purpose and to supply access to the bootloader on request from pgrm on the serial line.

There is an inconvenience in the previous method; when the application becomes garbled for a reason or another as we are developing, the bootloader is no longer accessible. So a rescue method is needed.

In general, it is a good idea to have a led showing processor activity: the blinking led and so I do. By using the same port as input at startup and checking the level we can decide what action to take either normal startup or direct jump to bootloader, for example, if the level is forced to 0.

For Avr's, the following piece of code, placed at the end of the system initialization routine, will test the condition.

For Pic's, a similar function is located in the boot section. The pin used for that is defined in the bootdef.h file and can be redefined.

...
      /* Jump to bootloader if pin activity is 0 */
   ClearDdrBit(ACTIVITY);  /* set activity as input */
   SetBit(ACTIVITY);      /*  activate the pull-up resistor */
   if ( Pin_Bit_Is_Clear(ACTIVITY) ) {
      /* ACTIVITY pin is connected to ground */
      bootloader(); /* jump to bootloader */ 
   }
   /* set portc */
   DDRC = DDRC_SET ;
...
   sei(); /* enable interrupts */

Just temporarily, connect the activity pin to ground, power on, and use pgrm as usually. Try read signature, if it works, you can program the device. After programming, remove the temporary connection for normal use.

How to use

See the page on Pgrm - a Microprocessor and Eprom Programmer for instructions.

Download

There is no direct download for Avrs. The bootloader file is included in all my Avr projects as boot.c. Download one or all of them.

For Pics, there is a test program tstboot. Its only purpose is to interface to the bootloader. It compiles with sdcc and gputils.