2024年8月

所需材料:openocd软件 + SWD调试器。

1. 安装openocd

我的开发环境是ubuntu服务器,所以我将在ubuntu编译安装openocd。执行以下命令拉取树莓派官方适配过的openocd仓库,并完成配置。

sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev pkg-config libhidapi-dev
git clone https://github.com/raspberrypi/openocd.git --branch rp2040 --recursive --depth=1
cd openocd
./bootstrap
./configure
./configure --enable-picoprobe --enable-cmsis-dap --disable-werror

应该可以看到如下内容:

OpenOCD configuration summary
--------------------------------------------------
MPSSE mode of FTDI based devices        yes (auto)
Raspberry Pi Pico Probe                 yes
ST-Link Programmer                      yes (auto)
TI ICDI JTAG Programmer                 yes (auto)
Keil ULINK JTAG Programmer              yes (auto)
Altera USB-Blaster II Compatible        yes (auto)
Bitbang mode of FT232R based devices    yes (auto)
Versaloon-Link JTAG Programmer          yes (auto)
TI XDS110 Debug Probe                   yes (auto)
CMSIS-DAP v2 Compliant Debugger         yes (auto)
OSBDM (JTAG only) Programmer            yes (auto)
eStick/opendous JTAG Programmer         yes (auto)
Andes JTAG Programmer                   yes (auto)
USBProg JTAG Programmer                 yes (auto)
Raisonance RLink JTAG Programmer        yes (auto)
Olimex ARM-JTAG-EW Programmer           yes (auto)
CMSIS-DAP Compliant Debugger            yes
Nu-Link Programmer                      yes (auto)
Cypress KitProg Programmer              yes (auto)
Altera USB-Blaster Compatible           yes (auto)
ASIX Presto Adapter                     yes (auto)
OpenJTAG Adapter                        yes (auto)
Linux GPIO bitbang through libgpiod     no
SEGGER J-Link Programmer                yes (auto)
Use Capstone disassembly framework      no

接下来编译,然后安装,安装完成后就可以在任意目录下使用可执行文件openocd了。

make -j4
make install

若想卸载,则在上述目录中执行make uninstall即可。


2. 准备SWD调试器(probe)

既可以购买树莓派官方的调试器,也可以根据官方手册使用树莓派pico制作调试器,本文使用了我自己设计的小尺寸调试器(围绕RP2040设计了小尺寸PCB,功能上与树莓派pico一致),下文将其称为probe。

给probe烧录固件

官方Github仓库发布的UF2文件,用BOOTSEL的方式烧录至probe中即可。若官方固件更新,同样可以用这种方式给probe更新固件。

probe连接至树莓派pico

我们要进行调试的设备为树莓派pico,调试器为probe,把它们的SWD、UART引脚连接起来,同时也要保证两个设备共地。

probe GND   -- pico GND
probe GPIO2 -- pico SWCLK
probe GPIO3 -- pico SWDIO
probe GPIO4(UART1 TX) -- pico GPIO1(UART0 RX)
probe GPIO5(UART1 RX) -- pico GPIO0(UART0 TX)

设备pico可以用以下方式供电:

  • 使用USB-typeC分别给probe和pico供电,probe预留的5V输出电源线不接。
  • 使用USB-typeC给probe供电,probe使用预留的5V输出电源线给设备pico供电。

3. 使用probe的SWD引脚烧录程序

使用SWD烧录程序需要的是ELF文件,假设我们编译后的文件路径为/test/build/hello_test.elf,那么用下面的命令就可以将ELF文件烧录至树莓派pico。

sudo openocd -f interface/cmsis-dap.cfg -c "adapter speed 5000" -f target/rp2040.cfg -s tcl -c "program /test/build/hello_test.elf verify reset exit"

这里给一份正常烧录的打印。

