ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart_component.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <cstdint>
5#include <cstring>
8#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
11#ifdef USE_UART_DEBUGGER
13#endif
14
15namespace esphome::uart {
16
25
26// A runtime setting like CRTSCTS and IXON/IXOFF in termios. A backend without the lines
27// keeps the value and ignores it, as a Linux driver does.
33
34#ifdef USE_UART_DEBUGGER
40#endif
41
42const LogString *parity_to_str(UARTParityOptions parity);
43
51
53 public:
54 static constexpr size_t RX_FULL_THRESHOLD_UNSET = 0;
55
56 // Writes an array of bytes to the UART bus.
57 // @param data A vector of bytes to be written.
58 void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
59
60 // Writes a single byte to the UART bus.
61 // @param data The byte to be written.
62 void write_byte(uint8_t data) { this->write_array(&data, 1); };
63
64 // Writes a null-terminated string to the UART bus.
65 // @param str Pointer to the null-terminated string.
66 void write_str(const char *str) {
67 const auto *data = reinterpret_cast<const uint8_t *>(str);
68 this->write_array(data, strlen(str));
69 };
70
71 // Pure virtual method to write an array of bytes to the UART bus.
72 // @param data Pointer to the array of bytes.
73 // @param len Length of the array.
74 virtual void write_array(const uint8_t *data, size_t len) = 0;
75
76 // Writes bytes that may live in PROGMEM; ESP8266 cannot read flash bytewise, so it copies chunks.
77#ifdef USE_ESP8266
78 void write_array_progmem(const uint8_t *data, size_t len);
79#else
80 void write_array_progmem(const uint8_t *data, size_t len) { this->write_array(data, len); }
81#endif
82
83 // Reads a single byte from the UART bus.
84 // @param data Pointer to the byte where the read data will be stored.
85 // @return True if a byte was successfully read, false otherwise.
86 bool read_byte(uint8_t *data) { return this->read_array(data, 1); };
87
88 // Pure virtual method to peek the next byte in the UART buffer without removing it.
89 // @param data Pointer to the byte where the peeked data will be stored.
90 // @return True if a byte is available to peek, false otherwise.
91 virtual bool peek_byte(uint8_t *data) = 0;
92
93 // Pure virtual method to read an array of bytes from the UART bus.
94 // @param data Pointer to the array where the read data will be stored.
95 // @param len Number of bytes to read.
96 // @return True if the specified number of bytes were successfully read, false otherwise.
97 virtual bool read_array(uint8_t *data, size_t len) = 0;
98
99 // Pure virtual method to return the number of bytes available for reading.
100 // @return Number of available bytes.
101 virtual size_t available() = 0;
102
103 // Returns how many bytes write_array() accepts right now without blocking.
104 // Platforms that cannot tell return SIZE_MAX: write_array() takes everything and may block.
105 virtual size_t available_for_write() { return SIZE_MAX; }
106
107 // Bytes a write can take now. Where available_for_write() cannot tell (SIZE_MAX), paces to the line time since
108 // last_write_ms, a loop start time: at most one loop interval and 4 s, at least one byte.
109 size_t paced_write_room(uint32_t last_write_ms);
110
111 // Pure virtual method to block until all bytes have been written to the UART bus.
112 // @return UARTFlushResult indicating whether the flush was confirmed, timed out, failed, or assumed successful.
113 virtual UARTFlushResult flush() = 0;
114
115 // Returns true if the underlying transport is connected and operational.
116 // Hardware UARTs always return true. USB-backed UARTs override to reflect actual connection state.
117 virtual bool is_connected() { return true; }
118
119 // Sets the maximum time to wait for TX to drain during flush().
120 // Only meaningful on ESP32 (IDF). Other platforms ignore this value.
121 // @param flush_timeout_ms Timeout in milliseconds; 0 means wait indefinitely.
122 virtual void set_flush_timeout(uint32_t flush_timeout_ms) {}
123
124 // Sets the TX (transmit) pin for the UART bus.
125 // @param tx_pin Pointer to the internal GPIO pin used for transmission.
126 void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; }
127
128 // Sets the RX (receive) pin for the UART bus.
129 // @param rx_pin Pointer to the internal GPIO pin used for reception.
130 void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; }
131
132 // Sets the flow control pin for the UART bus.
133 // @param flow_control_pin Pointer to the internal GPIO pin used for flow control.
134 void set_flow_control_pin(InternalGPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
135
136 // Sets the size of the RX buffer.
137 // @param rx_buffer_size Size of the RX buffer in bytes.
138 void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; }
139
140 // Gets the size of the RX buffer.
141 // @return Size of the RX buffer in bytes.
142 size_t get_rx_buffer_size() { return this->rx_buffer_size_; }
143
144 // Sets the RX FIFO full interrupt threshold.
145 // @param rx_full_threshold RX full interrupt threshold in bytes.
146 virtual void set_rx_full_threshold(size_t rx_full_threshold) {}
147
148 // Sets the RX FIFO full interrupt threshold.
149 // @param time RX full interrupt threshold in ms.
150 void set_rx_full_threshold_ms(uint8_t time);
151
152 // Gets the RX FIFO full interrupt threshold.
153 // @return RX full interrupt threshold in bytes.
154 size_t get_rx_full_threshold() { return this->rx_full_threshold_; }
155
156 // Sets the RX timeout interrupt threshold.
157 // @param rx_timeout RX timeout interrupt threshold (unit: time of sending one byte).
158 virtual void set_rx_timeout(size_t rx_timeout) {}
159
160 // Gets the RX timeout interrupt threshold.
161 // @return RX timeout interrupt threshold (unit: time of sending one byte).
162 size_t get_rx_timeout() { return this->rx_timeout_; }
163
164 // Sets the number of stop bits used in UART communication.
165 // @param stop_bits Number of stop bits.
166 void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; }
167
168 // Gets the number of stop bits used in UART communication.
169 // @return Number of stop bits.
170 uint8_t get_stop_bits() const { return this->stop_bits_; }
171
172 // Set the number of data bits used in UART communication.
173 // @param data_bits Number of data bits.
174 void set_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; }
175
176 // Get the number of data bits used in UART communication.
177 // @return Number of data bits.
178 uint8_t get_data_bits() const { return this->data_bits_; }
179
180 // Set the parity used in UART communication.
181 // @param parity Parity option.
182 void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
183
184 // Get the parity used in UART communication.
185 // @return Parity option.
186 UARTParityOptions get_parity() const { return this->parity_; }
187
188 // Set the baud rate for UART communication.
189 // @param baud_rate Baud rate in bits per second.
190 void set_baud_rate(uint32_t baud_rate) { baud_rate_ = baud_rate; }
191
192 // Get the baud rate for UART communication.
193 // @return Baud rate in bits per second.
195
196 // Set the line handshaking (RTS/CTS or XON/XOFF) used in UART communication. Unrelated to
197 // flow_control_pin, the RS485 driver enable; backends without handshaking ignore it.
198 // @param flow_control Flow control option.
199 void set_flow_control(UARTFlowControl flow_control) { this->flow_control_ = flow_control; }
200
201 // Get the flow control used in UART communication.
202 // @return Flow control option.
204
205#if defined(USE_ESP8266) || defined(USE_ESP32)
217 virtual void load_settings(bool dump_config) = 0;
218
229 void load_settings() { this->load_settings(true); }
230#endif // USE_ESP8266 || USE_ESP32
231
232#ifdef USE_UART_DEBUGGER
233 template<typename F> void add_debug_callback(F &&callback) { this->debug_callback_.add(std::forward<F>(callback)); }
234#endif
235
236 protected:
237 virtual void check_logger_conflict() = 0;
238 bool check_read_timeout_(size_t len = 1);
239
244 // ESP32 (both Arduino and ESP-IDF) always sets this at codegen time via set_rx_full_threshold().
245 // Other platforms (USB UART, Arduino, etc.) leave it unset.
247 size_t rx_timeout_{0};
249 uint8_t stop_bits_{0};
250 uint8_t data_bits_{0};
252 UARTParityOptions parity_{UART_CONFIG_PARITY_NONE}; // Must match the parity check in __init__.py
253#ifdef USE_UART_DEBUGGER
255#endif
256};
257
258} // namespace esphome::uart
UARTParityOptions get_parity() const
void write_byte(uint8_t data)
size_t paced_write_room(uint32_t last_write_ms)
void write_array_progmem(const uint8_t *data, size_t len)
bool check_read_timeout_(size_t len=1)
void write_array(const std::vector< uint8_t > &data)
UARTFlowControl get_flow_control() const
void set_baud_rate(uint32_t baud_rate)
void write_str(const char *str)
void set_rx_full_threshold_ms(uint8_t time)
virtual void set_rx_full_threshold(size_t rx_full_threshold)
virtual void load_settings(bool dump_config)=0
Load the UART settings.
bool read_byte(uint8_t *data)
virtual UARTFlushResult flush()=0
void set_tx_pin(InternalGPIOPin *tx_pin)
void set_parity(UARTParityOptions parity)
void set_flow_control_pin(InternalGPIOPin *flow_control_pin)
void load_settings()
Load the UART settings.
virtual void check_logger_conflict()=0
virtual void write_array(const uint8_t *data, size_t len)=0
void add_debug_callback(F &&callback)
virtual size_t available()=0
void set_rx_buffer_size(size_t rx_buffer_size)
void set_stop_bits(uint8_t stop_bits)
virtual size_t available_for_write()
virtual bool peek_byte(uint8_t *data)=0
virtual void set_flush_timeout(uint32_t flush_timeout_ms)
void set_rx_pin(InternalGPIOPin *rx_pin)
InternalGPIOPin * flow_control_pin_
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
virtual void set_rx_timeout(size_t rx_timeout)
virtual bool read_array(uint8_t *data, size_t len)=0
void set_data_bits(uint8_t data_bits)
void set_flow_control(UARTFlowControl flow_control)
static constexpr size_t RX_FULL_THRESHOLD_UNSET
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:43
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
@ UART_FLUSH_RESULT_FAILED
Confirmed: driver or hardware error.
@ UART_FLUSH_RESULT_TIMEOUT
Confirmed: timed out before TX completed.
const void size_t len
Definition hal.h:68
static void uint32_t