UART

概述

WQ7036 芯片中共有 4 个 UART(Universal Asynchronous Receiver/Transmitter,通用异步收发传输器),均位于应用子系统,可被 DMA 访问或 CPU 直接访问。

UART 的架构如下图所示。

../../_images/UART_architecture.png

UART 架构图

UART 以字符为导向,不要求在发送数据的同时发送时钟,可实现设备间的异步通信。UART 通信要求发送端和接收端的速率、停止位宽度、奇偶校验位相同。

UART 接口信号包含:

  • TXD:待发送的串行数据

  • RXD:已接收的串行数据

  • RTS(Request To Send):请求发送的流控信号

  • CTS(Clear To Send):允许发送的流控信号

UART 发送数据的流程如下:

  1. UART 的待发送数据来自 UART 内部存储(UART MEM)。数据可通过 CPU 直接写入 UART MEM,或通过 DMA 从其他位置搬运至 UART MEM。

  2. UART 寄存器配置 UART 控制器(UART CTRL)的工作模式。UART CTRL 读取 UART MEM 获得待发送数据。

  3. 控制器按 UART 相关协议将待发送数据转换成串行数据发送到 TXD 端。

UART 接收数据的流程如下:

  1. UART 收到串行数据 RXD。

  2. UART CTRL 按 UART 协议转换成并行的 byte 数据,并将数据存放于 UART MEM。

  3. CPU 直接读取数据或者通过 DMA 将数据搬运至其他 MEM。

功能特性

四个 UART 的 Tx/Rx 共享 2 KB x 8 bits UART MEM,且每个 UART 的 DMA 可选择。UART 的数据位宽度可配置成 5/6/7/8 bits,停止位宽度可以配置为 1/1.5/2/3/4 bits。

UART 支持:

  • 全双工异步通信

  • 收/发波特率可配

  • 奇偶校验

  • CTS/RTS 硬件流控

  • XON/XOFF 软件流控

  • RS232、RS485 和 IrDA 协议

  • Loopback 回环测试

工作模式

UART TXD/RXD 连接同一个 PAD,TXD 信号经过 GPIO matrix 为输出信号。

寄存器 uart_share_io_en 可以配置 UART 处于单线或双线模式:

  • uart_share_io_en = 1 :UART 处于单线模式(半双工)。

  • uart_share_io_en = 0 :UART 处于双线模式(全双工)。

当 UART 处于单线模式时,电路产生输出使能 uart_pad_share_oen 用于控制数据传输方向。

  • 配置寄存器 force_share_oen_low :此时 uart_pad_share_oen 一直为 0 ,接收数据。

  • 配置寄存器 force_share_oen_high :此时 uart_pad_share_oen 一直为 1 ,发送数据。

在单线模式下,当 Rx 处于输入状态且有数据待发送时,则需等待 Rx 接收完当前字节的数据,再将传输方向由输入转为输出,由 Tx 完成数据发送。此时,RXD 信号被强制置为高电平,后续若需要继续接受数据,需要再次修改传输方向。

资源依赖

  • UART

  • DMA (DMA 传输模式)

用法流程

1 /** 选择要初始化的端口 1 */
2 wq_uart_init(WQ_UART_PORT_1);
 1 /** 定义并初始化配置变量 */
 2 wq_uart_configuration_t uart_cfg;
 3 uart_cfg.baud_rate = 2000000;
 4 uart_cfg.data_bits = WQ_UART_DATA_BITS_8;
 5 uart_cfg.parity = WQ_UART_PARITY_NONE;
 6 uart_cfg.stop_bits = WQ_UART_STOP_BITS_1;
 7
 8 wq_uart_gpio_configuration_t pin_cfg;
 9 pin_cfg.tx = 73;
10 pin_cfg.rx = 74;
11
12 /** 配置 rx callback func*/
13 wq_uart_register_rx_callback(WQ_UART_PORT_1, rx_buf, BUF_SIZE,
14                              uart1_rx_callback);
1 /** 打开串口 */
2 wq_uart_open(WQ_UART_PORT_1, &uart_cfg, &pin_cfg);
1 /** 写字符串 */
2 wq_uart_puts(WQ_UART_PORT_1, str_1);
3
4 /** 写 buffer */
5 wq_uart_write_buffer(WQ_UART_PORT_1,buffer,buf_len);
6
7 /** 读 buffer */
8 wq_uart_read(WQ_UART_PORT_1,buffer,buf_len);
1 /** 关闭串口 */
2 wq_uart_close(WQ_UART_PORT_1);
1 /** 注销串口 */
2 wq_uart_deinit(WQ_UART_PORT_1);

参考示例

/examples/uart_demo/

API 介绍

This section introduces the UART module's enum, structure, functions and how to use this driver.

Defines

UART_MONITOR_DEBUG

uart monitor debug enable 0 for disable otherwise enable

