轻松玩转树莓派Pico之四、Ubuntu下在线debug环境搭建

目录

1、openocd编译安装

1)安装依赖

2)下载

3)编译

4)安装GDB

2、Picoprobe编译与连接

1)下载、编译

 2)开发板连接

 3)Picoprobe连接至Linux

3、手工运行openocd和gdb

4、VSCode在线调试

1)插件安装

2)插件配置

3)Debug


按照前文轻松玩转树莓派Pico之一、新手上路一文中的Pico固件更新方式(uf2文件格式),在正式开发过程中需要不断修改代码/更新固件,这种固件更新方式会感觉开发效率有些低。Pico开发板芯片为RP2040,ARM-Cortex-M0架构,官方开发板上留有SWD接口,那是不是可以在线debug呢?答案是肯定的。

官方文档:https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf 中有详细相关流程介绍。

官方文档中提供了2种在线debug方式:

方式1:树莓派作为主机,同时也作为openocd的仿真器,对Pico开发板进行固定烧录、在线debug。

方式2:使用1个专门的Pico开发板,作为Picoprobe,对另外一个目标Pico板进行debug

本文使用的是方式2,相关操作主机是在Ubuntu下进行。

另外,JLink也支持Pico在线调试,但对应的要求会相对更高。要求如下:

  1. J-Link software V7.64e or later
  2. J-Link HW revision 11 or later (J-Link EDU Mini and J-Trace PRO V1 or later also supported)
  3. SEGGER Embedded Studio for ARM V6.30 or later

对比后,JLink对应的硬件成本也比官方文档提供的方案高很多。

JLink操作Pico相关文档,可在Raspberry Pi Pico - SEGGER Wiki 页面查看。

本文所有操作流程均来源于getting-started-with-pico.pdf一文,只是将对应流程进行针对性的梳理。涉及到的章节有:

  1. Chapter 5. Flash Programming with SWD
  2. Chapter 6. Debugging with SWD
  3. Chapter 7. Using Visual Studio Code
  4. Appendix A: Using Picoprobe

1、openocd编译安装

1)安装依赖

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install automake autoconf build-essential texinfo libtool libftdi-dev libusb-1.0-0-dev

2)下载

git clone https://github.com/raspberrypi/openocd.git --recursive --branch rp2040 --depth=1

3)编译

在命令行下,依次执行:

cd openocd
./bootstrap
./configure --enable-picoprobe --prefix=/usr/local/openocd
make -j4
sudo make install

在运行./configure --enable-picoprobe过程中可能会出现如下错误,该问题是由于pkg-config未安装

./configure: line 4520: syntax error near unexpected token `0.23'
./configure: line 4520: `PKG_PROG_PKG_CONFIG(0.23)'

使用如下命令安装:

sudo apt install -y pkg-config

安装完成后,在命令行下输入:

openocd -v

确定安装是否完成。安装成功则会显示如下:

​Open On-Chip Debugger 0.11.0-g4f2ae61-dirty (2022-11-09-22:06)
Licensed under GNU GPL v2
For bug reports, read
        OpenOCD: Bug Reporting

由于我们将openocd安装到了/usr/local/openocd目录,执行以上指令会报错,

command not found: openocd

则需要手工修改一下环境变量。如在zsh下修改~/.zshrc加入:

export PATH=$PATH:/usr/local/openocd/bin

并使用source命令让环境变量生效,可执行:

source ~/.zshrc

4)安装GDB

在命令行下输入:

sudo apt install gdb-multiarch

安装完成后,确认是否安装成功;在命令行中输入:

gdb-multiarch

正常安装会显示如下日志:

GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.


For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)

2、Picoprobe编译与连接

Picoprobe支持swd和uart,可以实现在线debug的同时实现串口日志输出、串口通讯等功能。Picoprobe也是通过Pico(RP2040)实现,相关内容可查看Appendix A: Using Picoprobe 一章。

1)下载、编译

​git clone --recursive https://github.com/raspberrypi/picoprobe.git
​cd picoprobe
mkdir build && cd build
cmake ..
make -j4

该仓库下有freertos和CMSID_5子模块,子模块未下载会编译出错。

编译完成后,会在build目录下生成picoprobe.uf2

按照前文轻松玩转树莓派Pico之一、新手上路一文中更新固件的方式,将picoprobe.uf2更新至作为Picoprobe的开发板中。

 2)开发板连接

按照如下方式连接:

PicoA作为Picoprobe,PicoB作为真实debug开发板

