MRS?IDE?基于Eclipse,?代码体验与同出一处的STM32CubeIDE基本一样,?相比Keil来说,?体验好很多了,但是与VScode,?Clion体验还是差了不少,看了编译流程,?是使用make,?因此使用Cmake生成Make是没问题的。
准备工具:CMake,RISC-V Embedded GCC,make
其中CMake,?make比较好找。在经过我试验之后发现,?WCH使用的RISC-V编译器与通用的RISC-V编译器不太一样,?有些地方是修改过了,?在使用xpack-riscv-none-elf-gcc-12.1.0-2编译时 core-riscv.c?文件内大量的汇编语言是报错的,?应该使用特有指令。所以只能使用MRS安装路径下提供的RISC-V Embedded GCC,?
CMakeLists.txt与使用STM32的时候是差不多的,只需要修改少量地方,?一个是指定编译器平台,?一个是编译参数和链接参数
具体可以参考以下:
cmake_minimum_required(VERSION?3.15) set(CMAKE_SYSTEM_NAME?Generic) set(CMAKE_SYSTEM_PROCESSOR?riscv) if?(0) ????set(CMAKE_C_COMPILER?riscv-none-elf-gcc) ????set(CMAKE_CXX_COMPILER?riscv-none-elf-g++) ????set(CMAKE_ASM_COMPILER?riscv-none-elf-gcc) ????set(CMAKE_LINKER?riscv-none-elf-ld) ????set(CMAKE_AR?riscv-none-elf-ar) ????set(CMAKE_OBJCOPY?riscv-none-elf-objcopy) ????set(CMAKE_OBJDUMP?riscv-none-elf-objdump) ????set(SIZE?riscv-none-elf-size) ????set(GDB?riscv-none-elf-gdb) else() ????set(CMAKE_C_COMPILER?riscv-none-embed-gcc) ????set(CMAKE_CXX_COMPILER?riscv-none-embed-g++) ????set(CMAKE_ASM_COMPILER?riscv-none-embed-gcc) ????set(CMAKE_LINKER?riscv-none-embed-ld) ????set(CMAKE_AR?riscv-none-embed-ar) ????set(CMAKE_OBJCOPY?riscv-none-embed-objcopy) ????set(CMAKE_OBJDUMP?riscv-none-embed-objdump) ????set(SIZE?riscv-none-embed-size) ????set(GDB?riscv-none-embed-gdb) endif?() set(CMAKE_TRY_COMPILE_TARGET_TYPE?STATIC_LIBRARY) #?set(CMAKE_EXECUTABLE_SUFFIX_C?".elf") set(CMAKE_C_STANDARD?11) set(CMAKE_CXX_STANDARD?17) #?设置编译目标变量 #?set(PRJ_TARGET?"wch_template") #?设置项目名称 project(wch_template?LANGUAGES?C?CXX?ASM) #?设置项目根目录路径变量 #?set(PRJ_DIR?"C:/UserTemp/wch_cmake_template") #?add_executable(${PRJ_TARGET}) #?set_target_properties(${PRJ_TARGET}?PROPERTIES?LINKER_LANGUAGE?C) #?target_sources(${PRJ_TARGET}?PUBLIC #?????????"${PRJ_DIR}/User/main.c") #?MCU设置 add_compile_options(-march=rv32imac?-mabi=ilp32?-msmall-data-limit=8?-mno-save-restore) #?优化设置 #?add_compile_options(-Os?-g3) #?标准设置 #?add_compile_options(-std=gnu99) #?编译设置 add_compile_options( ????????-Werror ????????-Wunused ????????-Wuninitialized ????????-fmessage-length=0 ????????-ffunction-sections ????????-fdata-sections ????????-fsigned-char) #?处理ASM文件 add_compile_options($<$:-x$assembler-with-cpp>) if?("${CMAKE_BUILD_TYPE}"?STREQUAL?"Release") ????message(STATUS?"Maximum?optimization?for?speed") ????add_compile_options(-Ofast) elseif?("${CMAKE_BUILD_TYPE}"?STREQUAL?"RelWithDebInfo") ????message(STATUS?"Maximum?optimization?for?speed,?debug?info?included") ????add_compile_options(-Ofast?-g) elseif?("${CMAKE_BUILD_TYPE}"?STREQUAL?"MinSizeRel") ????message(STATUS?"Maximum?optimization?for?size") ????add_compile_options(-Os) else?() ????message(STATUS?"Minimal?optimization,?debug?info?included") ????add_compile_options(-Og?-g) endif?() include_directories( ????????"Driver/CH32V307/Peripheral/inc" ????????"Driver/CH32V307/Core" ????????"Driver/CH32V307/Debug" ????????"User") file(GLOB_RECURSE?SOURCES ????????"User/*.c" ????????"Startup/startup_ch32v30x_D8C.S" ????????"Driver/CH32V307/Core/*.c" ????????"Driver/CH32V307/Debug/*.c" ????????"Driver/CH32V307/Peripheral/src/*.c") set(LINKER_SCRIPT?${CMAKE_SOURCE_DIR}/CH32V307VCT6_FLASH.ld) #?链接设置 add_link_options(-march=rv32imac?-mabi=ilp32) add_link_options(-ffunction-sections?-fdata-sections?-fmessage-length=0?-fstack-usage) #add_link_options(-std=gnu11) add_link_options(-Wall?-Wfatal-errors) add_link_options(-Wl,-check-sections?-Wl,-fatal-warnings?-Wl,--gc-sections?-Wl,--print-memory-usage) add_link_options(-T?${LINKER_SCRIPT}) add_link_options(-nostartfiles?-Xlinker?--gc-sections) add_link_options("-Wl,-Map,${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map"?-Wl,--cref) add_link_options(--specs=nano.specs?--specs=nosys.specs) add_executable(${PROJECT_NAME}.elf?${SOURCES}?${LINKER_SCRIPT}) set(HEX_FILE?${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) set(BIN_FILE?${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) set(LIST_FILE?${PROJECT_BINARY_DIR}/${PROJECT_NAME}.list) add_custom_command(TARGET?${PROJECT_NAME}.elf?POST_BUILD ????????COMMAND?${SIZE}?--format=berkeley?$????????COMMAND?${CMAKE_OBJDUMP}?--all-headers?--demangle?--disassemble?$?>?${LIST_FILE} ????????COMMAND?${CMAKE_OBJCOPY}?-O?ihex?$?${HEX_FILE} ????????COMMAND?${CMAKE_OBJCOPY}?-O?binary?$?${BIN_FILE} ????????COMMENT?"Building?${HEX_FILE} Building?${BIN_FILE}")
?设置Clion工具链。
环境脚本内容需要修改你自己的路径:
set?PATH=path\to\your\MounRiver_Studio\toolchain\RISC-V?Embedded?GCC\bin;%PATH%
? ? ? ?选择工具链
? ?接下来就可以编译了
和MRS编译相比:
? ?2.?设置VSCode
? ?2.1.安装CMake,?CMake?Tool,?C/C++插件
? 2.2.?配置CMake,?主要配置CMake环境变量,?编译平台,?该配置文件需要在工程根目录下下新建 .vscode 文件夹,?在 .vscode
?文件夹下新建 settings.json?,?内容参考如下,?需要根据实际情况修改:
{ ????"C_Cpp.default.intelliSenseMode":?"gcc-x86", ????"C_Cpp.intelliSenseEngine":"Default", ????"cmake.cmakePath":?"C:/UserProgram/PortableApp/msys2/mingw64/bin/cmake.exe", ????"cmake.buildDirectory":?"${workspaceFolder}/cmake-build", ????"cmake.buildEnvironment":?{ ????????"PATH":?"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin" ????}, ????"cmake.environment":?{ ????????"PATH":?"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin" ????}, ????"cmake.defaultVariants":?{ ????????"buildType":?{ ????????????"default":?"debug", ????????????"description":?"The?build?type.", ????????????"choices":?{ ????????????????"debug":?{ ????????????????????"short":?"Debug", ????????????????????"long":?"Disable?optimizations?-?include?debug?information.", ????????????????????"buildType":?"Debug" ????????????????}, ????????????????"release":?{ ????????????????????"short":?"Release", ????????????????????"long":?"Optimize?for?speed?-?exclude?debug?information.", ????????????????????"buildType":?"Release" ????????????????}, ????????????????"minsize":?{ ????????????????????"short":?"MinSizeRel", ????????????????????"long":?"Optimize?for?smallest?binary?size?-?exclude?debug?information.", ????????????????????"buildType":?"MinSizeRel" ????????????????}, ????????????????"reldeb":?{ ????????????????????"short":?"RelWithDebInfo", ????????????????????"long":?"Optimize?for?speed?-?include?debug?information.", ????????????????????"buildType":?"RelWithDebInfo" ????????????????} ????????????} ????????} ????}, ????"cmake.additionalKits":?[ ????], ????"cmake.configureSettings":?{ ????????"CMAKE_ASM_COMPILER":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/riscv-none-elf-gcc.exe", ????????"CMAKE_CXX_COMPILER":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/riscv-none-elf-g++.exe", ????????"CMAKE_C_COMPILER":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/riscv-none-elf-gcc.exe", ????????"CMAKE_LINKER":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/riscv-none-elf-ld.exe", ????????"CMAKE_AR":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/", ????????"CMAKE_MAKE_PROGRAM":"C:/UserProgram/PortableApp/msys2/mingw64/bin/mingw32-make.exe", ????????"CMAKE_OBJCOPY":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/riscv-none-elf-objcopy.exe", ????????"CMAKE_OBJDUMP":"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin/riscv-none-elf-objdump.exe", ????????"CMAKE_SYSTEM_NAME":"Generic", ????????"CMAKE_BUILD_TYPE":"Debug", ????????"CMAKE_SYSTEM_PROCESSOR":"riscv" ????}, ????"cmake.copyCompileCommands":?"${workspaceFolder}/cmake-build/Compile.json", ????"cmake.configureEnvironment":?{ ????????"PATH":?"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin" ????}, ????"cmake.generator":"MinGW?Makefiles", ????"cmake.configureOnOpen":?false, ????"terminal.integrated.profiles.windows":?{ ????????"PWSH?7?Profile":?{ ????????????"source":?"PowerShell", ????????????"path":?"C:\\Program?Files\\PowerShell\\7\\pwsh.exe" ????????} ????}, ????"terminal.integrated.defaultProfile.windows":?"PWSH?7?Profile", ????"terminal.integrated.env.windows":?{ ????????"PATH":?"C:/UserProgram/MounRiver/MounRiver_Studio/toolchain/RISC-V?Embedded?GCC/bin" ????} }
? ?另外还需配置C/C++插件,?主要是? intelliSenseMode?和?configurationProvider
"configurationProvider":?"ms-vscode.cmake-tools", "intelliSenseMode":?"gcc-x86"
? ?接下配置CMake,?编译平台在CMakeLists.txt里指定了,?路径环境变量在?settings.json?里? cmake.environment?指定了。
? ?VSCode底边栏应该是这样的:
在左边栏,?点击Cmake图标,?再点击配置项目
接下来编译即可,?点击底边栏的 Build,?等待完成,?无错误的话,?应该输出如下:
示例工程:
使用VSCode,?Clion?均无法调试下载,?OpenOCD是WCH修改编译过才能识别WLink,?原生OpenOCD是无法识别WLink的,?期待WCH进一步开放相关资料