[PC] --Wi-Fi--> [树莓派5] --USB--> [DAPLink] --SWD--> [STM32目标板]

树莓派配置

测试Daplink USB连接状态

lsusb

必要的包

  • openocd
  • gdb-multiarch
  • git
  • make
sudo apt install openocd gdb-multiarch git make

OpenOCD

openocd -f interface/cmsis-dap.cfg -f target/stm32f1x.cfg

默认情况下,OpenOCD启动的GDB服务器(3333端口)只监听本地回环地址(127.0.0.1

我们需要让OpenOCD监听所有网络接口(0.0.0.0

nano ~/remote_daplink.cfg
# 指定使用CMSIS-DAP接口
adapter driver cmsis-dap

# 使用SWD协议,STM32F1支持SWD
transport select swd

# 可选:设置适配器速度,如果连接不稳定可以降低速度(单位kHz)
adapter speed 1000

# 指定STM32F1x的目标配置文件
source [find target/stm32f1x.cfg]

bindto 0.0.0.0
gdb_port 3333
telnet_port 4444
tcl_port 6666

# 可选:如果你希望OpenOCD在后台运行(作为守护进程
# daemon_startup attach
sudo openocd -f ~/remote_daplink.cfg

Tips: -d:输出调试信息

后台运行

nohup sudo openocd -f ~/remote_daplink.cfg > /tmp/openocd.log 2>&1 &

PC配置

安装 ARM GNU Toolchain

主要是为了arm-none-eabi-gdb程序。

» ARM GNU Toolchain

arm-none-eabi-gdb (GDB) 调试器依赖于 OpenOCD 调试服务器。GDB中下达的命令,都会通过网络发送到OpenOCD,再由OpenOCD翻译成底层的JTAG/SWD命令,通过调试探针发送给芯片执行。

Keil Output

Options for Target -> Output
文件类型用途
.axf包含完整调试信息的可执行文件。用于调试和通过OpenOCD+GDB烧录。
.hexIntel Hex格式文件,常用于生产烧录。
.bin纯二进制镜像文件,常用于OTA升级等。

VSCode

首先在VSCode中安装 Cortex-Debug 插件

远程调试,编辑 ./vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Remote Debug (STM32F1)",
      "cwd": "${workspaceFolder}",
      "executable": "./MDK-ARM/RaspVisionCar/RaspVisionCar.axf",
      "request": "launch",
      "type": "cortex-debug",
      "servertype": "external",
      "gdbTarget": "192.168.156.107:3333",
      "gdbPath": "arm-none-eabi-gdb",
      "device": "STM32F103C8",
      "runToEntryPoint": "main"

    },
......
arm-none-eabi-gdb "D:/Projects/STM32/RaspVisionCar/MDK-ARM/RaspVisionCar/RaspVisionCar.axf"
target extended-remote 192.168.156.107:3333
monitor reset halt
load

远程烧录,编辑`./vscode/tasks.json:

{  
    "label": "Flash via Remote GDB (OpenOCD)",  
    "type": "shell",  
    "command": "arm-none-eabi-gdb", // 确保在 PATH 中(GNU Arm Embedded Toolchain)  
    "args": [  
        "build/RaspVisionCar.elf",  
        "-q",  
        "-ex", "target extended-remote ${input:remoteGdbHost}:3333",  
        "-ex", "monitor reset halt",  
        "-ex", "load",  
        "-ex", "monitor reset",  
        "-ex", "detach",  
        "-ex", "quit"  
    ],  
    "group": "build",  
    "presentation": {  
        "echo": true,  
        "reveal": "silent",  
        "focus": false,  
        "panel": "shared",  
        "clear": true  
    },  
    "dependsOn": "Build Project (Release)" // 远程烧录前先构建  
}