Pico A GND -> Pico B GND
Pico A GP2 -> Pico B SWCLK
Pico A GP3 -> Pico B SWDIO
Pico A GP4/UART1 TX -> Pico B GP1/UART0 RX
Pico A GP5/UART1 RX -> Pico B GP0/UART0 TX 

Pico A VSYS -> Pico B VSYS 

这样就可以直接用1个usb接口,同时使用swd和uart功能

 3)Picoprobe连接至Linux

由于我是通过vmware虚拟机+ubuntu的开发方式,USB设备默认连接到了Windows主机,需要手工按照“虚拟机”->“可移动设备”->“Picoprobe”->“连接”,将Picoprobe连接到Ubuntu下。

在ubuntu下可通过通过dmesg | grep usb命令查看是否加载成功。 

 注:每次加载成功后,需要设置一下usb权限;在命令行下输入:

sudo chmod -R 777 /dev/bus/usb/

否则在运行中会出现如下错误:

Error: libusb_open() failed with LIBUSB_ERROR_ACCESS
Error: Failed to open or find the device
Error: Can't find a picoprobe device! Please check device connections and permissions.

3、手工运行openocd和gdb

openocd不仅可以用来在线debug,同时也可以用来烧录固件。

1)烧录固件

在命令行下输入:

cd pico-examples/build/hello_world/serial
openocd -f interface/picoprobe.cfg -f target/rp2040.cfg -c "program hello_serial.elf verify reset exit"

即可进行固件烧录

2)在线debug

在命令行下输入:

openocd -f interface/picoprobe.cfg -f target/rp2040.cfg -s tcl

正常启动openocd,显示日志如下:

Open On-Chip Debugger 0.11.0-g228ede4-dirty (2022-11-23-18:24)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'swd'
adapter speed: 5000 kHz


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 : 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
  • 同时在另一命令终端中输入:
cd pico-examples/build/hello_world/serial
gdb-multiarch hello_serial.elf

在正常显示(gdb) :后输入:

target remote localhost:3333

然后就可以输入b main等gdb指令,即可进行手工debug。

4、VSCode在线调试

使用手工运行openocd和gdb,对gdb熟悉度要求很高,我们可以借助VSCode相关功能进行可视化在线调试,无须再次手工输入一堆命令。

相关内容可以查看Chapter 7. Using Visual Studio Code一章。

1)插件安装

  • C/C++:用于C/C++语法支持,跳转,运行和调试功能
  • CMake:用于Cmake语法支持
  • Cmake Tools:让VSCode提供各种CMake编译相关的小工具,包括在底部状态栏显示一些快捷工具
  • Cortex-Debug:用来debug ARM cortex-M系列单片机

2)插件配置

VSCode运行和调试功能依赖C/C++插件和工程目录下的.vscode目录下相关.json文件配置。pico-examples工程提供了对应的配置文件。

在命令行下输入:

cd pico-examples
mkdir .vscode
cp ide/vscode/launch-raspberrypi-swd.json .vscode/launch.json
cp ide/vscode/settings.json .vscode/settings.json

由于我用的是Picoprobe,默认的lauch.json使用的是raspberrypi-swd模式,完成以上操作后,我们需要修改一下launch.json,将

 "configFiles": [
                "interface/raspberrypi-swd.cfg",
                "target/rp2040.cfg"
            ],

修改为:

 "configFiles": [
                "interface/picoprobe.cfg",
                "target/rp2040.cfg"
            ],

"configFiles"配置项为Cortex-Debug插件对应的相关配置文件。

3)Debug

点击VSCode的“运行和调试功能”按钮,点击“debug”按钮,VSCode会将当前目录下多个可执行人文件全部列出,让我们选择需要运行的目标文件。

  • 选择对应elf文件

  • 在线运行

VSCode会对当前工程下修改的文件做一次编译,编译没有错误后,调用openocd将选择后的elf下载至pico开发板,然后调用gdb,并跳转到main函数。

 点击右上角调试按钮,即可运行对应功能,分别对应“全速运行”、“运行至断点”、“单步调试”等功能。

  • debug过程中的相关信息查看

可通过“变量”、“监视”、“调用堆栈”、“断点”功能,查看代码运行过程中的相关值。

同时也可以通过Cortex-Debug插件对应的rp2040.svd文件提供的相关信息,查看“CORTEX PERIPHERALS”、“CORTEX REGISTERS”、“PERIPHERALS”等功能实时查看RP2040在运行过程中的状态,用于快速定位问题。

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值