Programming RP2040 with ST-LINK

rp2040st-link

Disclaimer: This is my first time using OpenOCD and programming ARM microcontroller, so I really don’t know what I am doing.

I’m trying to program Raspberry Pi Pico (RP2040) board using ST-LINK/V2 (mini) from Waveshare and OpenOCD. This programmer officially doesn’t support OpenOCD, but since OpenOCD support ST_LINK/V2 and it has SWDIO and SWCLK pins I think it should be okay. When I’m trying to use latest OpenOCD from git I got:

$ openocd -f interface/stlink.cfg -f target/rp2040-core0.cfg -c "program ~/git/pico-examples/build/blink/blink.elf verify reset exit"
Open On-Chip Debugger 0.11.0+dev-00433-g97db87c22 (2021-10-31-14:55)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
Error: Debug adapter doesn't support 'swd' transport
/usr/bin/../share/openocd/scripts/target/rp2040-core0.cfg:3: Error: 
in procedure 'script' 
at file "embedded:startup.tcl", line 26
at file "/usr/bin/../share/openocd/scripts/target/rp2040-core0.cfg", line 3

So apparently official OpenOCD doesn’t want to work with my programmer. There is a STMicroelectronics fork of OpenOCD made for ST-LINK, but it doesn’t support RP2040, but when I’m trying to pass rp2040-core0.cfg from official OpenOCD I got same error:

$ ../src/openocd -f interface/stlink.cfg -f /usr/share/openocd/scripts/target/rp2040-core0.cfg -c "program ~/git/pico-examples/build/blink/blink.elf verify reset exit"
Open On-Chip Debugger 0.11.0-rc2+dev-gff701ce (2021-10-31-15:03)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
Error: Debug adapter doesn't support 'swd' transport
/usr/share/openocd/scripts/target/rp2040-core0.cfg:3: Error: 
in procedure 'script' 
at file "embedded:startup.tcl", line 26
at file "/usr/share/openocd/scripts/target/rp2040-core0.cfg", line 3

And at last, fork of OpenOCD from Raspberry Pi gave me best result:

$ ../src/openocd -f interface/stlink-dap.cfg -c "transport select dapdirect_swd" -f target/rp2040.cfg -c "program ~/git/pico-examples/build/blink/blink.elf verify reset exit"
Open On-Chip Debugger 0.10.0+dev-g71510a7 (2021-10-31-15:11)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
dapdirect_swd
Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Info : STLINK V2J27S6 (API v2) VID:PID 0483:3748
Info : Target voltage: 3.261252
Error: init mode failed (unable to connect to the target)
Error: No Valid JTAG Interface Configured.

I checked many times connection between boards and they are fine. I tried to plug in RP2040’s USB while holding BOOTSEL then program it and same result.
What am I doing wrong?

Best Answer

It's true that the original ST-LINK firmware does not support multi-drop SWD debug protocol. RP2040 has two CPU cores, and multi-drop SWD support seems to be required even when you only want to connect to one of them.

The open source BlackMagic Probe firmware supports ST-LINK V2 hardware and adds the RP2040 support. The full installation instructions are here, summarized below:

  1. Build blackmagic firmware with make PROBE_HOST=stlink ST_BOOTLOADER=1
  2. Upload firmware to ST-LINK board with stlink-tool src/blackmagic.bin
  3. The device should appear as two serial ports, you can connect to the first one with gdb using command target ext /dev/ttyACM0.
  4. Using mon s will list available CPUs on the SWD bus, and attach 1 will connect to the first one.