文件结构
├─.vscode
├─build
├─cmake
├─Drivers
│ ├─CMSIS
│ │ ├─Device
│ │ │ └─ST
│ │ │ └─STM32F1xx
│ │ │ ├─Include
│ │ │ └─Source
│ │ │ └─Templates
│ │ └─Include
│ └─STM32F1xx_HAL_Driver
│ ├─Inc
│ │ └─Legacy
│ └─Src
├─freertos
│ ├─include
│ └─portable
│ ├─GCC
│ │ └─ARM_CM3
│ └─MemMang
├─Link
├─startup
└─User
├─Inc
└─Src
build.bat
@echo off
IF /i "%1"=="-C" (
rd /s /q build
) ELSE IF /i "%1"=="" (
IF EXIST build (
cd build
cmake -G "MinGW Makefiles" ..
make
cd ..
) ELSE (
md build
cd build
cmake -G "MinGW Makefiles" ..
make
cd ..
)
)
# No operating system
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
cmake_minimum_required(VERSION 3.21)
# 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)
# skip compiler checks
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
project(MiniBot C CXX ASM)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
add_compile_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
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")
# Enable assembler files preprocessing
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(
./
# ${CMAKE_SOURCE_DIR}/drivers/CMSIS/Include
# ${CMAKE_SOURCE_DIR}/drivers/CMSIS/Device/ST/STM32F1xx/Include
# ${CMAKE_SOURCE_DIR}/drivers/STM32F1xx_HAL_Driver/Inc
# ${CMAKE_SOURCE_DIR}/drivers/STM32F1xx_HAL_Driver/Inc/Legacy
# ${CMAKE_SOURCE_DIR}/main
)
add_definitions(-DUSE_HAL_DRIVER -D__MICROLIB -DSTM32F1 -DSTM32F1xx -DSTM32F103xB)
# aux_source_directory(${CMAKE_SOURCE_DIR}/drivers/STM32F1xx_HAL_Driver/Src HAL_DRIVER)
# aux_source_directory(${CMAKE_SOURCE_DIR}/drivers/CMSIS/Device/ST/STM32F1xx/Source/Templates SYSTEM)
# aux_source_directory(${CMAKE_SOURCE_DIR}/main MAIN)
aux_source_directory(${CMAKE_SOURCE_DIR}/User USER)
set(STARTUP ${CMAKE_SOURCE_DIR}/startup/startup_stm32f103xb.s)
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Link/STM32F103C8Tx_FLASH.ld)
add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map)
add_link_options(-mcpu=cortex-m3 -mthumb -mthumb-interwork)
add_link_options(-T ${LINKER_SCRIPT})
add_executable(${PROJECT_NAME}.elf ${USER} ${STARTUP} ${LINKER_SCRIPT})
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 $ ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
cmake/arm-none-eabi.cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_C_COMPILER ${CROSS_COMPILE_PREFIX}gcc)
set(CMAKE_CXX_COMPILER ${CROSS_COMPILE_PREFIX}g++)
set(CMAKE_ASM_COMPILER ${CROSS_COMPILE_PREFIX}gcc)
set(CMAKE_AR ${CROSS_COMPILE_PREFIX}ar)
set(CMAKE_OBJCOPY ${CROSS_COMPILE_PREFIX}objcopy)
set(CMAKE_OBJDUMP ${CROSS_COMPILE_PREFIX}objdump)
set(SIZE ${CROSS_COMPILE_PREFIX}size)
cmake/cortex_m0.cmake
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR cortex-m0)
SET(MCPU_FLAGS "-mcpu=cortex-m0 -mthumb")
SET(VFP_FLAGS "")
ADD_DEFINITIONS(
-DARM_MATH_CM0
)
cmake/cortex_m3.cmake
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR cortex-m3)
#添加 编译标志
SET(MCPU_FLAGS "-mthumb -mthumb-interwork -mcpu=cortex-m3")
#添加浮点运算标准
SET(VFP_FLAGS "-mfloat-abi=soft")
add_definitions(
-DUSE_HAL_DRIVER
-DSTM32F1
)
cmake/cortex_m4.cmake
SET(CMAKE_SYSTEM_NAME Generic)
SET(CMAKE_SYSTEM_PROCESSOR cortex-m4)
SET(MCPU_FLAGS "-mthumb -mcpu=cortex-m4")
SET(VFP_FLAGS "-mfloat-abi=soft")
ADD_DEFINITIONS(
-DARM_MATH_CM4
-DARM_MATH_MATRIX_CHECK
-DARM_MATH_ROUNDING
-D__FPU_PRESENT=1
)
cmake/gcc-flags.cmake
#判断包含架构文件
if(CMAKE_ARCH_TYPE MATCHES CM0)
include(${CMAKE_SOURCE_DIR}/cmake/cortex_m0.cmake)
elseif(CMAKE_ARCH_TYPE MATCHES CM3)
include(${CMAKE_SOURCE_DIR}/cmake/cortex_m3.cmake)
elseif(CMAKE_ARCH_TYPE MATCHES CM4)
include(${CMAKE_SOURCE_DIR}/cmake/cortex_m4.cmake)
endif()
set(LINK_FLAGS "-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map -T ${LINKER_SCRIPT}")
#汇编文件按照c文件处理
add_compile_options($<$:-x$assembler-with-cpp>)
# 设置c++ c 标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 11)
# 设置编译类型
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(DBG_FLAGS "-g3 -gdwarf-2 -O0")
elseif(CMAKE_BUILD_TYPE MATCHES Release)
set(DBG_FLAGS "-O3")
endif()
#设置编译选项
set(CMAKE_C_FLAGS "${MCPU_FLAGS} ${VFP_FLAGS} -std=gnu99 -Wall -fdata-sections -ffunction-sections ${DBG_FLAGS}" CACHE INTERNAL "C compiler flags")
set(CMAKE_C_FLAGS_DEBUG "" CACHE INTERNAL "C compiler flags: Debug")
set(CMAKE_C_FLAGS_RELEASE "" CACHE INTERNAL "C compiler flags: Release")
set(CMAKE_CXX_FLAGS "${MCPU_FLAGS} ${VFP_FLAGS} -fno-rtti -fno-exceptions -fno-builtin -Wall -fdata-sections -ffunction-sections ${DBG_FLAGS}" CACHE INTERNAL "Cxx compiler flags")
set(CMAKE_CXX_FLAGS_DEBUG "" CACHE INTERNAL "Cxx compiler flags: Debug")
set(CMAKE_CXX_FLAGS_RELEASE "" CACHE INTERNAL "Cxx compiler flags: Release")
set(CMAKE_ASM_FLAGS "${MCPU_FLAGS} ${VFP_FLAGS} -x assembler-with-cpp ${DBG_FLAGS}" CACHE INTERNAL "ASM compiler flags")
set(CMAKE_ASM_FLAGS_DEBUG "" CACHE INTERNAL "ASM compiler flags: Debug")
set(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "ASM compiler flags: Release")
set(CMAKE_EXE_LINKER_FLAGS "${LINK_FLAGS}" CACHE INTERNAL "Exe linker flags")
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "" CACHE INTERNAL "Shared linker flags")
cmake/postbuild.cmake
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 $ ${HEX_FILE}
COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE}
COMMENT "Building ${HEX_FILE}
Building ${BIN_FILE}")
CMakeLists.txt
#设置编译器前缀
set(CROSS_COMPILE_PREFIX arm-none-eabi-)
include(${CMAKE_SOURCE_DIR}/cmake/arm-none-eabi.cmake)
#设置最小版本
cmake_minimum_required(VERSION 3.21)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
#显示编译过程
set ( CMAKE_VERBOSE_MAKEFILE on )
# 项目名称
project(test)
# 项目构建类型
# set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_ARCH_TYPE "CM3")
# 添加头文件路径
include_directories(
./
./User/Inc
./Drivers/CMSIS/Include/
./Drivers/CMSIS/Device/ST/STM32F1xx/Include
./Drivers/STM32F1xx_HAL_Driver/Inc/Legacy
./Drivers/STM32F1xx_HAL_Driver/Inc
./freertos/include
./freertos/portable/GCC/ARM_CM3
./freertos/portable/MemMang
)
#添加源码路径
aux_source_directory(${CMAKE_SOURCE_DIR}/User/Src USER)
aux_source_directory(${CMAKE_SOURCE_DIR}/Drivers DRIVER)
aux_source_directory(${CMAKE_SOURCE_DIR}/freertos FREERTOS)
#添加启动代码路径
set(STARTUP ${CMAKE_SOURCE_DIR}/startup/startup_stm32f103xb.s)
#添加链接脚本路径
set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/Link/STM32F103C8Tx_FLASH.ld)
# 宏定义
add_definitions(
-DSTM32F103xB
)
# gcc cmake文件
include(${CMAKE_SOURCE_DIR}/cmake/gcc-flags.cmake)
# 打印 配置选择
MESSAGE(STATUS "-----------------------------------------------")
MESSAGE(STATUS "CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE} ")
MESSAGE(STATUS "CMAKE_C_FLAGS: ${CMAKE_C_FLAGS} ")
MESSAGE(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS} ")
MESSAGE(STATUS "CMAKE_ASM_FLAGS: ${CMAKE_ASM_FLAGS} ")
MESSAGE(STATUS "CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS} ")
MESSAGE(STATUS "-----------------------------------------------")
#生成目标文件
add_executable(${PROJECT_NAME}.elf
${STARTUP}
${LINKER_SCRIPT}
${USER}
${DRIVER}
${FREERTOS}
)
include(${CMAKE_SOURCE_DIR}/cmake/postbuild.cmake)