1. Basic Concepts of UART
UART (Universal Asynchronous Receiver/Transmitter) is a universal asynchronous transceiver with the following characteristics:
- Asynchronous Communication: No clock line is required, only TX (transmit) and RX (receive) data lines are needed.
- Full-Duplex Transmission: Supports simultaneous data transmission and reception.
- Flexible Configuration: Baud rate, data bits, parity bits, and stop bits can be set.
Typical Application Scenarios
- Serial debugging output (replacing printf)
- Communication with GPS, Bluetooth, and WiFi modules
- Interaction with PC host or other microcontrollers
2. ESP32 UART Hardware Features
The ESP32 integrates three UART controllers:
| UART Number | Default Function | Configurability |
|---|---|---|
| UART0 | Firmware download and debugging output | Pin can be remapped |
| UART1 | Typically used for peripheral communication | Pin can be remapped |
| UART2 | General communication | Pin can be remapped |
Key Features:
- Supports baud rate range: 110bps – 5Mbps
- Configurable data bits (5-8 bits), stop bits (1/1.5/2 bits)
- Supports parity checking (even/odd/no parity)
- Supports hardware flow control (RTS/CTS)
- Supports interrupt and DMA transfer modes
3. ESP-IDF UART Programming Interface
1. Configuration Structure
typedef struct {
int baud_rate; // Baud rate
uart_word_length_t data_bits; // Data bits
uart_parity_t parity; // Parity bits
uart_stop_bits_t stop_bits; // Stop bits
uart_hw_flowcontrol_t flow_ctrl; // Hardware flow control
uint8_t rx_flow_ctrl_thresh; // Flow control threshold
} uart_config_t;
2. Common API Functions
| Function | Description |
|---|---|
<span>uart_driver_install()</span> |
Install UART driver |
<span>uart_param_config()</span> |
Configure UART parameters |
<span>uart_set_pin()</span> |
Set TX/RX pins |
<span>uart_write_bytes()</span> |
Send data |
<span>uart_read_bytes()</span> |
Receive data |
<span>uart_flush()</span> |
Clear receive buffer |
4. UART Communication Examples
1. Basic Sending Example
#include "driver/uart.h"
#include "esp_log.h"
#define UART_PORT_NUM UART_NUM_1
#define UART_TX_PIN GPIO_NUM_17
#define UART_RX_PIN GPIO_NUM_16
#define BUF_SIZE 1024
void app_main(void) {
// UART parameter configuration
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_DISABLE
};
// Install and configure UART
uart_param_config(UART_PORT_NUM, &uart_config);
uart_set_pin(UART_PORT_NUM, UART_TX_PIN, UART_RX_PIN,
UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(UART_PORT_NUM, BUF_SIZE, 0, 0, NULL, 0);
// Send data
const char *test_str = "Hello UART\n";
while (1) {
uart_write_bytes(UART_PORT_NUM, test_str, strlen(test_str));
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
2. Data Reception Handling
uint8_t data[BUF_SIZE];
int len = uart_read_bytes(UART_PORT_NUM, data, BUF_SIZE, pdMS_TO_TICKS(1000));
if (len > 0) {
data[len] = '\0'; // Add string terminator
ESP_LOGI("UART", "Recv: %s", (char*)data);
}
3. Serial Echo Implementation
uint8_t data[BUF_SIZE];
while (1) {
int len = uart_read_bytes(UART_PORT_NUM, data, BUF_SIZE, pdMS_TO_TICKS(100));
if (len > 0) {
uart_write_bytes(UART_PORT_NUM, (const char*)data, len); // Echo data
}
}
5. Advanced Application Techniques
-
Automatic Baud Rate Detection:
- Automatically calculate baud rate by measuring the width of the start bit
- Applicable in situations where the target device’s baud rate is uncertain
DMA Transfer:
- Set rx_buffer and tx_buffer sizes when using
<span>uart_driver_install()</span> - Suitable for high-speed data transmission, reducing CPU load
Hardware Flow Control:
- Configure RTS/CTS pins to implement hardware flow control
- Prevents data loss, suitable for high-speed communication
Interrupt Handling:
- Register interrupt handler functions to handle specific events
- Such as frame errors, parity errors, etc.
6. Common Troubleshooting
-
No Data Transmission:
- Check if TX/RX pin connections are correct
- Ensure baud rate settings are consistent on both ends
- Verify that hardware connections are normal
Data Corruption:
- Check baud rate, data bits, stop bits, and parity settings
- Ensure good ground connection
Data Loss:
- Increase the size of the receive buffer
- Lower the baud rate or enable hardware flow control
- Check if the program processes received data in a timely manner