#include <gx_uart.h>
#define UART_PORT0 0
#define BUF_SIZE 1024
static uint8_t uart_buffer[BUF_SIZE];
static uint32_t received_length = 0;
static uint32_t need_length = BUF_SIZE;
static int uart_can_receive_callback(int port, int length, void *priv)
{
gx_uart_recv_buffer(port, uart_buffer, length);
gx_uart_stop_async_recv(port);
return 0;
}
static int uart_can_send_callback(int port, int length, void *priv)
{
gx_uart_send_buffer(port, uart_buffer, length);
gx_uart_stop_async_send(port);
return 0;
}
static int uart_send_done_callback(int port, void *priv)
{
gx_uart_async_send_buffer_stop(port);
return 0;
}
static int uart_recv_done_callback(int port, void *priv)
{
gx_uart_async_recv_buffer_stop(port);
return 0;
}
int uart_example(void)
{
char ch;
// 串口功能初始化
gx_uart_init(UART_PORT0, 115200);
/* 场景1: 单字符接收和发送 */
ch = gx_uart_getc(UART_PORT0);
gx_uart_putc(UART_PORT0, &ch);
/* 场景2: 字符串接收和发送(阻塞方式) */
gx_uart_read(UART_PORT0, uart_buffer, BUF_SIZE);
gx_uart_write(UART_PORT0, uart_buffer, BUF_SIZE);
/* 场景3: 异步收发串口数据(中断模式) */
// 启动串口异步接收, uart_can_receive_callback 中断回调中实际接收串口数据
gx_uart_start_async_recv(UART_PORT0, uart_can_receive_callback, NULL);
// 启动串口异步发送, uart_can_send_callback 中断回调中实际发送串口数据
gx_uart_start_async_send(UART_PORT0, uart_can_send_callback, NULL);
/* 场景4: 异步收发串口数据(DMA模式) */
// 启动串口异步接收, 当接收完成后,调用 uart_recv_done_callback 回调函数通知
gx_uart_async_recv_buffer(UART_PORT0, uart_buffer, BUF_SIZE, uart_recv_done_callback, NULL);
// 启动串口异步发送, 当发送完成后,调用 uart_send_done_callback 回调函数通知
gx_uart_async_send_buffer(UART_PORT0, uart_buffer, BUF_SIZE, uart_send_done_callback, NULL);
return 0;
}