1. 概述

通用异步收发传输器(Universal Asynchronous Receiver/Transmitter,通常称为 UART)是一种异步收发传输器,提供了与外部设备进行全双工数据交换的灵活方式。TG6210A共有 4 组 UART,其中UART0做为log口用于调试,其他UART可以用于客户应用(sdk暂时支持uart1和2)。

2. API参考

2.1 UART初始化

  • 函数原型:
    int hosal_uart_init(hosal_uart_dev_t *uart);
  • 功能描述:初始化UART
  • 函数参数:
    • uart:需要初始化的UART句柄

如果gpio%12=0,那么该gpio可以作为uart1的tx
如果gpio%12=1,那么该gpio可以作为uart1的rx
如果gpio%12=4,那么该gpio可以作为uart2的tx
如果gpio%12=5,那么该gpio可以作为uart2的tx

  1. typedef struct {
  2. uint8_t uart_id; /**< @brief UART id */
  3. uint8_t tx_pin; /**< @brief UART tx pin */
  4. uint8_t rx_pin; /**< @brief UART rx pin */
  5. uint8_t cts_pin; /**< @brief UART cts pin */
  6. uint8_t rts_pin; /**< @brief UART rts pin */
  7. uint32_t baud_rate; /**< @brief UART baud rate */
  8. hosal_uart_data_width_t data_width; /**< @brief UART data width */
  9. hosal_uart_parity_t parity; /**< @brief UART parity bit */
  10. hosal_uart_stop_bits_t stop_bits; /**< @brief UART stop btis */
  11. hosal_uart_flow_control_t flow_control; /**< @brief UART flow control */
  12. hosal_uart_mode_t mode; /**< @brief UART int or pull mode */
  13. } hosal_uart_config_t;
