Home

Learn how to Program and Debug Raspberry Pi Pico with SWD

In this tutorial, we will learn how to upload a program to Raspberry Pi Pico using SWD and also debug with SWD. Till now, we have been using drag-and-drop method for uploading program to Pico. But if you want to use SWD Interface of Pico to program and debug, then this tutorial is for you. Learn the steps for Programming Raspberry Pi Pico with SWD and also debugging the code with SWD, OpenOCD and GDB.

Program-Raspberry-Pi-Pico-with-SWD-Image-1

A Brief Note on SWD

Have you ever worked on developing an Embedded System Application? If the answer is yes, then you might be familiar with the term ‘Debugging’. In embedded systems, which are mainly designed with a single task (or a very small number and specific set of tasks) in mind, the process of debugging and testing is very critical as we often work as low as CPU Register level.

There are several hardware and software solutions for debugging embedded systems. One such offering is called Serial Wire Debug or SWD in short. SWD is a Debug and Trace Port embedded into the silicon of most modern ARM based Microcontrollers and Microprocessors.

使用SWD探针(通常连接到微控制器的SWD端口并将其映射到USB)您可以使用微控制器的闪光灯,调试固件,添加断点,浏览代码等。

SWD,GDB(GNU调试器)和OpenOCD(支持调试,嵌入式系统的系统内编程和边界扫描)的组合是非常有力的调试设置,尤其是对于ARM Cortex Cortex系列处理器。

覆盆子pi picoSWD Programming and Debug

Like all ARM Cortex processors, the Raspberry Pi Pico also has dedicated hardware for debugging via the SWD Interface. The two wires required for SWD Debugging are called SWDIO (bidirectional SWD Data) and SWCLK (SWD Clock).

On the Raspberry Pi Pico, the SWD Pins are separated from the rest of the GPIO Pins and are placed at the bottom of the Board.

Raspberry-pi-pico-SWD引线

RASPBERRY PI PICO板上RP2040的2线SWD接口允许您执行以下操作:

  • 将程序上传到外部闪存或内部SRAM中。
  • 控制处理器的执行状态,即运行,停止,步骤,设置断点等。
  • Access processors memory and IO peripherals (which are memory mapped) through the system bus.

在Raspberry Pi中安装工具

如前所述,调试任何ARM皮层处理器需要GDB和OpenOCD。因此,我们现在将在主机系统中安装这两个,在我的情况下,这是一个运行最新Raspberry Pi OS的Raspberry Pi。

OpenOCD

要了解SWD协议并控制ARM Cortex处理器(在RP2040的两个情况下),您需要一个名为OpenOCD的特殊翻译器。现在让我们看看如何在Raspberry Pi中安装OpenOCD。

笔记:以下步骤将在/home/home/pi/pico/openOCD中安装OpenOCD。

cd ~
CD PICO
sudo apt install automake autoconf build-esenthe texinfo libtool libftdi-dev libusb-1.0-0-0- dev

Pico-SWD-1

这将由OpenOCD安装需要的所有工具. Next, we will clone the OpenOCD into our host and install OpenOCD. Enter the following commands one after the other.

git克隆https://github.com/raspberrypi/openocd.git –recursive - Branch RP2040 –Depth = 1
cd openocd
./bootstrap
./configure –enable-ftdi –enable-sysfsgpio –enable-bcm2835gpio
make -j4
sudo制作安装

我将花一些时间构建和安装OpenOCD。高枕无忧。

GDB

Next step is to install GDB. To install GDB Multi-Arch, use the following command:

sudo apt install gdb-multiarch

Pico-SWD-2

我们将在下一节中看到如何使用OpenOCD和GDB进行调试。

接线覆盆子Pi Pico和Raspberry Pi

在查看如何使用SWD编程的Raspberry Pi Pico之前,您必须首先正确接线Raspberry Pi Pico。

重要的提示:I do not know the exact reason but before making the connections, I had to shutdown Raspberry Pi to successfully program Raspberry Pi Pico using SWD. I read in Raspberry Pi Forum that both the Raspberry Pi and the target i.e., Raspberry Pi Pico in this case must be powered down before connecting SWD pins.

由于Raspberry Pi Pico通过USB连接到Raspberry Pi(为了供电),我所要做的就是关闭Raspberry Pi,建立SWD连接,然后在Raspberry Pi上供电。

