优化实现串口驱动,SPI驱动 W25QXX还需要初始化验证修复
This commit is contained in:
@ -8,12 +8,57 @@ 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; /*!< 停止位 */
|
||||
@ -36,12 +81,19 @@ typedef struct {
|
||||
```c
|
||||
void uart_init(void);
|
||||
```
|
||||
初始化UART模块
|
||||
初始化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);
|
||||
@ -118,6 +170,16 @@ uint8_t uart_is_rx_ready(void);
|
||||
/* 初始化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");
|
||||
|
||||
|
||||
@ -18,6 +18,7 @@
|
||||
* @brief UART configuration structure
|
||||
*/
|
||||
typedef struct {
|
||||
hal_uart_instance_t instance;
|
||||
uint32_t baudrate;
|
||||
hal_uart_parity_t parity;
|
||||
hal_uart_stopbits_t stopbits;
|
||||
|
||||
@ -21,6 +21,7 @@ void uart_init(void) {
|
||||
hal_uart_init();
|
||||
|
||||
/* Set default configuration */
|
||||
uart.config.instance = HAL_UART_INSTANCE_1;
|
||||
uart.config.baudrate = 115200;
|
||||
uart.config.parity = HAL_UART_PARITY_NONE;
|
||||
uart.config.stopbits = HAL_UART_STOPBITS_1;
|
||||
@ -58,7 +59,7 @@ void uart_send(const uint8_t *data, size_t length) {
|
||||
return;
|
||||
}
|
||||
|
||||
hal_uart_send(data, length);
|
||||
hal_uart_send(uart.config.instance, data, length);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,7 +71,7 @@ void uart_send_string(const char *str) {
|
||||
return;
|
||||
}
|
||||
|
||||
hal_uart_send((const uint8_t *)str, strlen(str));
|
||||
hal_uart_send(uart.config.instance, (const uint8_t *)str, strlen(str));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -84,7 +85,7 @@ size_t uart_receive(uint8_t *data, size_t length) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return hal_uart_receive(data, length);
|
||||
return hal_uart_receive(uart.config.instance, data, length);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,7 +93,7 @@ size_t uart_receive(uint8_t *data, size_t length) {
|
||||
* @retval 1 if ready, 0 otherwise
|
||||
*/
|
||||
uint8_t uart_is_tx_ready(void) {
|
||||
return hal_uart_is_tx_ready();
|
||||
return hal_uart_is_tx_ready(uart.config.instance);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -100,5 +101,5 @@ uint8_t uart_is_tx_ready(void) {
|
||||
* @retval 1 if data available, 0 otherwise
|
||||
*/
|
||||
uint8_t uart_is_rx_ready(void) {
|
||||
return hal_uart_is_rx_ready();
|
||||
return hal_uart_is_rx_ready(uart.config.instance);
|
||||
}
|
||||
Reference in New Issue
Block a user