本文以【STM32F103ZET6】单片机作为示例来进行演示,标准库/HAL库的工程是通用的,修改CMakeLists.txt里面的源文件和头文件引用部分即可。
更多细节请参考【VSCode编辑+GCC for ARM交叉编译工具链+Makefile构建+OpenOCD调试(基于STM32的标准库)】
下面列出几个主要的配置文件:
- settings.json
- c_cpp_properties.json
- tasks.json
- launch.json
- CMakeLists.txt
- ⭐ MCU链接脚本.ld、MCU汇编启动文件.s、MCU寄存器文件.svd请参考【VSCode编辑+GCC for ARM交叉编译工具链+Makefile构建+OpenOCD调试(基于STM32的标准库)】文章获取。
settings.json:
{
// 字符集编码选择
"files.encoding": "utf8",
// 自动保存任意文件
"files.autoSave": "afterDelay",
// 开启 material icons
"workbench.iconTheme": "office-material-icon-theme",
// theme主题设置
"workbench.colorTheme": "Default Dark Modern",
//粘贴时格式化代码
"editor.formatOnPaste": true,
//保存时格式化代码
"editor.formatOnSave": true,
//设置字体的大小,最小值能设置为6
"editor.fontSize": 15,
//设置字体的粗细
"editor.fontWeight": "500",
//设置字体的样式
// "terminal.integrated.fontFamily":"Courier New",
//使用Ctrl+滚轮缩放编辑区的字体大小
"editor.mouseWheelZoom": true,
//使用Ctrl+滚轮缩放终端Terminal的字体大小
"terminal.integrated.mouseWheelZoom": true,
//设置为false,这样打开新的文件时,不会自动关闭旧的文件
"workbench.editor.enablePreview": false,
"security.workspace.trust.enabled": false,
"VsCodeTaskButtons.showCounter": true,
"VsCodeTaskButtons.tasks": [
{
"label": "$(tools) Build", // 显示标签
"task": "CMake build", // 对应tasks.json里的任务label
"tooltip": "🛠️ build" // 工具提示框
},
{
"label": "$(notebook-delete-cell) Clean",
"task": "CMake clean",
"tooltip": "🧹 clean"
},
{
"label": "$(notebook-delete-cell) $(tools) Re-bulid", //"$(notebook-delete-cell) & $(tools)",
"task": "CMake cleanRebuild",
"tooltip": "🛠️ rebuild" // "🧹 & 🛠️ rebuild"
},
{
"label": "$(zap) Download",
// "task": "CMake download",
"tasks": [
{
"label": "⚓ CMSIS-DAP-Link", //icon copied from https://emojipedia.org/
"task": "flash with CMSIS-DAP-Link"
},
{
"label": "⤵️ ST-Link", //icon copied from https://emojipedia.org/
"task": "flash with ST-Link"
},
{
"label": "🚀 J-Link", //icon copied from https://emojipedia.org/
"task": "flash with J-Link"
}
],
"tooltip": "⚡ download"
}
],
// "terminal.integrated.defaultProfile.windows": "Git Bash",
"C_Cpp.default.cppStandard": "gnu++17",
"C_Cpp.default.intelliSenseMode": "gcc-arm",
"C_Cpp.default.cStandard": "gnu17",
"C_Cpp.default.compilerPath": "E:/embedded_dev_tools/xpack-arm-none-eabi-gcc-13.3.1-1.1/bin/arm-none-eabi-gcc.exe"
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "ARM-GCC",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "E:/embedded_dev_tools/xpack-arm-none-eabi-gcc-13.3.1-1.1/bin/arm-none-eabi-gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++17",
"intelliSenseMode": "gcc-arm",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
tasks.json:
{
//快捷键ctrl+shift+B调出各个task命令
"tasks": [
{
"type": "cmake",
"label": "CMake build",
"command": "build",
"targets": [
"${workspaceRootFolderName}.elf"
],
"group": "build",
"problemMatcher": [
"$gcc"
]
},
{
"type": "cmake",
"label": "CMake cleanRebuild",
"command": "cleanRebuild",
"targets": [
"${workspaceRootFolderName}.elf"
],
"group": "build",
"problemMatcher": [
"$gcc"
]
},
{
"type": "cmake",
"label": "CMake clean",
"command": "clean",
"problemMatcher": [
"$gcc"
]
},
{
"type": "shell",
"label": "flash with CMSIS-DAP-Link",
"command": "openocd",
"args": [
"-f",
"interface/cmsis-dap.cfg",
"-f",
"target/stm32f1x.cfg",
"-c",
"program build/${workspaceRootFolderName}.elf verify reset exit"
],
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "flash with ST-Link",
"command": "openocd",
"args": [
"-f",
"interface/stlink.cfg",
"-f",
"target/stm32f1x.cfg",
"-c",
"program build/${workspaceRootFolderName}.elf verify reset exit"
],
"problemMatcher": [
"$gcc"
],
"group": "build"
},
{
"type": "shell",
"label": "flash with J-Link",
"command": "openocd",
"args": [
"-f",
"interface/jlink-swd.cfg",
"-f",
"target/stm32f1x.cfg",
"-c",
"program build/${workspaceRootFolderName}.elf verify reset exit"
],
"problemMatcher": [
"$gcc"
],
"group": "build"
}
],
"version": "2.0.0"
}
launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug with CMSIS-DAP-Link",
"cwd": "${workspaceRoot}",
"executable": "./build/${workspaceRootFolderName}.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"device": "STM32F103VET6",
// "openOCDLaunchCommands": [
// "adapter speed 10000"
// ],
"configFiles": [
"interface/cmsis-dap.cfg", // D:/Software/arm_riscv_develop_tools/openocd/openocd/scripts/interface
"target/stm32f1x.cfg" // D:/Software/arm_riscv_develop_tools/openocd/openocd/scripts/target
],
"svdFile": "./STM32F103.svd", //选择寄存器文件
"liveWatch": {
"enabled": true,
"samplesPerSecond": 4
},
"searchDir": [],
"runToEntryPoint": "main",
"showDevDebugOutput": "none",
"preLaunchTask": "flash with CMSIS-DAP-Link"
},
{
"name": "Debug with ST-Link",
"cwd": "${workspaceRoot}",
"executable": "./build/${workspaceRootFolderName}.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"device": "STM32F103VET6",
// "openOCDLaunchCommands": [
// "adapter speed 10000"
// ],
"configFiles": [
"interface/stlink.cfg", // D:/Software/arm_riscv_develop_tools/openocd/openocd/scripts/interface
"target/stm32f1x.cfg" // D:/Software/arm_riscv_develop_tools/openocd/openocd/scripts/target
],
"svdFile": "./STM32F103.svd", //选择寄存器文件
"liveWatch": {
"enabled": true,
"samplesPerSecond": 4
},
"searchDir": [],
"runToEntryPoint": "main",
"showDevDebugOutput": "none",
"preLaunchTask": "flash with ST-Link"
},
{
"name": "Debug with J-Link",
"cwd": "${workspaceRoot}",
"executable": "./build/${workspaceRootFolderName}.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"device": "STM32F103VET6",
// "openOCDLaunchCommands": [
// "adapter speed 10000"
// ],
"configFiles": [
"interface/jlink-swd.cfg", // D:/Software/arm_riscv_develop_tools/openocd/openocd/scripts/interface
"target/stm32f1x.cfg" // D:/Software/arm_riscv_develop_tools/openocd/openocd/scripts/target
],
"svdFile": "./STM32F103.svd", //选择寄存器文件
"liveWatch": {
"enabled": true,
"samplesPerSecond": 4
},
"searchDir": [],
"runToEntryPoint": "main",
"showDevDebugOutput": "none",
"preLaunchTask": "flash with J-Link"
}
]
}
CMakeLists.txt:
#THIS FILE IS AUTO GENERATED FROM THE TEMPLATE! DO NOT CHANGE!
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
# cmake最低版本需求
cmake_minimum_required(VERSION 3.20)
# specify cross compilers and tools
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_AR arm-none-eabi-ar)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP arm-none-eabi-objdump)
set(SIZE arm-none-eabi-size)
# 链接的类型设置为STATIC, 以便嵌入式ARM-GNU通过CMake的"编译器检查"
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
# project settings
# .HEX .bin .elf的文件名设置
project(Led_Toggle C CXX ASM)
# C/C++语言标准版本配置
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 17)
#Uncomment for hardware floating point
#add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING)
#add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
#add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16)
#Uncomment for software floating point
#add_compile_options(-mfloat-abi=soft)
#编译选项定义
# -mcpu=cortex-m3 告诉编译器为 ARM Cortex-M3 处理器生成代码
# -mthumb 启用 Thumb 指令集,Thumb 指令集是一种 16 位指令集
# -march=armv7-m 指定目标架构,ARMv7-M 是 Cortex-M3 处理器使用的架构
set(MCU_FLAGS -mcpu=cortex-m3 -mthumb -mthumb-interwork) # 编译选项定义修改处
#使用-Wall可开启所有警告;通过-Werror将所有警告视为error
#-ffunction-sections -fdata-sections将函数和数据放在单独的节中,这在链接阶段非常有用,因为链接器可以删除未使用的函数,从而减小最终可执行文件的大小
#开启-fmessage-length=0会让编译器展示所有的消息而不会限制错误和警告输出的长度
set(OPTIMIZE_COMPILE_FLAGS -ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
# set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16")
add_compile_options(${MCU_FLAGS})
add_compile_options(${OPTIMIZE_COMPILE_FLAGS})
# add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0)
# uncomment to mitigate c++17 absolute addresses warnings
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register")
# /*
# 编译等级选项:优化等级
# -O0:无任何优化,关闭所有优化选项
# -O、-O1:1级优化,
# -O2: 2级优化,
# -Os: 2.5级优化,-Os启用所有通常不会增加代码大小的-O2优化。 它还执行旨在减少代码大小的进一步优化。
# -O3: 最高级优化。
# -Og:优化调试体验。 -Og启用不会干扰调试的优化。 它是标准编辑 - 编译 - 调试周期可以选择的优化级别,提供合理的优化级别,同时保持快速编译和良好的调试体验。
# -Ofast:无视严格的标准合规性。 -Ofast启用所有-O3优化。 它还打开并非对所有符合标准的程序有效的优化。
# */
# Release 进行优化,提高速度 -排除调试信息
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
message(VERBOSE "Maximum optimization for speed")
add_compile_options(-Ofast)
# RelWithDebInfo 进行优化,提高速度 -包含调试信息
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
message(VERBOSE "Maximum optimization for speed, debug info included")
add_compile_options(-Ofast -g)
# MinSizeRel 优化二进制大小 -排除调试信息
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel")
message(VERBOSE "Maximum optimization for size")
add_compile_options(-Os)
# Debug 禁用优化 -包含调试信息
else ()
message(VERBOSE "Minimal optimization, debug info included")
add_compile_options(-O0 -g)
endif ()
#添加C文件编译宏定义
add_definitions(-DUSE_HAL_DRIVER
-D__MICROLIB
-DSTM32F103xE
-DUSE_STDPERIPH_DRIVER
-DSTM32F10X_HD)
# add_definitions(-DUSE_STDPERIPH_DRIVER -DSTM32F10X_HD)
#添加头文件路径,即.h文件
include_directories(./STM32F10x_FWLib/inc
./SYSTEM/delay
./SYSTEM/sys
./SYSTEM/usart
./HARDWARE/inc
./USER/inc
./CORE/inc
./CMSIS/inc)
#添加汇编启动文件路径,startup文件是STM32CubeMX生成的(需要gcc版本,不要使用MDK版本)
ENABLE_LANGUAGE(ASM) #为了让cmake识别启动文件
set(SRC_STARTUP "${CMAKE_SOURCE_DIR}/startup_s/startup_stm32f103xe.s")
#添加源文件路径,即.c文件
#file语法,前一个参数是固定的GLOB_RECURSE, 后面一个参数自行定义
file(GLOB_RECURSE SOURCES ./STM32F10x_FWLib/src/*.c
./USER/src/*.c
./CMSIS/src/*.c
./CORE/src/*.c
./SYSTEM/delay/*.c
./SYSTEM/sys/*.c
./SYSTEM/usart/*.c
./HARDWARE/src/*.c)
#添加.ld链接脚本路径
set(LINKER_SCRIPT "${CMAKE_SOURCE_DIR}/link_script/STM32F103VETx_FLASH.ld")
#-specs=nosys.specs -specs=nano.specs选择链接精简C库,而非标准C库
#-u _printf_float支持浮点数打印
#-cref则生成交叉引用表方便查找未定义的符号引用(比如编译时出现的undefined reference)
#--print-memory-usage选项提供链接器文件中定义的每个内存区域使用的内存的详细信息
set(OPTIMIZE_LINK_FLAGS -Wl,--no-warn-rwx-segments -flto -specs=nano.specs -specs=nosys.specs -u _printf_float) # -u _scanf_float
set(MAP_FLAGS -Wl,--cref,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
#链接选项配置
add_link_options(${MCU_FLAGS})
add_link_options(-T ${LINKER_SCRIPT})
# add_link_options(-Wl,--no-warn-rwx-segments,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
# add_link_options(-specs=nano.specs -specs=nosys.specs -u _printf_float) # -u _scanf_float
add_link_options(${OPTIMIZE_LINK_FLAGS})
add_link_options(${MAP_FLAGS})
#根据源文件、汇编启动文件、链接脚本 生成 .elf可执行文件
add_executable(${PROJECT_NAME}.elf ${SRC_STARTUP} ${SOURCES} ${LINKER_SCRIPT})
set(ELF_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.elf)
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
细节注意:如果读者将上述的CMakeLists.txt文件移植到自己的标准库/HAL库工程后,需要重新配置CMake相关的本地嵌入式工具链,否则会报错。参考方法的截图如下,