添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Peripherals API
  • Analog to Digital Converter (ADC) Oneshot Mode Driver
  • Analog to Digital Converter (ADC) Continuous Mode Driver
  • Analog to Digital Converter (ADC) Calibration Driver
  • Clock Tree
  • Digital To Analog Converter (DAC)
  • GPIO & RTC GPIO
  • General Purpose Timer (GPTimer)
  • Inter-Integrated Circuit (I2C)
  • Inter-IC Sound (I2S)
  • LED Control (LEDC)
  • Motor Control Pulse Width Modulator (MCPWM)
  • Pulse Counter (PCNT)
  • Remote Control Transceiver (RMT)
  • SD Pull-up Requirements
  • SDMMC Host Driver
  • SD SPI Host Driver
  • SDIO Card Slave Driver
  • Sigma-Delta Modulation (SDM)
  • SPI Flash API
  • SPI Master Driver
  • SPI Slave Driver
  • ESP32-WROOM-32SE (Secure Element)
  • Touch Sensor
  • Two-Wire Automotive Interface (TWAI)
  • Universal Asynchronous Receiver/Transmitter (UART)
    • Introduction
    • Functional Overview
    • Overview of RS485 Specific Communication 0ptions
    • Application Examples
    • API Reference
    • Introduction

      A Universal Asynchronous Receiver/Transmitter (UART) is a hardware feature that handles communication (i.e., timing requirements and data framing) using widely-adopted asynchronous serial communication interfaces, such as RS232, RS422, and RS485. A UART provides a widely adopted and cheap method to realize full-duplex or half-duplex data exchange among different devices.

      The ESP32 chip has 3 UART controllers (also referred to as port), each featuring an identical set of registers to simplify programming and for more flexibility.

      Each UART controller is independently configurable with parameters such as baud rate, data bit length, bit ordering, number of stop bits, parity bit, etc. All the regular UART controllers are compatible with UART-enabled devices from various manufacturers and can also support Infrared Data Association (IrDA) protocols.

      Functional Overview

      The overview describes how to establish communication between an ESP32 and other UART devices using the functions and data types of the UART driver. A typical programming workflow is broken down into the sections provided below:

    • Set Communication Parameters - Setting baud rate, data bits, stop bits, etc.

    • Set Communication Pins - Assigning pins for connection to a device

    • Install Drivers - Allocating ESP32's resources for the UART driver

    • Run UART Communication - Sending/receiving data

    • Use Interrupts - Triggering interrupts on specific communication events

    • Deleting a Driver - Freeing allocated resources if a UART communication is no longer required

    • Steps 1 to 3 comprise the configuration stage. Step 4 is where the UART starts operating. Steps 5 and 6 are optional.

      The UART driver's functions identify each of the UART controllers using uart_port_t . This identification is needed for all the following function calls.

      Set Communication Parameters

      UART communication parameters can be configured all in a single step or individually in multiple steps.

      Single Step

      Call the function uart_param_config() and pass to it a uart_config_t structure. The uart_config_t structure should contain all the required parameters. See the example below.

      const uart_port_t uart_num = UART_NUM_2;
      uart_config_t uart_config = {
          .baud_rate = 115200,
          .data_bits = UART_DATA_8_BITS,
          .parity = UART_PARITY_DISABLE,
          .stop_bits = UART_STOP_BITS_1,
          .flow_ctrl = UART_HW_FLOWCTRL_CTS_RTS,
          .rx_flow_ctrl_thresh = 122,
      // Configure UART parameters
      ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config));
      

      For more information on how to configure the hardware flow control options, please refer to peripherals/uart/uart_echo.

      Multiple Steps

      Configure specific parameters individually by calling a dedicated function from the table given below. These functions are also useful if re-configuring a single parameter.

      Functions for Configuring specific parameters individually

      Set Communication Pins

      After setting communication parameters, configure the physical GPIO pins to which the other UART device will be connected. For this, call the function uart_set_pin() and specify the GPIO pin numbers to which the driver should route the TX, RX, RTS, and CTS signals. If you want to keep a currently allocated pin number for a specific signal, pass the macro UART_PIN_NO_CHANGE.

      The same macro UART_PIN_NO_CHANGE should be specified for pins that will not be used.

      // Set UART pins(TX: IO4, RX: IO5, RTS: IO18, CTS: IO19)
      ESP_ERROR_CHECK(uart_set_pin(UART_NUM_2, 4, 5, 18, 19));
      

      Install Drivers

      Once the communication pins are set, install the driver by calling uart_driver_install() and specify the following parameters:

    • Size of TX ring buffer

    • Size of RX ring buffer

    • Event queue handle and size

    • Flags to allocate an interrupt

    • The function allocates the required internal resources for the UART driver.

      // Setup UART buffered IO with event queue
      const int uart_buffer_size = (1024 * 2);
      QueueHandle_t uart_queue;
      // Install UART driver using an event queue here
      ESP_ERROR_CHECK(uart_driver_install(UART_NUM_2, uart_buffer_size, \
                                              uart_buffer_size, 10, &uart_queue, 0));
      

      Once this step is complete, you can connect the external UART device and check the communication.

      Run UART Communication

      Serial communication is controlled by each UART controller's finite state machine (FSM).

      The process of sending data involves the following steps:

    • Write data into TX FIFO buffer

    • FSM serializes the data

    • FSM sends the data out

    • The process of receiving data is similar, but the steps are reversed:

    • FSM processes an incoming serial stream and parallelizes it

    • FSM writes the data into RX FIFO buffer

    • Read the data from RX FIFO buffer

    • Therefore, an application only writes and reads data from a specific buffer using uart_write_bytes() and uart_read_bytes() respectively, and the FSM does the rest.

      Transmit Data

      After preparing the data for transmission, call the function uart_write_bytes() and pass the data buffer's address and data length to it. The function copies the data to the TX ring buffer (either immediately or after enough space is available), and then exit. When there is free space in the TX FIFO buffer, an interrupt service routine (ISR) moves the data from the TX ring buffer to the TX FIFO buffer in the background. The code below demonstrates the use of this function.

      // Write data to UART.
      char* test_str = "This is a test string.\n";
      uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str));
      

      The function uart_write_bytes_with_break() is similar to uart_write_bytes() but adds a serial break signal at the end of the transmission. A 'serial break signal' means holding the TX line low for a period longer than one data frame.

      // Write data to UART, end with a break signal.
      uart_write_bytes_with_break(uart_num, "test break\n",strlen("test break\n"), 100);
      

      Another function for writing data to the TX FIFO buffer is uart_tx_chars(). Unlike uart_write_bytes(), this function does not block until space is available. Instead, it writes all data which can immediately fit into the hardware TX FIFO, and then return the number of bytes that were written.

      There is a 'companion' function uart_wait_tx_done() that monitors the status of the TX FIFO buffer and returns once it is empty.

      // Wait for packet to be sent
      const uart_port_t uart_num = UART_NUM_2;
      ESP_ERROR_CHECK(uart_wait_tx_done(uart_num, 100)); // wait timeout is 100 RTOS ticks (TickType_t)
      

      Receive Data

      Once the data is received by the UART and saved in the RX FIFO buffer, it needs to be retrieved using the function uart_read_bytes(). Before reading data, you can check the number of bytes available in the RX FIFO buffer by calling uart_get_buffered_data_len(). An example of using these functions is given below.

      // Read data from UART.
      const uart_port_t uart_num = UART_NUM_2;
      uint8_t data[128];
      int length = 0;
      ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
      length = uart_read_bytes(uart_num, data, length, 100);
      

      If the data in the RX FIFO buffer is no longer needed, you can clear the buffer by calling uart_flush().

      Software Flow Control

      If the hardware flow control is disabled, you can manually set the RTS and DTR signal levels by using the functions uart_set_rts() and uart_set_dtr() respectively.

      Communication Mode Selection

      The UART controller supports a number of communication modes. A mode can be selected using the function uart_set_mode(). Once a specific mode is selected, the UART driver handles the behavior of a connected UART device accordingly. As an example, it can control the RS485 driver chip using the RTS line to allow half-duplex RS485 communication.

      // Setup UART in rs485 half duplex mode
      ESP_ERROR_CHECK(uart_set_mode(uart_num, UART_MODE_RS485_HALF_DUPLEX));
      

      Use Interrupts

      There are many interrupts that can be generated depending on specific UART states or detected errors. The full list of available interrupts is provided in ESP32 Technical Reference Manual > UART Controller (UART) > UART Interrupts and UHCI Interrupts [PDF]. You can enable or disable specific interrupts by calling uart_enable_intr_mask() or uart_disable_intr_mask() respectively.

      The uart_driver_install() function installs the driver's internal interrupt handler to manage the TX and RX ring buffers and provides high-level API functions like events (see below).

      The API provides a convenient way to handle specific interrupts discussed in this document by wrapping them into dedicated functions:

    • Event detection: There are several events defined in uart_event_type_t that may be reported to a user application using the FreeRTOS queue functionality. You can enable this functionality when calling uart_driver_install() described in Install Drivers. An example of using Event detection can be found in peripherals/uart/uart_events.

    • FIFO space threshold or transmission timeout reached: The TX and RX FIFO buffers can trigger an interrupt when they are filled with a specific number of characters, or on a timeout of sending or receiving data. To use these interrupts, do the following:

    • Configure respective threshold values of the buffer length and timeout by entering them in the structure uart_intr_config_t and calling uart_intr_config()

    • Enable the interrupts using the functions uart_enable_tx_intr() and uart_enable_rx_intr()

    • Disable these interrupts using the corresponding functions uart_disable_tx_intr() or uart_disable_rx_intr()

    • Pattern detection: An interrupt triggered on detecting a 'pattern' of the same character being received/sent repeatedly. This functionality is demonstrated in the example peripherals/uart/uart_events. It can be used, e.g., to detect a command string with a specific number of identical characters (the 'pattern') at the end. The following functions are available:

    • Configure and enable this interrupt using uart_enable_pattern_det_baud_intr()

    • Disable the interrupt using uart_disable_pattern_det_intr()

    • Deleting a Driver

      If the communication established with uart_driver_install() is no longer required, the driver can be removed to free allocated resources by calling uart_driver_delete().

      Macros

      The API also defines several macros. For example, UART_HW_FIFO_LEN defines the length of hardware FIFO buffers; UART_BITRATE_MAX gives the maximum baud rate supported by the UART controllers, etc.

      Overview of RS485 Specific Communication 0ptions

      The following section uses [UART_REGISTER_NAME].[UART_FIELD_BIT] to refer to UART register fields/bits. For more information on a specific option bit, see ESP32 Technical Reference Manual > UART Controller (UART) > Register Summary [PDF]. Use the register name to navigate to the register description and then find the field/bit.

    • UART_RS485_CONF_REG.UART_RS485_EN: setting this bit enables RS485 communication mode support.

    • UART_RS485_CONF_REG.UART_RS485TX_RX_EN: if this bit is set, the transmitter's output signal loops back to the receiver's input signal.

    • UART_RS485_CONF_REG.UART_RS485RXBY_TX_EN: if this bit is set, the transmitter will still be sending data if the receiver is busy (remove collisions automatically by hardware).

    • The ESP32's RS485 UART hardware can detect signal collisions during transmission of a datagram and generate the interrupt UART_RS485_CLASH_INT if this interrupt is enabled. The term collision means that a transmitted datagram is not equal to the one received on the other end. Data collisions are usually associated with the presence of other active devices on the bus or might occur due to bus errors.

      The collision detection feature allows handling collisions when their interrupts are activated and triggered. The interrupts UART_RS485_FRM_ERR_INT and UART_RS485_PARITY_ERR_INT can be used with the collision detection feature to control frame errors and parity bit errors accordingly in RS485 mode. This functionality is supported in the UART driver and can be used by selecting the UART_MODE_RS485_APP_CTRL mode (see the function uart_set_mode()).

      The collision detection feature can work with circuit A and circuit C (see Section Interface Connection Options). In the case of using circuit A or B, the RTS pin connected to the DE pin of the bus driver should be controlled by the user application. Use the function uart_get_collision_flag() to check if the collision detection flag has been raised.

      The ESP32 UART controllers themselves do not support half-duplex communication as they cannot provide automatic control of the RTS pin connected to the RE/DE input of RS485 bus driver. However, half-duplex communication can be achieved via software control of the RTS pin by the UART driver. This can be enabled by selecting the UART_MODE_RS485_HALF_DUPLEX mode when calling uart_set_mode().

      Once the host starts writing data to the TX FIFO buffer, the UART driver automatically asserts the RTS pin (logic 1); once the last bit of the data has been transmitted, the driver de-asserts the RTS pin (logic 0). To use this mode, the software would have to disable the hardware flow control function. This mode works with all the used circuits shown below.

      Interface Connection Options

      This section provides example schematics to demonstrate the basic aspects of ESP32's RS485 interface connection.

    • The schematics below do not necessarily contain all required elements.

    • The analog devices ADM483 & ADM2483 are examples of common RS485 transceivers and can be replaced with other similar transceivers.

    • Circuit A: Collision Detection Circuit

              VCC ---------------+
                         +-------x-------+
              RXD <------| R             |
                         |              B|----------<> B
              TXD ------>| D    ADM483   |
      ESP                |               |     RS485 bus side
              RTS ------>| DE            |
                         |              A|----------<> A
                    +----| /RE           |
                    |    +-------x-------+
                    |            |
                   GND          GND
      

      This circuit is preferable because it allows for collision detection and is quite simple at the same time. The receiver in the line driver is constantly enabled, which allows the UART to monitor the RS485 bus. Echo suppression is performed by the UART peripheral when the bit UART_RS485_CONF_REG.UART_RS485TX_RX_EN is enabled.

      Circuit B: Manual Switching Transmitter/Receiver Without Collision Detection

              VCC ---------------+
                         +-------x-------+
              RXD <------| R             |
                         |              B|-----------<> B
              TXD ------>| D    ADM483   |
      ESP                |               |     RS485 bus side
              RTS --+--->| DE            |
                    |    |              A|-----------<> A
                    +----| /RE           |
                         +-------x-------+
      

      This circuit does not allow for collision detection. It suppresses the null bytes that the hardware receives when the bit UART_RS485_CONF_REG.UART_RS485TX_RX_EN is set. The bit UART_RS485_CONF_REG.UART_RS485RXBY_TX_EN is not applicable in this case.

      Circuit C: Auto Switching Transmitter/Receiver

       VCC1 <-------------------+-----------+           +-------------------+----> VCC2
                     10K ____   |           |           |                   |
                    +---|____|--+       +---x-----------x---+    10K ____   |
                    |                   |                   |   +---|____|--+
      RX <----------+-------------------| RXD               |   |
                         10K ____       |                  A|---+---------------<> A (+)
                    +-------|____|------| PV    ADM2483     |   |    ____  120
                    |   ____            |                   |   +---|____|---+  RS485 bus side
            VCC1 <--+--|____|--+------->| DE                |                |
                    10K        |        |                  B|---+------------+--<> B (-)
                            ---+    +-->| /RE               |   |    ____
               10K          |       |   |                   |   +---|____|---+
              ____       | /-C      +---| TXD               |    10K         |
      TX >---|____|--+_B_|/   NPN   |   |                   |                |
                         |\         |   +---x-----------x---+                |
                         | \-E      |       |           |                    |
                            |       |       |           |                    |
                           GND1    GND1    GND1        GND2                 GND2
      

      This galvanically isolated circuit does not require RTS pin control by a software application or driver because it controls the transceiver direction automatically. However, it requires suppressing null bytes during transmission by setting UART_RS485_CONF_REG.UART_RS485RXBY_TX_EN to 1 and UART_RS485_CONF_REG.UART_RS485TX_RX_EN to 0. This setup can work in any RS485 UART mode or even in UART_MODE_UART.

      peripherals/uart/uart_echo

      Configuring UART settings, installing the UART driver, and reading/writing over the UART1 interface.

      peripherals/uart/uart_events

      Reporting various communication events, using pattern detection interrupts.

      peripherals/uart/uart_async_rxtxtasks

      Transmitting and receiving data in two separate FreeRTOS tasks over the same UART.

      peripherals/uart/uart_select

      Using synchronous I/O multiplexing for UART file descriptors.

      peripherals/uart/uart_echo_rs485

      Setting up UART driver to communicate over RS485 interface in half-duplex mode. This example is similar to peripherals/uart/uart_echo but allows communication through an RS485 interface chip connected to ESP32 pins.

      peripherals/uart/nmea0183_parser

      Obtaining GPS information by parsing NMEA0183 statements received from GPS via the UART peripheral.

    • This header file is a part of the API provided by the driver component. To declare that your component depends on driver, add the following to your CMakeLists.txt:

      REQUIRES driver
      
      PRIV_REQUIRES driver
      esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, QueueHandle_t *uart_queue, int intr_alloc_flags)

      Install UART driver and set the UART to the default configuration.

      UART ISR handler will be attached to the same CPU core that this function is running on.

      Rx_buffer_size should be greater than UART_HW_FIFO_LEN(uart_num). Tx_buffer_size should be either zero or greater than UART_HW_FIFO_LEN(uart_num).

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • rx_buffer_size -- UART RX ring buffer size.

    • tx_buffer_size -- UART TX ring buffer size. If set to zero, driver will not use TX buffer, TX function will block task until all data have been sent out.

    • queue_size -- UART event queue size/depth.

    • uart_queue -- UART event queue handle (out param). On success, a new queue handle is written here to provide access to UART events. If set to NULL, driver will not use an event queue.

    • intr_alloc_flags -- Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info. Do not set ESP_INTR_FLAG_IRAM here (the driver's ISR handler is not located in IRAM)

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • bool uart_is_driver_installed(uart_port_t uart_num)

      Checks whether the driver is installed or not.

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • true driver is installed

    • false driver is not installed

    • esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit)

      Set UART data bits.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • data_bit -- UART data bits

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t *data_bit)

      Get the UART data bit configuration.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • data_bit -- Pointer to accept value of UART data bits.

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success, result will be put in (*data_bit)

    • esp_err_t uart_set_stop_bits(uart_port_t uart_num, uart_stop_bits_t stop_bits)

      Set UART stop bits.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • stop_bits -- UART stop bits

    • Returns
    • ESP_OK Success

    • ESP_FAIL Fail

    • esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t *stop_bits)

      Get the UART stop bit configuration.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • stop_bits -- Pointer to accept value of UART stop bits.

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success, result will be put in (*stop_bit)

    • esp_err_t uart_set_parity(uart_port_t uart_num, uart_parity_t parity_mode)

      Set UART parity mode.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • parity_mode -- the enum of uart parity configuration

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success

    • esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t *parity_mode)

      Get the UART parity mode configuration.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • parity_mode -- Pointer to accept value of UART parity mode.

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success, result will be put in (*parity_mode)

    • esp_err_t uart_get_sclk_freq(uart_sclk_t sclk, uint32_t *out_freq_hz)

      Get the frequency of a clock source for the HP UART port.

      Parameters
    • sclk -- Clock source

    • out_freq_hz -- [out] Output of frequency, in Hz

    • Returns
    • ESP_ERR_INVALID_ARG: if the clock source is not supported

    • otherwise ESP_OK

    • esp_err_t uart_set_baudrate(uart_port_t uart_num, uint32_t baudrate)

      Set UART baud rate.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • baudrate -- UART baud rate.

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success

    • esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t *baudrate)

      Get the UART baud rate configuration.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • baudrate -- Pointer to accept value of UART baud rate

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success, result will be put in (*baudrate)

    • esp_err_t uart_set_line_inverse(uart_port_t uart_num, uint32_t inverse_mask)

      Set UART line inverse mode.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • inverse_mask -- Choose the wires that need to be inverted. Using the ORred mask of uart_signal_inv_t

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh)

      Set hardware flow control.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • flow_ctrl -- Hardware flow control mode

    • rx_thresh -- Threshold of Hardware RX flow control (0 ~ UART_HW_FIFO_LEN(uart_num)). Only when UART_HW_FLOWCTRL_RTS is set, will the rx_thresh value be set.

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_set_sw_flow_ctrl(uart_port_t uart_num, bool enable, uint8_t rx_thresh_xon, uint8_t rx_thresh_xoff)

      Set software flow control.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1)

    • enable -- switch on or off

    • rx_thresh_xon -- low water mark

    • rx_thresh_xoff -- high water mark

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t *flow_ctrl)

      Get the UART hardware flow control configuration.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • flow_ctrl -- Option for different flow control mode.

    • Returns
    • ESP_FAIL Parameter error

    • ESP_OK Success, result will be put in (*flow_ctrl)

    • esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask)

      Clear UART interrupt status.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • clr_mask -- Bit mask of the interrupt status to be cleared.

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask)

      Set UART interrupt enable.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • enable_mask -- Bit mask of the enable bits.

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask)

      Clear UART interrupt enable bits.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • disable_mask -- Bit mask of the disable bits.

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_enable_rx_intr(uart_port_t uart_num)

      Enable UART RX interrupt (RX_FULL & RX_TIMEOUT INTERRUPT)

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_disable_rx_intr(uart_port_t uart_num)

      Disable UART RX interrupt (RX_FULL & RX_TIMEOUT INTERRUPT)

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_disable_tx_intr(uart_port_t uart_num)

      Disable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)

      Parameters

      uart_num -- UART port number

      Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh)

      Enable UART TX interrupt (TX_FULL & TX_TIMEOUT INTERRUPT)

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • enable -- 1: enable; 0: disable

    • thresh -- Threshold of TX interrupt, 0 ~ UART_HW_FIFO_LEN(uart_num)

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)

      Assign signals of a UART peripheral to GPIO pins.

      If the GPIO number configured for a UART signal matches one of the IOMUX signals for that GPIO, the signal will be connected directly via the IOMUX. Otherwise the GPIO and signal will be connected via the GPIO Matrix. For example, if on an ESP32 the call uart_set_pin(0, 1, 3, -1, -1) is performed, as GPIO1 is UART0's default TX pin and GPIO3 is UART0's default RX pin, both will be connected to respectively U0TXD and U0RXD through the IOMUX, totally bypassing the GPIO matrix. The check is performed on a per-pin basis. Thus, it is possible to have RX pin binded to a GPIO through the GPIO matrix, whereas TX is binded to its GPIO through the IOMUX.

      Internal signal can be output to multiple GPIO pads. Only one GPIO pad can connect with input signal.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • tx_io_num -- UART TX pin GPIO number.

    • rx_io_num -- UART RX pin GPIO number.

    • rts_io_num -- UART RTS pin GPIO number.

    • cts_io_num -- UART CTS pin GPIO number.

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_set_rts(uart_port_t uart_num, int level)

      Manually set the UART RTS pin level.

      UART must be configured with hardware flow control disabled.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • level -- 1: RTS output low (active); 0: RTS output high (block)

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_set_dtr(uart_port_t uart_num, int level)

      Manually set the UART DTR pin level.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • level -- 1: DTR output low; 0: DTR output high

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_set_tx_idle_num(uart_port_t uart_num, uint16_t idle_num)

      Set UART idle interval after tx FIFO is empty.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • idle_num -- idle interval after tx FIFO is empty(unit: the time it takes to send one bit under current baudrate)

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config)

      Set UART configuration parameters.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • uart_config -- UART parameter settings

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf)

      Configure UART interrupts.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • intr_conf -- UART interrupt settings

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait)

      Wait until UART TX FIFO is empty.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • ticks_to_wait -- Timeout, count in RTOS ticks

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • ESP_ERR_TIMEOUT Timeout

    • int uart_tx_chars(uart_port_t uart_num, const char *buffer, uint32_t len)

      Send data to the UART port from a given buffer and length.

      This function will not wait for enough space in TX FIFO. It will just fill the available TX FIFO and return when the FIFO is full.

      This function should only be used when UART TX buffer is not enabled.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • buffer -- data buffer address

    • len -- data length to send

    • Returns
    • (-1) Parameter error

    • OTHERS (>=0) The number of bytes pushed to the TX FIFO

    • int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)

      Send data to the UART port from a given buffer and length,.

      If the UART driver's parameter 'tx_buffer_size' is set to zero: This function will not return until all the data have been sent out, or at least pushed into TX FIFO.

      Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer, UART ISR will then move data from the ring buffer to TX FIFO gradually.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • src -- data buffer address

    • size -- data length to send

    • Returns
    • (-1) Parameter error

    • OTHERS (>=0) The number of bytes pushed to the TX FIFO

    • int uart_write_bytes_with_break(uart_port_t uart_num, const void *src, size_t size, int brk_len)

      Send data to the UART port from a given buffer and length,.

      If the UART driver's parameter 'tx_buffer_size' is set to zero: This function will not return until all the data and the break signal have been sent out. After all data is sent out, send a break signal.

      Otherwise, if the 'tx_buffer_size' > 0, this function will return after copying all the data to tx ring buffer, UART ISR will then move data from the ring buffer to TX FIFO gradually. After all data sent out, send a break signal.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • src -- data buffer address

    • size -- data length to send

    • brk_len -- break signal duration(unit: the time it takes to send one bit at current baudrate)

    • Returns
    • (-1) Parameter error

    • OTHERS (>=0) The number of bytes pushed to the TX FIFO

    • int uart_read_bytes(uart_port_t uart_num, void *buf, uint32_t length, TickType_t ticks_to_wait)

      UART read bytes from UART buffer.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • buf -- pointer to the buffer.

    • length -- data length

    • ticks_to_wait -- sTimeout, count in RTOS ticks

    • Returns
    • (-1) Error

    • OTHERS (>=0) The number of bytes read from UART buffer

    • esp_err_t uart_flush(uart_port_t uart_num)

      Alias of uart_flush_input. UART ring buffer flush. This will discard all data in the UART RX buffer.

      Instead of waiting the data sent out, this function will clear UART rx buffer. In order to send all the data in tx FIFO, we can use uart_wait_tx_done function.

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_flush_input(uart_port_t uart_num)

      Clear input buffer, discard all the data is in the ring-buffer.

      In order to send all the data in tx FIFO, we can use uart_wait_tx_done function.

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_get_buffered_data_len(uart_port_t uart_num, size_t *size)

      UART get RX ring buffer cached data length.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • size -- Pointer of size_t to accept cached data length

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_get_tx_buffer_free_size(uart_port_t uart_num, size_t *size)

      UART get TX ring buffer free space size.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • size -- Pointer of size_t to accept the free space size

    • Returns
    • ESP_OK Success

    • ESP_ERR_INVALID_ARG Parameter error

    • esp_err_t uart_disable_pattern_det_intr(uart_port_t uart_num)

      UART disable pattern detect function. Designed for applications like 'AT commands'. When the hardware detects a series of one same character, the interrupt will be triggered.

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • esp_err_t uart_enable_pattern_det_baud_intr(uart_port_t uart_num, char pattern_chr, uint8_t chr_num, int chr_tout, int post_idle, int pre_idle)

      UART enable pattern detect function. Designed for applications like 'AT commands'. When the hardware detect a series of one same character, the interrupt will be triggered.

      Parameters
    • uart_num -- UART port number.

    • pattern_chr -- character of the pattern.

    • chr_num -- number of the character, 8bit value.

    • chr_tout -- timeout of the interval between each pattern characters, 16bit value, unit is the baud-rate cycle you configured. When the duration is more than this value, it will not take this data as at_cmd char.

    • post_idle -- idle time after the last pattern character, 16bit value, unit is the baud-rate cycle you configured. When the duration is less than this value, it will not take the previous data as the last at_cmd char

    • pre_idle -- idle time before the first pattern character, 16bit value, unit is the baud-rate cycle you configured. When the duration is less than this value, it will not take this data as the first at_cmd char.

    • Returns
    • ESP_OK Success

    • ESP_FAIL Parameter error

    • int uart_pattern_pop_pos(uart_port_t uart_num)

      Return the nearest detected pattern position in buffer. The positions of the detected pattern are saved in a queue, this function will dequeue the first pattern position and move the pointer to next pattern position.

      The following APIs will modify the pattern position info: uart_flush_input, uart_read_bytes, uart_driver_delete, uart_pop_pattern_pos It is the application's responsibility to ensure atomic access to the pattern queue and the rx data buffer when using pattern detect feature.

      If the RX buffer is full and flow control is not enabled, the detected pattern may not be found in the rx buffer due to overflow.

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • (-1) No pattern found for current index or parameter error

    • others the pattern position in rx buffer.

    • int uart_pattern_get_pos(uart_port_t uart_num)

      Return the nearest detected pattern position in buffer. The positions of the detected pattern are saved in a queue, This function do nothing to the queue.

      The following APIs will modify the pattern position info: uart_flush_input, uart_read_bytes, uart_driver_delete, uart_pop_pattern_pos It is the application's responsibility to ensure atomic access to the pattern queue and the rx data buffer when using pattern detect feature.

      If the RX buffer is full and flow control is not enabled, the detected pattern may not be found in the rx buffer due to overflow.

      Parameters

      uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

      Returns
    • (-1) No pattern found for current index or parameter error

    • others the pattern position in rx buffer.

    • esp_err_t uart_pattern_queue_reset(uart_port_t uart_num, int queue_length)

      Allocate a new memory with the given length to save record the detected pattern position in rx buffer.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1).

    • queue_length -- Max queue length for the detected pattern. If the queue length is not large enough, some pattern positions might be lost. Set this value to the maximum number of patterns that could be saved in data buffer at the same time.

    • Returns
    • ESP_ERR_NO_MEM No enough memory

    • ESP_ERR_INVALID_STATE Driver not installed

    • ESP_FAIL Parameter error

    • ESP_OK Success

    • esp_err_t uart_set_mode(uart_port_t uart_num, uart_mode_t mode)

      UART set communication mode.

      This function must be executed after uart_driver_install(), when the driver object is initialized.

      Parameters
    • uart_num -- Uart number to configure, the max port number is (UART_NUM_MAX -1).

    • mode -- UART UART mode to set

    • Returns
    • ESP_OK Success

    • ESP_ERR_INVALID_ARG Parameter error

    • esp_err_t uart_set_rx_full_threshold(uart_port_t uart_num, int threshold)

      Set uart threshold value for RX fifo full.

      If application is using higher baudrate and it is observed that bytes in hardware RX fifo are overwritten then this threshold can be reduced

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1)

    • threshold -- Threshold value above which RX fifo full interrupt is generated

    • Returns
    • ESP_OK Success

    • ESP_ERR_INVALID_ARG Parameter error

    • ESP_ERR_INVALID_STATE Driver is not installed

    • esp_err_t uart_set_tx_empty_threshold(uart_port_t uart_num, int threshold)

      Set uart threshold values for TX fifo empty.

      Parameters
    • uart_num -- UART port number, the max port number is (UART_NUM_MAX -1)

    • threshold -- Threshold value below which TX fifo empty interrupt is generated

    • Returns
    • ESP_OK Success

    • ESP_ERR_INVALID_ARG Parameter error

    • ESP_ERR_INVALID_STATE Driver is not installed

    • esp_err_t uart_set_rx_timeout(uart_port_t uart_num, const uint8_t tout_thresh)

      UART set threshold timeout for TOUT feature.

      Parameters
    • uart_num -- Uart number to configure, the max port number is (UART_NUM_MAX -1).

    • tout_thresh -- This parameter defines timeout threshold in uart symbol periods. The maximum value of threshold is 126. tout_thresh = 1, defines TOUT interrupt timeout equal to transmission time of one symbol (~11 bit) on current baudrate. If the time is expired the UART_RXFIFO_TOUT_INT interrupt is triggered. If tout_thresh == 0, the TOUT feature is disabled.

    • Returns
    • ESP_OK Success

    • ESP_ERR_INVALID_ARG Parameter error

    • ESP_ERR_INVALID_STATE Driver is not installed

    • esp_err_t uart_get_collision_flag(uart_port_t uart_num, bool *collision_flag)

      Returns collision detection flag for RS485 mode Function returns the collision detection flag into variable pointed by collision_flag. *collision_flag = true, if collision detected else it is equal to false. This function should be executed when actual transmission is completed (after uart_write_bytes()).

      Parameters
    • uart_num -- Uart number to configure the max port number is (UART_NUM_MAX -1).

    • collision_flag -- Pointer to variable of type bool to return collision flag.

    • Returns
    • ESP_OK Success

    • ESP_ERR_INVALID_ARG Parameter error