DAP-Link 是ARM官方的一款开源的调试仿真器,也叫CMSIS-DAP, 几乎支持所有Cortex-M内核的MCU,不挑品牌厂家. 其功能等价于ST的ST-Link.
在Win10 Keil5 MDK中可以不需驱动, 不需配置, 即插即用. 在 Linux 的 PlatformIO 中, 需要做一些配置才能正常使用.
默认的目录是 /home/[你的用户名]/.platformio/ , 下面能看到 packages, penv, platforms 这几个目录, 这一步很重要, 因为需要往配置目录中添加新文件
在 platforms/ststm32/boards 目录下, 新增一个 bluepill_ch32f103c8.json 文件, 文件内容如下,
{ "build": { "arduino": { "variant_h": "variant_PILL_F103Cx.h" }, "core": "stm32", "cpu": "cortex-m3", "extra_flags": "-DSTM32F1 -DSTM32F103xB", "f_cpu": "72000000L", "hwids": [ [ "0x1EAF", "0x0003" ], [ "0x1EAF", "0x0004" ] ], "mcu": "stm32f103c8t6", "product_line": "STM32F103xB", "variant": "STM32F1xx/F103C8T_F103CB(T-U)", "zephyr": { "variant": "stm32_min_dev_blue" } }, "debug": { "default_tools": [ "stlink" ], "jlink_device": "STM32F103C8", "openocd_extra_args": [ "-c", "reset_config none" ], "openocd_target": "ch32f1x", "svd_path": "STM32F103xx.svd" }, "frameworks": [ "arduino", "mbed", "cmsis", "libopencm3", "stm32cube", "zephyr" ], "name": "BluePill CH32F103C8", "upload": { "maximum_ram_size": 20480, "maximum_size": 65536, "protocol": "stlink", "protocols": [ "jlink", "cmsis-dap", "stlink", "blackmagic", "mbed", "dfu" ] }, "url": "http://www.wch.cn/products/CH32F103.html", "vendor": "Generic" }
这个文件是基于 bluepill_f103c8.json 修改的, 主要就是修改了 "openocd_target": "ch32f1x",
, 更换了 openocd 的配置文件.
添加之后, 在 PlatformIO 的 Boards 中, 就能搜索到名为"BluePill CH32F103C8"的开发板
将 board 修改为自定义的 bluepill_ch32f103c8
, 修改之后配置类似于
[env:bluepill_ch32f103c8_dap] platform = ststm32 board = bluepill_ch32f103c8 framework = cmsis upload_protocol = cmsis-dap debug_tool = cmsis-dap
在 packages/tool-openocd/scripts/target 目录下, 新增一个 ch32f1x.cfg 文件, 内容为
# CH32F103x set CPUTAPID 0x2ba01477 source [find target/stm32f1x.cfg]
配置的内容, 是在 stm32f1x.cfg 这个配置的基础上定义芯片ID是 0x2ba01477, 这样就不需要在 platformio.ini 中额外配置了
大部分情况下, 这时候连上DAP-Link就可以烧录了, 输出为
openocd -d2 -s /home/milton/.platformio/packages/tool-openocd/scripts -f interface/cmsis-dap.cfg -c "transport select swd" -f target/ch32f1x.cfg -c "reset_config none" -c "program {.pio/build/bluepill_ch32f103c8_dap/firmware.elf} verify reset; shutdown;" xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:15) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html debug_level: 2 swd Info : DEPRECATED target event trace-config; use TPIU events {pre,post}-{enable,disable} none separate Info : CMSIS-DAP: SWD supported Info : CMSIS-DAP: Atomic commands supported Info : CMSIS-DAP: FW Version = 2.0.0 Info : CMSIS-DAP: Serial# = 205544643577 Info : CMSIS-DAP: Interface Initialised (SWD) 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 DPIDR 0x2ba01477 Info : stm32f1x.cpu: Cortex-M3 r2p1 processor detected Info : stm32f1x.cpu: target has 6 breakpoints, 4 watchpoints Info : starting gdb server for stm32f1x.cpu on 3333 Info : Listening on port 3333 for gdb connections target halted due to debug-request, current mode: Thread xPSR: 0x01000000 pc: 0xfffffffe msp: 0xfffffffc ** Programming Started ** Info : device id = 0x20000410 Info : flash size = 64kbytes ** Programming Finished ** ** Verify Started ** ** Verified OK ** ** Resetting Target ** shutdown command invoked
如果出现这样的错误
target halted due to debug-request, current mode: Thread xPSR: 0x01000000 pc: 0x0800014c msp: 0x20005000 ** Programming Started ** Info : device id = 0x20000410 Info : flash size = 64kbytes Info : SWD DPIDR 0x2ba01477 Error: Failed to write memory at 0x2000003c Error: error writing to flash at address 0x08000000 at offset 0x00000000 embedded:startup.tcl:1070: Error: ** Programming Failed ** in procedure 'program' in procedure 'program_error' called at file "embedded:startup.tcl", line 1135 at file "embedded:startup.tcl", line 1070 *** [upload] Error 1
说明DAP-Link已经正常识别芯片, 但是在写入时出错, 可以将频率降低后再试. 频率的设置在
packages/tool-openocd/scripts/target/stm32f1x.cfg
修改其中的
# JTAG speed should be <= F_CPU/6. F_CPU after reset is 8MHz, so use F_JTAG = 1MHz adapter speed 1000
可以设置为500或800后再次尝试
WCH-Link是基于WCH549芯片的下载和调试模块, 支持3.3V/5V双电源输出, 支持USB转TTL串口, 用LED指示灯标识工作状态, 有两种工作模式:
资料查看
WCH-Link 使用的是带USB和串口功能的8位MCU做主控, 价格非常有竞争力, 当前的成本估计在七元以内,
这个价格ST-Link和DAP-Link基本上是做不到的, 主控芯片的成本都不止这个数了.
与 DAP-Link 的配置一样
与 DAP-Link 的配置一样
在 packages/tool-openocd/scripts/target 目录下, 新增 ch32f1x.cfg 文件, 内容为
# CH32F103x set CPUTAPID 0x2ba01477 cmsis_dap_vid_pid 0x1a86 0x8011 source [find target/stm32f1x.cfg]
与 DAP-Link 的配置相比, 增加了一行 cmsis_dap_vid_pid 0x1a86 0x8011
, 这样 openocd 才能将 WCH-Link 识别为 CMSIS DAP 设备.
安装 PlatformIO 的时候, 应该在 /etc/udev/rules.d/ 目录下添加过 99-platformio-udev.rules 这个文件, 用于给 USB 设备添加 0666 权限.
而 WCH-Link 对于 PlatformIO 来说是新设备, 在 99-platformio-udev.rules 中并未对其添加规则, 如果不设置, 会出现权限错误
Error: could not open device 0x1a86:0x8011: Access denied (insufficient permissions) Error: unable to open CMSIS-DAP device 0x1a86:0x8011 Error: unable to find a matching CMSIS-DAP device Error: No Valid JTAG Interface Configured. *** [upload] Error 255
在 99-platformio-udev.rules 中需要新增 WCH Link 的配置, 在最底下添加两行
# WCH Link (CMSIS-DAP compatible adapter) ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="8011", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1", ENV{ID_MM_PORT_IGNORE}="1"
保存后执行
sudo udevadm control --reload-rules
然后重新拔插WCHLink让权限生效
以上配置正确的话, 烧录输出为
CURRENT: upload_protocol = cmsis-dap openocd -d2 -s /home/milton/.platformio/packages/tool-openocd/scripts -f interface/cmsis-dap.cfg -c "transport select swd" -f target/ch32f1x.cfg -c "reset_config none" -c "program {.pio/build/bluepill_f103c8_ch32/firmware.elf} verify reset; shutdown;" xPack OpenOCD x86_64 Open On-Chip Debugger 0.11.0+dev (2021-10-16-21:15) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html debug_level: 2 swd Info : DEPRECATED target event trace-config; use TPIU events {pre,post}-{enable,disable} none separate Info : CMSIS-DAP: SWD supported Info : CMSIS-DAP: FW Version = 2.0.0 Info : CMSIS-DAP: Interface Initialised (SWD) 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 DPIDR 0x2ba01477 Info : stm32f1x.cpu: Cortex-M3 r2p1 processor detected Info : stm32f1x.cpu: target has 6 breakpoints, 4 watchpoints Info : starting gdb server for stm32f1x.cpu on 3333 Info : Listening on port 3333 for gdb connections target halted due to debug-request, current mode: Thread xPSR: 0x01000000 pc: 0x0800014c msp: 0x20005000 ** Programming Started ** Info : device id = 0x20000410 Info : flash size = 64kbytes ** Programming Finished ** ** Verify Started ** ** Verified OK ** ** Resetting Target ** shutdown command invoked
如果需要两种烧录调试工具并存的话, 可以创建两个board配置, 在 platformio.ini 中创建两个env分别使用不同的board, 使用时通过 [platformio] default_envs = xxxx
进行切换