Chapter 2. API Reference
const uart_port_t uart_num = UART_NUM_1;
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));
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.
Table 5: Functions for Configuring specific parameters individually
Parameter to Configure Function
Baud rate uart_set_baudrate()
Number of transmitted bits uart_set_word_length() selected out of uart_word_length_t
Parity control uart_set_parity() selected out of uart_parity_t
Number of stop bits uart_set_stop_bits() selected out of uart_stop_bits_t
Hardware flow control mode uart_set_hw_flow_ctrl() selected out of
uart_hw_flowcontrol_t
Communication mode uart_set_mode() selected out of uart_mode_t
Each of the above functions has a _get_ counterpart to check the currently set value. For example, to check the
current baud rate value, call uart_get_baudrate().
Setting 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 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_1, 4, 5, 18, 19));
Driver Installation 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 will allocate 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_1, 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.
Espressif Systems 459
Submit Document Feedback
Release v4.4