Typedefs

typedef struct wq_uart_configuration wq_uart_configuration_t

UART configuration

typedef struct wq_uart_gpio_configuration wq_uart_gpio_configuration_t

UART gpio configuration

typedef struct wq_uart_flow_control_cfg wq_uart_flow_control_cfg_t

UART flow control configuration

typedef void (*wq_uart_write_done_callback)(const void *buffer, uint32_t length)

Uart write done callback func Invoke when block send done.

备注

buffer can't be free before this callback are invoke.

typedef void (*wq_uart_rx_callback)(const void *buffer, uint32_t length)

Uart receive callback. Invoke when data receive on uart.

Enums

enum WQ_UART_DATA_BITS

Uart data bits

Values:

enumerator WQ_UART_DATA_BITS_MIN_BITS

Used for checking, the min value it can take

enumerator WQ_UART_DATA_BITS_5

5 Data bits

enumerator WQ_UART_DATA_BITS_6

6 Data bits

enumerator WQ_UART_DATA_BITS_7

7 Data bits

enumerator WQ_UART_DATA_BITS_8

8 Data bits

enumerator WQ_UART_DATA_BITS_MAX_BITS

Used for checking, the max value it can take

enum WQ_UART_PARITY

Uart parity modes

Values:

enumerator WQ_UART_PARITY_NONE

No parity enabled

enumerator WQ_UART_PARITY_EVEN

Even parity

enumerator WQ_UART_PARITY_ODD

Odd parity

enum WQ_UART_STOP_BITS

Uart stop bits modes

Values:

enumerator WQ_UART_STOP_BITS_1

1 Stop bit

enumerator WQ_UART_STOP_BITS_1_5

1.5 Stop bits

enumerator WQ_UART_STOP_BITS_2

2 Stop bits

enum WQ_UART_FLOWCTRL

Uart flow control modes

Values:

enumerator WQ_UART_FLOWCTRL_RTS
enumerator WQ_UART_FLOWCTRL_CTS
enumerator WQ_UART_FLOWCTRL_CTS_RTS
enum WQ_UART_THR

Uart threshold type

Values:

enumerator WQ_UART_THR_FLOWCTRL
enumerator WQ_UART_THR_RXTIMEOUT
enumerator WQ_UART_THR_RXFULL
enumerator WQ_UART_THR_TXEMPTY
enum WQ_UART_DIRECTION

Uart uniq line direction type

Values:

enumerator WQ_UART_RX
enumerator WQ_UART_TX

Functions

void wq_uart_init(WQ_UART_PORT port)

This function is to init uart module.

参数:

port -- is uart port.

void wq_uart_deinit(WQ_UART_PORT port)

This function is to deinitialize the uart module.

参数:

port -- is uart port.

void wq_uart_flow_on(WQ_UART_PORT port)

This function is to open the uart flow.

参数:

port -- is uart port.

void wq_uart_flow_off(WQ_UART_PORT port)

This function is to close the uart flow.

参数:

port -- is uart port.

WQ_RET wq_uart_gpio_config(WQ_UART_PORT port, const wq_uart_gpio_configuration_t *cfg)

This function is to config uart gpio.

参见

WQ_RET

参数:
  • port -- is uart port.

  • cfg -- is uart gpio configuration.

返回:

WQ_RET

WQ_RET wq_uart_flow_control_config(WQ_UART_PORT port, const wq_uart_flow_control_cfg_t *cfg)

This function is to config the uart flow control.

参见

WQ_RET

参数:
  • port -- is uart port.

  • cfg -- is uart flow control configuration.

返回:

WQ_RET

WQ_RET wq_uart_dma_config(WQ_UART_PORT port, const wq_uart_dma_config_t *cfg)

This function is to config the uart dma.

参数:
  • port -- is uart port.

  • cfg -- is uart dma configuration.

返回:

WQ_RET WQ_RET_OK for success else error.

WQ_RET wq_uart_open(WQ_UART_PORT port, const wq_uart_configuration_t *cfg, const wq_uart_gpio_configuration_t *gpio_cfg)

This function is to open the uart module.

参数:
  • port -- is uart port.

  • cfg -- is uart configuration.

  • gpio_cfg -- is uart gpio configuration.

返回:

WQ_RET WQ_RET_OK for success else error.

WQ_RET wq_uart_close(WQ_UART_PORT port)

This function is to close the uart module.

参数:

port -- is uart port.

返回:

WQ_RET WQ_RET_OK for success else error.

void wq_uart_write_buffer(WQ_UART_PORT port, const char *buffer, uint32_t length)

This function is to send data in buffer.

参数:
  • port -- is uart port

  • buffer -- is data to send

  • length -- is data lenth

WQ_RET wq_uart_write(WQ_UART_PORT port, const char *string, uint32_t length, wq_uart_write_done_callback cb)

