How To Debug And Upload Programs To The Raspberry Pi Pico Using SWD?
In this article, we’ll learn how to debug and upload programs to the Raspberry Pi Pico using SWD. We have been uploading programs to Pico using the drag-and-drop method up until this point. However, if you want to program and debug using the Pico SWD Interface, then this tutorial is for you. Discover how to program a Raspberry Pi Pico with SWD and debug your programs using SWD, OpenOCD, and GDB.

An Overview of SWD
Have you ever worked on creating a system application for an embedded system? You might be familiar with the term “debugging” if the answer is yes. The process of debugging and testing in embedded systems, which are typically designed with a single purpose (or a very small number and precise set of tasks) in mind, is very important because we frequently operate as low as CPU Register level.
For debugging embedded systems, there are numerous hardware and software options available. One such product is known as Serial Wire Debug, or SWD. The silicon of the majority of contemporary ARM-based microcontrollers and microprocessors includes a Debug and Trace Port called SWD.

With just two wires and a SWD Probe (a small piece of hardware that is frequently linked to the microcontroller’s SWD port and maps it to USB), you may write the flash memory, debug the firmware, add breakpoints, step through the code, and perform other tasks.
A extremely potent debugging configuration, especially for ARM Cortex series processors, consists of SWD, GDB (GNU Debugger), and OpenOCD (an on-chip debugger that provides debugging, in-system programming, and boundary scan for embedded systems).
Programming and debugging for the Raspberry Pi Pico SWD
The Raspberry Pi Pico features specialised hardware for debugging via the SWD Interface, just like all ARM Cortex processors. The SWDIO (bidirectional SWD Data) and SWCLK lines are needed for SWD Debugging (SWD Clock).
The SWD Pins are located at the bottom of the Board on the Raspberry Pi Pico and are set apart from the other GPIO Pins.
You can perform the following tasks with the Raspberry Pi Pico board’s 2-wire SWD Interface of RP2040:
- Program upload into internal SRAM or external flash.
- Manage the processor’s state of execution, including the ability to run, halt, step, set breakpoints, etc.
- Utilize the system bus to access the memory and memory-mapped IO peripherals of the processor.
Installing Tools in Raspberry Pi
GDB and OpenOCD are necessary for debugging any ARM Cortex Processor, as was already described. We will therefore proceed to install these two on our host machine, in this example a Raspberry Pi running the most recent version of Raspberry Pi OS.
OpenOCD
You need a specialised translator called OpenOCD to comprehend the SWD Protocol and manage the ARM Cortex Processor (two in the case of the RP2040). Let’s examine how to set up OpenOCD on a Raspberry Pi immediately.
Please take note that the below actions will install OpenOCD in /home/pi/pico/openocd. cd
cd ~
cd pico
sudo apt install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0- dev
This will set up every piece of equipment needed by OpenOCD. The OpenOCD will then be installed after being cloned into our host. The following commands should be entered one after another.
git clone https://github.com/raspberrypi/openocd.git –recursive –branch rp2040 –depth=1
cd openocd
./bootstrap
./configure –enable-ftdi –enable-sysfsgpio –enable-bcm2835gpio
make -j4
sudo make install
I’ll spend some time installing and building OpenOCD.
GDB
Installing GDB is the following step. Use the following command to install GDB Multi-Arch:
sudo apt install gdb-multiarch
In the following section, we’ll look at how to debug using OpenOCD and GDB.
Wiring Raspberry Pi Pico and Raspberry Pi
You must first properly wire the Raspberry Pi Pico before learning how to programme it using SWD.
Important Information: I’m not sure why, but I had to shut down the Raspberry Pi before making the connections in order to successfully programme the Raspberry Pi Pico using SWD. The Raspberry Pi and the target, in this case the Raspberry Pi Pico, must both be turned off before connecting the SWD pins, according to information I found on the Raspberry Pi Forum.
All I had to do was shut down Raspberry Pi, make the SWD Connections, and then turn on Raspberry Pi because Raspberry Pi Pico is powered by Raspberry Pi and connected to it by USB.
The connections between the Raspberry Pi and Raspberry Pi Pico that must be made are all listed in the following table.
.
| Raspberry Pi Pico | Raspberry Pi |
| SWDIO | GPIO 24 (PIN 18) |
| SWD GND | GND (PIN 20) |
| SWCLK | GPIO 25 (PIN 22) |
Programming Raspberry Pi Pico with SWD
To better understand how Raspberry Pi Pico SWD Programming functions, let’s use the “Blink” application as an example. If you recall, we already developed the Blink programme, which produced a few target files, in the “Programming Raspberry Pi Pico with C” instruction.
We used.uf2 files for drag-and-drop programming over USB. However, OpenOCD uploads the programme via an.elf file.
The Raspberry Pi Pico with SWD can be programmed using the commands below.
cd ~
cd pico
cd pico-examples/build/
make blink
openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg -c “program blink/blink.elf verify reset exit”
Upload an elf file to the Raspberry Pi Pico, restart the board, and close OpenOCD. If all goes according to plan, your terminal should show a message similar to this and the LED on the Raspberry Pi Pico should begin to flicker.
Debugging Raspberry Pi Pico with SWD
Now let’s look at the code debugging process using SWD, OpenOCD, and GDB. GDB was already installed in the step before. You must now use the CMake directive “-DCMAKE BUILD TYPE=Debug” to include the debug-related settings in the build files.
But before that, you need to remove the ‘build’ directory from ‘pico-examples’ directory and create a new ‘build’ directory. Use the following commands to build examples with Debug information.
cd ~/pico/pico-examples/
rm -rf build
mkdir build
cd build
export PICO_SDK_PATH=../../pico-sdk
cmake -DCMAKE_BUILD_TYPE=Debug ..
Let us use the ‘hello_world’ example and build for the serial variant. You cannot use USB based serial connection for SWD Debugging as the USB Device will be disconnected when stopping the processor core in debugging.
Also, the connections between Raspberry Pi Pico and Raspberry Pi to view the output of Raspberry Pi Pico’s UART Serial Output on Raspberry Pi is as follows:
| Raspberry Pi Pico | Raspberry Pi |
| GPIO 0 (UART0_TX) | GPIO 15 (UART_RX0) PIN 10 |
| GPIO 1 (UART0_RX) | GPIO 14 (UART_TX0) PIN 8 |
| GND | GND (Pin 14) |
After making the connections, open the hello_world UART directory and build it.
cd hello_world/serial
make -j4
Use OpenOCD to open the GDB Server.
openocd -f interface/raspberrypi-swd.cfg -f target/rp2040.cfg
Keep this terminal as it is and open another terminal window and browse to UART Serial directory in the build directory.
cd ~
cd pico/pico-examples/build/hello_world/serial/
Open GDB and connect to OpenOCD Server.
gdb-multiarch hello_serial.elf
(gdb) target remote localhost:3333
To load the program into the flash memory of Raspberry Pi Pico, use load command.
(gdb) load
Start running the code.
(gdb) monitor reset init
(gdb) continue
If you are familiar with GDB commands, you can explore them.
Conclusion
I hope all of you understand the process of Programming and Debugging Raspberry Pi Pico with SWD. We MATHA ELECTRONICS will be back soon with more informative blogs.