本帖最后由 obit 于 2022-6-17 00:59 编辑
#申请原创#
一:下载以下软件
1.mingw64
x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z
https://nchc.dl.sourceforge.net/project/mingw-w64/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/8.1.0/threads-posix/seh/x86_64-8.1.0-release-posix-seh-rt_v6-rev0.7z
将bin目录下的mingw32-make.exe改名成make.exe
2.msys64
msys2-x86_64-latest.tar.xz
https://mirrors.tuna.tsinghua.edu.cn/msys2/distrib/msys2-x86_64-latest.tar.xz
这里的make命令不支持中文路径,改名成任意名字
3.xPack GNU GCC 最新公版
xpack-riscv-none-elf-gcc
https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v12.1.0-2/xpack-riscv-none-elf-gcc-12.1.0-2-win32-x64.zip
4.OpenOCD
从MRS安装目录提取,linux用户可直接从http://file.mounriver.com/tools/MRS_Toolchain_Linux_x64_V1.40.tar.xz下载
二:分别解压上述软件到指定目录,并在系统环境变量添加上述软件bin目录
此电脑->属性->高级系统设置->环境变量->Path
三:编写Makefile
四:VScode安装Shortcut Menu Bar扩展
在"配置->扩展->Shortcut Menu Bar configuration"中为VScode增加四个Button,
执行命令分别为
workbench.action.tasks.build
workbench.action.tasks.runTask | clean
workbench.action.tasks.runTask | download
workbench.action.debug.start
依次是构建、清除、下载、调试
五:配置VScode json文件,相关文件路径请改为自己的安装目录
tasks.json
{
"tasks": [
{
"type": "shell",
"label": "build",
"command": "make -j",
"problemMatcher":"$gcc",
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成任务。"
},
{
"type": "shell",
"label": "clean",
"command": "make purge",
"group": {
"kind": "build"
},
"detail": "清除所有生成文件"
},
{
"type": "shell",
"label": "erase",
"command": "openocd",
"args": [
"-c",
"adapter driver wlink;wlink_set;jtag newtap riscv cpu -irlen 5 -expected-id 0x00001;target create riscv.cpu.0 riscv -chain-position riscv.cpu;riscv.cpu.0 configure -work-area-phys 0x80000000 -work-area-size 10000 -work-area-backup 1;flash bank riscv.flash wch_riscv 0x00000000 0 0 0 riscv.cpu.0",
"-c",
"init;halt;flash erase_sector wch_riscv 0 last",
"-c",
"exit"
]
},
{
"type": "shell",
"label": "program",
"command": "openocd",
"args": [
"-c",
"adapter driver wlink;wlink_set;jtag newtap riscv cpu -irlen 5 -expected-id 0x00001;target create riscv.cpu.0 riscv -chain-position riscv.cpu;riscv.cpu.0 configure -work-area-phys 0x80000000 -work-area-size 10000 -work-area-backup 1;flash bank riscv.flash wch_riscv 0x00000000 0 0 0 riscv.cpu.0",
"-c",
"init;halt;program build/output.hex verify;wlink_reset_resume;exit"
]
},
{
"label": "download",
"dependsOrder": "sequence",
"dependsOn": [
"erase",
"program"
]
},
{
"label": "start-openocd",
"type": "shell",
"command": "openocd",
"args": [
"-c",
"adapter driver wlink;wlink_set;jtag newtap riscv cpu -irlen 5 -expected-id 0x00001;target create riscv.cpu.0 riscv -chain-position riscv.cpu;riscv.cpu.0 configure -work-area-phys 0x80000000 -work-area-size 10000 -work-area-backup 1;flash bank riscv.flash wch_riscv 0x00000000 0 0 0 riscv.cpu.0",
],
"isBackground": true,
"problemMatcher": [
{
"pattern": [
{
"regexp": ".",
"file": 1,
"location": 2,
"message": 3
}
],
"background": {
"activeOnStart": true,
"beginsPattern": ".",
"endsPattern": ".",
}
}
],
"detail": "执行openocd后台"
},
{
"label": "stop-openocd",
"command": "echo ${input:terminate}",
"type": "shell"
}
],
"inputs": [
{
"id": "terminate",
"type": "command",
"command": "workbench.action.tasks.terminate",
"args": "terminateAll"
}
],
"version": "2.0.0"
}
launch.json
{
"configurations": [
{
"name": "C/C++: riscv-none-elf-gcc.exe 生成和调试活动文件",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/output.elf",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "riscv-none-elf-gdb.exe",
"setupCommands": [
{
"description": "设置MCU架构",
"text": "set architecture riscv:rv32",
"ignoreFailures": false
},
{
"description": "指定调试目标文件",
"text": "file C:/Users/hys/Desktop/CH32V103/build/output.elf",
"ignoreFailures": false
},
{
"description": "连接GDB Server",
"text": "target extended-remote :3333",
"ignoreFailures": false
},
{
"description": "加载到MCU",
"text": "load",
"ignoreFailures": false
},
{
"text": "break main"
}
],
"preLaunchTask": "start-openocd",
"postDebugTask": "stop-openocd",
// "launchCompleteCommand": "None",
}
],
"version": "2.0.0"
}
六:注意
代码中所有__attribute__((interrupt("WCH-Interrupt-fast"))),需改为__attribute__((interrupt()))。参见http://m.**/bbs_thread-1155567-1-1.html
CFLAGS -march=rv32imac要改为-march=rv32imac_zicsr_zifencei
LDFLAGS 要增加-gdwarf-4,默认是dwarf-5调试有问题
GDB必须先执行命令set architecture riscv:rv32,否则连接Target会报错
Linux用户就不用下载mingw64和msys64了,只要设置好环境变量,gcc下载for linux的,其他基本一致
|