You are on page 1of 5

Mega64/128 AVR Boot Loader

Progressive Resources LLC October 22, 2003

The AVR Boot Loader (AVRBL) is a bootstrap loader that, once programmed into the
AVR boot-block memory area, allows reprogramming of AVR microcontrollers without
need for a chip programmer. The AVRBL makes use of the self-programming features
of the AVR microcontrollers to allow in-circuit reprogramming. Once the AVRBL is
programmed into the microcontroller, it remains resident until the chip is erased.
Application programs require only a very minimum software interface to use the
AVRBL.

This is a source code product and is packaged in the form of a CodeVisionAVR or


ImageCraft project. The scope and intent of this product is to provide a quick start to
get your product supporting a self-programming capability. Included in the package, with
the boot loader project, is a simple Windows application that allows you to download
an Intel formatted .hex file to your product from a PC serial communications port.

As currently configured, the AVRBL works via a USART port on the AVR. Other
interfaces such as the SPI port or even a parallel interface could be used with
modifications. The examples used herein all are for a serial port application.

Software Interface
When using a this boot loader with your application code there are a couple of basic
concepts to take into consideration:

1. A condition and jump instruction for getting back to the boot loader from the
main application code.
For example:
CVAVR:
if (UDR0 == '$') // did I receive a $ on the USART?
{ // if true, then jump to the AVRBL
#asm ( JMP 0x7C00) // 0xFC00 for the Mega128!!
}
ICC:
if (UDR0 == '$') // did I receive a $ on the USART?
{ // if true, then jump to the AVRBL
asm ( JMP (0x7C00*2)); // 0xFC00 for the Mega128!!
}

This example code checks to see if the character received on USART0 is a


$. If so, then the JMP instruction is executed, exiting the main application
and entering the boot loader directly. If the boot loader does not receive any
instructions within a selected amount of time it will jump to 0x0000,
effectively resetting the processor and starting the main application code.
Otherwise, if the boot loader does receive instructions, the opportunity is then
provided to download new firmware. This type of setup allows a downloading
tool on something like a PC or PDA to simply send a sequence of characters,

2003 Progressive Resources LLC


1
Mega64/128 AVR Boot Loader
Progressive Resources LLC October 22, 2003
followed by the .hex file in order to update an AVRs application program
flash area.

2. The boot loader must be placed in the boot section of flash memory starting at
0x7C00 (0xFC00 for the Mega128). When the chip is programmed with the
code containing the AVRBL, it is very important to be sure the boot size fuse
bits are set for a 1024 word boot section (BOOTSZ1:0 = 10) and the boot
reset fuse bit is set so that reset will invoke the boot loader instead of starting
the main application (BOOTRST=0).

Programming Protocol
After the AVBRL is started (via a reset, a power-up, or a jump from the main
application), the following protocol must be observed:

1. Upon power-up, reset, or as a result of a jump from the main application, the
AVRBL sends a ^(BOOTLOADER_ACTIVE_CHAR) at your selected baud
rate.

2. The host is then required to send the three-character entry sequence. This is used
to prevent an inadvertent attempt of reprogramming from taking place. If the
AVRBL does not receive these characters within the timeout period, the AVRBL
tests to see if there is code located in the main application area of flash. If there is,
then the AVRBL jumps to it, otherwise, execution stays within the AVRBL
indefinitely, waiting for the entry sequence.

3. Once the three-character entry sequence has been sent, the bootloader sends the
version string (Vx.xx) followed by a ?(READY_CHAR).

4. Upon receipt of the READY_CHAR, the host application should send the hex file
for the new/updated application program observing an X-ON / X_OFF
handshaking protocol to control data flow. The handshaking is very important as
the flash memory area writes much more slowly than the serial port can send data.
The programming software continues sending the hex file until it is all sent. After
each line of .hex file is received by the bootloader, one of three characters is
transmitted by the bootloader:
- ~ Line received with no errors.
- %Line received with no error, but an error occurred while flashing.
- - Checksum error detected while receiving the line.

5. After the programming is complete, the AVRBL sends either a #, meaning the
programming is all right, or an @indicating that an error has occurred and the
program did not load successfully. In most cases an error during programming
means that the main application program is corrupted and will need to be resent.

2003 Progressive Resources LLC