typedef struct {
    uint8_t       port;         /**< @brief UART port */
    hosal_uart_config_t config;     /**< @brief UART config */
    hosal_uart_callback_t tx_cb;        /**< @brief UART tx callback */
    void *p_txarg;              /**< @brief UART tx callback arg */
    hosal_uart_callback_t rx_cb;        /**< @brief UART rx callback */
    void *p_rxarg;              /**< @brief UART rx callback arg */
    hosal_uart_callback_t txdma_cb;     /**< @brief UART tx dma callback */
    void *p_txdma_arg;              /**< @brief UART tx dma callback arg */
    hosal_uart_callback_t rxdma_cb;     /**< @brief UART rx dma callback */
    void *p_rxdma_arg;              /**< @brief UART rx dma callback arg */
    hosal_dma_chan_t dma_tx_chan;       /**< @brief UART dma tx channel */
    hosal_dma_chan_t dma_rx_chan;       /**< @brief UART dma rx channel */
    void         *priv;         /**< @brief UART private data */
} hosal_uart_dev_t;
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      2.2 串口发送

  • 函数原型:
    int hosal_uart_send(hosal_uart_dev_t *uart,

const void *txbuf, uint32_t size);

  • 功能描述:使用polling的方式发送数据。
  • 函数参数:
    • uart:UART的句柄
    • txbuf:存放发送数据的buf
    • size:数据长度
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      2.3 串口接收

  • 函数原型:
    int hosal_uart_receive(hosal_uart_dev_t *uart,

void *data, uint32_t expect_size);

  • 功能描述:使用polling的方式接收数据。
  • 函数参数:
    • uart:UART的句柄
    • txbuf:存放接收数据的buf
    • size:数据长度
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      2.4 设置UART相关参数

  • 函数原型:
    int hosal_uart_ioctl (hosal_uart_dev_t *uart, int ctl, void *p_arg);

  • 功能描述:设置uart相关参数
  • 函数参数:

    • uart:UART的句柄
    • ctl:操作码
    • p_arg:参数
      #define HOSAL_UART_BAUD_SET          1   /**< @brief UART baud set */
      #define HOSAL_UART_BAUD_GET          2   /**< @brief UART baud get */
      #define HOSAL_UART_DATA_WIDTH_SET    3   /**< @brief UART data width set */
      #define HOSAL_UART_DATA_WIDTH_GET    4   /**< @brief UART data width get */
      #define HOSAL_UART_STOP_BITS_SET     5   /**< @brief UART stop bits set */
      #define HOSAL_UART_STOP_BITS_GET     6   /**< @brief UART stop bits get */
      #define HOSAL_UART_FLOWMODE_SET      7   /**< @brief UART flow mode set */
      #define HOSAL_UART_FLOWSTAT_GET      8   /**< @brief UART flow state get */
      #define HOSAL_UART_PARITY_SET        9   /**< @brief UART flow mode set */
      #define HOSAL_UART_PARITY_GET       10   /**< @brief UART flow state get */
      #define HOSAL_UART_MODE_SET         11   /**< @brief UART mode set */
      #define HOSAL_UART_MODE_GET         12   /**< @brief UART mode get */
      #define HOSAL_UART_FREE_TXFIFO_GET  13   /**< @brief UART free tx fifo get */
      #define HOSAL_UART_FREE_RXFIFO_GET  14   /**< @brief UART free rx fifo get */
      #define HOSAL_UART_FLUSH            15   /**< @brief Wait for the send to complete */
      #define HOSAL_UART_TX_TRIGGER_ON    16   /**< @brief UART TX trigger on */
      #define HOSAL_UART_TX_TRIGGER_OFF   17   /**< @brief UART TX trigger off */
      #define HOSAL_UART_DMA_TX_START     18   /**< @brief UART DMA TX start trans */
      #define HOSAL_UART_DMA_RX_START     19   /**< @brief UART DMA RX start trans */
      
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      2.5 设置UART回调函数

  • 函数原型:
    int hosal_uart_callback_set (hosal_uart_dev_t *uart,

int callback_type,
hosal_uart_callback_t pfn_callback,
void *arg);

  • 功能描述:设置UART回调函数
  • 函数参数:

    • uart:UART的句柄
    • callback_type:回调函数类型
    • pfn_callback:回调函数
    • arg:参数
      #define HOSAL_UART_TX_CALLBACK       1   /**< @brief UART tx idle interrupt callback */
      #define HOSAL_UART_RX_CALLBACK       2   /**< @brief UART rx complete callback */                                                                                                                          
      #define HOSAL_UART_TX_DMA_CALLBACK   3   /**< @brief UART tx DMA trans complete callback */
      #define HOSAL_UART_RX_DMA_CALLBACK   4   /**< @brief UART rx DMA trans complete callback */
      
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      2.6 反初始化UART

  • 函数原型:
    int hosal_uart_finalize(hosal_uart_dev_t *uart);

  • 功能描述:deinit UART
  • 函数参数:
    • uart:UART的句柄
  • 返回值:

    • 成功返回0。
    • 错误返回-1。

      3. Demo示例

      /**
      *  Define a UART device,
      *  TX pin : 16
      *  RX pin : 17
      *  baud : 2000000
      */
      HOSAL_UART_DEV_DECL(uart_dev, uart_id, 16, 17, 2000000);
      
  • 通过 hosal_uart_dev_t 定义一个uart设备。

hosal_uart_init(&uart_dev);
  • 调用 hosal_uart_init 初始化uart 。
    while (1) {
        /* Uart receive poll */
        ret = hosal_uart_receive(&uart_dev, data, sizeof(data));
        if (ret > 0) {
            /* Uart send poll */
            hosal_uart_send(&uart_dev, data, ret);
        }
    }
  • 通过 hosal_uart_send 接口以及 hosal_uart_receive 接口来收发UART数据。
  hosal_uart_finalize(&uart_dev);
  • 通过 hosal_uart_finalize 结束此uart的使用。