MRS?IDE?基于Eclipse,?代碼體驗與同出一處的STM32CubeIDE基本一樣,?相比Keil來說,?體驗好很多了,但是與VScode,?Clion體驗還是差了不少,看了編譯流程,?是使用make,?因此使用Cmake生成Make是沒問題的。
準(zhǔn)備工具:CMake,RISC-V Embedded GCC,make
其中CMake,?make比較好找。在經(jīng)過我試驗之后發(fā)現(xiàn),?WCH使用的RISC-V編譯器與通用的RISC-V編譯器不太一樣,?有些地方是修改過了,?在使用xpack-riscv-none-elf-gcc-12.1.0-2編譯時 core-riscv.c?文件內(nèi)大量的匯編語言是報錯的,?應(yīng)該使用特有指令。所以只能使用MRS安裝路徑下提供的RISC-V Embedded GCC,?
CMakeLists.txt與使用STM32的時候是差不多的,只需要修改少量地方,?一個是指定編譯器平臺,?一個是編譯參數(shù)和鏈接參數(shù)
具體可以參考以下:
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) #?設(shè)置編譯目標(biāo)變量 #?set(PRJ_TARGET?"wch_template") #?設(shè)置項目名稱 project(wch_template?LANGUAGES?C?CXX?ASM) #?設(shè)置項目根目錄路徑變量 #?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設(shè)置 add_compile_options(-march=rv32imac?-mabi=ilp32?-msmall-data-limit=8?-mno-save-restore) #?優(yōu)化設(shè)置 #?add_compile_options(-Os?-g3) #?標(biāo)準(zhǔn)設(shè)置 #?add_compile_options(-std=gnu99) #?編譯設(shè)置 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) #?鏈接設(shè)置 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}")
?設(shè)置Clion工具鏈。
環(huán)境腳本內(nèi)容需要修改你自己的路徑:
set?PATH=path\to\your\MounRiver_Studio\toolchain\RISC-V?Embedded?GCC\bin;%PATH%
? ? ? ?選擇工具鏈
? ?接下來就可以編譯了
和MRS編譯相比:
? ?2.?設(shè)置VSCode
? ?2.1.安裝CMake,?CMake?Tool,?C/C++插件
? 2.2.?配置CMake,?主要配置CMake環(huán)境變量,?編譯平臺,?該配置文件需要在工程根目錄下下新建 .vscode 文件夾,?在 .vscode
?文件夾下新建 settings.json?,?內(nèi)容參考如下,?需要根據(jù)實際情況修改:
{ ????"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里指定了,?路徑環(huán)境變量在?settings.json?里? cmake.environment?指定了。
? ?VSCode底邊欄應(yīng)該是這樣的:
在左邊欄,?點擊Cmake圖標(biāo),?再點擊配置項目
接下來編譯即可,?點擊底邊欄的 Build,?等待完成,?無錯誤的話,?應(yīng)該輸出如下:
示例工程:
使用VSCode,?Clion?均無法調(diào)試下載,?OpenOCD是WCH修改編譯過才能識別WLink,?原生OpenOCD是無法識別WLink的,?期待WCH進(jìn)一步開放相關(guān)資料