Open On-Chip Debugger 0.11.0-g8e3c38f (2024-08-11-09:40)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
adapter speed: 5000 kHz

Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Info : Using CMSIS-DAPv2 interface with VID:PID=0x2e8a:0x000c, serial=CB641C968F686D2F
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 0 SWDIO/TMS = 0 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x10000001
Info : rp2040.core0: hardware has 4 breakpoints, 2 watchpoints
Info : rp2040.core1: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x000000ea msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0xf1000000 pc: 0x000000ea msp: 0x20041f00
** Programming Started **
Info : RP2040 B0 Flash Probe: 2097152 bytes @10000000, in 512 sectors

target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
Info : Writing 12288 bytes starting at 0x0
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
** Programming Finished **
** Verify Started **
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000184 msp: 0x20041f00
** Verified OK **
** Resetting Target **
shutdown command invoked

4. 使用probe的SWD引脚调试程序

通过SWD连接到pico

通过probe连接到树莓派pico。

sudo openocd -f interface/cmsis-dap.cfg -c "adapter speed 5000" -f target/rp2040.cfg

可以看到下面的打印。

Open On-Chip Debugger 0.11.0-g8e3c38f (2024-08-11-09:40)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
adapter speed: 5000 kHz

Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : Using CMSIS-DAPv2 interface with VID:PID=0x2e8a:0x000c, serial=CB641C968F686D2F
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: FW Version = 2.0.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 0 SWDIO/TMS = 0 TDI = 0 TDO = 0 nTRST = 0 nRESET = 0
Info : CMSIS-DAP: Interface ready
Info : clock speed 5000 kHz
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x00000001
Info : SWD DPIDR 0x0bc12477
Info : SWD DLPIDR 0x10000001
Info : rp2040.core0: hardware has 4 breakpoints, 2 watchpoints
Info : rp2040.core1: hardware has 4 breakpoints, 2 watchpoints
Info : starting gdb server for rp2040.core0 on 3333
Info : Listening on port 3333 for gdb connections

可以看到,0核开启了gdb服务器,监听端口为3333

使用GDB调试

安装gdb。

sudo apt install gdb-multiarch

在SWD连接好后,新建一个shell终端,找到给树莓派pico烧录使用的ELF文件(我这里的文件名叫hello_test.elf),用gdb运行,然后连接到本地3333端口。

gdb hello_test.elf
# 在gdb终端中运行下面的命令
target remote localhost:3333

接下来就可以正常使用gdb进行调试了。


5. 使用probe的UART引脚连接串口

先在服务器安装minicom,用这个软件作为串口工具。在probe连接到树莓派pico之后,使用minicom打开设备ttyACM0

sudo apt-get install minicom
sudo minicom -b 115200 -o -D /dev/ttyACM0

这里的/dev/ttyACM0就是probe提供的串口设备。为了测试,我给树莓派pico烧录的程序会不停地通过串口(若执行了树莓派pico sdk里的函数stdio_init_all(),那么stdio将默认重定向到UART0,对应RP2040的GPIO0和GPIO1)打印HELLO!,效果如下所示。

Welcome to minicom 2.8

OPTIONS: I18n 
Port /dev/ttyACM0, 07:02:50

Press CTRL-A Z for help on special keys

HELLO!
HELLO!
HELLO!

串口可以正常打印HELLO。


6. 问题记录

突然无法正常使用SWD连接probe

现象如下所示。

Open On-Chip Debugger 0.11.0-g8e3c38f (2024-08-11-09:40)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
adapter speed: 5000 kHz

Info : auto-selecting first available session transport "swd". To override use 'transport select <transport>'.
Info : Hardware thread awareness created
Info : Hardware thread awareness created
Info : RP2040 Flash Bank Command
Error: unable to find a matching CMSIS-DAP device
Error: No Valid JTAG Interface Configured.

排查了很久没发现原因,偶然间看到一个网友转载的文章,最终发现是权限问题所致,使用sudo运行openocdminicom就正常了。