一种解决 CLion + OpenOCD无法正常写入和debug的方法

环境

系统:Windows

IDE:Jetbrains CLion + STM32CubeMX

Boards:野火STM32F103RCT6 MINI

仿真器:淘宝10元自制DAPLink

前言

原本这个仿真器使用Keil是可以正常下载调试的,但是作为外观党实在不想使用Keil,所以参考了B站稚晖君的专栏文章

[配置CLion用于STM32开发【优雅の嵌入式开发】](https://www.bilibili.com/read/cv6308000/)

前面一切顺利,直到最后进行程序下载时发现只能刷第一次,后面一直报错

Error: Error connecting DP: cannot read IDR 
Error: [stm32f1x.cpu] not halted (gdb fileio) Polling target stm32f1x.cpu failed, trying to reexamine 
Error: [stm32f1x.cpu] Examination failed Examination failed, GDB will be halted. Polling again in 100ms

Google找了各种论坛,发现有这种问题的人似乎不多,也没有给出一个有效的解决方案,大部分回答都是只能按住RESET进行下载,中途松开RESET按钮,我试了一下确实可以,但这也太折磨了,难道每次刷都要按一下吗?

用此方法进入GDB调试,单步执行时发现在HAL_Init下有一个HAL_MspInit会执行一个叫做__HAL_AFIO_REMAP_SWJ_DISABLE()的函数,好么这不真相大白了,刚刷进去就把SW禁用了,DAPLink包括CMSIS-DAP都是使用的SW模式连接,这不断谁断啊

解决方法

打开CubeMX工程,在Pinout&Configuration里,System Core -> SYS -> Debug 根据实际仿真器选择对应的,我这边使用DAPLink通过SW模式连接,则选择Trace Asynchronous Sw , System Wake-up按需求选择,我这边没有选择,Timebase Source选择默认SysTick。

屏幕截图 2024-07-02 001129.png

Generate之后重新编译进入Clion,可以看到下载后正常退出,而非报错断开连接

Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
adapter speed: 10000 kHz
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: JTAG supported
Info : CMSIS-DAP: FW Version = 1.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 10000 kHz
Info : SWD DPIDR 0x1ba01477
Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected
Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints
Info : [stm32f1x.cpu] Examination succeed
Info : gdb port disabled
[stm32f1x.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0x080003ac msp: 0x2000c000
** Programming Started **
Info : device id = 0x10036414
Info : flash size = 256 KiB
Warn : Adding extra erase range, 0x08000fb8 .. 0x08000fff
** Programming Finished **
shutdown command invoked

Debug也可以正常进入

xPack Open On-Chip Debugger 0.12.0+dev-01557-gdd1758272-dirty (2024-04-02-07:27)

Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
adapter speed: 10000 kHz
Info : CMSIS-DAP: SWD supported
Info : CMSIS-DAP: JTAG supported
Info : CMSIS-DAP: FW Version = 1.0
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 1 TDO = 1 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 10000 kHz
Info : SWD DPIDR 0x1ba01477
Info : [stm32f1x.cpu] Cortex-M3 r1p1 processor detected
Info : [stm32f1x.cpu] target has 6 breakpoints, 4 watchpoints
Info : [stm32f1x.cpu] Examination succeed
Info : starting gdb server for stm32f1x.cpu on 3333
Info : Listening on port 3333 for gdb connections
[stm32f1x.cpu] halted due to breakpoint, current mode: Thread
xPSR: 0x01000000 pc: 0x080003ac msp: 0x2000c000
(((READY)))
Info : tcl server disabled
Info : Listening on port 4444 for telnet connections
Info : accepting 'gdb' connection on tcp/3333
Info : device id = 0x10036414
Info : flash size = 256 KiB
Warn : Prefer GDB command "target extended-remote :3333" instead of "target remo
te :3333"
Debugger connected to tcp:localhost:3333

唯一一点需要注意的是,不知道为什么Clion的debug不会像Keil那样在入口暂停,而是直接开跑,所以最好在入口打一个断点

image-woxt.png

Q.E.D.