2
Mega64/128 AVR Boot Loader
Progressive Resources LLC October 22, 2003
6. The AVRBL then starts the newly programmed application software. As stated in
step 2, the AVRBL tests to see if there is code located in the main application area
of flash. If there is, the AVRBL jumps to it, otherwise, execution stays within the
AVRBL indefinitely, waiting for the entry sequence.

Customization
Below are some of the #defines which are located at the top of the AVRBL that allow
various changes to the timing and baud rate as well as the entry sequence. All of the
source code is provided in a CodeVisionAVR project allowing more extensive changes to
be made once you have familiarized yourself with the process.

NOTE: The character Vis reserved and should not be used for any of the
communication defines.

//********************************************
//define three character string to enter boot loader
#define char1 '@' /* reset codes.. start load codes.. */
#define char2 '&'
#define char3 '$'

// define bootloader active char


#define BOOTLOADER_ACTIVE_CHAR '^'
//define bootloader ready for file character
#define READY_CHAR '?'
// define line complete with no error character
#define LINE_COMPLETE_CHAR '~'
// define checksum error character
#define CS_ERROR_CHAR '-'
// define flash page error character
#define PAGE_ERROR_CHAR '%'
// define file complete, no errors character
#define FILE_COMPLETE_CHAR '@'
// define file complete, with errors character
#define FILE_ERROR_CHAR '#'

//define time out time in seconds


#define time_out_time 5

// UART Definitions
// select UART 0 or 1
#define USE_UART 1

//these two defines establish the baud rate


#define xtal 6000000L /* quartz crystal frequency [Hz] */

/* Baud rate */

2003 Progressive Resources LLC


3
Mega64/128 AVR Boot Loader
Progressive Resources LLC October 22, 2003
#define baud 19200
#define UBRRLO_VALUE ((xtal/16/baud-1)&0xFF)
#define UBRRHI_VALUE ((xtal/16/baud-1)>>8)

// The heart-beat definition:


// This output will be rapidly toggled while the bootloader
// is running. This is handy for using an LED to indicate
// what is going on..
//(If not wanted or used, then comment these lines out.)
//define the hear beat pin
#define HEART_BEAT PORTB.3 *see note
//define the pin as an output
#define HEART_BEAT_OUTPUT() DDRB.3 = 1 *see note

(*Note: These look slightly different in the ICC version, but work exactly the same.)

Software License
The use of AVR Boot Loader (AVRBL) indicates your understanding and acceptance of
the following terms and conditions. This license shall supersede any verbal or prior
verbal or written, statement or agreement to the contrary. If you do not understand or
accept these terms, or your local regulations prohibit "after sale" license agreements or
limited disclaimers, you must cease and desist using this product immediately.
This product is Copyright 2002 by Progressive Resources LLC, all rights reserved.
International copyright laws, international treaties and all other applicable national or
international laws protect this product. This software product and documentation may
not, in whole or in part, be copied, photocopied, translated, or reduced to any electronic
medium or machine readable form, without prior consent in writing, from Progressive
Resources LLC and according to all applicable laws. The sole owner of this product is
Progressive Resources LLC.

Operating License
You have the non-exclusive right to use any enclosed product but have no right to
distribute it as a source code product without the express written permission of
Progressive Resources LLC. Use over a "local area network" (within the same locale) is
permitted provided that only a single person, on a single computer uses the product at a
time. Use over a "wide area network" (outside the same locale) is strictly prohibited
under any and all circumstances.

Liability Disclaimer
This product and/or license is provided as is, without any representation or warranty of
any kind, either express or implied, including without limitation any representations or
endorsements regarding the use of, the results of, or performance of the product, Its
appropriateness, accuracy, reliability, or correctness. The user and/or licensee assume the
entire risk as to the use of this product. Progressive Resources LLC does not assume

2003 Progressive Resources LLC


4
Mega64/128 AVR Boot Loader
Progressive Resources LLC October 22, 2003
liability for the use of this product beyond the original purchase price of the software. In
no event will Progressive Resources LLC be liable for additional direct or indirect
damages including any lost profits, lost savings, or other incidental or consequential
damages arising from any defects, or the use or inability to use these products, even if
Progressive Resources LLC have been advised of the possibility of such damages.

Progressive Resources LLC


4105 Vincennes Road
Indianapolis, IN 46268

(317) 471-1577
(317) 471-1580 FAX
http://www.prllc.com

2003 Progressive Resources LLC


5