实现串口驱动,移植方便

This commit is contained in:
冯佳
2026-01-22 16:36:56 +08:00
parent 2ef4dac5bd
commit 51e8d79f78
151 changed files with 4064 additions and 11050 deletions

View File

@ -3,3 +3,4 @@ cmake_minimum_required(VERSION 3.22)
# Add all module subdirectories
add_subdirectory(led)
add_subdirectory(delay)
add_subdirectory(uart)

View File

@ -13,7 +13,8 @@ target_include_directories(delay PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
# Link with stm32cubemx library
# Link with stm32cubemx and hal libraries
target_link_libraries(delay PRIVATE
stm32cubemx
hal
)

56
Modules/delay/README.md Normal file
View File

@ -0,0 +1,56 @@
# Delay模块
## 功能描述
Delay模块提供了毫秒级和微秒级的延时功能用于在应用程序中实现精确的时间延迟。
## 依赖关系
- HAL层hal_delay
## 接口说明
### 函数接口
#### delay_init
```c
void delay_init(void);
```
初始化延迟模块
**参数**:无
**返回值**:无
#### delay_ms
```c
void delay_ms(uint32_t ms);
```
毫秒级延迟
**参数**
- ms: 延迟时间(毫秒)
**返回值**:无
#### delay_us
```c
void delay_us(uint32_t us);
```
微秒级延迟
**参数**
- us: 延迟时间(微秒)
**返回值**:无
## 使用示例
```c
/* 初始化延迟模块 */
delay_init();
/* 延迟1秒 */
delay_ms(1000);
/* 延迟500微秒 */
delay_us(500);
```

View File

@ -10,7 +10,8 @@
#ifndef DELAY_H
#define DELAY_H
#include "stm32f4xx_hal.h"
#include <stdint.h>
#include "hal_delay.h"
/**
* @brief Initialize delay module

View File

@ -9,21 +9,11 @@
#include "delay.h"
/**
* @brief DWT cycle counter frequency in MHz
*/
static uint32_t delay_tick_freq = 0;
/**
* @brief Initialize delay module
*/
void delay_init(void) {
/* Get the DWT cycle counter frequency */
delay_tick_freq = HAL_RCC_GetHCLKFreq() / 1000000U; /* Convert to MHz */
/* Enable DWT cycle counter */
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
hal_delay_init();
}
/**
@ -31,7 +21,7 @@ void delay_init(void) {
* @param ms: Delay time in milliseconds
*/
void delay_ms(uint32_t ms) {
HAL_Delay(ms);
hal_delay_ms(ms);
}
/**
@ -39,11 +29,5 @@ void delay_ms(uint32_t ms) {
* @param us: Delay time in microseconds
*/
void delay_us(uint32_t us) {
uint32_t start = DWT->CYCCNT;
uint32_t cycles = (uint32_t)(us * delay_tick_freq);
/* Wait until the delay is completed */
while ((DWT->CYCCNT - start) < cycles) {
/* Do nothing */
}
hal_delay_us(us);
}

View File

@ -13,7 +13,8 @@ target_include_directories(led PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
# Link with stm32cubemx library
# Link with stm32cubemx and hal libraries
target_link_libraries(led PRIVATE
stm32cubemx
hal
)

120
Modules/led/README.md Normal file
View File

@ -0,0 +1,120 @@
# LED模块
## 功能描述
LED模块用于控制板载LED的开关和状态切换提供了简单易用的API接口。
## 依赖关系
- HAL层hal_gpio
## 接口说明
### 数据结构
#### led_config_t
LED配置结构体用于初始化LED模块
```c
typedef struct {
hal_gpio_port_t gpio_port; /*!< GPIO端口 */
hal_gpio_pin_t gpio_pin; /*!< GPIO引脚 */
} led_config_t;
```
#### led_t
LED实例结构体用于管理LED状态
```c
typedef struct {
led_config_t config; /*!< LED配置 */
uint8_t state; /*!< 当前LED状态0: 关闭1: 开启) */
} led_t;
```
### 函数接口
#### led_init
```c
void led_init(led_t *led, const led_config_t *config);
```
初始化LED模块
**参数**
- led: LED实例指针
- config: LED配置结构体指针
**返回值**:无
#### led_on
```c
void led_on(led_t *led);
```
开启LED
**参数**
- led: LED实例指针
**返回值**:无
#### led_off
```c
void led_off(led_t *led);
```
关闭LED
**参数**
- led: LED实例指针
**返回值**:无
#### led_toggle
```c
void led_toggle(led_t *led);
```
切换LED状态
**参数**
- led: LED实例指针
**返回值**:无
#### led_get_state
```c
uint8_t led_get_state(const led_t *led);
```
获取LED当前状态
**参数**
- led: LED实例指针
**返回值**
- 0: LED关闭
- 1: LED开启
## 使用示例
```c
/* LED配置 */
static led_config_t led_config = {
.gpio_port = BSP_LED_PORT,
.gpio_pin = BSP_LED_PIN
};
/* LED实例 */
static led_t led;
/* 初始化LED */
led_init(&led, &led_config);
/* 开启LED */
led_on(&led);
/* 延迟500ms */
delay_ms(500);
/* 关闭LED */
led_off(&led);
/* 切换LED状态 */
led_toggle(&led);
/* 获取LED状态 */
uint8_t state = led_get_state(&led);
```

View File

@ -10,14 +10,16 @@
#ifndef LED_H
#define LED_H
#include "stm32f4xx_hal.h"
#include <stdint.h>
#include <stddef.h>
#include "hal_gpio.h"
/**
* @brief LED configuration structure
*/
typedef struct {
GPIO_TypeDef *gpio_port; /*!< GPIO port */
uint16_t gpio_pin; /*!< GPIO pin */
hal_gpio_port_t gpio_port; /*!< GPIO port */
hal_gpio_pin_t gpio_pin; /*!< GPIO pin */
} led_config_t;
/**

View File

@ -23,7 +23,7 @@ void led_init(led_t *led, const led_config_t *config) {
led->state = 0;
/* Ensure LED is initially off */
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_RESET);
hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_RESET);
}
/**
@ -35,7 +35,7 @@ void led_on(led_t *led) {
return;
}
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_SET);
hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_SET);
led->state = 1;
}
@ -48,7 +48,7 @@ void led_off(led_t *led) {
return;
}
HAL_GPIO_WritePin(led->config.gpio_port, led->config.gpio_pin, GPIO_PIN_RESET);
hal_gpio_write_pin(led->config.gpio_port, led->config.gpio_pin, HAL_GPIO_PIN_RESET);
led->state = 0;
}
@ -61,7 +61,7 @@ void led_toggle(led_t *led) {
return;
}
HAL_GPIO_TogglePin(led->config.gpio_port, led->config.gpio_pin);
hal_gpio_toggle_pin(led->config.gpio_port, led->config.gpio_pin);
led->state = !led->state;
}

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.22)
# Create uart library
add_library(uart STATIC)
# Add uart sources
target_sources(uart PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/uart.c
)
# Add uart include directories
target_include_directories(uart PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/inc
)
# Link with stm32cubemx and hal libraries
target_link_libraries(uart PRIVATE
stm32cubemx
hal
)

131
Modules/uart/README.md Normal file
View File

@ -0,0 +1,131 @@
# UART模块
## 功能描述
UART模块用于实现串口通信功能支持数据发送和接收提供了简单易用的API接口。
## 依赖关系
- HAL层hal_uart
## 接口说明
### 数据结构
#### uart_config_t
UART配置结构体用于初始化UART模块
```c
typedef struct {
uint32_t baudrate; /*!< 波特率 */
hal_uart_parity_t parity; /*!< 校验位 */
hal_uart_stopbits_t stopbits; /*!< 停止位 */
hal_uart_databits_t databits; /*!< 数据位 */
} uart_config_t;
```
#### uart_t
UART实例结构体用于管理UART状态
```c
typedef struct {
uart_config_t config; /*!< UART配置 */
uint8_t initialized; /*!< 初始化状态 */
} uart_t;
```
### 函数接口
#### uart_init
```c
void uart_init(void);
```
初始化UART模块
**参数**:无
**返回值**:无
#### uart_config
```c
void uart_config(const uart_config_t *config);
```
配置UART参数
**参数**
- config: UART配置结构体指针
**返回值**:无
#### uart_send
```c
void uart_send(const uint8_t *data, size_t length);
```
发送数据
**参数**
- data: 数据缓冲区指针
- length: 数据长度
**返回值**:无
#### uart_send_string
```c
void uart_send_string(const char *str);
```
发送字符串
**参数**
- str: 字符串指针
**返回值**:无
#### uart_receive
```c
size_t uart_receive(uint8_t *data, size_t length);
```
接收数据
**参数**
- data: 数据缓冲区指针
- length: 要接收的数据长度
**返回值**:实际接收的数据长度
#### uart_is_tx_ready
```c
uint8_t uart_is_tx_ready(void);
```
检查UART是否准备好发送
**参数**:无
**返回值**
- 1: 准备好
- 0: 未准备好
#### uart_is_rx_ready
```c
uint8_t uart_is_rx_ready(void);
```
检查UART是否有数据可读
**参数**:无
**返回值**
- 1: 有数据可读
- 0: 无数据可读
## 使用示例
```c
/* 初始化UART模块 */
uart_init();
/* 发送字符串 */
uart_send_string("Hello, UART!\r\n");
/* 发送数据 */
uint8_t data[] = {0x01, 0x02, 0x03};
uart_send(data, sizeof(data));
/* 接收数据 */
uint8_t buffer[10];
size_t len = uart_receive(buffer, sizeof(buffer));
```

79
Modules/uart/inc/uart.h Normal file
View File

@ -0,0 +1,79 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : uart.h
* @brief : UART driver module header file
******************************************************************************
*/
/* USER CODE END Header */
#ifndef UART_H
#define UART_H
#include <stdint.h>
#include <stddef.h>
#include "hal_uart.h"
/**
* @brief UART configuration structure
*/
typedef struct {
uint32_t baudrate;
hal_uart_parity_t parity;
hal_uart_stopbits_t stopbits;
hal_uart_databits_t databits;
} uart_config_t;
/**
* @brief UART instance structure
*/
typedef struct {
uart_config_t config;
uint8_t initialized;
} uart_t;
/**
* @brief Initialize UART module
*/
void uart_init(void);
/**
* @brief Configure UART parameters
* @param config: UART configuration structure
*/
void uart_config(const uart_config_t *config);
/**
* @brief Send data over UART
* @param data: Pointer to data buffer
* @param length: Data length in bytes
*/
void uart_send(const uint8_t *data, size_t length);
/**
* @brief Send string over UART
* @param str: Pointer to string
*/
void uart_send_string(const char *str);
/**
* @brief Receive data over UART
* @param data: Pointer to data buffer
* @param length: Data length to receive in bytes
* @retval Number of bytes received
*/
size_t uart_receive(uint8_t *data, size_t length);
/**
* @brief Check if UART is ready to send
* @retval 1 if ready, 0 otherwise
*/
uint8_t uart_is_tx_ready(void);
/**
* @brief Check if UART has data to receive
* @retval 1 if data available, 0 otherwise
*/
uint8_t uart_is_rx_ready(void);
#endif /* UART_H */

104
Modules/uart/src/uart.c Normal file
View File

@ -0,0 +1,104 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : uart.c
* @brief : UART driver module source file
******************************************************************************
*/
/* USER CODE END Header */
#include "uart.h"
#include <string.h>
/* UART instance */
static uart_t uart;
/**
* @brief Initialize UART module
*/
void uart_init(void) {
/* Initialize hardware abstraction layer */
hal_uart_init();
/* Set default configuration */
uart.config.baudrate = 115200;
uart.config.parity = HAL_UART_PARITY_NONE;
uart.config.stopbits = HAL_UART_STOPBITS_1;
uart.config.databits = HAL_UART_DATABITS_8;
/* Configure UART with default settings */
hal_uart_config((hal_uart_config_t *)&uart.config);
uart.initialized = 1;
}
/**
* @brief Configure UART parameters
* @param config: UART configuration structure
*/
void uart_config(const uart_config_t *config) {
if (config == NULL) {
return;
}
/* Update configuration */
uart.config = *config;
/* Configure hardware */
hal_uart_config((hal_uart_config_t *)&uart.config);
}
/**
* @brief Send data over UART
* @param data: Pointer to data buffer
* @param length: Data length in bytes
*/
void uart_send(const uint8_t *data, size_t length) {
if (data == NULL || length == 0) {
return;
}
hal_uart_send(data, length);
}
/**
* @brief Send string over UART
* @param str: Pointer to string
*/
void uart_send_string(const char *str) {
if (str == NULL) {
return;
}
hal_uart_send((const uint8_t *)str, strlen(str));
}
/**
* @brief Receive data over UART
* @param data: Pointer to data buffer
* @param length: Data length to receive in bytes
* @retval Number of bytes received
*/
size_t uart_receive(uint8_t *data, size_t length) {
if (data == NULL || length == 0) {
return 0;
}
return hal_uart_receive(data, length);
}
/**
* @brief Check if UART is ready to send
* @retval 1 if ready, 0 otherwise
*/
uint8_t uart_is_tx_ready(void) {
return hal_uart_is_tx_ready();
}
/**
* @brief Check if UART has data to receive
* @retval 1 if data available, 0 otherwise
*/
uint8_t uart_is_rx_ready(void) {
return hal_uart_is_rx_ready();
}