CMSIS-DAP和openOCD那些事

这篇文章只是日常瞎折腾,仅仅经验之谈,没有太多技术含量。

出于好奇,在某宝上看到了一些自己做的CMSIS-DAP下载器,说是开源方案,开源的血统天生优于盗版Jlink OB,都是来自于X893的源码修改版,把原先的Jtag替换成了swd。实际上硬件方案与Jlink OB并无二致,都是一颗 stm32f103c8t6,几乎没有其他外围电路。这么多的卖家,只是区分在做工优劣上,并没有速度上明显提升的。买了两个做工比较好的,可以自己diy玩一玩。

下载的一些坑

  1. stm32体验良好

原本只是在Keil上写stm32,发现下载调试都挺快,跟Jlink没太大区别,免驱还自带串口,只能说美滋滋了。毕竟CMSIS-DAP没有像Jlink自带Commander,只是Keil内部调用,不能自己猥琐欲为,所以msys2顺手打下了:

pacman -S openocd

其实还可以到这里下载openOCD二进制程序,openOCD作为CMSIS-DAP的Commander

然后是新建一个配置文件(ocd-stm32.cfg),使用swd接口

interface cmsis-dap
transport select swd
source [find target/stm32f1x.cfg]

然后连接CMSIS-DAP和stm32f103,加载脚本

openocd -f ./ocd-stm32.cfg

这样显示就是连接成功了,可以看到时钟速度是1000 Khz

Open On-Chip Debugger 0.9.0 (2015-05-19-12:09)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : only one transport option; autoselect 'swd'
Warn : Transport "swd" was already selected
adapter speed: 1000 kHz
adapter_nsrst_delay: 100
none separate
cortex_m reset_config sysresetreq
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : CMSIS-DAP: FW Version = 1.0
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 1000 kHz
Info : SWD IDCODE 0x1ba01477
Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints

openOCD默认端口telnet 4444,gdb server 3333,然后telnet连接到 127.0.0.1:4444,就可以猥琐欲为了。下载一个hex文件到flash:

λ telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> halt
 target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0x08000200 msp: 0x20000418
> flash write_image erase ./F103.hex
 auto erase enabled
device id = 0x10036414
flash size = 512kbytes
wrote 2048 bytes from file ./F103.hex in 0.450548s (4.439 KiB/s)
> reset
  1. K60体验极差

作为一个参加NXP智能汽车竞赛的低端玩家,如果也能和stm32一样的体验岂不是美滋滋,就不用来回插着线一堆杂乱的线调试了。按照智能车的尿性,历来采用祖传的IAR调试。显然这种廉价的DAP,不符合IAR高贵的身份。于是我想按照stm32的方式试一试,如果真的跟上面那么简单就错了!!

照着上面的改成了k60,发现怎么也连不上。于是写了下面这样的配置,把时钟降到了100Khz,发现可以稳定连上。找问题的时候发现,Jlink OB的swd时钟确实可以到1000Khz,感觉应该不太会是硬件的问题,但是stm32在1000Kh并没有问题的。

interface cmsis-dap
transport select swd
source [find target/k60.cfg]
adapter_khz 100

msys2给我安装了最新的openOCD(0.10.0)。

$ openocd -f ocd-k60.cfg
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : add flash_bank kinetis k60.flash
adapter speed: 1000 kHz
none separate
cortex_m reset_config sysresetreq
adapter speed: 100 kHz
Info : CMSIS-DAP: SWD  Supported
Info : CMSIS-DAP: Interface Initialised (SWD)
Info : CMSIS-DAP: FW Version = 1.0
Info : SWCLK/TCK = 1 SWDIO/TMS = 1 TDI = 0 TDO = 0 nTRST = 0 nRESET = 1
Info : CMSIS-DAP: Interface ready
Info : clock speed 100 kHz
Info : SWD DPIDR 0x2ba01477
Info : MDM: Chip is unsecured. Continuing.
Info : k60.cpu: hardware has 6 breakpoints, 4 watchpoints

发现并没有什么不对,于是。。。

> halt
 target halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0x000005c0 msp: 0x20000870
> flash write_image erase DZ.hex
 auto erase enabled
Flash operation not possible in current run mode: SMC_PMSTAT: 0x0
Issue a 'reset init' command.
failed erasing sectors 0 to 0
embedded:startup.tcl:476: Error:
in procedure 'program'
in procedure 'program_error' called at file "embedded:startup.tcl", line 532
at file "embedded:startup.tcl", line 476

错误出在这里

Flash operation not possible in current run mode: SMC_PMSTAT: 0x0

找了好半天,我回忆起很久以前下载是没有这个问题的,又看了一波NXP的官网,猜测可能是我仍然在使用早期的K60DZ导致的,所以退回到了(0.9.0)版,一切正常。同样在OpenOCD上CMSIS-DAP和Jlink OB的下载速度还是有一定差距的,Jlink OB和DAP在stm32上的下载速度是接近的。至于介于1000K - 100Khz的频率并没有尝试,默认是IAR也是100Khz,也没有感觉难以忍受。

λ telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> halt
 target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x000005c4 msp: 0x20000870
> program DZ.elf
 MDM: Chip is unsecured. Continuing.
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x000004d8 msp: 0x20000888
** Programming Started **
auto erase enabled
Probing flash info for bank 0
flash configuration field erased, please reset the device
wrote 2048 bytes from file DZ.elf in 1.068414s (1.872 KiB/s)
** Programming Finished **
> reset
 MDM: Chip is unsecured. Continuing.

其实CMSIS-DAP在K60上也没有那么差

一些总结

  1. elf,hex,bin文件格式

elf文件是交叉编译器直接产生的包含了代码各种段和调试信息,信息量最大

bin只是单纯的数据和机器指令,实际下载到单片机中的也就是这个东西

hex相对bin格式多了一个起始地址,是以文本格式保存

hex和bin分别可以同过elf得到

*-objcopy -O binary out.elf out.bin
*-objcopy -O ihex out.elf out.hex
  1. openOCD下载

程序一般是这么下载的

flash write_image erase out.hex

官方文档有提到通过 program 命令来下载elf,bin,hex,下载的起始地址是必要的,但是bin格式没有,所以需要在后面指定起始地址,否则为缺省值。官方的文档对openOCD解释的可以说是很充分了,很有参考价值。

program out.elf
program out.hex
program out.bin [address]

K60带了这么一个命令,应该是用于解锁K60用的。help 命令有详细的说明

kinetis mdm <reset|mass_erase...>

你可能感兴趣的:(ARM)