硬體環境與Windows下相同, 不詳細介紹
沁恆已經開源WCH-Link的協定, 因此這部分的選項將會很豐富, 這裡還是以沁恆官方的客製化版 RISC-V Embedded GCC 和 OpenOCD 為例進行說明.
從 http://mounriver.com/download 下載最新的工具鏈. 當前版本是 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://openocd.org/doc/doxygen/bugs.html
基於 CH32V20x 的參考例程 https://www.wch.cn/downloads/CH32V20xEVT_ZIP.html, 調整結構並增加 Makefile, 已經提交至 GitHub, 可以直接匯出進行編譯和燒錄.
專案地址: https://github.com/IOsetting/ch32v208-template
從 GitHub 匯出專案
git clone https://github.com/IOsetting/ch32v208-template.git
根據自己的環境, 調整 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
開啟終端, 用沁恆客製化的 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://openocd.org/doc/doxygen/bugs.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 的 Makefile 擴充套件會自動識別對應的工具鏈和依賴庫, 程式碼提示和高亮開箱即用.
需要設定的是編譯和燒錄的快捷命令, 可以通過 Ctrl+Shift+P 調出選單, 用 Tasks:Configure Task 進行設定, 或者直接在 .vscode 目錄下建立 tasks.json 進行設定
tasks.json 的例子
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// 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 可以用於 debug CH32V208, 但是不能直接使用, 需要一些調整.
首先是 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 安裝
在 .vscode 目錄下新建檔案 launch.json, 我使用的設定如下
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"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"
}
]
}
關於設定項的說明:
-gcc
設定完成後就可以開始 Debug了, 可以通過右側的 Run And Debug 面板, 點綠色三角形啟動, 也可以按 F5啟動, 我使用的是 IntelliJ IDEA Keybinding, 所以debug快捷鍵和 IDEA 是一樣的, 單步 F8, 繼續 F9, 進入 F7. 在 Run And Debug 面板左側可以觀察變數和外設暫存器對應的值. 非常方便.