之前写过一篇CLion+openocd+stm32cubemx基于HAL库搭建开发环境,搭建起来开发很舒服,基本外设配置以及驱动代码都不用自己写,直接就能用STM32CubeMX生成的,做一些小项目上手非常快,但是带来了一些副作用,比如想换GD32等国产的芯片时就比较懵了,国产的这些芯片基本都没有类似STM32CubeMX这样的图形化代码脚手架生成工具,一般都是使用标准库开发,所以这里我准备切换为使用标准库开发,后面想用国产芯片试试也好上手,另外现在AI的发展,使用AI去生成标准库的各类驱动代码效率也是十分高了
基础框架
这里如果从0开始新建文件夹的话,还得自己手写CMakeLists.txt工程描述文件,配置各种编译参数,对于我这种新手可能容易出错不太友好,所以这里基础的代码框架我准备还是借助STM32CubeMX生成一个,然后再在其基础上改。
打开STM32CubeMX选择芯片型号stm32f103c8t6a,什么也不用配置直接来到Project Manager输入项目名称路径,以及工具链为CMake即可生成代码,然后把.mxproject、.ioc等cubemx相关的文件删掉,以及把Drivers文件夹里的文件全部删了
下载标准库替换
https://www.st.com/en/embedded-software/stsw-stm32054.html
来到ST官网下载官方的标准外设库固件包,解压得到以下文件

其中CMSIS和STM32F10x_StdPeriph_Driver里核心驱动代码,直接全部拿来放到Drivers文件夹里,不过里面有很多没用的文档文件我都给删了,而且里面还有很多.s结尾的启动文件,因为我这里用stm32f103c8t6,所以我只需要一个gcc_ride7/startup_stm32f10x_md.s其他都给删了,并且把这一个拿到最外面了
删完后CMSIS里其实就剩core_cm3.c、core_cm3.h这两个文件了
还有几个文件需要从官方的工程样例里面拿,到STM32F10x_StdPeriph_Examples/GPIO/IOToggle里,把以下四个文件搬到Core里
- stm32f10x_conf.h
- stm32f10x_it.c
- stm32f10x_it.h
- system_stm32f10x.c
修改CMakeList.txt文件
最主要的是cmake/stm32cubemx/CMakeLists.txt这个文件,而不是工程最外层那个
主要中间这几行:
cmake_minimum_required(VERSION 3.22)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
# STM32CubeMX generated symbols (macros)
set(MX_Defines_Syms
USE_STDPERIPH_DRIVER
STM32F10X_MD
$<$<CONFIG:Debug>:DEBUG>
)
# STM32CubeMX generated include paths
set(MX_Include_Dirs
${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Inc
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/inc
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/CMSIS
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/BSP
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/Components
)
# STM32CubeMX generated application sources
set(MX_Application_Src
${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/stm32f10x_it.c
${CMAKE_CURRENT_SOURCE_DIR}/../../startup_stm32f10x_md.s
)
# STM32 HAL/LL Drivers
set(STM32_Drivers_Src
${CMAKE_CURRENT_SOURCE_DIR}/../../Core/Src/system_stm32f10x.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/misc.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_adc.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_bkp.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_can.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_cec.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_crc.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_dac.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_dbgmcu.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_dma.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_exti.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_flash.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_fsmc.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_gpio.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_i2c.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_pwr.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_rcc.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_rtc.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_spi.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_tim.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_usart.c
${CMAKE_CURRENT_SOURCE_DIR}/../../Drivers/STM32F10x_StdPeriph_Driver/src/stm32f10x_wwdg.c
)
# Drivers Midllewares
# Link directories setup
set(MX_LINK_DIRS
)
# Project static libraries
set(MX_LINK_LIBS
STM32_Drivers
${TOOLCHAIN_LINK_LIBRARIES}
)
# Interface library for includes and symbols
add_library(stm32cubemx INTERFACE)
target_include_directories(stm32cubemx INTERFACE ${MX_Include_Dirs})
target_compile_definitions(stm32cubemx INTERFACE ${MX_Defines_Syms})
# Create STM32_Drivers static library
add_library(STM32_Drivers OBJECT)
target_sources(STM32_Drivers PRIVATE ${STM32_Drivers_Src})
target_link_libraries(STM32_Drivers PUBLIC stm32cubemx)
# Add STM32CubeMX generated application sources to the project
target_sources(${CMAKE_PROJECT_NAME} PRIVATE ${MX_Application_Src})
# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE ${MX_LINK_DIRS})
# Add libraries to the project
target_link_libraries(${CMAKE_PROJECT_NAME} ${MX_LINK_LIBS})
# Add the map file to the list of files to be removed with 'clean' target
set_target_properties(${CMAKE_PROJECT_NAME} PROPERTIES ADDITIONAL_CLEAN_FILES ${CMAKE_PROJECT_NAME}.map)
# Validate that STM32CubeMX code is compatible with C standard
if((CMAKE_C_STANDARD EQUAL 90) OR (CMAKE_C_STANDARD EQUAL 99))
message(ERROR "Generated code requires C11 or higher")
endif()
检查这些文件、路径是否都正确
最后在CMake配置参数里增加以下参数:
-DCMAKE_TOOLCHAIN_FILE=cmake/gcc-arm-none-eabi.cmake
然后应该就可以编译了,模版代码如下:
https://github.com/chengpei/stm32-std-template
我还增加了i2c1、uart1等的驱动代码,测试了读取mpu6050发送到串口的测试代码
评论区