BootLoader is code that runs when a microcontroller is powered on or restarted. Basically, it defines an environment for running application code. It is the Boot-Loader that configures the hardware and loads the application code from any storage medium or received through external communication and lets the application run. Thus, a Boot-Loader must perform the following basic function: Initialize the controller peripherals,
Any code run from the BLS can use self-programming mode (SPM). Using the SPM feature, a code from the BLS section can read or write the entire flash memory, including the BLS section from where the code is running. This feature can be used to load any application binary code into flash memory and let it run. Required initialization of controller peripherals like USART, port bits etc. and initialization of external devices like LCD etc. can be done from the BLS itself before loading the application code.
AVR flash memory is divided into two sections: the Applications section and the Boot-Loader (BLS) section. In the case of ATMEGA16 it has 16 KB of flash memory of which 15 KB is application section and the remaining 1 KB is BLS. The memory architecture of ATMEGA16 is shown in the following figure;
Fig. 2: ATMEGA16 Flash memory architecture
The code for the BLS and the application section can be written normally and there is not much difference. The only thing to be careful about is the size of the binary code. It must not be more than 1 KB, otherwise it will not be possible to code programmed in BLS. The project on AVR BLS coding discusses how to program a simple code in the BLS of ATMEGA16 microcontroller with the help of AVR studio as IDE, USBasp as programmer and AVR-Burnomat as recording software.
In this particular project, the Boot-Loader is coded to perform UART initialization along with a simple LEAD pin initialization and also an external hardware initialization from the 4-bit LCD . Therefore the application codes do not require these codes, they still work because before executing the application codes the initialization functions will be executed by the Boot-Loader.
The BLS code has initialization functions that should not be present in application codes. The initialization functions used in the Boot-Loader code for this project are given below;
Function |
Description |
empty lcd_init (empty) |
Initialize the LCD in 4-bit mode |
empty usert_init (void) |
Initialize the user at baud rate 9600 with transmit and receive enabled |
DDRD = 0x80; |
LED pin initialization as output |
Fig.3: Boot functions used AVR Boot-Loader code
Any application code can directly use the following function calls to access USART, LCD and LED without initializing their functions anywhere in the code.
Function |
Description |
lcd_clear |
Clean the LCD |
lcd_string |
Display a string on the LCD |
usert_send_string |
Send a string via usert |
PORTD &= 0x7F; |
Turn on the LED |
PORTD = 0x80; |
Turn OFF the LED |
Fig. 4: Function calls used by application code to access peripherals on the AVR
Hardware initialization before executing application code is explained in detail in a project in AVR BLS Hardware Initialization.
The main function of the Boot-Loader is to load code in binary format from a storage medium or that can be received through external communication with other devices to flash memory. The SPM resource available for code running in BLS helps load binary application code into flash memory. The task of writing BLS code with SPM has been simplified by the APIs available in the header file
FUNCTION |
DESCRIPTION |
PARAMETER |
boot_page_erase (address) |
Delete the flash page referred to by the address |
A byte address in flash |
boot_page_fill (address, data) |
Fill Boot-Loader temporary page buffer for flash address with data word |
The address is a byte address. Data is a word |
boot_page_write (address) |
Write the Boot-Loader temporary page buffer to the flash page containing the address |
Flash byte address |
Figure 5: Important APIs in AVR
The steps required to do SPM on application flash memory are explained in a project on using SPM in AVR flash-to-flash programming.
In this particular project, the Boot-Loader is coded in such a way that it will attempt to load any application binary code that has been loaded into the AVR microcontroller's built-in internal EEPROM. The APIs available on
The API provided by
uint8_t eeprom_read_byte (const uint8_t *p)
FUNCTION |
DESCRIPTION |
PARAMETER |
uint8_t eeprom_read_byte (const uint8_t *p) |
The function returns a byte of data that is stored in the EEPROM address referred to by the pointer p |
The pointer refers to the EEPROM address from which the data byte needs to be read |
Figure 6: API
With the help of the APIs discussed above
{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C} · {C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C} Step: 1 Delete the flash page that is about to write
{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C} · {C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C} Step: 2 Store the binary code that is read from EEPROM into a temporary buffer before writing to a flash page
{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C} · {C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C}{C} Step: 3 Program the temporary buffer filled on the already deleted flash page
After performing the above three steps, the Boot-Loader can jump using the instruction
asm(“jmp 0x0000”);
to the application code section and let the newly programmed application run.
Flash the Boot-Loader code to BLS first, any small size application code to EEPROM memory using the steps explained in the previous project on AVR BLS Blinking LED . Once EEPROM programming is complete and the controller is reset, the Boot-Loader will begin running. It can be seen that it is performing initialization functions and loading the application from the integrated EEPROM.
Fig. 7: Writing BLS code to the BLS of the AVR circuit on the breadboard
Fig. 9: LCD display initialized by BLS code in AVR configuration on breadboard
Fig. 10: USART initialized with baud rate 9600 by BLS of AVR configuration on breadboard
Project source code
###
#define F_CPU 8000000
#define _LCD_H #ifndef F_CPU #define F_CPU 8000000 #endif #include
###
Project source code
###
#ifndef _USART_H
#define rs PA0 #define rw PA1 #define en PA2 #include
###
Circuit diagrams
Circuit diagram of how to write a simple bootloader for AVR in C language |
Project Components
- ATmega16
- LCD
- LED
- Resistor