CLion+pyocd配置教程

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-gdbserverpyocd_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 中断点、单步、查看变量。

相关推荐
开发笔记-阿牛10 分钟前
解析一款有意思的产品||CK6159A语音芯片设计的儿童专注力训练机-舒尔特玩具
stm32·单片机·嵌入式硬件·音频
残影飞雪1 小时前
ESP32-C5下载烧录固件失败报错的原因
单片机·嵌入式硬件
搞全栈小苏2 小时前
SIMetrix_simplis8.4安装教程——附安装包
stm32·单片机·嵌入式硬件·电力电子
qq_573967823 小时前
STM32控制器
stm32·单片机·嵌入式硬件
某林2124 小时前
履带小车底盘stm32F103RCT6开发
人工智能·stm32·单片机·嵌入式硬件·架构·人机交互·ros2
sramdram5 小时前
低功耗MCU微控制器在动态血糖仪(CGM)中的应用方案
单片机·嵌入式硬件·mcu·mcu微控制器
Dlrb12115 小时前
ARM--IMX6ULL的SDK学习,BSP开发工程管理,以及链接脚本与Makefile的编写
单片机·嵌入式硬件·arm·sdk·链接脚本·i.mx6ull·cortex-a7
李永奉5 小时前
杰理可视化SDK开发-配置ANC主动降噪功能模块教程
单片机·嵌入式硬件·物联网
云泽8086 小时前
嵌入式环境搭建(一):Keil MDK5 安装流程及 DFP 器件包配置实战
嵌入式硬件·keil5·keil mdk5·dfp器件包配置
亿道电子Emdoor6 小时前
【Aitium】PCB导出DXF文件出现eDuplicateRecordName报错
stm32·单片机·嵌入式硬件·altium