- UART
- 接口列表
- 接口详情
- int32_t hal_uart_init(uart_dev_t *uart)
- int32_t hal_uart_send(uart_dev_t uart, const void data, uint32_t size, uint32_t timeout)
- int32_t hal_uart_recv(uart_dev_t uart, void data, uint32_t expect_size, uint32_t timeout)
- int32_t hal_uart_recv_II(uart_dev_t uart, void data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout)
- int32_t hal_uart_finalize(uart_dev_t *uart)
- 相关结数据结构
- 使用示例
UART
更新时间:2019-01-25 14:41:26
接口列表
接口详情
int32_t hal_uart_init(uart_dev_t *uart)
描述 |
初始化指定UART |
参数 |
uart:UART设备描述,定义需要初始化的UART参数 |
返回值 |
返回成功或失败, 返回0表示UART初始化成功,非0表示失败 |
int32_t hal_uart_send(uart_dev_t uart, const void data, uint32_t size, uint32_t timeout)
描述 |
从指定的UART发送数据 |
参数 |
uart:UART设备描述,定义需要初始化的UART参数 |
|
data:指向要发送数据的数据指针 |
|
size:要发送的数据字节数 |
|
timeout:超时时间(单位ms),如果希望一直等待设置为HAL_WAIT_FOREVER |
返回值 |
返回成功或失败, 返回0表示UART数据发送成功,非0表示失败 |
int32_t hal_uart_recv(uart_dev_t uart, void data, uint32_t expect_size, uint32_t timeout)
描述 |
从指定的UART接收数据 |
参数 |
uart:UART设备描述,定义需要初始化的UART参数 |
|
data:指向接收缓冲区的数据指针 |
|
expect_size:期望接收的数据字节数 |
|
timeout:超时时间(单位ms),如果希望一直等待设置为HAL_WAIT_FOREVER |
返回值 |
返回成功或失败, 返回0表示成功接收expect_size个数据,非0表示失败 |
int32_t hal_uart_recv_II(uart_dev_t uart, void data, uint32_t expect_size, uint32_t *recv_size, uint32_t timeout)
描述 |
从指定的UART接收数据2 |
参数 |
uart:UART设备描述,定义需要初始化的UART参数 |
|
data:指向接收缓冲区的数据指针 |
|
expect_size:期望接收的数据字节数 |
|
recv_size:实际接收数据字节数 |
|
timeout:超时时间(单位ms),如果希望一直等待设置为HAL_WAIT_FOREVER |
返回值 |
返回成功或失败, 返回0表示成功接收expect_size个数据,非0表示失败 |
int32_t hal_uart_finalize(uart_dev_t *uart)
描述 |
关闭指定UART |
参数 |
uart:UART设备描述,定义需要初始化的UART参数 |
返回值 |
类型:int 返回成功或失败, 返回0表示UART关闭成功,非0表示失败。 |
相关结数据结构
uart_dev_t
typedef struct {
uint8_t port;
uart_config_t config;
void *priv;
} uart_dev_t;
uart_config_t
typedef struct {
uint32_t baud_rate;
hal_uart_data_width_t data_width;
hal_uart_parity_t parity;
hal_uart_stop_bits_t stop_bits;
hal_uart_flow_control_t flow_control;
hal_uart_mode_t mode;
} uart_config_t;
hal_uart_data_width_t
typedef enum {
DATA_WIDTH_5BIT,
DATA_WIDTH_6BIT,
DATA_WIDTH_7BIT,
DATA_WIDTH_8BIT,
DATA_WIDTH_9BIT
} hal_uart_data_width_t;
hal_uart_parity_t
typedef enum {
NO_PARITY,
ODD_PARITY,
EVEN_PARITY
} hal_uart_parity_t;
hal_uart_stop_bits_t
typedef enum {
STOP_BITS_1,
STOP_BITS_2
} hal_uart_stop_bits_t;
hal_uart_flow_control_t
typedef enum {
FLOW_CONTROL_DISABLED,
FLOW_CONTROL_CTS,
FLOW_CONTROL_RTS,
FLOW_CONTROL_CTS_RTS
} hal_uart_flow_control_t;
hal_uart_mode_t
typedef enum {
MODE_TX,
MODE_RX,
MODE_TX_RX
} hal_uart_mode_t;
使用示例
#include <aos/hal/uart.h>
#define UART1_PORT_NUM 1
#define UART_BUF_SIZE 10
#define UART_TX_TIMEOUT 10
#define UART_RX_TIMEOUT 10
uart_dev_t uart1;
char uart_data_buf[UART_BUF_SIZE];
int application_start(int argc, char *argv[])
{
int count = 0;
int ret = -1;
int i = 0;
int rx_size = 0;
uart1.port = UART1_PORT_NUM;
uart1.config.baud_rate = 115200;
uart1.config.data_width = DATA_WIDTH_8BIT;
uart1.config.parity = NO_PARITY;
uart1.config.stop_bits = STOP_BITS_1;
uart1.config.flow_control = FLOW_CONTROL_DISABLED;
uart1.config.mode = MODE_TX_RX;
ret = hal_uart_init(&uart1);
if (ret != 0) {
printf("uart1 init error !\n");
}
for (i = 0; i < UART_BUF_SIZE; i++) {
uart_data_buf[i] = i + 1;
}
ret = hal_uart_send(&uart1, uart_data_buf, UART_BUF_SIZE, UART_TX_TIMEOUT);
if (ret == 0) {
printf("uart1 data send succeed !\n");
}
while(1) {
ret = hal_uart_recv_II(&uart1, uart_data_buf, UART_BUF_SIZE,
&rx_size, UART_RX_TIMEOUT);
if ((ret == 0) && (rx_size == UART_BUF_SIZE)) {
printf("uart1 data received succeed !\n");
ret = hal_uart_send(&uart1, uart_data_buf, rx_size, UART_TX_TIMEOUT);
if (ret == 0) {
printf("uart1 data send succeed !\n");
}
}
aos_msleep(100);
};
}
注:port为逻辑端口号,其与物理端口号的对应关系见具体的对接实现
移植说明
新建hal_uart_xxmcu.c和hal_uart_xxmcu.h的文件,并将这两个文件放到platform/mcu/xxmcu/hal目录下。在hal_uart_xxmcu.c中实现所需要的hal函数,hal_uart_xxmcu.h中放置相关宏定义。<br /> 参考platform/mcu/stm32l4xx/src/STM32L496G-Discovery/hal/hal_uart_stm32l4.c