沁恒 CH32V208(三): CH32V208 Ubuntu22.04 Makefile VSCode环境配置

您所在的位置:网站首页 hive环境配置 沁恒 CH32V208(三): CH32V208 Ubuntu22.04 Makefile VSCode环境配置

沁恒 CH32V208(三): CH32V208 Ubuntu22.04 Makefile VSCode环境配置

2023-05-08 15:49| 来源: 网络整理| 查看: 265

目录 沁恒 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://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

沁恒V1.60 的 openocd 不支持 CH32V208, 烧录不报错, 但是不运行, 要用 V1.70 版本才行.

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://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 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: . Find the GDB manual and other documentation resources online at: . 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 = 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: 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" } ] }

关于配置项的说明:

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 面板左侧可以观察变量和外设寄存器对应的值. 非常方便.

相关链接 使用公版 GCC

沁恒当前的 GCC 版本为8, 还没发布更高的版本.

公版GCC可以从这里下载 https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/

如果使用公版GCC 12编译,

需要将编译参数中的 -march=rv32imac 替换为 -march=rv32imac_zicsr, 参考 https://www.blaatschaap.be/on-riscv-bare-metal-toolchains/. 需要增加 _lseek, _close, _isatty 这些函数, 否则编译会报错. 注释掉 __attribute__((interrupt("WCH-Interrupt-fast"))), 这个中断类型在社区版 GCC 中无法识别 避免使用预编译的 .a 库文件, 用到库文件的地方会工作不正常 WCH-Link 的 OpenOCD 源码

仓库地址: https://github.com/Seneral/riscv-openocd-wch

这个验证过能编译, 编译命令

git clone https://github.com/Seneral/riscv-openocd-wch.git cd riscv-openocd-wch/ git submodule update --init --recursive ./bootstrap ./configure --disable-jlink --enable-wlink --disable-werror make -j8 ./src/openocd --version

和沁恒提供的 openocd 的对比

riscv-openocd-wch$ ./src/openocd --version Open On-Chip Debugger 0.11.0+dev-g395b49ca4 (2023-05-04-00:35) Licensed under GNU GPL v2 For bug reports, read http://openocd.org/doc/doxygen/bugs.html riscv-openocd-wch$ /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

如果用这个仓库代码编译的openocd下载, 要使用沁恒定制的V1.60版本的openocd的cfg, V1.60对应的编程器名称是wlink, 在V1.70里改名为wlinke了.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3