沁恒 CH32V208(三): CH32V208 Ubuntu22.04 Makefile VSCode環境配置
以沁恒官方的定制版 RISC-V Embedded GCC 和 OpenOCD 為例說明 Ubuntu 下如何配置基于Makefile的開發環境, 如何進行命令行Debug, 以及VSCode下Debug環境的配置目錄
- 沁恒 CH32V208(一): CH32V208WBU6 評估板上手報告和Win10環境配置
- 沁恒 CH32V208(二): CH32V208的儲存結構, 啟動模式和時鐘
- 沁恒 CH32V208(三): CH32V208 Ubuntu22.04 Makefile VSCode環境配置
硬件部分
- CH32V208WBU6 評估板
- WCH-LinkE 或 WCH-Link
硬件環境與Windows下相同, 不詳細介紹
軟件部分
沁恒已經開源WCH-Link的協議, 因此這部分的選項將會很豐富, 這里還是以沁恒官方的定制版 RISC-V Embedded GCC 和 OpenOCD 為例進行說明.
下載
從 http://www.zmynmublwnt.cn/uploads/allimg/dbpfvtrywqb 下載最新的工具鏈. 當前版本是 MRS_Toolchain_Linux_X64_V170.tar.xz, 對于 CH32V208, V1.60版本也能支持. 壓縮包中包含 RISC-V Embedded GCC 和 OpenOCD.
配置
解壓工具鏈,
sudo tar -xvf MRS_Toolchain_Linux_X64_V170.tar.xz
在解壓后的目錄下有一個README, 這個文件比較重要. 因為沁恒每次出新版本都可能有一些變動, 導致前一個版本的 cfg 或者命令行無法使用, 這個 README 中會列舉當前版本可用的燒錄和debug命令, 需要留意.
將工具鏈移動到合適的位置, 并修改owner為root避免誤修改
sudo mkdir -p /opt/gcc-riscv/
sudo mv "MRS_Toolchain_Linux_x64_V1.70/RISC-V Embedded GCC" /opt/gcc-riscv/riscv-wch-embedded-gcc-v1.70
sudo chown -R root:root /opt/gcc-riscv/riscv-wch-embedded-gcc-v1.70
sudo mkdir -p /opt/openocd/
sudo mv MRS_Toolchain_Linux_x64_V1.70/OpenOCD /opt/openocd/wch-openocd-v1.70
sudo chown -R root:root /opt/openocd/wch-openocd-v1.70
額外的動態鏈接庫, 在 beforeinstall/start.sh 里是直接復制到 /usr/lib, 穩妥起見, 還是單獨建一個目錄放進去
sudo mkdir -p /usr/lib/wch/
sudo cp -P beforeinstall/lib* /usr/lib/wch/
sudo ldconfig
配置設備權限
根據 start.sh 中執行的命令, 需要將兩個規則文件復制到 /etc/udev/rules.d.
先檢查一下 /etc/udev/ 下是否已經存在相關的配置, 如果有, 需要和這兩個規則整合一下, 如果沒有, 直接復制然后更新就可以了
sudo cp beforeinstall/50-wch.rules /etc/udev/rules.d
sudo cp beforeinstall/60-openocd.rules /etc/udev/rules.d
# Reload rules
sudo udevadm control --reload-rules
驗證
執行這兩個命令應該能看到正確的輸出, 如果有報錯, 需要先排查問題
~$ /opt/gcc-riscv/riscv-wch-embedded-gcc-v1.70/bin/riscv-none-embed-gcc --version
riscv-none-embed-gcc (xPack GNU RISC-V Embedded GCC, 64-bit) 8.2.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
~$ /opt/openocd/wch-openocd-v1.70/bin/openocd --version
Open On-Chip Debugger 0.11.0+dev-02415-gfad123a16-dirty (2023-02-22-15:09)
Licensed under GNU GPL v2
For bug reports, read
http://www.zmynmublwnt.cn/uploads/allimg/5mvicooagei.html
運行示例項目
基于 CH32V20x 的參考例程 https://www.wch.cn/downloads/CH32V20xEVT_ZIP.html, 調整結構并增加 Makefile, 已經提交至 GitHub, 可以直接導出進行編譯和燒錄.
項目地址: https://github.com/IOsetting/ch32v208-template
從 GitHub 導出項目
git clone http://www.zmynmublwnt.cn/uploads/allimg/npbppoglx2i
根據自己的環境, 調整 Makefile 中的路徑信息, 其它內容可以保持默認
##### Toolchains #######
GCC_TOOCHAIN ?= /opt/gcc-riscv/riscv-wch-embedded-gcc-v1.70/bin
OPENOCD_PATH ?= /opt/openocd/wch-openocd-v1.70/bin
執行編譯
make clean
make
如果CH32V208評估板已經通過 WCH-Link 連接上PC, 可以執行下面的命令進行燒錄
make flash
GDB Debug
打開終端, 用沁恒定制的 openocd 啟動 GDB Server, 注意要連上 WCH-Link, 不然 Server 會報錯退出.
/opt/openocd/wch-openocd-v1.70/bin$ ./openocd -f wch-riscv.cfg
Open On-Chip Debugger 0.11.0+dev-02415-gfad123a16-dirty (2023-02-22-15:09)
Licensed under GNU GPL v2
For bug reports, read
http://www.zmynmublwnt.cn/uploads/allimg/5mvicooagei.html
Info : only one transport option; autoselect 'sdi'
Warn : Transport "sdi" was already selected
Ready for Remote Connections
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Info : WCH-Link-CH549 mode:RV version 2.8
Info : wlink_init ok
Info : clock speed 6000 kHz
Info : [wch_riscv.cpu.0] datacount=2 progbufsize=8
Info : [wch_riscv.cpu.0] Examined RISC-V core; found 1 harts
Info : [wch_riscv.cpu.0] XLEN=32, misa=0x40901105
[wch_riscv.cpu.0] Target successfully examined.
Info : starting gdb server for wch_riscv.cpu.0 on 3333
Info : Listening on port 3333 for gdb connections
在第二個終端中, 啟動 GDB Client
/opt/gcc-riscv/riscv-wch-embedded-gcc-v1.70/bin/riscv-none-embed-gdb Build/app.elf
GNU gdb (xPack GNU RISC-V Embedded GCC, 64-bit) 8.3
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-unknown-linux-gnu --target=riscv-none-embed".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://github.com/sifive/freedom-tools/issues>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from Build/app.elf...
設置GDB參數
(gdb) set mem inaccessible-by-default off
(gdb) set remotetimeout unlimited
(gdb) set architecture riscv:rv32
The target architecture is assumed to be riscv:rv32
連接到GDB服務, load 載入程序, b 設置斷點, c 繼續執行, i r 查看寄存器, i local 查看全部局部變量, list 查看代碼. c過程中可以用Ctrl+C暫停, quit 退出
(gdb) target remote localhost:3333
Remote debugging using localhost:3333
0x00000428 in Delay_Ms (n=n@entry=1000) at Debug/debug.c:74
74 while((SysTick->SR & (1 << 0)) != (1 << 0));
(gdb) i r pc
pc 0x428 0x428 <Delay_Ms+46>
(gdb) load
Loading section .init, size 0x38 lma 0x0
Loading section .vector, size 0x148 lma 0x38
Loading section .text, size 0x1e4c lma 0x180
Loading section .data, size 0x88 lma 0x1fcc
Start address 0x0, load size 8276
Transfer rate: 4 KB/sec, 2069 bytes/write.
(gdb) i r pc
pc 0x0 0x0 <_start>
(gdb) b main
Breakpoint 1 at 0x25e: file User/main.c, line 55.
(gdb) c
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.
Breakpoint 1, main () at User/main.c:55
55 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
(gdb) i r pc
pc 0x25e 0x25e <main>
(gdb) list
50 */
51 int main(void)
52 {
53 u8 i = 0;
54
55 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
56 Delay_Init();
57 USART_Printf_Init(115200);
58 printf("SystemClk:%ld\r\n", SystemCoreClock);
59
(gdb)
配置 VSCode 開發環境
如果以上步驟都已經順利完成, 直接在 VSCode 中打開這個項目目錄就可以了. VSCode 的 Makefile 擴展會自動識別對應的工具鏈和依賴庫, 代碼提示和高亮開箱即用.
需要配置的是編譯和燒錄的快捷命令, 可以通過 Ctrl+Shift+P 調出菜單, 用 Tasks:Configure Task 進行配置, 或者直接在 .vscode 目錄下創建 tasks.json 進行配置
tasks.json 的例子
{
// See http://www.zmynmublwnt.cn/uploads/allimg/zoakyu1qyll
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "clean & build",
"type": "shell",
"command": "make clean; make -j4",
"problemMatcher": []
},
{
"label": "build",
"type": "shell",
"command": "make -j4"
},
{
"label": "build & download",
"type": "shell",
"command": "make -j4; make flash"
}
]
}
配置之后, 可以通過 Alt + Shift + F10 調出 task 菜單, 選擇對應的任務進行編譯或下載.
配置 VSCode Cortex Debug
VSCode 的 Cortex Debug 可以用于 debug CH32V208, 但是不能直接使用, 需要一些調整.
Cortex Debug 降級到 1.4.4
首先是 Cortex Debug 的版本, 當前版本是 1.10.0, 這個版本運行沁恒的 gdb client 會提示如下錯誤
ERROR: GDB major version should be >= 9, yours is 8; GDB could not start as expected. Bad installation or version mismatch. See if you can start gdb from a shell prompt and check its version (Must be >= 9)
沁恒定制的這個gcc已經8.3很久了, 等著沁恒升級不太現實, 只能將 Cortex Debug 降級到 1.4.4 使用, 這是支持gcc 8的最后一個版本. 在 VSCode 的擴展中打開 Cortex Debug 的介紹頁, 在 Uninstall 右側的小箭頭點擊展開, 能看到 Install Another Version 的菜單, 在里面選擇 1.4.4 安裝
配置文件 launch.json
在 .vscode 目錄下新建文件 launch.json, 我使用的配置如下
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: http://www.zmynmublwnt.cn/uploads/allimg/dta1hq05czv
"version": "0.2.0",
"configurations": [
{
"name": "Cortex Debug",
"cwd": "${workspaceFolder}",
"executable": "${workspaceFolder}/Build/app.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"serverpath": "/opt/openocd/wch-openocd-v1.70/bin/openocd",
"configFiles": [
"${workspaceFolder}/Misc/wch-riscv.cfg.v1.70"
],
"runToEntryPoint": "main",
"runToMain": true, // false: run to reset handler
"preLaunchTask": "build", // task from tasks.json
// "preLaunchCommands": ["Build all"], if not using preLaunchTask
"showDevDebugOutput": "raw", // log level: parsed, raw, both(include parsed and raw)
"device": "CH32V208",
"svdFile": "${workspaceFolder}/Misc/ch32v208xx.svd",
"toolchainPrefix": "/opt/gcc-riscv/riscv-wch-embedded-gcc-v1.70/bin/riscv-none-embed"
}
]
}
關于配置項的說明:
- executable: 指向的是當前項目生成的 elf 文件
- servertype: 只能是 openocd
- serverpath: 這個很重要, 必須指向沁恒定制的 openocd 可執行文件
- configFiles: 當前的 openocd 版本是 1.70, 用倉庫里的cfg, 或者用 openocd 同目錄下的 wch-ricsv.cfg 都可以
- preLaunchTask: 填的是 tasks.json 中配置的任務, 如果找不到這個任務, 啟動時會有提示
- showDevDebugOutput: 用于在下方的 DEBUG CONSOLE 輸出 GDB 日志, 可以選 both, parsed, raw, none, 其中 raw是顯示原始內容, parsed 是格式化過的, both 是兩種都顯示
- device: 對于 openocd 貌似可以隨便填
- svdFile: 標識外設寄存器名稱與地址關系的文件, 在debug時可以直接通過寄存器名稱查看對應地址的值, 倉庫中的 svd 是從沁恒的 MounRiver 開發環境中復制過來的.
- toolchainPrefix: 指向沁恒定制的 gcc, 注意是前綴, 不需要帶后面的
-gcc
運行 Debug
配置完成后就可以開始 Debug了, 可以通過右側的 Run And Debug 面板, 點綠色三角形啟動, 也可以按 F5啟動, 我使用的是 IntelliJ IDEA Keybinding, 所以debug快捷鍵和 IDEA 是一樣的, 單步 F8, 繼續 F9, 進入 F7. 在 Run And Debug 面板左側可以觀察變量和外設寄存器對應的值. 非常方便.