# 功能特性 ## UART API 功能 * 支持指定端口收发数据,不同芯片端口数量不一样 * 可编程串口波特率 * 支持按单/多字节收发数据 * 支持同步/异步收发数据 * 支持 FIFO 中断触发门限配置 ## UART 数据收发流程 ### 同步收发 * 同步收发相关接口 ```c // 输出一个字符 void gx_uart_putc(int port, int ch); // 输出一个字符, 如果是换行,转换成回车输出 void gx_uart_compatible_putc(int port, int ch); // 获取一个字符 int gx_uart_getc(int port); // 尝试获取一个字符,如果没有获取到字符,立马返回 int gx_uart_try_getc(int port, char *c); // 同步读取指定长度的数据 int gx_uart_read(int port, unsigned char *buf, int len); //非阻塞试读取指定长度的数据 int gx_uart_read_non_block(int port, unsigned char *buf, int len, unsigned int timeout_ms); // 同步写指定长度的数据 int gx_uart_write(int port, const unsigned char *buf, int len); ``` * 同步收发数据流程图 ![](../../../../gximage/gximage41_uart_sync_read_write.png) ### 异步收发 提供了两种异步收发的模式 * 中断模式 * DMA 模式 #### 中断模式 * 中断模式收发数据调用的函数接口 ```c // 开始异步发送串口数据 int gx_uart_start_async_send(int port, UART_CAN_SEND_CALLBACK callback, void *priv); // 停止异步发送串口数据 int gx_uart_stop_async_send(int port); // 开始异步接收串口数据 int gx_uart_start_async_recv(int port, UART_CAN_RECV_CALLBACK callback, void *priv); // 停止异步接收串口数据 int gx_uart_stop_async_recv(int port); // 发送串口数据,但是不检查 fifo 是否满 int gx_uart_send_buffer(int port, unsigned char *buffer, int length); // 接收串口数据,但是不检查 fifo 中是否有数据 int gx_uart_recv_buffer(int port, unsigned char *buffer, int length); ``` * 中断模式收发数据流程图 ![](../../../../gximage/gximage42_uart_async_read_write_int.png) #### DMA 模式 * DMA 模式收发数据调用的函数接口 ```c // 异步发送串口数据 int gx_uart_async_send_buffer(int port, unsigned char *buffer, int length, UART_SEND_DONE_CALLBACK callback, void *priv); // 异步接收串口数据 int gx_uart_async_recv_buffer(int port, unsigned char *buffer, int length, UART_RECV_DONE_CALLBACK callback, void *priv); // 停止串口异步发送接口 int gx_uart_async_send_buffer_stop(int port); // 停止串口异步接收接口 int gx_uart_async_recv_buffer_stop(int port); ``` * DMA 模式收发数据流程图 ![](../../../../gximage/gximage43_uart_async_read_write_dma.png) ## UART FIFO 操作 * FIFO是先进先出缓冲区的意思,即串口接收到的数据可以先进入FIFO,不必马上进入中断服务程序接收,这样可以节省CPU时间。对于发送数据也一样可以把要发送的数据一起写入FIFO,串口控制器按照写入的顺序依次发送出去。 * FIFO 相关接口 ```c // 设置串口发送 fifo 中断触发门限 int gx_uart_set_send_fifo_threshold(int port, GX_UART_FIFO_SEND_THRESHOLD threshold); // 设置接收 fio 中断触发门限 int gx_uart_set_recv_fifo_threshold(int port, GX_UART_FIFO_RECV_THRESHOLD threshold); // 清空发送 fifo void gx_uart_flush_send_fifo(int port); // 清空接收 fifo void gx_uart_flush_recv_fifo(int port); ```