Skip to content
systemcoding.dev
Embedded

Upload Firmware Without a Programmer Using an AVR Serial Bootloader

Install the Optiboot bootloader on an ATmega328P with an ISP programmer once, set the boot fuses, then upload sketches over a serial cable with avrdude.

7 min read
Summarize in AI
Flat illustration of a microcontroller wired to an LED and a sensor, with the Arduino, Arm and C logos.

Arduino boards upload code over a USB cable with no programmer attached. That works because the chip carries a small program, the bootloader, that already knows how to receive new firmware over a serial line and write it into flash. The same trick works on a bare ATmega328P on a breadboard or on your own PCB, once you have put the bootloader on the chip. This post explains the mechanism, shows how to install Optiboot with an ISP programmer and the right fuses, and how to upload with avrdude afterward. It uses avrdude 7.3 and Optiboot 8.x, and it ends with a small script that speaks the protocol by hand so you can see what happens on the wire.

Why an AVR can write its own flash

Most microcontrollers can read their flash while running. An AVR can also write it, but with a restriction: the SPM instruction (store program memory) that programs a flash page only works when executed from a protected region called the boot section, at the top of flash. Code in the main (application) section cannot call it. So the AVR family has a built-in design for bootloaders. A small program in the boot section receives bytes and uses SPM to write them into the application section.

Two settings control it, both held in fuse bytes, the permanent configuration bits that survive power cycles and programming:

  • **BOOTSZ** selects the size of the boot section: 256, 512, 1024 or 2048 words on the ATmega328P.

  • **BOOTRST** selects where the chip starts after reset. When it is programmed (0 in the fuse), the reset vector moves from address 0x0000 to the start of the boot section, so the bootloader runs before the application.

The sequence at every reset is then: the bootloader starts, waits a short time for a host that wants to upload, and if none appears, jumps to the application at address zero.

What Optiboot is

Optiboot is the bootloader used on Arduino Uno and Nano boards. It is open source, fits in 512 bytes on the ATmega328P, and speaks a subset of the STK500 version 1 protocol, which avrdude understands. Its small size matters because everything it occupies is flash you cannot use for the application: a 32 KB ATmega328P keeps 31.5 KB for your code with Optiboot installed.

Installing the bootloader once

The bootloader itself must be written with a programmer, since there is nothing on a blank chip to receive it. Use any ISP programmer (a USBasp, an Atmel-ICE, or another Arduino running the ArduinoISP sketch) connected to the six ISP pins: MISO, MOSI, SCK, RESET, VCC and GND.

Optiboot ships as a .hex file for each supported chip and clock. For an ATmega328P at 16 MHz and 115200 baud, the file in the Arduino AVR core is optiboot_atmega328.hex. Two avrdude commands do the work: first set the fuses, then write the bootloader and lock the boot section.

Shell
avrdude -c usbasp -p m328p -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFD:m
avrdude -c usbasp -p m328p -U flash:w:optiboot_atmega328.hex:i -U lock:w:0x0F:m

The fuse values are the ones Arduino uses for the Uno at 16 MHz with an external crystal. Each byte decodes as follows:

FuseValueMeaning
Low (lfuse)0xFFExternal full-swing crystal, long start-up time, no clock divide
High (hfuse)0xDEBOOTRST programmed, BOOTSZ = 256 words (512 bytes), SPI programming kept enabled, EEPROM erased on chip erase
Extended (efuse)0xFDBrown-out detector at 2.7 V

In the high fuse, bit 0 is BOOTRST and a 0 means programmed. 0xDE is 1101 1110, so bit 0 is 0: the chip resets into the boot section. Bits 2 and 1 are BOOTSZ1 and BOOTSZ0, which are 1 and 1 in 0xDE. The reference for the ATmega328P datasheet is the table "Boot size configuration"; on this chip 0xDE selects a 256-word (512-byte) boot section starting at word address 0x3F00, which is exactly the space Optiboot is built to fit in.

Be careful with fuses. Writing a wrong low fuse can switch the chip to a clock source that is not connected, and the chip then stops responding to the programmer until you supply that clock. Check the values against the fuse calculator for your clock before running the command, and read them back first with avrdude -c usbasp -p m328p -U lfuse:r:-:h.

The lock byte 0x0F write-protects the boot section, so a buggy application cannot overwrite the bootloader with SPM. The Uno's lock bytes mark the boot section as protected against self-programming (the BLB1 bits) while still letting the application section be updated.

Uploading over serial

Now the chip can be updated with nothing but a serial connection. Connect a USB-to-serial adapter at 5 V or 3.3 V to match the chip's supply: adapter TX to chip RXD (pin 2), adapter RX to chip TXD (pin 3), ground to ground. Then:

Shell
avrdude -c arduino -p m328p -P /dev/ttyUSB0 -b 115200 -U flash:w:blink.hex:i

-c arduino selects the STK500v1 protocol over a serial port, -b 115200 the speed that Optiboot is built for (some older boards use 57600), and -U flash:w:blink.hex:i writes the Intel HEX file to flash. A typical run ends like this:

Text
avrdude: AVR device initialized and ready to accept instructions
avrdude: device signature = 0x1e950f (probably m328p)
avrdude: reading input file blink.hex for flash
avrdude: writing flash (924 bytes)
Writing | ################################################## | 100% 0.15 s
avrdude: 924 bytes of flash written
avrdude: verifying flash memory against blink.hex
Reading | ################################################## | 100% 0.11 s
avrdude: 924 bytes of flash verified

avrdude done.  Thank you.

The byte counts and times depend on your program, so treat them as an example. The device signature 0x1e950f is the ATmega328P, and avrdude reads it as its first sanity check.

The auto-reset trick

Optiboot only waits for the host for about a second after reset. The host has to reset the chip at the right moment. On an Arduino board, the USB-serial chip's DTR line is connected through a 100 nF capacitor to the RESET pin. When avrdude opens the port, DTR goes low, the capacitor pulses RESET low, the chip restarts and the bootloader begins listening, just as avrdude starts talking.

On your own board you replicate this with a 100 nF capacitor between the adapter's DTR and the chip's RESET (pin 1), plus the usual 10 kilohm pull-up from RESET to VCC. If your adapter has no DTR pin you can reset the board by hand: start avrdude and press the reset button at once. A board that never answers and then works when you press reset at the right time is missing this capacitor.

Seeing the protocol by hand

The bootloader's conversation is short enough to type. STK500v1 frames end in the byte 0x20 (Sync_CRC_EOP). The first command is Get Sync, the bytes 0x30 0x20, and the bootloader answers with 0x14 (In_Sync) and 0x10 (OK). The following pyserial 3.5 script resets the chip with DTR, sends the sync command and reads the answer, and asks for the device signature (0x75 0x20):

avr_sync.pypython
import time
import serial

with serial.Serial("/dev/ttyUSB0", 115200, timeout=1) as port:
    port.dtr = False                 # pulse DTR to reset the AVR
    time.sleep(0.05)
    port.dtr = True
    time.sleep(0.1)

    port.reset_input_buffer()
    port.write(b"\x30\x20")          # STK_GET_SYNC, CRC_EOP
    print("sync   :", port.read(2).hex())

    port.write(b"\x75\x20")          # STK_READ_SIGN, CRC_EOP
    print("signature:", port.read(5).hex())

When the bootloader is present and in time, the output is:

Text
sync   : 1410
signature: 141e950f10

14 is STK_INSYNC, then the three signature bytes 1e 95 0f, then 10 for STK_OK. If sync prints nothing, the bootloader did not hear you: a missing reset capacitor, a wrong baud rate or an unprogrammed BOOTRST fuse.

Pitfalls specific to bootloaders

  • Wrong baud rate. An Uno at 16 MHz uses 115200 with current Optiboot, an old Duemilanove used 57600. A mismatch looks exactly like no bootloader at all.

  • Clock mismatch. A chip running from its 8 MHz internal oscillator (a common default on a new AVR) cannot talk at the rate a 16 MHz build expects. Bootloader and fuses must match the real clock.

  • **BOOTRST not programmed.** The chip then starts the application at address zero and never runs the bootloader. Reading hfuse shows whether bit 0 is zero.

  • A watchdog left on by the application. Older bootloaders cannot start properly if the application enabled the watchdog and the reset leaves it running. Optiboot 8 clears the watchdog on entry, so keep it current.

  • Overwriting the bootloader. If the lock bits do not protect the boot section, a very large application can clobber it, and the next reset runs nothing. The programmer is then needed to restore it.

  • Application size. With a 512-byte bootloader the usable flash ends at 0x7E00. Linking an image into that range fails in avrdude with an address error.

What the bootloader costs and gives

On a 32 KB part, Optiboot takes 512 bytes, about 1.6 percent of the flash, plus a delay of up to one second at every reset while it waits for a host. In return you get uploads over a cable that costs a few dollars, and a recovery path that does not depend on a separate programmer, as long as the bootloader itself stays intact. For a product that updates in the field, a custom bootloader with validation, as the guide in this cluster describes, replaces it. For a development board or a hobby project, Optiboot is hard to beat.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.