ESPHome 2025.10.3
Loading...
Searching...
No Matches
uart_component_esp_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2
4#include <cinttypes>
8#include "esphome/core/log.h"
9#include "esphome/core/gpio.h"
10#include "driver/gpio.h"
11#include "soc/gpio_num.h"
12
13#ifdef USE_LOGGER
15#endif
16
17namespace esphome {
18namespace uart {
19static const char *const TAG = "uart.idf";
20
22 uart_parity_t parity = UART_PARITY_DISABLE;
23 if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
24 parity = UART_PARITY_EVEN;
25 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
26 parity = UART_PARITY_ODD;
27 }
28
29 uart_word_length_t data_bits;
30 switch (this->data_bits_) {
31 case 5:
32 data_bits = UART_DATA_5_BITS;
33 break;
34 case 6:
35 data_bits = UART_DATA_6_BITS;
36 break;
37 case 7:
38 data_bits = UART_DATA_7_BITS;
39 break;
40 case 8:
41 data_bits = UART_DATA_8_BITS;
42 break;
43 default:
44 data_bits = UART_DATA_BITS_MAX;
45 break;
46 }
47
48 uart_config_t uart_config{};
49 uart_config.baud_rate = this->baud_rate_;
50 uart_config.data_bits = data_bits;
51 uart_config.parity = parity;
52 uart_config.stop_bits = this->stop_bits_ == 1 ? UART_STOP_BITS_1 : UART_STOP_BITS_2;
53 uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
54 uart_config.source_clk = UART_SCLK_DEFAULT;
55 uart_config.rx_flow_ctrl_thresh = 122;
56
57 return uart_config;
58}
59
61 static uint8_t next_uart_num = 0;
62
63#ifdef USE_LOGGER
64 bool logger_uses_hardware_uart = true;
65
66#ifdef USE_LOGGER_USB_CDC
68 // this is not a hardware UART, ignore it
69 logger_uses_hardware_uart = false;
70 }
71#endif // USE_LOGGER_USB_CDC
72
73#ifdef USE_LOGGER_USB_SERIAL_JTAG
75 // this is not a hardware UART, ignore it
76 logger_uses_hardware_uart = false;
77 }
78#endif // USE_LOGGER_USB_SERIAL_JTAG
79
80 if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 &&
81 logger::global_logger->get_uart_num() == next_uart_num) {
82 next_uart_num++;
83 }
84#endif // USE_LOGGER
85
86 if (next_uart_num >= SOC_UART_NUM) {
87 ESP_LOGW(TAG, "Maximum number of UART components created already");
88 this->mark_failed();
89 return;
90 }
91 this->uart_num_ = static_cast<uart_port_t>(next_uart_num++);
92 this->lock_ = xSemaphoreCreateMutex();
93
94 xSemaphoreTake(this->lock_, portMAX_DELAY);
95
96 this->load_settings(false);
97
98 xSemaphoreGive(this->lock_);
99}
100
101void IDFUARTComponent::load_settings(bool dump_config) {
102 uart_config_t uart_config = this->get_config_();
103 esp_err_t err = uart_param_config(this->uart_num_, &uart_config);
104 if (err != ESP_OK) {
105 ESP_LOGW(TAG, "uart_param_config failed: %s", esp_err_to_name(err));
106 this->mark_failed();
107 return;
108 }
109
110 if (this->rx_pin_) {
111 this->rx_pin_->setup();
112 }
113 if (this->tx_pin_ && this->rx_pin_ != this->tx_pin_) {
114 this->tx_pin_->setup();
115 }
116
117 int8_t tx = this->tx_pin_ != nullptr ? this->tx_pin_->get_pin() : -1;
118 int8_t rx = this->rx_pin_ != nullptr ? this->rx_pin_->get_pin() : -1;
119 int8_t flow_control = this->flow_control_pin_ != nullptr ? this->flow_control_pin_->get_pin() : -1;
120
121 uint32_t invert = 0;
122 if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted())
123 invert |= UART_SIGNAL_TXD_INV;
124 if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted())
125 invert |= UART_SIGNAL_RXD_INV;
126
127 err = uart_set_line_inverse(this->uart_num_, invert);
128 if (err != ESP_OK) {
129 ESP_LOGW(TAG, "uart_set_line_inverse failed: %s", esp_err_to_name(err));
130 this->mark_failed();
131 return;
132 }
133
134 err = uart_set_pin(this->uart_num_, tx, rx, flow_control, UART_PIN_NO_CHANGE);
135 if (err != ESP_OK) {
136 ESP_LOGW(TAG, "uart_set_pin failed: %s", esp_err_to_name(err));
137 this->mark_failed();
138 return;
139 }
140
141 if (uart_is_driver_installed(this->uart_num_)) {
142 uart_driver_delete(this->uart_num_);
143 if (err != ESP_OK) {
144 ESP_LOGW(TAG, "uart_driver_delete failed: %s", esp_err_to_name(err));
145 this->mark_failed();
146 return;
147 }
148 }
149 err = uart_driver_install(this->uart_num_, /* UART RX ring buffer size. */ this->rx_buffer_size_,
150 /* UART TX ring buffer size. If set to zero, driver will not use TX buffer, TX function will
151 block task until all data have been sent out.*/
152 0,
153 /* UART event queue size/depth. */ 20, &(this->uart_event_queue_),
154 /* Flags used to allocate the interrupt. */ 0);
155 if (err != ESP_OK) {
156 ESP_LOGW(TAG, "uart_driver_install failed: %s", esp_err_to_name(err));
157 this->mark_failed();
158 return;
159 }
160
161 err = uart_set_rx_full_threshold(this->uart_num_, this->rx_full_threshold_);
162 if (err != ESP_OK) {
163 ESP_LOGW(TAG, "uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
164 this->mark_failed();
165 return;
166 }
167
168 err = uart_set_rx_timeout(this->uart_num_, this->rx_timeout_);
169 if (err != ESP_OK) {
170 ESP_LOGW(TAG, "uart_set_rx_timeout failed: %s", esp_err_to_name(err));
171 this->mark_failed();
172 return;
173 }
174
175 auto mode = this->flow_control_pin_ != nullptr ? UART_MODE_RS485_HALF_DUPLEX : UART_MODE_UART;
176 err = uart_set_mode(this->uart_num_, mode);
177 if (err != ESP_OK) {
178 ESP_LOGW(TAG, "uart_set_mode failed: %s", esp_err_to_name(err));
179 this->mark_failed();
180 return;
181 }
182
183 if (dump_config) {
184 ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->uart_num_);
185 this->dump_config();
186 }
187}
188
190 ESP_LOGCONFIG(TAG, "UART Bus %u:", this->uart_num_);
191 LOG_PIN(" TX Pin: ", tx_pin_);
192 LOG_PIN(" RX Pin: ", rx_pin_);
193 LOG_PIN(" Flow Control Pin: ", flow_control_pin_);
194 if (this->rx_pin_ != nullptr) {
195 ESP_LOGCONFIG(TAG,
196 " RX Buffer Size: %u\n"
197 " RX Full Threshold: %u\n"
198 " RX Timeout: %u",
200 }
201 ESP_LOGCONFIG(TAG,
202 " Baud Rate: %" PRIu32 " baud\n"
203 " Data Bits: %u\n"
204 " Parity: %s\n"
205 " Stop bits: %u",
206 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
207 this->check_logger_conflict();
208}
209
210void IDFUARTComponent::set_rx_full_threshold(size_t rx_full_threshold) {
211 if (this->is_ready()) {
212 esp_err_t err = uart_set_rx_full_threshold(this->uart_num_, rx_full_threshold);
213 if (err != ESP_OK) {
214 ESP_LOGW(TAG, "uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
215 return;
216 }
217 }
218 this->rx_full_threshold_ = rx_full_threshold;
219}
220
221void IDFUARTComponent::set_rx_timeout(size_t rx_timeout) {
222 if (this->is_ready()) {
223 esp_err_t err = uart_set_rx_timeout(this->uart_num_, rx_timeout);
224 if (err != ESP_OK) {
225 ESP_LOGW(TAG, "uart_set_rx_timeout failed: %s", esp_err_to_name(err));
226 return;
227 }
228 }
229 this->rx_timeout_ = rx_timeout;
230}
231
232void IDFUARTComponent::write_array(const uint8_t *data, size_t len) {
233 xSemaphoreTake(this->lock_, portMAX_DELAY);
234 uart_write_bytes(this->uart_num_, data, len);
235 xSemaphoreGive(this->lock_);
236#ifdef USE_UART_DEBUGGER
237 for (size_t i = 0; i < len; i++) {
238 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
239 }
240#endif
241}
242
243bool IDFUARTComponent::peek_byte(uint8_t *data) {
244 if (!this->check_read_timeout_())
245 return false;
246 xSemaphoreTake(this->lock_, portMAX_DELAY);
247 if (this->has_peek_) {
248 *data = this->peek_byte_;
249 } else {
250 int len = uart_read_bytes(this->uart_num_, data, 1, 20 / portTICK_PERIOD_MS);
251 if (len == 0) {
252 *data = 0;
253 } else {
254 this->has_peek_ = true;
255 this->peek_byte_ = *data;
256 }
257 }
258 xSemaphoreGive(this->lock_);
259 return true;
260}
261
262bool IDFUARTComponent::read_array(uint8_t *data, size_t len) {
263 size_t length_to_read = len;
264 if (!this->check_read_timeout_(len))
265 return false;
266 xSemaphoreTake(this->lock_, portMAX_DELAY);
267 if (this->has_peek_) {
268 length_to_read--;
269 *data = this->peek_byte_;
270 data++;
271 this->has_peek_ = false;
272 }
273 if (length_to_read > 0)
274 uart_read_bytes(this->uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
275 xSemaphoreGive(this->lock_);
276#ifdef USE_UART_DEBUGGER
277 for (size_t i = 0; i < len; i++) {
278 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
279 }
280#endif
281 return true;
282}
283
285 size_t available;
286
287 xSemaphoreTake(this->lock_, portMAX_DELAY);
288 uart_get_buffered_data_len(this->uart_num_, &available);
289 if (this->has_peek_)
290 available++;
291 xSemaphoreGive(this->lock_);
292
293 return available;
294}
295
297 ESP_LOGVV(TAG, " Flushing");
298 xSemaphoreTake(this->lock_, portMAX_DELAY);
299 uart_wait_tx_done(this->uart_num_, portMAX_DELAY);
300 xSemaphoreGive(this->lock_);
301}
302
304
305} // namespace uart
306} // namespace esphome
307
308#endif // USE_ESP32
BedjetMode mode
BedJet operating mode.
virtual void mark_failed()
Mark this component as failed.
bool is_ready() const
virtual void setup()=0
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
void set_rx_timeout(size_t rx_timeout) override
bool peek_byte(uint8_t *data) override
void write_array(const uint8_t *data, size_t len) override
void set_rx_full_threshold(size_t rx_full_threshold) override
bool read_array(uint8_t *data, size_t len) override
bool check_read_timeout_(size_t len=1)
InternalGPIOPin * flow_control_pin_
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:91
@ UART_SELECTION_USB_CDC
Definition logger.h:88
Logger * global_logger
Definition logger.cpp:294
const char *const TAG
Definition spi.cpp:8
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:33
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:304