This function is to write through the uart.

参数:
  • port -- is uart port.

  • string -- is the input content.

  • length -- is the length of the input.

  • cb -- is the uart write done callback variable quantity.

返回:

WQ_RET WQ_RET_OK for success else error.

void wq_uart_flush(WQ_UART_PORT port)

This function is to flush the uart.

参数:

port -- is uart port.

WQ_RET wq_uart_read_dma(WQ_UART_PORT port, char *string, uint32_t length, wq_uart_rx_callback cb)

This function is to read through the dma.

参数:
  • port -- is uart port.

  • string -- is the string of the output.

  • length -- is the length of the output.

  • cb -- is the callback for read done.

返回:

WQ_RET WQ_RET_OK for success else error.

uint32_t wq_uart_read(WQ_UART_PORT port, uint8_t *buffer, uint32_t length)

This function is to read through the uart.

参数:
  • port -- is uart port.

  • buffer -- is the buffer of the output.

  • length -- is the length of the output.

返回:

uint32_t interrupt service routine prototype.

WQ_RET wq_uart_register_rx_callback(WQ_UART_PORT port, uint8_t *buffer, uint32_t length, wq_uart_rx_callback callback)

This function is to register the rx callback of the uart.

参数:
  • port -- is uart port.

  • buffer -- is the buffer of the received content.

  • length -- is the length of the received content.

  • callback -- is the uart rx callback variable quantity.

返回:

WQ_RET WQ_RET_INVAL or WQ_RET_AGAIN or WQ_RET_OK.

WQ_RET wq_uart_set_rx_buffer(WQ_UART_PORT port, uint8_t *buffer, uint32_t length)

This function is to set the uart rx buffer.

参数:
  • port -- is uart port.

  • buffer -- is the buffer of the received content.

  • length -- is the length of the received content.

返回:

WQ_RET WQ_RET_OK for success else error.

void wq_uart_unregister_rx_callback(WQ_UART_PORT port)

This function is to unregister the rx callback of the uart.

参数:

port -- is uart port.

void wq_uart_set_threshold(WQ_UART_PORT port, WQ_UART_THR thr, uint32_t value)

Set uart interrupt threshold.

参见

WQ_UART_PORT

参见

WQ_UART_THR

参数:
  • port -- [in]

  • thr -- [in]

  • value -- [in] value of the threshold

void wq_uart_putc(WQ_UART_PORT port, char c)

This function is to put a character through the uart.

参数:
  • port -- is uart port.

  • c -- is the character to put.

void wq_uart_puts(WQ_UART_PORT port, const char *s)

This function is to put a string of characters through the uart.

参数:
  • port -- is uart port.

  • s -- is the first pointer of the string.

void wq_uart_rx_reset(WQ_UART_PORT port)

This function is to clear rx fifo.

参数:

port -- is uart port

void wq_uart_share_io_enable(WQ_UART_PORT port)

This function is to enable uart share io.

参数:

port -- is uart port

void wq_uart_share_io_disable(WQ_UART_PORT port)

This function is to disable uart share io.

参数:

port -- is uart port

WQ_RET wq_uart_force_set_direction(WQ_UART_PORT port, WQ_UART_DIRECTION direction)

This function is to force control share io uart pin direction.

参数:
  • port -- is uart port

  • direction -- is uart direction

返回:

WQ_RET WQ_RET_OK for success else error.

void wq_uart_disable_force_direction_mode(WQ_UART_PORT port)

This function is to disable force control share io uart pin direction mode.

参数:

port -- is uart port

struct wq_uart_configuration
#include <wq_uart.h>

UART configuration

Public Members

uint32_t baud_rate

Uart baudrate

WQ_UART_DATA_BITS data_bits

Uart data bits

WQ_UART_PARITY parity

Uart parity

WQ_UART_STOP_BITS stop_bits

Uart stop bits

struct wq_uart_gpio_configuration
#include <wq_uart.h>

UART gpio configuration

Public Members

WQ_GPIO_ID tx

Uart Txd io, if same with Txd will auto use share io mode

WQ_GPIO_ID rx

Uart Txd io, if same with Txd will auto use share io mode

struct wq_uart_flow_control_cfg
#include <wq_uart.h>

UART flow control configuration

Public Members

WQ_UART_FLOWCTRL type

Uart Flow control type

WQ_GPIO_ID cts

Uart Flow control cts gpio

WQ_GPIO_ID rts

Uart Flow control rts gpio

struct wq_uart_dma_config_t
#include <wq_uart.h>

UART flow control configuration

Public Members

bool tx_use_dma

Is Uart TX use dma

bool rx_use_dma

Is Uart RX use dma

WQ_DMA_CH_PRIORITY tx_priority

TX dma priority

WQ_DMA_CH_PRIORITY rx_priority

RX dma priority