下表显示了您需要建立的Raspberry Pi和Raspberry Pi Pico之间的所有必要连接。

覆盆子pi pico

Raspberry Pi
SWDIO

GPIO 24 (PIN 18)

SWD GND

GND(引脚20)
SWCLK

GPIO 25(引脚22)

Program-Raspberry-Pi-Pico-with-SWD-Image-2

与SWD编程Raspberry Pi Pico

让我们以“眨眼”程序为例,以了解Raspberry Pi Pico SWD编程的工作方式。如果您记得在带有C的“编程Raspberry Pi pico”教程中,我们已经构建了Blink程序,从而产生了一些目标文件。

For drag-and-drop programming via USB, we used the .uf2 file. But OpenOCD uses .elf file to upload the program.

使用以下命令与SWD编程Raspberry Pi Pico。

cd ~
CD PICO
CD pico-examples/build/
眨眼

Pico-SWD-3

openOCD -F接口/raspberrypi -swd.cfg -f target/rp2040.cfg-c “program blink/blink.elf verify reset exit”

Pico-SWD-4

上面的命令将调用OpenOCD将blink.elf文件编程为Raspberry pi pico,重置板并退出openOCD。如果一切顺利,您的终端应该显示这样的东西,并且Raspberry Pi Pico上的LED应该开始闪烁。

Pico-SWD-5

Debugging Raspberry Pi Pico with SWD

现在让我们看看如何调试SUP SUR SWD,OpenOCD和GDB的代码。我们已经在上一步中安装了GDB。现在,要在构建文件中包括与调试相关的设置,您已经使用了CMAKE指令'-DCMAKE_BUILD_TYPE=Debug’。

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构建
CD构建
export PICO_SDK_PATH=../../pico-sdk
cmake -dcmake_build_type = debug ..

Pico-SWD-6

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.

此外,Raspberry Pi Pico和Raspberry Pi之间的连接以查看Raspberry Pi Pico的输出uartSerial Output on Raspberry Pi is as follows:

覆盆子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(引脚14)

建立连接后,打开Hello_world UART目录并构建它。

CD hello_world/串行
make -j4

Use OpenOCD to open the GDB Server.

openOCD -F接口/raspberrypi -swd.cfg -f target/rp2040.cfg

Pico-SWD-7

保持此终端的原状,并打开另一个终端窗口,然后浏览构建目录中的UART串行目录。

cd ~
CD pico/pico-examples/build/hello_world/serial/serial/

Open GDB and connect to OpenOCD Server.

gdb-multiarch hello_serial.elf
(GDB)目标远程主机:3333

Pico-SWD-8

To load the program into the flash memory of Raspberry Pi Pico, use load command.

(GDB)负载

Start running the code.

(gdb) monitor reset init
(gdb) continue

Pico-SWD-9

If you are familiar with GDB commands, you can explore them.

结论

与SWD一起编程和调试Raspberry Pi Pico的完整教程。了解Raspberry pi pico SWD接口的工作原理,Raspberry Pi pico和Raspberry Pi之间的必要连接,用于SWD的程序Raspberry Pi pico与SWD,使用GDB使用SWD调试Raspberry Pi pico。

5个回应

  1. 谢谢你。

    只是为了让您知道,桌子上有一个错字,说明如何将SWD连接到PICO。

    GPIO 24 (PIN 18) and also GPIO 25 (PIN 18)

  2. Is it possible to use an ST-LINK for SWD? I don’t happen to have a Raspberry Pi lying around.

    1. Technically, it should be possible but not directly with st link firmware. You have to flash a different firmware (Segger’s J Link or Black Magic Probe). I personally haven’t tried this.

  3. 这是一个很好的过程,我遇到的问题之一是SWD连接偶尔对我来说只能工作。我通过在PI和PICO之间找到最短的电线来解决问题。可能我的原始电线可能是一个问题,但是由于带宽较高,使用短线似乎是合理的。我已经看到其他使用色带连接器的设置,也许扭曲的对也可以更好地工作。我现在拥有的电线大约3英寸,我完全没有任何问题。

发表评论

您的电子邮件地址不会被公开。

电子产品hub Favicon
<\/i>","library":""}}" data-widget_type="nav-menu.default">
Baidu