Blog Archives

AVR Bootloader(Based on avr109 application note)

hello every one hope you all are having great time .In this post we will try and comprehend how bootloader’s are written .

For many hobbyist  out their using AVR Microcontroller  bootloader is a black box. Many questions arise once you start using a bootloader.i have listed bellow few of them.

1.what exactly is happening in the bootloader code ……?

2.how does it work…..?

3.what is this boot switch why do i need to press this switch to program the controller ……..?

well ,same questions we bugging even me when i started using a bootloader to program some xyz board.Then i decided to write a basic bootloader to program an AVR microcontroller  using the  UART.

their are many open source bootloaders available on net and few of them are available as application notes on the atmel site .i decided to start with avr109 based bootloader .AVR109 based bootloader protocol was much simpler compared to the protocol of  stk500 based bootloader.

Let us start with few basics about the flash memory which you need to understand before diving into the bootloader code.

The flash memory is divided into two parts whose size is fixed

1. Read while write(RWW)

2. non Read while write(NRWW)

The main difference between the RWW and NRWW section is:

  • when erasing or writing a page in RWW section,the NRWW can be read during the operation.Hence the CPU is not halted.
  • when erasing or writing a page in NRWW section,the CPU is halted during the entire operation.

The flash memory is organised in term of pages.The page size in ATMEGA64 is 128 words.

Any update to the flash memory of the microcontroller happens in terms of pages either writing or erasing.The same thing happens even in the EEPROM memory.

For a Embedded Devveloper the flash memory is divided into two parts

1.application section

2.bootloader loader section

One should also know that the size of the these sections can  be configured using the fuse bits(BOOTSZ)

The following table illustrates the effect of  the BOOTSZ fuse

The application code is present in the RWW section while the BootLoader code is present in the NRWW section.while the application code can also extend into the NRWW section,but this should be avoided.if you don’t require a bootloader entire flash memory can be used for application code.Their are bootlock bits as well to enable the protection of the code resident on the flash memory.

The steps involved in programming an AVR using a Bootloader are as follows:

step 1:Entering into the bootloader

This can be done by jumping to the starting address of the bootloader from the application code by using a function pointer(if written in c language).Alternatively it can also be done with the help pf the BOOTRST flag which is present in the HFUSE.

by default this flag is 1 (unprogrammed).we need to program this flag i.e clear this flag to make the AVR to jump to starting address of the bootloader upon RESET.

Steps to load a page and erase a page in the flash memory is :

1.fill the page buffer(unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address))

2.Page Write(boot_page_write())

and to erase a page (boot_page_erase())

winavr provides the api’s to do most of the stuff relating to the the page load,page write & page erase (avr/boot.h avr/pgmspace.h)

/* AVR-GCC/avr-libc */
# include <avr/boot.h>

# include <avr/pgmspace.h>

The above two lines of code should enable you to use the api’s

The bootloader uses input from the external switch to enter into the bootlaoder

if(SWITCH==PRESSED)

{

START BOOTLOADER

}

ELSE

{

JUMP TO APPLICATION CODE

}

The above mentioned condition is at the starting of the bootloader .To enter into the bootloader we need to restart the AVR and then press the designated external switch to enter into the bootloader part.

In this case i have used sw1 of uniboard connected to PD6

Initially burn the bootloader code into the AVR using a usb or serial programmer .The .hex file can be found in the download section of this post.

NOTE:before buring the code into the AVR change the value of HFUSE 0xCC

instructions to use the bootloader:

1.press the reset switch when leds are blinking press sw1 to make the AVR to start the bootlaoder.

2.once the bootloader is started the leds on portc will stop blinking .This indicated the bootloader is started and it is waiting for the commands from UART1 at 19200 baud rate 8 data bits and 1 stop bits

3.To program the avr on windows we can use saina prog which is a freeware

can be downloaded from the following site

4.The following settings need to be done on saina prog change the name of the comport according to the number of the comport connected to the board.

Download code: