UART

Last Updated on : 2025-04-28 02:13:31download

Overview

A universal asynchronous receiver-transmitter (UART) is a universal serial data bus used for asynchronous communication. The bus supports bidirectional communication and can achieve full-duplex transmission and reception.

API description

tkl_uart_init

OPERATE_RET tkl_uart_init(TUYA_UART_NUM_E port_id, TUYA_UART_BASE_CFG_T *cfg);
  • Features:

    • Initialize the specified UART instance using the port number and basic configuration, and return the initialization result.
  • Parameters:

    • port_id: The port number.

      typedef enum {
           TUYA_UART_NUM_0,		// UART 0
           TUYA_UART_NUM_1,		// UART 1
           TUYA_UART_NUM_2,	        // UART 2
           TUYA_UART_NUM_3,	        // UART 3
           TUYA_UART_NUM_4,	        // UART 4
           TUYA_UART_NUM_5,	        // UART 5
           TUYA_UART_NUM_MAX,
       } TUYA_UART_NUM_E;
      
    • cfg: The UART basic configuration, including baud rate, parity bit, stop bit, and flow control.

      typedef struct {
          UINT_T                      baudrate;
          TUYA_UART_PARITY_TYPE_E     parity;
          TUYA_UART_DATA_LEN_E        databits;
          TUYA_UART_STOP_LEN_E        stopbits;
          TUYA_UART_FLOWCTRL_TYPE_E   flowctrl;
      } TUYA_UART_BASE_CFG_T;
      
      • TUYA_UART_PARITY_TYPE_E:

        Name Definition
        TUYA_UART_PARITY_TYPE_NONE 0x (no parity bit)
        TUYA_UART_PARITY_TYPE_ODD 0x1 (odd parity bit)
        TUYA_UART_PARITY_TYPE_EVEN 0x2 (even parity bit)
      • TUYA_UART_DATA_LEN_E:

        Name Definition
        TUYA_UART_DATA_LEN_5BIT 0x5 (data length of 5 bits)
        TUYA_UART_DATA_LEN_6BIT 0x6 (data length of 6 bits)
        TUYA_UART_DATA_LEN_7BIT 0x7 (data length of 7 bits)
        TUYA_UART_DATA_LEN_8BIT 0x8 (data length of 8 bits)
      • TUYA_UART_STOP_LEN_E:

        Name Definition
        TUYA_UART_STOP_LEN_1BIT 0x1 (1 stop bit)
        TUYA_UART_STOP_LEN_1_5BIT1 0x2 (1.5 stop bits)
        TUYA_UART_STOP_LEN_2BIT 0x3 (2 stop bits)
      • TUYA_UART_FLOWCTRL_TYPE_E:

        Name Definition
        TUYA_UART_FLOWCTRL_NONE 0x0 (no flow control)
        TUYA_UART_FLOWCTRL_RTSCTS 0x1 (Request/clear to send)
        TUYA_UART_FLOWCTRL_XONXOFF 0x2 (pause/resume transmission)
        TUYA_UART_FLOWCTRL_DTRDSR 0x3 (Data terminal ready/data ready)
  • Return value:

    • OPRT_OK: Success.
    • For more information, see the definitions of uart type in the file tuya_error_code.h.

tkl_uart_deinit

OPERATE_RET tkl_uart_deinit(TUYA_UART_NUM_E port_id);
  • Features:

    • Deinitialize the UART port.
    • This interface stops the ongoing transmission (if any) and releases the related software and hardware resources.
  • Parameters:

    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
  • Return value:

    • OPRT_OK: Success.
    • For more information, see the definitions of uart type in the file tuya_error_code.h.

tkl_uart_write

INT_T tkl_uart_write(TUYA_UART_NUM_E port_id, VOID_T *buff, UINT16_T len);
  • Features:
    • The UART port sends data.
  • Parameters:
    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
    • buff: The buffer of the data to be sent.

    • len: The length of the data to be sent.

  • Return value:
    • ≥ 0: The length of the data to be sent.
    • < 0: An error occurred while sending data.

tkl_uart_rx_irq_cb_reg

VOID_T tkl_uart_rx_irq_cb_reg(TUYA_UART_NUM_E port_id, TUYA_UART_IRQ_CB rx_cb);
  • Features:

    • Register the UART receive interrupt callback function.
  • Parameters:

    • port_id: The port number.

       typedef enum {
           TUYA_UART_NUM_0,		// UART 0
           TUYA_UART_NUM_1,		// UART 1
           TUYA_UART_NUM_2,	        // UART 2
           TUYA_UART_NUM_3,	        // UART 3
           TUYA_UART_NUM_4,	        // UART 4
           TUYA_UART_NUM_5,	        // UART 5
           TUYA_UART_NUM_MAX,
       } TUYA_UART_NUM_E;
      
    • rx_cb: The receive interrupt callback function.

      The callback function type TUYA_UART_IRQ_CB is defined as follows, where port_id is the port number:

      typedef VOID_T (*TUYA_UART_IRQ_CB)(TUYA_UART_NUM_E port_id);
      
  • Return value:

    • VOID: Empty.

tkl_uart_tx_irq_cb_reg

VOID_T tkl_uart_tx_irq_cb_reg(TUYA_UART_NUM_E port_id, TUYA_UART_IRQ_CB tx_cb);
  • Features:

    • Register the UART transmit interrupt callback function.
  • Parameters:

    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
    • tx_cb: The transmit interrupt callback function.

      The callback function type TUYA_UART_IRQ_CB is defined as follows, where port_id is the port number:

      typedef VOID_T (*TUYA_UART_IRQ_CB)(TUYA_UART_NUM_E port_id);
      
  • Return value:

    • VOID: Empty.

tkl_uart_read

INT_T tkl_uart_read(TUYA_UART_NUM_E port_id, VOID_T *buff, UINT16_T len);
  • Features:

    • Read data from the UART.
  • Parameters:

    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
    • buff: The buffer of the data to be received.

    • len: The length of data to be received.

  • Return value:

    • ≥ 0: The length of the data to be sent.
    • < 0: An error occurred while sending data.

tkl_uart_set_tx_int

OPERATE_RET tkl_uart_set_tx_int(TUYA_UART_NUM_E port_id, BOOL_T enable);
  • Features:
    • Enable or disable the UART transmit interrupt.
  • Parameters:
    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
    • enable: Enable or disable the interrupt.

      • 0: Disable
      • 1: Enable
  • Return value:
    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_uart_set_rx_flowctrl

OPERATE_RET tkl_uart_set_rx_flowctrl(TUYA_UART_NUM_E port_id, BOOL_T enable);
  • Features:

    • Enable or disable the UART receive flow control.
  • Parameters:

    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
    • enable: Enable or disable the flow control.

      • 0: Disable
      • 1: Enable
  • Return value:

    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_uart_wait_for_data

OPERATE_RET tkl_uart_wait_for_data(TUYA_UART_NUM_E port_id, INT_T timeout_ms);
  • Features:
    • Wait to receive data.
  • Parameters:
    • port_id: The port number.

       typedef enum {
           TUYA_UART_NUM_0,		// UART 0
           TUYA_UART_NUM_1,		// UART 1
           TUYA_UART_NUM_2,	        // UART 2
           TUYA_UART_NUM_3,	        // UART 3
           TUYA_UART_NUM_4,	        // UART 4
           TUYA_UART_NUM_5,	        // UART 5
           TUYA_UART_NUM_MAX,
       } TUYA_UART_NUM_E;
      
    • timeout_ms: The number of milliseconds to wait.

  • Return value:
    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

tkl_uart_ioctl

OPERATE_RET tkl_uart_ioctl(TUYA_UART_NUM_E port_id, UINT32_T cmd, VOID *arg);
  • Features:

    • UART control commands.
  • Parameters:

    • port_id: The port number.

      typedef enum {
          TUYA_UART_NUM_0,		// UART 0
          TUYA_UART_NUM_1,		// UART 1
          TUYA_UART_NUM_2,	        // UART 2
          TUYA_UART_NUM_3,	        // UART 3
          TUYA_UART_NUM_4,	        // UART 4
          TUYA_UART_NUM_5,	        // UART 5
          TUYA_UART_NUM_MAX,
      } TUYA_UART_NUM_E;
      
    • cmd: The UART control commands.

      • TUYA_UART_IOCTL_CMD_E:

        Name Definition
        TUYA_UART_SUSPEND_CMD 0x0 (Pause the UART)
        TUYA_UART_RESUME_CMD 0x1 (Resume the UART)
        TUYA_UART_FLUSH_CMD 0x2 (Refresh the UART buffer)
        TUYA_UART_RECONFIG_CMD 0x3 (Reinitialize the UART)
        TUYA_UART_USER_CMD 0x4 (User-defined command)
    • arg: The parameter of the specified control command.

  • Return value:

    • OPRT_OK: Success.
    • For more information, see tuya_error_code.h.

Example

Example 1

int uart_loopback_test(void)
{
    UINT_T port_id;
    TUYA_UART_BASE_CFG_T cfg;
    OPERATE_RET ret = OPRT_OK;
    const int bufsize = 8;
    unsigned char tx[bufsize], rx[bufsize];
    INT_T bytes;
    int i;

    // Start

    port_id = TUYA_UART_NUM_0;

    cfg.baudrate = 115200;
    cfg.databits = TUYA_UART_DATA_LEN_8BIT;
    cfg.parity = TUYA_UART_PARITY_TYPE_NONE;
    cfg.stopbits = TUYA_UART_STOP_LEN_1BIT;
    cfg.flowctrl = TUYA_UART_FLOWCTRL_NONE;

    ret = tkl_uart_init(port_id, &cfg);

    for (i = 0; i < bufsize; i++) {
        tx[i] = 'A' + i;
    }

    // Loop 3 times
    for(i=0;i<3;i++) {
        bzero(rx, sizeof(rx));

        bytes = tkl_uart_write(port_id, tx, sizeof(tx));
        if (bytes <= 0) {
            // Fail
            ret = OPRT_COM_ERROR;
         } else {
             // Wait at most 5 seconds until the data is ready
            ret = tkl_uart_wait_for_data(port_id, 5000);
            if (ret == OPRT_TIMEOUT) {
               // timeout
               tkl_uart_deinit(port_id);
               return OPRT_COM_ERROR;
            }

            bytes = tkl_uart_read(port_id, rx, sizeof(rx));
            if (bytes < 0) {
                // Fail
                ret = OPRT_COM_ERROR;
            } else {
                if (memcmp(tx, rx, bufsize) != 0) {
                    // Data is not identical
                    ret = OPRT_COM_ERROR;
                } else {
                    ret = OPRT_OK;
                }
            }
        }

        if (ret != OPRT_OK) {
            // Fail
        } else {
            // OK
        }
    }
    //Deinit
    tkl_uart_deinit(port_id);
    return ret;
}

Example 2

static int sg_rx_flag = 0;
static int sg_tx_flag = 0;

static VOID_T tuya_rx_cb(TUYA_UART_NUM_E port_id)
{
    // Mutex lock
    sg_rx_flag = 1;
    // Mutex unlock
}

static static VOID_T tuya_tx_cb(TUYA_UART_NUM_E port_id)
{
    // Mutex lock
    sg_tx_flag = 1;
    // Mutex unlock
}

int uart_loopback_test(void)
{
    UINT_T port_id;
    TUYA_UART_BASE_CFG_T cfg;
    OPERATE_RET ret = OPRT_OK;
    const int bufsize = 8;
    unsigned char tx[bufsize], rx[bufsize];
    INT_T bytes;
    static int sl_first_time =1;
    int i;

    // Start

    sg_rx_flag = 0;
    sg_tx_flag = 0;

    port_id = TUYA_UART_NUM_0;

    cfg.baudrate = 115200;
    cfg.databits = TUYA_UART_DATA_LEN_8BIT;
    cfg.parity = TUYA_UART_PARITY_TYPE_NONE;
    cfg.stopbits = TUYA_UART_STOP_LEN_1BIT;
    cfg.flowctrl = TUYA_UART_FLOWCTRL_NONE;

    ret = tkl_uart_init(port_id, &cfg);

    if(tkl_uart_set_tx_int(port_id,1) < 0) {
        // Fail
        tkl_uart_deinit(port_id);
        return OPRT_COM_ERROR;
    }

    for (i = 0; i < bufsize; i++) {
        tx[i] = 'A' + i;
    }

    // Loop 3 times
    for(i=0;i<3;i++) {

        bzero(rx, sizeof(rx));


        if(sl_first_time || sg_tx_flag) {
            bytes = tkl_uart_write(port_id, tx, sizeof(tx));
        }else {
            continue;
        }

        if (bytes <= 0) {
            // Fail
             ret = OPRT_COM_ERROR;
        } else {
        // Wait at most 5 seconds until the data is ready
        ret = tkl_uart_wait_for_data(port_id, 5000);

        if(sg_rx_flag) {
            bytes = tkl_uart_read(port_id, rx, sizeof(rx));
            if (bytes < 0) {
               // Fail
               ret = OPRT_COM_ERROR;
            } else {
                if (memcmp(tx, rx, bufsize) != 0) {
                // Data is not identical
                ret = OPRT_COM_ERROR;
                } else {
                    ret = OPRT_OK;
                }
            }
            sg_rx_flag = 0;
           }
        }
        if (ret != OPRT_OK) {
        // Fail
        } else {
        // OK
        }
    }  
    // Disable int
    tkl_uart_set_tx_int(port_id,0);
    // Deinit
    tkl_uart_deinit(port_id);
    return ret;
}