CLion 使用 pyOCD / OpenOCD 烧录调试说明
本文记录本工程在 CLion 中使用 ATK-HS-V3-CMSIS-DAP 无线烧录器的配置流程。
当前工程芯片为 STM32F103C8Tx,链接脚本为 STM32F103XX_FLASH.ld,Flash 为 64 KB。
1. 当前推荐方案
当前推荐使用:
bash
pyOCD + ATK-HS-V3-CMSIS-DAP
不推荐当前使用:
bash
OpenOCD + ATK-HS-V3-CMSIS-DAP
原因见第 5 节。
2. 已验证的工具路径
pyOCD:
bash
/home/sg/.local/bin/pyocd
GDB:
bash
/opt/st/stm32cubeclt_1.21.0/GNU-tools-for-STM32/bin/arm-none-eabi-gdb
OpenOCD 0.12.0:
bash
/opt/openocd-0.12.0/bin/openocd
工程 ELF:
bash
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/build/Debug/motor_wlwg.elf
3. pyOCD 命令行验证
3.1 查看 DAP 是否识别
bash
pyocd list --probes
正常输出应能看到类似:
text
ATK ATK-HS-V3-CMSIS-DAP ATK 20190528
3.2 读取芯片 ID 验证 SWD 连接
bash
pyocd commander -t stm32f103rc -c "read32 0xE0042000" -c "exit"
已验证可读到:
text
e0042000: 20030410
说明 CMSIS-DAP 到 STM32 的 SWD 链路正常。
3.3 命令行烧录
本工程已提供脚本:
bash
./pyocd_flash.sh
脚本内容等价于:
bash
pyocd erase -t stm32f103rc --chip --frequency 500000
pyocd flash -t stm32f103rc --frequency 500000 build/Debug/motor_wlwg.elf
pyocd reset -t stm32f103rc --frequency 500000
说明:
text
-t stm32f103rc
pyOCD 当前内置目标里没有 stm32f103c8,但 STM32F103RC 与 STM32F103C8 属于同系列,Flash 算法可用于本工程烧录。工程实际固件约 26 KB,未超过 C8 的 64 KB Flash。
text
--frequency 500000
无线 DAP 建议先用低速 500 kHz,稳定后再考虑提高。
4. CLion 配置 pyOCD
4.1 配置烧录
打开:
text
File -> Settings -> Tools -> External Tools
点击 +,新增一个 External Tool。
填写:
text
Name:
pyOCD Flash
Program:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/pyocd_flash.sh
Arguments:
Working directory:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control
使用方式:
text
Tools -> External Tools -> pyOCD Flash
它会执行:
text
整片擦除 -> 烧录 ELF -> 复位运行
4.2 配置调试
本工程已提供 GDB Server 启动脚本:
bash
./pyocd_gdbserver.sh
脚本内容等价于:
bash
pyocd gdbserver -t stm32f103rc --frequency 500000 -p 3333
在 CLion 中打开:
text
Run -> Edit Configurations
点击 +,选择:
text
Embedded GDB Server
填写:
text
Name:
pyOCD Debug
Target:
motor_wlwg
Executable:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/build/Debug/motor_wlwg.elf
Debugger:
Custom GDB
GDB:
/opt/st/stm32cubeclt_1.21.0/GNU-tools-for-STM32/bin/arm-none-eabi-gdb
GDB Server:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/pyocd_gdbserver.sh
GDB Server args:
'target remote' args:
localhost:3333
Working directory:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control
重点:
text
GDB 必须填 arm-none-eabi-gdb
GDB Server 必须填 pyocd_gdbserver.sh
不要把 pyocd-gdbserver 或 pyocd_gdbserver.sh 填到 GDB 栏里。
4.3 正常连接日志
启动调试后,正常日志应类似:
text
Target type is stm32f103rc
DP IDR = 0x1ba01477
CPU core #0: Cortex-M3
GDB server listening on port 3333
Client 1 connected on port 3333
调试器已连接至 localhost:3333
这说明:
text
pyOCD 已识别芯片
SWD 连接正常
GDB Server 已启动
CLion 已连接 GDB Server
4.4 常见错误
错误:
text
pyocd-gdbserver: error: unrecognized arguments: --interpreter=mi2
原因:
text
CLion 把 GDB 参数 --interpreter=mi2 传给了 GDB Server。
通常是把 pyocd-gdbserver 填到了 Debugger/GDB 栏。
修复:
text
Debugger/GDB 填 arm-none-eabi-gdb
GDB Server 填 pyocd_gdbserver.sh
错误:
text
Generic 'cortex_m' target type is selected by default
原因:
text
pyOCD 没有指定 -t stm32f103rc。
修复:
text
使用 pyocd_gdbserver.sh
5. 为什么当前不使用 OpenOCD
5.1 现象
原始 OpenOCD 0.11.0 报错:
text
Error: unable to find a matching CMSIS-DAP device
Error: No Valid JTAG Interface Configured.
升级 OpenOCD 0.12.0 后,仍然报错:
text
Error: unable to find a matching CMSIS-DAP device
5.2 设备能被 Linux 识别
lsusb 能看到:
text
ID 04d8:00df Microchip Technology, Inc. ATK-HS-V3-CMSIS-DAP
USB 描述符显示它有 HID 接口:
text
Interface Descriptor:
bInterfaceClass 3 Human Interface Device
5.3 OpenOCD 找不到的关键原因
OpenOCD 的 CMSIS-DAP HID 后端依赖 HIDAPI 枚举 HID 设备。
当前系统虽然能看到 USB 设备,也显示接口绑定到了 usbhid:
text
Class=Human Interface Device, Driver=usbhid
但没有为 ATK-HS-V3 生成对应的:
bash
/dev/hidraw*
实际 /dev/hidraw* 列表里没有 04d8:00df 对应节点。
所以 OpenOCD/HIDAPI 枚举不到这个 ATK-HS-V3。
5.4 为什么 pyOCD 可以
pyOCD 能识别:
text
ATK ATK-HS-V3-CMSIS-DAP
并且已经验证能读芯片 ID、整片擦除、烧录、复位。
所以当前工程实际可用路径是:
text
pyOCD 可用
OpenOCD 不可用
6. 如果以后要配置 OpenOCD
如果换了更兼容的 CMSIS-DAP、ST-Link、J-Link,或者系统能正常生成 ATK 的 hidraw 节点,可以再尝试 OpenOCD。
6.1 确认 OpenOCD 版本
bash
/opt/openocd-0.12.0/bin/openocd --version
应输出:
text
Open On-Chip Debugger 0.12.0
6.2 CMSIS-DAP 配置文件
当前 dap.cfg:
tcl
adapter driver cmsis-dap
cmsis_dap_backend hid
cmsis_dap_vid_pid 0x04d8 0x00df
transport select swd
set FLASH_SIZE 0x10000
source [find target/stm32f1x.cfg]
adapter speed 1000
如果是 CMSIS-DAP v2 bulk 设备,可以尝试:
tcl
adapter driver cmsis-dap
cmsis_dap_backend usb_bulk
cmsis_dap_vid_pid 0x04d8 0x00df
transport select swd
set FLASH_SIZE 0x10000
source [find target/stm32f1x.cfg]
adapter speed 1000
6.3 命令行测试 OpenOCD 连接
bash
sudo /opt/openocd-0.12.0/bin/openocd \
-s /opt/openocd-0.12.0/share/openocd/scripts \
-f /home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/dap.cfg \
-c "init" \
-c "reset halt" \
-c "shutdown"
如果成功,应能看到类似:
text
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
如果还是:
text
unable to find a matching CMSIS-DAP device
说明 OpenOCD 仍然没枚举到 DAP。
6.4 OpenOCD 烧录命令
bash
sudo /opt/openocd-0.12.0/bin/openocd \
-s /opt/openocd-0.12.0/share/openocd/scripts \
-f /home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/dap.cfg \
-c "program \"/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/build/Debug/motor_wlwg.elf\" verify reset exit"
6.5 CLion 配置 OpenOCD 调试
如果 OpenOCD 命令行测试已经成功,再在 CLion 中配置。
打开:
text
Run -> Edit Configurations -> + -> Embedded GDB Server
填写:
text
Name:
OpenOCD Debug
Target:
motor_wlwg
Executable:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/build/Debug/motor_wlwg.elf
Debugger:
Custom GDB
GDB:
/opt/st/stm32cubeclt_1.21.0/GNU-tools-for-STM32/bin/arm-none-eabi-gdb
GDB Server:
/opt/openocd-0.12.0/bin/openocd
GDB Server args:
-s /opt/openocd-0.12.0/share/openocd/scripts -f /home/sg/SG/sg-db-stm32-v2/motor_wlwg_control/dap.cfg
'target remote' args:
localhost:3333
Working directory:
/home/sg/SG/sg-db-stm32-v2/motor_wlwg_control
注意:
text
OpenOCD 默认 GDB 端口是 3333
不要把 OpenOCD 填到 GDB 栏。OpenOCD 是 GDB Server,不是 GDB。
7. 实时调试说明
pyOCD、OpenOCD、ST-Link、J-Link 都可以做:
text
断点
单步
查看变量
查看寄存器
查看调用栈
但是命中断点或单步时,MCU 会停止运行。
对电机控制程序要特别注意:
text
不要在电机运行控制环中随意打断点
因为 CPU 停止后 PWM、控制环、通信状态都可能异常。
如果要"不停机实时观察变量",更推荐:
text
串口周期输出关键变量
SWO / ITM
SEGGER RTT
示波器 / 逻辑分析仪
J-Link 体验通常更好,是因为它对 RTT、SWO、Live Watch 的工具链支持更完整。但普通断点本质上仍然会 halt CPU。
8. 当前工程推荐日常流程
8.1 编译
bash
cmake --preset Debug
cmake --build --preset Debug
8.2 烧录运行
bash
./pyocd_flash.sh
8.3 CLion 调试
使用配置:
text
pyOCD Debug
确认 GDB Server 日志有:
text
GDB server listening on port 3333
Client 1 connected on port 3333
然后即可在 CLion 中断点、单步、查看变量。