cmake stm32 模板
发布人:shili8
发布时间:2025-03-12 21:45
阅读次数:0
**STM32 CMake 模板**
=====================本文提供了一个基本的 CMake 模板,用于编译 STM32项目。这个模板支持多个目标文件、静态库和可执行程序的构建。
**目录结构**
---------------
bashstm32-cmake-template/ CMakeLists.txtsrc/ main.cuart.cled.cinclude/ stm32f4xx.hstm32f4xx_gpio.hstm32f4xx_rcc.hstm32f4xx_usart.hcmake-cache/
**CMakeLists.txt**
-------------------
cmake# CMakeLists.txt#项目名称和版本号project(STM32_CMAKE_TEMPLATE VERSION1.0) # 指定编译器set(CMAKE_C_COMPILER arm-none-eabi-gcc) set(CMAKE_CXX_COMPILER arm-none-eabi-g++) # 指定目标文件set(TARGET_NAME "stm32_cmake_template") set(SYSCLK_FREQ80000000) # 系统时钟频率# 添加源文件add_executable(${TARGET_NAME} src/main.c src/uart.c src/led.c) # 添加头文件目录include_directories(include) # 添加静态库add_library(stm32f4xx_lib STATIC IMPORTED) set_property(TARGET stm32f4xx_lib PROPERTY IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/libstm32f4xx.a) # 添加目标文件依赖关系target_link_libraries(${TARGET_NAME} PRIVATE stm32f4xx_lib) # 添加编译选项add_compile_options(-Wall -Wextra -pedantic)
**src/main.c**
----------------
c// src/main.c#include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" #include "stm32f4xx_usart.h" int main(void) { // 初始化GPIO、RCC和USART GPIO_InitTypeDef gpio_init; RCC_ClockInitTypeDef rcc_clock_init; USART_InitTypeDef usart_init; // 配置GPIO gpio_init.Pin = GPIO_PIN_5; // GPIO口5 gpio_init.Mode = GPIO_MODE_OUTPUT; // 输出模式 HAL_GPIO_Init(GPIO_PORT_A, &gpio_init); // 配置RCC rcc_clock_init.ClockSource = RCC_CLOCKSOURCE_HSI; // 时钟源为HSI HAL_RCC_ClockInit(&rcc_clock_init); // 配置USART usart_init.BaudRate =9600; // 波特率为9600 HAL_USART_Init(USART1, &usart_init); while (1) { // 循环发送数据 HAL_USART_Transmit(USART1, "Hello, World!", strlen("Hello, World!"),100); } return0; }
**src/uart.c**
----------------
c// src/uart.c#include "stm32f4xx.h" #include "stm32f4xx_usart.h" void HAL_USART_TxCpltCallback(USART_HandleTypeDef *huart) { // 发送完成回调函数} void HAL_USART_RxCpltCallback(USART_HandleTypeDef *huart) { // 接收完成回调函数}
**src/led.c**
----------------
c// src/led.c#include "stm32f4xx.h" #include "stm32f4xx_gpio.h" void LED_Init(void) { // 初始化LED GPIO口} void LED_Toggle(void) { // 切换LED状态}
**include/stm32f4xx.h**
-------------------------
c// include/stm32f4xx.h#ifndef STM32F4XX_H_ #define STM32F4XX_H_ #include "stm32f4xx_rcc.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_usart.h" #endif // STM32F4XX_H_
**include/stm32f4xx_gpio.h**
------------------------------
c// include/stm32f4xx_gpio.h#ifndef STM32F4XX_GPIO_H_ #define STM32F4XX_GPIO_H_ #include "stm32f4xx.h" typedef struct { uint16_t Pin; // GPIO口号 uint8_t Mode; // 模式} GPIO_InitTypeDef; void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *gpio_init); #endif // STM32F4XX_GPIO_H_
**include/stm32f4xx_rcc.h**
---------------------------
c// include/stm32f4xx_rcc.h#ifndef STM32F4XX_RCC_H_ #define STM32F4XX_RCC_H_ #include "stm32f4xx.h" typedef struct { uint8_t ClockSource; // 时钟源} RCC_ClockInitTypeDef; void HAL_RCC_ClockInit(RCC_ClockInitTypeDef *rcc_clock_init);
**include/stm32f4xx_usart.h**
------------------------------
c// include/stm32f4xx_usart.h#ifndef STM32F4XX_USART_H_ #define STM32F4XX_USART_H_ #include "stm32f4xx.h" typedef struct { uint16_t BaudRate; // 波特率} USART_InitTypeDef; void HAL_USART_Init(USART_TypeDef *huart, USART_InitTypeDef *usart_init); void HAL_USART_Transmit(USART_TypeDef *huart, const char *data, uint32_t len, uint32_t timeout);
本文提供了一个基本的 CMake 模板,用于编译 STM32项目。这个模板支持多个目标文件、静态库和可执行程序的构建。