ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart_component_esp_idf.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include <driver/uart.h>
7#include "uart_component.h"
8#ifdef USE_UART_WAKE_LOOP_ON_RX
9#include <driver/uart_select.h>
10#endif
11
12namespace esphome::uart {
13
19class IDFUARTComponent final : public UARTComponent, public Component {
20 public:
21 // User provided, not "= default": `new(p) IDFUARTComponent()` would zero-fill .bss that is already zero.
23 void setup() override;
24 void dump_config() override;
25 float get_setup_priority() const override { return setup_priority::BUS; }
26
27 void set_rx_full_threshold(size_t rx_full_threshold) override;
28 void set_rx_timeout(size_t rx_timeout) override;
29
30 void write_array(const uint8_t *data, size_t len) override;
31
32 bool peek_byte(uint8_t *data) override;
33 bool read_array(uint8_t *data, size_t len) override;
34
35 size_t available() override;
36 size_t available_for_write() override;
37 UARTFlushResult flush() override;
38
39 void set_flush_timeout(uint32_t flush_timeout_ms) override { this->flush_timeout_ms_ = flush_timeout_ms; }
40
43 void set_tx_buffer_size(size_t tx_buffer_size) { this->tx_buffer_size_ = tx_buffer_size; }
44
45 void set_clock_source(uart_sclk_t clock_source) { this->clock_source_ = static_cast<uint8_t>(clock_source); }
46
47 uint8_t get_hw_serial_number() { return this->uart_num_; }
48
50 void flush_input() {
51 this->has_peek_ = false;
52 uart_flush_input(this->uart_num_);
53 }
54
67 void load_settings(bool dump_config) override;
68 using UARTComponent::load_settings; // also bring in the no-arg overload for convenience
69
87 esp_err_t apply_settings_live();
88
89 void on_shutdown() override;
90
91 protected:
92 void check_logger_conflict() override;
93#ifdef USE_GPIO_HOLD
94 void set_pins_hold_(bool hold);
95#endif
97 // Re-applies what uart_param_config() resets: inversion, RX threshold/timeout, mode.
98 esp_err_t apply_line_settings_();
99 uart_port_t uart_num_{UART_NUM_MAX};
100 uart_config_t get_config_();
101
102 struct Framing {
104 uint8_t data_bits;
105 uint8_t stop_bits;
107 size_t rx_full_threshold; // sized for the baud rate, so rolled back with it
108 };
110 return {this->baud_rate_, this->data_bits_, this->stop_bits_, this->parity_, this->rx_full_threshold_};
111 }
112 void set_framing_(const Framing &framing);
113 // Last framing the driver accepted; baud_rate 0 means none yet.
115
116 bool has_peek_{false};
117 uint8_t peek_byte_{0};
118 uint8_t clock_source_{UART_SCLK_DEFAULT};
121
122#ifdef USE_UART_WAKE_LOOP_ON_RX
123 // ISR callback for UART RX data notification — wakes the main loop directly.
124 static void uart_rx_isr_callback(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken);
125#endif // USE_UART_WAKE_LOOP_ON_RX
126};
127
128} // namespace esphome::uart
129#endif // USE_ESP32
ESP-IDF UART driver wrapper.
void set_flush_timeout(uint32_t flush_timeout_ms) override
void set_rx_timeout(size_t rx_timeout) override
void set_framing_(const Framing &framing)
void set_tx_buffer_size(size_t tx_buffer_size)
TX ring buffer size for the driver; 0 leaves TX unbuffered so write_array() blocks until the FIFO has...
void flush_input()
Discard everything received so far: the peek cache and the driver's RX buffer.
bool peek_byte(uint8_t *data) override
uint8_t clock_source_
uart_sclk_t stored in a byte; the IDF values are all small.
void load_settings()
Load the UART settings.
void write_array(const uint8_t *data, size_t len) override
void set_clock_source(uart_sclk_t clock_source)
void set_rx_full_threshold(size_t rx_full_threshold) override
bool read_array(uint8_t *data, size_t len) override
uint32_t flush_timeout_ms_
0 means wait indefinitely (portMAX_DELAY).
static void uart_rx_isr_callback(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken)
esp_err_t apply_settings_live()
Apply the current framing (baud rate, parity, data/stop bits) to the installed driver in place,...
float get_setup_priority() const override
void load_settings()
Load the UART settings.
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:39
UARTFlushResult
Result of a flush() call.
const void size_t len
Definition hal.h:68
static void uint32_t