[Knowledge snippet] - STM32 bootloader

You programmed STM32 microcontroller using Nucleo or Discovery boards. That means you used Serial Wire Debug (SWD) for programming/debugging. Now, you are designing a PCB with a STM32 microcontroller on it, which means you have to be able to program it. One solution is uploading the code through a bootloader - a small piece of software, made by ST that has been saved in the protected (read-only) memory of the microcontroller. It’s a simple program which reads data, sent from the host (PC) using a specific protocol, and saves it into FLASH memory.

STM32 microcontroller system memory boot mode - this document describes hardware resources used by the bootloader and the bootloader activation pattern (what needs to be set to activate the bootloader).

Lower-end micros can only use UART for flashing, higher-end can use:

  • CAN - documentation ID: AN3154
  • USART - documentation ID: AN3155
  • USB - documentation ID: AN3156
  • I2C - documentation ID: AN4221
  • SPI - documentation ID: AN4286

To activate the bootloader you have to assert one or two pins (BOOT0 and/or BOOT1) high, while the mcu boots. It would be nice if the microcontroller could do it by itself, but since you have to reboot it, this becomes tricky. Here are a few options to solve that:

  • Make one of the GPIO pins output high to charge a small capacitor and, after a short period, reset micro using the NVIC_SystemReset() instruction.
  • Use buttons - hold a button to pull boot pin high and press reset button (you can often find the reset button circuitry in the micro’s datasheet).
  • Jump into bootloader code from the application code - check this video out.

Be sure to:

  • …power all of the VDD pins.
  • …pull the VREF high.

Since the communication with the bootloader uses a specific protocol, you have to use stm32flash tool to transfer the new firmware correctly.