Introduction

When devices are on field, users should be able to update it without debugger, and this is why some bootloader are integrated. A bootloader allow reflashing over all kinds of communication bus (UART, CAN, USB, …).

Here we will focus on a bootloader (dapboot ) which will permit to reflash a firwmware over USB via the DFU protocol (Device Firmware Upgrade, see 1, 2 ) on a “blue pill” (STM32F103).

stm32f103c8t6_pinout2

Build the bootloader

Sources are availables at https://github.com/devanlai/dapboot and could be obtained with the command:

git clone https://github.com/devanlai/dapboot.git

Once the project downloaded, create a local.mk in /src with the content below:

TARGET ?= BLUEPILL

# Enable button for bootloader entry after reset
DEFS += -DHAVE_BUTTON=1
#DEFS += -DBUTTON_ACTIVE_HIGH=1
#DEFS += -DBUTTON_GPIO_PORT=GPIOB
#DEFS += -DBUTTON_GPIO_PIN=GPIO2

The step above is optional, but it will permit to switch in bootloader after a reset when BOOT1 pin (yellow jumper) is set HIGH.

Next we can build the bootloader:

cd dapboot/src
make clean
make

Build operation will provide a file named dapboot.bin which will be used on the blue pill.

Flash the bootloader

In pre-requisite of this step you will need to install STM32 ST-LINK Utility, and connect an ST-Link V2 (or a cheap clone for few bucks on Ebay, Aliexpress, …) to your Blue pill.

bluepill_with_stlink

Once connections are OK, open STM32 ST-LINK Utility, select “Connect to target”, then perform a “Full chip erase”, and go to “Target” > “Program…”

stm32_stlink_flashing

Select the dapboot.bin via “Browse”, and trigger the flashing by pressing “Start”. When the flashing is done you can remove the ST-Link probe.

Test the firmware upgrade

Dapboot bootloader is flashed and running, so you can connect a usb cable to the micro usb connector of the blue pill.

Windows will detect the device, and if your web browser is running a popup will appear which propose to go to https://devanlai.github.io/webdfu/dfu-util/.

webdfu_popup

The web interface permit to flash any binary via DFU from your browser.

webdfu

  1. Select “Connect”, choose “Dapboot DFU bootloader”.
  2. Go to “Firmware download” box, select the blinky.bin (software built on my side which toggle the LED on PC13 each second).
  3. Select “Download”.

A progress bar will appear, and if the download is successful, bootloader will reset the MCU and execute the Blinky software.

Important! if you want to return in DFU mode, set the BOOT1 input to HIGH via the yellow jumper and press the reset button.

Build the firmware

As the dapboot bootloader is located in the first 8kB of flash memory, the start address of our firmware/application should be moved of 8kB (0x2000), which lead to the start address 0x8002000 for the STM32F103C8T6 present on the blue pill.

If you are using Keil MDK-ARM for development, here is how to proceed:

  1. Go to “Project” > “Options for target …”.
  2. In the “Target” tab, set the start address to 0x8002000 and the size to 0xE000.

keil_flash_range_cropped

  1. Go to “Output” tab and verify that “Create HEX” file is checked.
  2. Close the “Options for target” window and build your application.

The output file generated by Keil is not suited for the DFU Web interface, a conversion Hex to Bin is required (see TODO).

When the file is converted to .bin, you can follow the same step than in “Test the firmware upgrade” section, but this time you will need to set the BOOT1 input to HIGH via the yellow jumper and press the reset button.