EspUart is the ESP32-C6 implementation of the BaseUart interface,
providing comprehensive UART (Universal Asynchronous Receiver-Transmitter) functionality
specifically optimized for ESP32-C6 microcontrollers running ESP-IDF v5.5+.
It offers both basic and advanced UART features with hardware-specific optimizations.
Features
ESP32-C6 UART Controller - Full support for ESP32-C6 UART capabilities
Multiple Ports - Support for multiple UART ports
Hardware Flow Control - RTS/CTS hardware flow control
DMA Integration - High-performance DMA transfers
Interrupt Support - Configurable interrupt handling
// Enable hardware flow controlhf_uart_err_terr=uart.SetFlowControl(HF_UART_FLOW_CONTROL_HARDWARE);if(err!=HF_UART_ERR_OK){printf("Failed to set flow control: %d\n",err);return;}// Check available bytes before readinghf_size_tavailable;err=uart.GetBytesAvailable(&available);if(err==HF_UART_ERR_OK){printf("Bytes available: %zu\n",available);if(available>0){charbuffer[256];err=uart.ReadString(buffer,sizeof(buffer),0);// Non-blockingif(err==HF_UART_ERR_OK){printf("Received: %s\n",buffer);}}}
// Interrupt callback functionvoiduart_interrupt_callback(hf_uart_event_tevent,void*user_data){switch(event){caseHF_UART_EVENT_RX_DATA:printf("UART RX data available\n");break;caseHF_UART_EVENT_TX_DONE:printf("UART TX completed\n");break;caseHF_UART_EVENT_ERROR:printf("UART error occurred\n");break;default:break;}}// Set interrupt callbackhf_uart_err_terr=uart.SetInterruptCallback(uart_interrupt_callback,nullptr);if(err!=HF_UART_ERR_OK){printf("Failed to set interrupt callback: %d\n",err);return;}// Enable interrupt-based communication// The callback will be called when data is available or transmission is complete
Multiple UART Ports
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Create multiple UART instancesEspUartuart1(HF_UART_PORT_1,GPIO_NUM_1,GPIO_NUM_3,115200);EspUartuart2(HF_UART_PORT_2,GPIO_NUM_17,GPIO_NUM_16,9600);// Initialize bothif(!uart1.Initialize()||!uart2.Initialize()){printf("Failed to initialize UART ports\n");return;}// Use different baud ratesuart1.SetBaudRate(115200);uart2.SetBaudRate(9600);// Send data on both portsuart1.WriteString("Port 1 message\n");uart2.WriteString("Port 2 message\n");
ESP32-C6 Specific Features
Multiple UART Ports
Support for multiple UART ports with independent configuration.
Hardware Flow Control
RTS/CTS hardware flow control for reliable data transmission.
DMA Integration
High-performance DMA transfers for large data blocks.
Interrupt Support
Configurable interrupt handling for efficient data processing.
Error Handling
The EspUart class provides comprehensive error handling with specific error codes:
HF_UART_ERR_OK - Operation successful
HF_UART_ERR_INVALID_ARG - Invalid parameter
HF_UART_ERR_NOT_INITIALIZED - UART not initialized
HF_UART_ERR_TIMEOUT - Operation timeout
HF_UART_ERR_BUFFER_FULL - Buffer full
HF_UART_ERR_BUFFER_EMPTY - Buffer empty
HF_UART_ERR_PARITY_ERROR - Parity error
HF_UART_ERR_FRAMING_ERROR - Framing error
Performance Considerations
Baud Rate: Choose appropriate baud rate for your application
Buffer Size: Use appropriate buffer sizes for your data
Flow Control: Enable flow control for reliable transmission
Interrupts: Use interrupts for efficient data handling