Skip to content
systemcoding.dev
Embedded

Enter the STM32 System Bootloader and Flash Over UART

Use the STM32 built-in system bootloader to program firmware over a USB serial adapter, with BOOT0 wiring, the handshake bytes and CubeProgrammer commands.

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

Every STM32 leaves the factory with a bootloader already inside it, in a protected area called system memory. It means you can program a new chip over a plain serial connection, with no debug probe, just a USB-to-serial adapter and a jumper. This post shows how to enter that bootloader on an STM32F407, which pins carry the serial link, how to flash a binary with STM32CubeProgrammer 2.17 and with the open-source stm32flash tool, and what the first few bytes of the conversation look like, which helps when nothing answers. The behavior is documented in application note AN2606 (boot modes per product) and AN3155 (the USART protocol).

What the system bootloader is

The system bootloader is a small program stored in read-only memory by ST during manufacture. You cannot erase it or change it. When the chip resets with the right boot pins, the processor starts that program instead of your application. It listens on several interfaces at once, and the first one that receives a valid command wins. On the STM32F407 the options include UART, USB DFU, CAN and I2C or SPI, depending on which pins are wired. For UART, AN2606 lists the pins used on the F407:

InterfaceTX pinRX pin
USART1PA9PA10
USART3PB10 or PC10PB11 or PC11

The simplest choice is USART1 on PA9 and PA10. Always check AN2606 for your exact part, since the pins change from family to family.

Wiring

