Flashing and debugging non-development STM32 boards under Linux

In a previous article we’ve seen how to flash and debug STM32 boards that have an onboard SMTLINK-V2 circuitry, such as the Nucleo and Discovery boards.

However this is not always the case, as for many STM32F103 boards (like the blue pill or the red pill); so we need another way. Luckily, there is.

The easy way

the easy way to flash and debug any board is to buy an STLINK clone online for a few euros; then use it as explained in the previous article.

The DIY way

You can also flash an existing board to make it a debugging dongle powered by the Black Magic Probe firmware.

Before that: flashing without a probe

But, since we dont’t have a dongle yet, how do we flash the microcontroller? Well, for the STM32 boards there is a BOOT0 mode helping us.

When the BOOT0 pin is pulled high on those STM32 chips (i.e. it’s connected to a +3.3V source) a “flashing through serial” mode is enabled, allowing to flash it through a serial port connected.

If you don’t have a serial port you’ll need an USB to TTL dongle; in the following of this article we’ll suppose that you have a serial dongle showing up as /dev/ttyUSB0

You will also need then the stm32flash utility: it’usually available on the distribution; on Ubuntu do

$ sudo apt install stm32flash

Move the jumper on BOOT0 so that it’s connected to the 3.3V; connect the TX pin on the dongle to PA9 and the RX pin on PA10; then power the board.

Boot0 pin on the blue pill board.

To flash the board the command is

$ stm32flash -w <your-hex-image> -v /dev/ttyUSB0 -b 9600 -S<write starting address>

Black Magic Probe

Assuming that you’re using a cheap STM32f103 board to host BMP, you’ll need to build from the sources:

$ git clone https://github.com/blacksphere/blackmagic.git
$ cd blackmagic
$ make PROBE_HOST=stlink

If all goes well you now have two binaries: one for the DFU updater and one for the probe itself. Go ahead and flash the two, the DFU starting at 0x8000000 and the probe firmware starting at 0x8002000 (assumiing that the serial port is /dev/ttyUSB0):

$ stm32flash -w blackmagic_dfu.bin -v /dev/ttyUSB0 -b 9600 -S0x8000000
$ stm32flash -w blackmagic.bin -v /dev/ttyUSB0 -b 9600 -S0x8002000

To debug, connect PB14 on the freshly created BMP to the SWDIO pin on the target board, PA5 to SWCLK, GND to GND and +3.3V to +3.3V; then run gdb:

$ arm-none-eabi-gdb <your-executable.elf> 
target extended-remote /dev/ttyACM1
monitor swdp_scan
attach 1

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *