We have a few options for debug. One is to use a combo of openocd
and gdb
. The other is to use probe-rs
's cargo embed
.
These examples use this project template for reference: https://github.com/rp-rs/rp2040-project-template
Debugging works best with opt-level = 0
for [profile.dev]
in Cargo.toml
, otherwise we're limited in where we can set breakpoints, and there is some other unexpected behavior.
This requires a debug probe. There are many options, but a simple one is to just flash probe fw onto another rp2040. Note that the fw link there is not the latest release. Wiring instructions can be found in appendix here. https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf Note that the sw instructions are somewhat dated.
This requires a recent version of openocd
(0.12.0
or above). The default version on Ubuntu 22.04 is 0.11.0, so we need to build from source.
Install openocd:
sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev
git clone https://git.code.sf.net/p/openocd/code openocd --depth 1
cd openocd
./bootstrap
./configure
make -j99
sudo make install
In three separate terminals: Build, flash, and monitor prints:
cargo run
The above printing currently conflicts with openocd
, so it needs to be killed before proceeding. It's possible to share both print and debug streams, but requires additional setup. https://ferrous-systems.com/blog/gdb-and-defmt/
Launch gdb server:
(the -f
args don't necessarily point to relative files)
openocd -f interface/cmsis-dap.cfg -c "adapter speed 5000" -f target/rp2040.cfg -s tcl
Launch gdb:
gdb-multiarch -q -ex "target extended-remote :3333" target/thumbv6m-none-eabi/debug/rp2040-project-template
The steps are outlined here. https://github.com/rp-rs/rp2040-project-template#alternative-runners
It works well with a vscode plugin, including step through, inspection, and logging.
It also supposedly works with other editors supporting DAP, but I haven't tested that out yet.
GDB does not have first-class support with probe-rs
(the maintainers are pushing for DAP), but you can try GDB by modifying the bottom of Embed.toml
to contain these lines:
[default.gdb]
enabled = true
gdb_connection_string = "127.0.0.1:3333"
Then launch with:
cargo embed
gdb-multiarch -q -ex "target extended-remote :3333" target/thumbv6m-none-eabi/debug/rp2040-project-template
This does not work perfectly. For example, you can't restart the target from GDB, and breakpoints are not as crisp (even with no optimizations).
Another probe option is to use the RPi SBC as both the dev machine and probe, and this likely requires the custom openocd branch to support bitbanging SWD (as mentioned in the picoprobe instructions). But when running with a secondary rp2040 probe, mainline openocd works fine. https://github.com/raspberrypi/pico-setup/blob/master/pico_setup.sh raspberrypi/openocd#77
GDB reference: https://sourceware.org/gdb/download/onlinedocs/refcard.pdf