Files
stm32f407ve_black/Modules/uart/README.md

193 lines
3.7 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# UART模块
## 功能描述
UART模块用于实现串口通信功能支持数据发送和接收提供了简单易用的API接口。
## 依赖关系
- HAL层hal_uart
## 接口说明
### 枚举类型
#### hal_uart_parity_t
UART校验位枚举
```c
typedef enum {
HAL_UART_PARITY_NONE = 0U, /*!< 无奇偶校验 */
HAL_UART_PARITY_ODD = 1U, /*!< 奇校验 */
HAL_UART_PARITY_EVEN = 2U /*!< 偶校验 */
} hal_uart_parity_t;
```
#### hal_uart_stopbits_t
UART停止位枚举
```c
typedef enum {
HAL_UART_STOPBITS_1 = 0U, /*!< 1位停止位 */
HAL_UART_STOPBITS_2 = 1U /*!< 2位停止位 */
} hal_uart_stopbits_t;
```
#### hal_uart_databits_t
UART数据位枚举
```c
typedef enum {
HAL_UART_DATABITS_8 = 0U, /*!< 8位数据位 */
HAL_UART_DATABITS_9 = 1U /*!< 9位数据位 */
} hal_uart_databits_t;
```
#### hal_uart_instance_t
UART实例枚举
```c
typedef enum {
HAL_UART_INSTANCE_1 = 0U, /*!< UART1实例 */
HAL_UART_INSTANCE_2, /*!< UART2实例 */
HAL_UART_INSTANCE_3, /*!< UART3实例 */
HAL_UART_INSTANCE_4, /*!< UART4实例 */
HAL_UART_INSTANCE_5, /*!< UART5实例 */
HAL_UART_INSTANCE_6, /*!< UART6实例 */
HAL_UART_INSTANCE_MAX /*!< 实例数量最大值 */
} hal_uart_instance_t;
```
### 数据结构
#### uart_config_t
UART配置结构体用于初始化UART模块
```c
typedef struct {
hal_uart_instance_t instance; /*!< UART实例 */
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模块设置默认配置
**参数**:无
**返回值**:无
**默认配置**
- 实例HAL_UART_INSTANCE_1
- 波特率115200
- 校验位HAL_UART_PARITY_NONE
- 停止位HAL_UART_STOPBITS_1
- 数据位HAL_UART_DATABITS_8
#### 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参数 */
uart_config_t config = {
.instance = HAL_UART_INSTANCE_1,
.baudrate = 115200,
.parity = HAL_UART_PARITY_NONE,
.stopbits = HAL_UART_STOPBITS_1,
.databits = HAL_UART_DATABITS_8
};
uart_config(&config);
/* 发送字符串 */
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));
```