ESPHome 2026.5.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 <cstring>
7#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
10#ifdef USE_UART_DEBUGGER
12#endif
13
14namespace esphome::uart {
15
21
22#ifdef USE_UART_DEBUGGER
28#endif
29
30const LogString *parity_to_str(UARTParityOptions parity);
31
39
41 public:
42 static constexpr size_t RX_FULL_THRESHOLD_UNSET = 0;
43
44 // Writes an array of bytes to the UART bus.
45 // @param data A vector of bytes to be written.
46 void write_array(const std::vector<uint8_t> &data) { this->write_array(&data[0], data.size()); }
47
48 // Writes a single byte to the UART bus.
49 // @param data The byte to be written.
50 void write_byte(uint8_t data) { this->write_array(&data, 1); };
51
52 // Writes a null-terminated string to the UART bus.
53 // @param str Pointer to the null-terminated string.
54 void write_str(const char *str) {
55 const auto *data = reinterpret_cast<const uint8_t *>(str);
56 this->write_array(data, strlen(str));
57 };
58
59 // Pure virtual method to write an array of bytes to the UART bus.
60 // @param data Pointer to the array of bytes.
61 // @param len Length of the array.
62 virtual void write_array(const uint8_t *data, size_t len) = 0;
63
64 // Reads a single byte from the UART bus.
65 // @param data Pointer to the byte where the read data will be stored.
66 // @return True if a byte was successfully read, false otherwise.
67 bool read_byte(uint8_t *data) { return this->read_array(data, 1); };
68
69 // Pure virtual method to peek the next byte in the UART buffer without removing it.
70 // @param data Pointer to the byte where the peeked data will be stored.
71 // @return True if a byte is available to peek, false otherwise.
72 virtual bool peek_byte(uint8_t *data) = 0;
73
74 // Pure virtual method to read an array of bytes from the UART bus.
75 // @param data Pointer to the array where the read data will be stored.
76 // @param len Number of bytes to read.
77 // @return True if the specified number of bytes were successfully read, false otherwise.
78 virtual bool read_array(uint8_t *data, size_t len) = 0;
79
80 // Pure virtual method to return the number of bytes available for reading.
81 // @return Number of available bytes.
82 virtual size_t available() = 0;
83
84 // Pure virtual method to block until all bytes have been written to the UART bus.
85 // @return UARTFlushResult indicating whether the flush was confirmed, timed out, failed, or assumed successful.
86 virtual UARTFlushResult flush() = 0;
87
88 // Returns true if the underlying transport is connected and operational.
89 // Hardware UARTs always return true. USB-backed UARTs override to reflect actual connection state.
90 virtual bool is_connected() { return true; }
91
92 // Sets the maximum time to wait for TX to drain during flush().
93 // Only meaningful on ESP32 (IDF). Other platforms ignore this value.
94 // @param flush_timeout_ms Timeout in milliseconds; 0 means wait indefinitely.
95 virtual void set_flush_timeout(uint32_t flush_timeout_ms) {}
96
97 // Sets the TX (transmit) pin for the UART bus.
98 // @param tx_pin Pointer to the internal GPIO pin used for transmission.
99 void set_tx_pin(InternalGPIOPin *tx_pin) { this->tx_pin_ = tx_pin; }
100
101 // Sets the RX (receive) pin for the UART bus.
102 // @param rx_pin Pointer to the internal GPIO pin used for reception.
103 void set_rx_pin(InternalGPIOPin *rx_pin) { this->rx_pin_ = rx_pin; }
104
105 // Sets the flow control pin for the UART bus.
106 // @param flow_control_pin Pointer to the internal GPIO pin used for flow control.
107 void set_flow_control_pin(InternalGPIOPin *flow_control_pin) { this->flow_control_pin_ = flow_control_pin; }
108
109 // Sets the size of the RX buffer.
110 // @param rx_buffer_size Size of the RX buffer in bytes.
111 void set_rx_buffer_size(size_t rx_buffer_size) { this->rx_buffer_size_ = rx_buffer_size; }
112
113 // Gets the size of the RX buffer.
114 // @return Size of the RX buffer in bytes.
115 size_t get_rx_buffer_size() { return this->rx_buffer_size_; }
116
117 // Sets the RX FIFO full interrupt threshold.
118 // @param rx_full_threshold RX full interrupt threshold in bytes.
119 virtual void set_rx_full_threshold(size_t rx_full_threshold) {}
120
121 // Sets the RX FIFO full interrupt threshold.
122 // @param time RX full interrupt threshold in ms.
123 void set_rx_full_threshold_ms(uint8_t time);
124
125 // Gets the RX FIFO full interrupt threshold.
126 // @return RX full interrupt threshold in bytes.
127 size_t get_rx_full_threshold() { return this->rx_full_threshold_; }
128
129 // Sets the RX timeout interrupt threshold.
130 // @param rx_timeout RX timeout interrupt threshold (unit: time of sending one byte).
131 virtual void set_rx_timeout(size_t rx_timeout) {}
132
133 // Gets the RX timeout interrupt threshold.
134 // @return RX timeout interrupt threshold (unit: time of sending one byte).
135 size_t get_rx_timeout() { return this->rx_timeout_; }
136
137 // Sets the number of stop bits used in UART communication.
138 // @param stop_bits Number of stop bits.
139 void set_stop_bits(uint8_t stop_bits) { this->stop_bits_ = stop_bits; }
140
141 // Gets the number of stop bits used in UART communication.
142 // @return Number of stop bits.
143 uint8_t get_stop_bits() const { return this->stop_bits_; }
144
145 // Set the number of data bits used in UART communication.
146 // @param data_bits Number of data bits.
147 void set_data_bits(uint8_t data_bits) { this->data_bits_ = data_bits; }
148
149 // Get the number of data bits used in UART communication.
150 // @return Number of data bits.
151 uint8_t get_data_bits() const { return this->data_bits_; }
152
153 // Set the parity used in UART communication.
154 // @param parity Parity option.
155 void set_parity(UARTParityOptions parity) { this->parity_ = parity; }
156
157 // Get the parity used in UART communication.
158 // @return Parity option.
159 UARTParityOptions get_parity() const { return this->parity_; }
160
161 // Set the baud rate for UART communication.
162 // @param baud_rate Baud rate in bits per second.
163 void set_baud_rate(uint32_t baud_rate) { baud_rate_ = baud_rate; }
164
165 // Get the baud rate for UART communication.
166 // @return Baud rate in bits per second.
168
169#if defined(USE_ESP8266) || defined(USE_ESP32)
181 virtual void load_settings(bool dump_config){};
182
193 virtual void load_settings(){};
194#endif // USE_ESP8266 || USE_ESP32
195
196#ifdef USE_UART_DEBUGGER
197 template<typename F> void add_debug_callback(F &&callback) { this->debug_callback_.add(std::forward<F>(callback)); }
198#endif
199
200 protected:
201 virtual void check_logger_conflict() = 0;
202 bool check_read_timeout_(size_t len = 1);
203
208 // ESP32 (both Arduino and ESP-IDF) always sets this at codegen time via set_rx_full_threshold().
209 // Other platforms (USB UART, Arduino, etc.) leave it unset.
211 size_t rx_timeout_{0};
213 uint8_t stop_bits_{0};
214 uint8_t data_bits_{0};
216#ifdef USE_UART_DEBUGGER
218#endif
219};
220
221} // namespace esphome::uart
UARTParityOptions get_parity() const
void write_byte(uint8_t data)
virtual void load_settings(bool dump_config)
Load the UART settings.
bool check_read_timeout_(size_t len=1)
virtual void load_settings()
Load the UART settings.
void write_array(const std::vector< uint8_t > &data)
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)
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)
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 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)
static constexpr size_t RX_FULL_THRESHOLD_UNSET
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:36
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.
std::string size_t len
Definition helpers.h:1045
static void uint32_t