Black Magic Probe on Blue Pill
This is a writeup on flashing Blue Pill (a cheap STM32 ARM devboard) with Black Magic Probe (BMP). This is one of the cheapest JTAG/SWD debugger options for ARM Cortex MCUs.
The tool itself was developed as opensource by a team of amazing people. You can buy a ready made BMP at 1BitSquared which I recommend if you can spare the money and aren't looking for uses for all the devboards in your drawer.
For writeups of other peoples' processes see explanation of how to use nix by marble and another one by Olivier Fauchon.
To follow this process you'll need:
- Blue Pill with STM32F103 with 128k Flash
- a TTL level UART (USB to UART in my case)
- a PC with linux (this process is Debian-like linux specific, similar tools are available for MacOS and Windows, but are not described here)
First install the compiler, the stm32flash and dfu-util for flashing the Blue Pill board and add yourself to the dialout group for access to serial ports.
sudo apt install gcc-arm-none-eabi sudo apt install stm32flash sudo apt install dfu-util sudo adduser $USER dialout
Clone the Black Magic repository and cd into the directory.
git clone firstname.lastname@example.org:blacksphere/blackmagic.git cd blackmagic/
Based on documentation in blackmagic/src/platform/swlink/README.md the correct platform for my Blue Pill is "swlink". Run make with the correct PROBE_HOST parameter.
make clean make PROBE_HOST=swlink
This should build 2 files, one with the actual firmware, one with the DFU bootloader as seen on the output below.
LD blackmagic.elf OBJCOPY blackmagic.bin CC platforms/swlink/usbdfu.c CC platforms/stm32/dfucore.c CC platforms/stm32/dfu_f1.c LD blackmagic_dfu.elf OBJCOPY blackmagic_dfu.bin OBJCOPY blackmagic_dfu.hex
Next flash the DFU bootloader. Set BOOT0 jumper to 1 and BOOT1 jumper to 01. Connect the TTL-level UART (Tx to A10, Rx to A9, GND to GND). After that connect the power.
NOTE If you're using a USB to UART converter with 5V pin exposed you probably want to also use it to power the devboard. If you're using an external power source (like the USB port) remember that all Grounds have to be equal.
Then flash the DFU bootloader with the following command.
stm32flash -w blackmagic_dfu.bin -v -g 0x8000000 /dev/ttyUSB0
In my case the USB to UART converter is at /dev/ttyUSB0, check
dmesg to figure out which device appears after pluging your UART converter in.
Wait until the stm32flash tool is done.
stm32flash 0.5 http://stm32flash.sourceforge.net/ Using Parser : Raw BINARY Interface serial_posix: 57600 8E1 Version : 0x22 Option 1 : 0x00 Option 2 : 0x00 Device ID : 0x0410 (STM32F10xxx Medium-density) - RAM : 20KiB (512b reserved by bootloader) - Flash : 128KiB (size first sector: 4x1024) - Option RAM : 16b - System RAM : 2KiB Write to memory Erasing memory Wrote and verified address 0x08001e7c (100.00%) Done. Starting execution at address 0x08000000... done.
After the process is done disconnect power, disconnect the UART, set BOOT0 and BOOT1 jumpers to 0 and connect the Blue Pill board to your PC via USB on the Blue Pill devboard.
dmesg should show the device recognised as Black Magic Probe.
[1805904.543529] usb 1-1: New USB device found, idVendor=1d50, idProduct=6017, bcdDevice= 1.00 [1805904.543535] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [1805904.543539] usb 1-1: Product: Black Magic Probe DFU (SWLINK) v1.7.1-283-ge1a928b [1805904.543542] usb 1-1: Manufacturer: Black Sphere Technologies [1805904.543544] usb 1-1: SerialNumber: xxxxxxxx
To flash the actual firmware use dfu-util.
sudo dfu-util -d 1d50:6017 -s 0x8002000:leave -D blackmagic.bin
which should report that the download completed successfully.
dfu-util 0.9 Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc. Copyright 2010-2016 Tormod Volden and Stefan Schmidt This program is Free Software and has ABSOLUTELY NO WARRANTY Please report bugs to http://sourceforge.net/p/dfu-util/tickets/ dfu-util: Invalid DFU suffix signature dfu-util: A valid DFU suffix will be required in a future dfu-util release!!! Opening DFU capable USB device... ID 1d50:6017 Run-time device DFU version 011a Claiming USB DFU Interface... Setting Alternate Setting #0 ... Determining device status: state = dfuIDLE, status = 0 dfuIDLE, continuing DFU mode device DFU version 011a Device returned transfer size 1024 DfuSe interface name: "Internal Flash " Downloading to address = 0x08002000, size = 103400 Download [=========================] 100% 103400 bytes Download done. File downloaded successfully Transitioning to dfuMANIFEST state
After resetting the board
dmesg should show additional 2 serial devices.
[1805962.040564] usb 1-1: New USB device found, idVendor=1d50, idProduct=6018, bcdDevice= 1.00 [1805962.040569] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [1805962.040573] usb 1-1: Product: Black Magic Probe (SWLINK) v1.7.1-283-ge1a928b [1805962.040576] usb 1-1: Manufacturer: Black Sphere Technologies [1805962.040579] usb 1-1: SerialNumber: xxxxxxxx [1805962.044634] cdc_acm 1-1:1.0: ttyACM0: USB ACM device [1805962.045446] cdc_acm 1-1:1.2: ttyACM1: USB ACM device
This completes the flashing, to verify that the Black Magic Probe is functioning correctly start gdb, connect to the debugger at the first ACM device and query for the monitor version.
gdb GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2 Copyright (C) 2020 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> [...] (gdb) target extended-remote /dev/ttyACM0 Remote debugging using /dev/ttyACM0 (gdb) monitor version Black Magic Probe(SWLINK) v1.7.1-283-ge1a928b, Hardware Version 1 Copyright (C) 2015 Black Sphere Technologies Ltd. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> (gdb) q
Based on the AN2606 application note ↩