You need three connections to a 3.3 V serial adapter, and one jumper:

  1. Adapter TX to chip PA10 (the chip's USART1 receive).

  2. Adapter RX to chip PA9 (the chip's USART1 transmit).

  3. Adapter GND to chip ground.

  4. BOOT0 pulled high (3.3 V) for the moment of reset.

The adapter must run at 3.3 V logic. A 5 V adapter can damage the chip's pins. Most development boards (a Nucleo, a Discovery board, a Black Pill) label BOOT0 and have a jumper or a button for it. On boards with a BOOT1 pin, such as the STM32F4 Discovery, that pin must also be low, since the F4 uses the two pins together to choose between flash, system memory and SRAM. For BOOT0 = 1 and BOOT1 = 0, the chip boots from system memory.

A power-on with BOOT0 high, or a reset while BOOT0 is held high, starts the bootloader. After flashing you pull BOOT0 low and reset again to run your application.

Flashing with STM32CubeProgrammer

ST's free tool, STM32CubeProgrammer, includes a command line program STM32_Programmer_CLI that talks to the system bootloader. With the board in boot mode and the adapter showing up as /dev/ttyUSB0 on Linux (or a COM port on Windows):

Shell
STM32_Programmer_CLI -c port=/dev/ttyUSB0 br=115200 -w build/app.bin 0x08000000 -v -g 0x08000000

The options work as follows. -c port=... br=115200 connects over the serial port at 115200 baud, and the tool selects the even parity that the bootloader protocol requires. -w build/app.bin 0x08000000 writes the binary file starting at the given address, which is the start of flash. -v verifies by reading back. -g 0x08000000 starts execution at that address after programming. A successful run ends with output like this (timing and exact wording vary by version):

Text
Serial Port /dev/ttyUSB0 is successfully opened.
Port configuration: parity = even, baudrate = 115200, data-bit = 8,
                    stop-bit = 1.0, flow-control = off
Activating device: OK
Board       : --
Chip ID: 0x413
BootLoader protocol version: 3.1
Device name : STM32F40xx/F41xx
Device type : MCU
Device CPU  : Cortex-M4

Memory Programming ...
Opening and parsing file: app.bin
  File          : app.bin
  Size          : 47616 Bytes
  Address       : 0x08000000

Erasing memory corresponding to segment 0:
Erasing internal memory sector 0
Erasing internal memory sector 1
Download in Progress:
File download complete
Time elapsed during download operation: 00:00:06.172

Verifying ...
Download verified successfully

Start operation achieved successfully

Notice Chip ID: 0x413. That identifies the F405, F407, F415 and F417 line, and you can use it as a quick check that the tool is talking to the chip you think it is.

With stm32flash

The stm32flash tool, an open-source program available in most Linux package managers, does the same:

Shell
stm32flash -b 115200 -w build/app.bin -v -g 0x08000000 /dev/ttyUSB0

It writes (-w), verifies (-v) and runs the code at the given address (-g). If you run it with no write option, it prints the chip it detected, which is a helpful first test of the wiring.

What happens on the wire

When something does not answer, it helps to know the conversation. AN3155 defines a simple byte protocol, with 8 data bits, even parity and one stop bit. The first thing the host does is send a single byte, 0x7F. The bootloader uses its edge timing to measure the baud rate, which is how it works without being told. If it understood, it replies with 0x79, the ACK byte. If it rejects something it replies 0x1F, the NACK.

The following Python script, using pyserial 3.5, performs the handshake and asks for the chip's product ID. The Get ID command is the byte 0x02 followed by its complement 0xFD; every command is sent with its complement as a checksum:

probe.pypython
import serial

ACK = 0x79

def expect_ack(port, what):
    b = port.read(1)
    if not b or b[0] != ACK:
        raise RuntimeError(f"no ACK after {what}: got {b!r}")

with serial.Serial("/dev/ttyUSB0", 115200, parity=serial.PARITY_EVEN,
                   timeout=1) as port:
    port.write(b"\x7f")                  # autobaud handshake
    expect_ack(port, "0x7F")

    port.write(bytes([0x02, 0xFD]))      # Get ID command + complement
    expect_ack(port, "Get ID")
    n = port.read(1)[0]                  # number of following bytes minus 1
    pid = port.read(n + 1)
    expect_ack(port, "product ID")
    print(f"product ID: 0x{int.from_bytes(pid, 'big'):04X}")

With an F407 in boot mode, this prints:

Text
product ID: 0x0413

If the first read returns nothing, the bootloader did not start, which is the most common failure and is almost always a boot pin, a wiring or a baud rate problem rather than a software one.

Troubleshooting

When the tool says it cannot connect, work through this list in order.

  • Is the chip really in boot mode? Measure BOOT0 while resetting. It must be high when the reset ends. A board with BOOT1 tied high will not boot from system memory.

  • Are TX and RX crossed correctly? The adapter's TX goes to the chip's RX, and the other way round. Swapping them is the single most common wiring error.

  • Is the adapter at 3.3 V logic? A 5 V adapter may work or may damage the chip. A level that is too low also fails.

  • Is something else driving the pins? Peripherals or a debugger attached to PA9 and PA10 disturb the signals. Disconnect them.

  • Even parity? The protocol needs 8E1. A terminal program set to 8N1 sees garbage or nothing, and the tool you use must match.

  • Is the serial port busy? Another program, such as a terminal window, may hold it open.

  • Has the chip's read-out protection been set? At level 1 the bootloader refuses to read flash and responds with a NACK to read commands. The RDP post in this cluster explains how to regress it, which erases the chip.

A logic analyzer on the adapter's TX line shows the 0x7F byte as a train of edges. The chip answers on its TX line within a few milliseconds. If you see the byte go out and nothing come back, the problem is on the chip side, boot mode or RX wiring.

When to use it, and when not

The system bootloader is excellent for factory programming and for development boards without a probe. A production line can run STM32_Programmer_CLI once per board over serial or USB, with no debug connector at all. It also saves a bricked board. If your application disables the debug pins or crashes at start-up, so a probe cannot connect, pulling BOOT0 high and reflashing over serial recovers the chip.

It is not a field update mechanism. A person has to set a boot pin, and the protocol has no authentication. If a user must be able to update the device themselves, write your own bootloader that accepts firmware without a pin change, as the neighbouring guide in this cluster describes. A custom one can also keep the device running while it downloads, and verify what it received before writing.

After programming

Once the image is written and verified, return the board to normal: pull BOOT0 low and reset. If you passed -g to the programmer, the application starts without a reset, which is handy in a test fixture, but the application then runs with the bootloader's leftover configuration. Reset once with BOOT0 low in any test that depends on a clean start-up.

If the application uses a vector table offset (because it is linked above a custom bootloader), program both images in one session, each with its own -w and address.

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.