一. 开发工具安装
工具安装和使用参考:
Linux下使用VSCode,GCC,OpenOCD实现STM32一键编译烧录调试(STM32CubeMX篇)
二. 测试工程简介
此文使用STM32的标准外设库(STM32F4xx_StdPeriph_Driver)编写点灯测试代码,其中有3个文件是从STM32CubeMX篇复制过来的:
- startup_stm32f427xx.s 由于编译器与MDK内置的不同,因此启动文件要用GCC专用的
- STM32F427VGTx_FLASH.ld 用于连接文件,CMake要用到
- STM32F427x.svd 用于调试时可以直观地看到MCU各个外设的状态
三. CMake工具
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,使用方法参考:CMake tutorial
1. CMakeLists.txt
这里贴出CMake最重要的文件CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(test02)
#以下用于屏蔽 error: unrecognized command line option ‘-rdynamic’
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
#指定编译工具
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(CMAKE_SIZE "arm-none-eabi-size")
#编译相关选项
set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16")
set(CMAKE_C_FLAGS "${MCU_FLAGS} -Wall -Wno-unknown-pragmas") #-w -Wall
set(CMAKE_C_FLAGS_DEBUG "-O0 -g2 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_BUILD_TYPE "Release")
#设置宏定义,对应MDK里Target Options里的选项
add_definitions(-DUSE_STDPERIPH_DRIVER -DSTM32F427X)
#设置工程包含当前目录
#set(CMAKE_INCLUDE_CURRENT_DIR ON)
#设置头文件包含目录
include_directories(
.
CMSIS
CMSIS/Include
STM32F4xx_StdPeriph_Driver/inc
)
#startup文件是STM32CubeMX生成的
ENABLE_LANGUAGE(ASM)
set(SRC_STARTUP "CMSIS/startup_stm32f427xx.s")
aux_source_directory(. SRC_MAIN)
aux_source_directory(CMSIS/ SRC_CMSIS)
aux_source_directory(STM32F4xx_StdPeriph_Driver/src/ SRC_STD_LIB)
#连接生成,ld文件是STM32CubeMX生成的
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/STM32F427VGTx_FLASH.ld")
set(CMAKE_EXE_LINKER_FLAGS
"-specs=nosys.specs -T${LINKER_SCRIPT} -lc -lm -lnosys -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections"
)
#生成可执行文件
add_executable(${PROJECT_NAME}.elf ${SRC_STARTUP} ${SRC_MAIN} ${SRC_CMSIS} ${SRC_STD_LIB})
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} -Obinary ${ELF_FILE} ${BIN_FILE}
COMMAND ${CMAKE_OBJCOPY} -Oihex ${ELF_FILE} ${HEX_FILE}
COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"
COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex"
COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin"
COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
COMMENT "Invoking: Cross ARM GNU Print Size"
)
有几点需要注意:
- 调试STM32需要设置成调试模式set(CMAKE_BUILD_TYPE "Debug")
- 头文件包含路径需要填写完整include_directories()
- 源文件用aux_source_directory()的方式打包整个目录
- 为了让cmake识别启动文件,需要ENABLE_LANGUAGE(ASM)
- 设置链接脚本文件,注意存放的位置
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/STM32F427VGTx_FLASH.ld")
2. 生成Makefile
进入build目录cd build然后输入cmake ..,成功生成Makefile
3. make编译
在build目录直接执行make命令,开始编译并生成可执行文件
然后再利用openocd烧录进STM32板子验证即可,具体操作参考STM32CubeMX篇
四. json脚本实现一键编译烧录调试
1. launch.json
内容与STM32CubeMX篇一样
{
// 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": "${workspaceRoot}",
"executable": "./build/test01.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"configFiles": [
"interface/stlink-v2.cfg",
"target/stm32f4x.cfg"
],
"armToolchainPath": "/opt/gcc-arm-none-eabi-10-2020-q4-major/bin",
"svdFile": "STM32F427x.svd",
"preLaunchTask": "Build"
}
]
}
2. task.json
这里与STM32CubeMX篇不同,用到了两条指令
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [".."]
},
{
"label": "make",
"type": "shell",
"command": "make",
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn":[
"cmake",
"make"
],
}
]
}
3. 一键编译调试
按F5直接自动编译调试
一. 开发工具安装
工具安装和使用参考:
Linux下使用VSCode,GCC,OpenOCD实现STM32一键编译烧录调试(STM32CubeMX篇)
二. 测试工程简介
此文使用STM32的标准外设库(STM32F4xx_StdPeriph_Driver)编写点灯测试代码,其中有3个文件是从STM32CubeMX篇复制过来的:
- startup_stm32f427xx.s 由于编译器与MDK内置的不同,因此启动文件要用GCC专用的
- STM32F427VGTx_FLASH.ld 用于连接文件,CMake要用到
- STM32F427x.svd 用于调试时可以直观地看到MCU各个外设的状态
三. CMake工具
CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,使用方法参考:CMake tutorial
1. CMakeLists.txt
这里贴出CMake最重要的文件CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(test02)
#以下用于屏蔽 error: unrecognized command line option ‘-rdynamic’
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "")
set(CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "")
#指定编译工具
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(CMAKE_SIZE "arm-none-eabi-size")
#编译相关选项
set(MCU_FLAGS "-mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16")
set(CMAKE_C_FLAGS "${MCU_FLAGS} -Wall -Wno-unknown-pragmas") #-w -Wall
set(CMAKE_C_FLAGS_DEBUG "-O0 -g2 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "-O3")
set(CMAKE_BUILD_TYPE "Debug")
#set(CMAKE_BUILD_TYPE "Release")
#设置宏定义,对应MDK里Target Options里的选项
add_definitions(-DUSE_STDPERIPH_DRIVER -DSTM32F427X)
#设置工程包含当前目录
#set(CMAKE_INCLUDE_CURRENT_DIR ON)
#设置头文件包含目录
include_directories(
.
CMSIS
CMSIS/Include
STM32F4xx_StdPeriph_Driver/inc
)
#startup文件是STM32CubeMX生成的
ENABLE_LANGUAGE(ASM)
set(SRC_STARTUP "CMSIS/startup_stm32f427xx.s")
aux_source_directory(. SRC_MAIN)
aux_source_directory(CMSIS/ SRC_CMSIS)
aux_source_directory(STM32F4xx_StdPeriph_Driver/src/ SRC_STD_LIB)
#连接生成,ld文件是STM32CubeMX生成的
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/STM32F427VGTx_FLASH.ld")
set(CMAKE_EXE_LINKER_FLAGS
"-specs=nosys.specs -T${LINKER_SCRIPT} -lc -lm -lnosys -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map,--cref -Wl,--gc-sections"
)
#生成可执行文件
add_executable(${PROJECT_NAME}.elf ${SRC_STARTUP} ${SRC_MAIN} ${SRC_CMSIS} ${SRC_STD_LIB})
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} -Obinary ${ELF_FILE} ${BIN_FILE}
COMMAND ${CMAKE_OBJCOPY} -Oihex ${ELF_FILE} ${HEX_FILE}
COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"
COMMAND ${CMAKE_COMMAND} -E copy ${HEX_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.hex"
COMMAND ${CMAKE_COMMAND} -E copy ${BIN_FILE} "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.bin"
COMMAND ${CMAKE_SIZE} --format=berkeley ${PROJECT_NAME}.elf ${PROJECT_NAME}.hex
COMMENT "Invoking: Cross ARM GNU Print Size"
)
有几点需要注意:
- 调试STM32需要设置成调试模式set(CMAKE_BUILD_TYPE "Debug")
- 头文件包含路径需要填写完整include_directories()
- 源文件用aux_source_directory()的方式打包整个目录
- 为了让cmake识别启动文件,需要ENABLE_LANGUAGE(ASM)
- 设置链接脚本文件,注意存放的位置
set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/STM32F427VGTx_FLASH.ld")
2. 生成Makefile
进入build目录cd build然后输入cmake ..,成功生成Makefile
3. make编译
在build目录直接执行make命令,开始编译并生成可执行文件
然后再利用openocd烧录进STM32板子验证即可,具体操作参考STM32CubeMX篇
四. json脚本实现一键编译烧录调试
1. launch.json
内容与STM32CubeMX篇一样
{
// 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": "${workspaceRoot}",
"executable": "./build/test01.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"configFiles": [
"interface/stlink-v2.cfg",
"target/stm32f4x.cfg"
],
"armToolchainPath": "/opt/gcc-arm-none-eabi-10-2020-q4-major/bin",
"svdFile": "STM32F427x.svd",
"preLaunchTask": "Build"
}
]
}
2. task.json
这里与STM32CubeMX篇不同,用到了两条指令
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"options": {
"cwd": "${workspaceFolder}/build"
},
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [".."]
},
{
"label": "make",
"type": "shell",
"command": "make",
},
{
"label": "Build",
"dependsOrder": "sequence",
"dependsOn":[
"cmake",
"make"
],
}
]
}
3. 一键编译调试
按F5直接自动编译调试
举报