Summary of How To Write a Simple Bootloader For AVR In C language
This article explains how to write a simple Boot-Loader for the ATMEGA16 microcontroller in C. The Boot-Loader initializes hardware peripherals like USART, an LED, and a 4-bit LCD before loading an application code from the internal EEPROM into the flash memory using Self Programming Mode (SPM). It details the use of AVR Studio 4, USBASP programmer, and specific header files like `` and ``.
Parts used in the Simple Boot-Loader for AVR:
- ATmega16 microcontroller
- 4-bit LCD display
- LED
- Resistor
A bootloader is a small program in a protected part of flash that can write the rest of the flash. It lets you update a board over a serial port with no programmer, which is how an Arduino Uno loads sketches. This guide explains how the AVR boot section works and walks through a compact bootloader in C.
- The SPM instruction that writes flash only works when the code is running in the boot loader section (ATmega328P datasheet DS40002061B, section 27.3.2).
- You choose the boot section size with the BOOTSZ fuses; on the ATmega328P it is 256, 512, 1024 or 2048 words (ATmega328P datasheet DS40002061B).
- Our example compiles to about 540 bytes, so it needs the 1024 byte option.
- Test with an ISP programmer connected: a mistake can make the chip stop accepting uploads over serial.

How it works
- A fuse (BOOTRST) makes the chip start at the first address of the boot section instead of address 0.
- The bootloader waits a short time for a command on the serial port.
- If it receives a page of data it erases the target flash page, fills the page buffer word by word and writes it.
- If nothing arrives (or it gets a “go” command), it jumps to address 0 and the application starts.
Boot section sizes on the ATmega328P
| BOOTSZ1:0 | Boot section size | Starts at (word address) | Starts at (byte address) |
|---|---|---|---|
| 11 | 256 words (512 bytes) | 0x3F00 | 0x7E00 |
| 10 | 512 words (1024 bytes) | 0x3E00 | 0x7C00 |
| 01 | 1024 words (2048 bytes) | 0x3C00 | 0x7800 |
| 00 | 2048 words (4096 bytes) | 0x3800 | 0x7000 |
From the Boot Size Configuration table (ATmega328P datasheet DS40002061B). Fuse bits are inverted: 1 means unprogrammed and 0 means programmed.
Why the flash is split in two
Flash is divided into a Read-While-Write (RWW) section and a No-Read-While-Write (NRWW) section. When a page in the RWW section is erased or written, the CPU can keep running code from the NRWW section. When a page in the NRWW section is written, the CPU stops until it finishes. The bootloader therefore lives in NRWW flash, and application pages are in the RWW section, so the bootloader can carry on running while it writes them (ATmega328P datasheet DS40002061B, section 27.4).
The bootloader code
It uses avr-libc’s avr/boot.h macros. The protocol is deliberately simple: send p, a two-byte page number (low byte first) and 128 data bytes, and the bootloader answers with a dot.
// projects-raspberry.com
// A teaching bootloader for ATmega328P: receives flash pages over the UART and writes them with SPM.
// Protocol: 'p', page number (2 bytes, low first), 128 data bytes -> reply '.' 'g' -> run the application
#define F_CPU 16000000UL
#define BAUD 19200
#define BOOT_START 0x7C00UL // byte address of the 1024 byte boot section (BOOTSZ1:0 = 10)
#include <avr/io.h>
#include <avr/boot.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <util/setbaud.h>
#include <stdint.h>
static void uart_init(void)
{
UBRR0H = UBRRH_VALUE;
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#endif
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8N1
}
static void uart_put(uint8_t c)
{
while (!(UCSR0A & (1 << UDRE0))) {}
UDR0 = c;
}
static int16_t uart_get(uint32_t timeout)
{
while (!(UCSR0A & (1 << RXC0))) {
if (--timeout == 0) return -1;
}
return UDR0;
}
static void write_page(uint32_t byte_addr, const uint8_t *buf)
{
eeprom_busy_wait();
boot_page_erase(byte_addr);
boot_spm_busy_wait();
for (uint16_t i = 0; i < SPM_PAGESIZE; i += 2) {
uint16_t word = buf[i] | ((uint16_t)buf[i + 1] << 8);
boot_page_fill(byte_addr + i, word);
}
boot_page_write(byte_addr);
boot_spm_busy_wait();
boot_rww_enable();
}
int main(void)
{
static uint8_t page[SPM_PAGESIZE];
cli();
uart_init();
for (;;) {
int16_t cmd = uart_get(2000000UL); // roughly a second or two of waiting
if (cmd == 'p') {
uint16_t n = (uint16_t)uart_get(1000000UL);
n |= (uint16_t)uart_get(1000000UL) << 8;
for (uint8_t i = 0; i < SPM_PAGESIZE; i++) page[i] = (uint8_t)uart_get(1000000UL);
if ((uint32_t)n * SPM_PAGESIZE < BOOT_START) { // never overwrite the bootloader itself
write_page((uint32_t)n * SPM_PAGESIZE, page);
uart_put('.');
} else {
uart_put('!');
}
} else {
break; // 'g' or timeout: start the application
}
}
((void (*)(void))0)(); // jump to the application reset vector
return 0;
}
Build and install it
avr-gcc -Os -mmcu=atmega328p -Wl,--section-start=.text=0x7C00 -o bootloader.elf bootloader.c
avr-objcopy -O ihex bootloader.elf bootloader.hex
avr-size bootloader.elf
The --section-start option places the code at byte address 0x7C00, the start of a 1024 byte boot section. Then program the hex file and the fuses with an ISP programmer. The fuse settings for this layout are BOOTSZ1:0 = 10 and BOOTRST programmed. Work out the whole fuse byte from the datasheet table for your device, and read the current fuses before changing them.
What a real bootloader adds
| Feature | Why |
|---|---|
| Checksum per page | Detects corrupted data before it is written. |
| Handshake and flow control | Stops the host sending faster than flash can be written. |
| Protection of the boot section | The lock bits can stop the application writing over it. |
| A standard protocol | Lets existing tools such as avrdude work with it. |
The Optiboot bootloader that ships with the Uno is a good example of a very small, mature design: it is only about 512 bytes.
Quick answers
More from this site
- USART frame sizes: the serial code used here
- Introduction to Arduino Uno: a board with a bootloader
- ATmega644 board: programming over ISP
Downloads and references
- 💾 C code (avr-bootloader-code.zip)
bootloader.c and build commands.
Quick Solutions to Questions related to Simple Boot-Loader for AVR:
- What is the primary function of a Boot-Loader in a microcontroller?
It initializes controller peripherals, allows user selection of applications, loads the selected application, and lets it execute. - How does the ATMEGA16 divide its flash memory?
The 16 KB flash memory is divided into a 15 KB application section and a 1 KB Boot-Loader section (BLS). - Which feature allows BLS code to read or write the entire flash memory?
The Self Programming Mode (SPM) feature enables the code in the BLS section to access all flash memory including itself. - Can application codes initialize hardware if the Boot-Loader already did it?
No, the application codes do not require initialization functions because the Boot-Loader executes them before running the application. - Which header file provides APIs for Self Programming Mode operations?
The header file contains important APIs like boot_page_erase, boot_page_fill, and boot_page_write. - How is data read from the internal EEPROM in this project?
The eeprom_read_byte function from the header is used to read data bytes from the built-in EEPROM. - What command jumps execution to the application code section after loading?
The statement asm jmp 0x0000 is used to jump to the application code section and let it execute. - What software tools are recommended for writing and burning this Boot-Loader?
The article recommends using AVR Studio 4 as the IDE, USBASP as the programmer, and AVR-Burnomat as the burner software.
