目錄
- 沁恆CH32F103C8T6(一): Keil5環境配置,示例運行和燒錄
- 沁恆CH32F103C8T6(二): Linux PlatformIO環境配置, 示例運行和燒錄
- 沁恆CH32F103C8T6(三): PlatformIO DAPLink和WCHLink下載配置
使用 DAP-Link 燒錄
DAP-Link 是ARM官方的一款開源的調試仿真器,也叫CMSIS-DAP, 幾乎支持所有Cortex-M內核的MCU,不挑品牌廠家. 其功能等價於ST的ST-Link.
在Win10 Keil5 MDK中可以不需驅動, 不需配置, 即插即用. 在 Linux 的 PlatformIO 中, 需要做一些配置才能正常使用.
1. 首先定位到 PlatformIO 的配置目錄
默認的目錄是 /home/[你的用戶名]/.platformio/ , 下面能看到 packages, penv, platforms 這幾個目錄, 這一步很重要, 因為需要往配置目錄中添加新文件
2. 在開發板配置中添加 CH32F103C8
在 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"的開發板
3. 修改 platformio.ini
將 board 修改為自定義的 bluepill_ch32f103c8
, 修改之后配置類似於
[env:bluepill_ch32f103c8_dap]
platform = ststm32
board = bluepill_ch32f103c8
framework = cmsis
upload_protocol = cmsis-dap
debug_tool = cmsis-dap
4. 在OpenOCD配置中添加 CH32F1X
在 packages/tool-openocd/scripts/target 目錄下, 新增一個 ch32f1x.cfg 文件, 內容為
# CH32F103x
set CPUTAPID 0x2ba01477
source [find target/stm32f1x.cfg]
配置的內容, 是在 stm32f1x.cfg 這個配置的基礎上定義芯片ID是 0x2ba01477, 這樣就不需要在 platformio.ini 中額外配置了
5. 燒錄
大部分情況下, 這時候連上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
6. 調節頻率
如果出現這樣的錯誤
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 燒錄
WCH-Link是基於WCH549芯片的下載和調試模塊, 支持3.3V/5V雙電源輸出, 支持USB轉TTL串口, 用LED指示燈標識工作狀態, 有兩種工作模式:
- CMSIS-DAP模式: 可以用於帶有SWD接口的 ARM 內核MCU的調試和下載
- RISC-V模式: 可用於RISC-V架構的沁恆系列MCU調試和下載,
資料查看
- WCH-Link仿真調試器模塊 http://www.wch.cn/products/WCH-Link.html
- 資料下載 http://www.wch.cn/bbs/thread-71088-1.html
WCH-Link 使用的是帶USB和串口功能的8位MCU做主控, 價格非常有競爭力, 當前的成本估計在七元以內,
這個價格ST-Link和DAP-Link基本上是做不到的, 主控芯片的成本都不止這個數了.
1. 在開發板配置中添加 CH32F103
與 DAP-Link 的配置一樣
2. 修改 platformio.ini
與 DAP-Link 的配置一樣
3. 在OpenOCD配置中添加 CH32F1X
在 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 設備.
4. 修改 99-platformio-udev.rules
安裝 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讓權限生效
5. 燒錄
以上配置正確的話, 燒錄輸出為
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
進行切換