ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
uart_component_esp_idf.cpp
Go to the documentation of this file.
1#ifdef USE_ESP_IDF
2
4#include <cinttypes>
8#include "esphome/core/log.h"
9
10#ifdef USE_LOGGER
12#endif
13
14namespace esphome {
15namespace uart {
16static const char *const TAG = "uart.idf";
17
19 uart_parity_t parity = UART_PARITY_DISABLE;
20 if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
21 parity = UART_PARITY_EVEN;
22 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
23 parity = UART_PARITY_ODD;
24 }
25
26 uart_word_length_t data_bits;
27 switch (this->data_bits_) {
28 case 5:
29 data_bits = UART_DATA_5_BITS;
30 break;
31 case 6:
32 data_bits = UART_DATA_6_BITS;
33 break;
34 case 7:
35 data_bits = UART_DATA_7_BITS;
36 break;
37 case 8:
38 data_bits = UART_DATA_8_BITS;
39 break;
40 default:
41 data_bits = UART_DATA_BITS_MAX;
42 break;
43 }
44
45 uart_config_t uart_config{};
46 uart_config.baud_rate = this->baud_rate_;
47 uart_config.data_bits = data_bits;
48 uart_config.parity = parity;
49 uart_config.stop_bits = this->stop_bits_ == 1 ? UART_STOP_BITS_1 : UART_STOP_BITS_2;
50 uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
51 uart_config.source_clk = UART_SCLK_DEFAULT;
52 uart_config.rx_flow_ctrl_thresh = 122;
53
54 return uart_config;
55}
56
58 static uint8_t next_uart_num = 0;
59
60#ifdef USE_LOGGER
61 bool logger_uses_hardware_uart = true;
62
63#ifdef USE_LOGGER_USB_CDC
65 // this is not a hardware UART, ignore it
66 logger_uses_hardware_uart = false;
67 }
68#endif // USE_LOGGER_USB_CDC
69
70#ifdef USE_LOGGER_USB_SERIAL_JTAG
72 // this is not a hardware UART, ignore it
73 logger_uses_hardware_uart = false;
74 }
75#endif // USE_LOGGER_USB_SERIAL_JTAG
76
77 if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 &&
78 logger::global_logger->get_uart_num() == next_uart_num) {
79 next_uart_num++;
80 }
81#endif // USE_LOGGER
82
83 if (next_uart_num >= SOC_UART_NUM) {
84 ESP_LOGW(TAG, "Maximum number of UART components created already");
85 this->mark_failed();
86 return;
87 }
88 this->uart_num_ = static_cast<uart_port_t>(next_uart_num++);
89 this->lock_ = xSemaphoreCreateMutex();
90
91 xSemaphoreTake(this->lock_, portMAX_DELAY);
92
93 uart_config_t uart_config = this->get_config_();
94 esp_err_t err = uart_param_config(this->uart_num_, &uart_config);
95 if (err != ESP_OK) {
96 ESP_LOGW(TAG, "uart_param_config failed: %s", esp_err_to_name(err));
97 this->mark_failed();
98 return;
99 }
100
101 int8_t tx = this->tx_pin_ != nullptr ? this->tx_pin_->get_pin() : -1;
102 int8_t rx = this->rx_pin_ != nullptr ? this->rx_pin_->get_pin() : -1;
103
104 uint32_t invert = 0;
105 if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted())
106 invert |= UART_SIGNAL_TXD_INV;
107 if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted())
108 invert |= UART_SIGNAL_RXD_INV;
109
110 err = uart_set_line_inverse(this->uart_num_, invert);
111 if (err != ESP_OK) {
112 ESP_LOGW(TAG, "uart_set_line_inverse failed: %s", esp_err_to_name(err));
113 this->mark_failed();
114 return;
115 }
116
117 err = uart_set_pin(this->uart_num_, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
118 if (err != ESP_OK) {
119 ESP_LOGW(TAG, "uart_set_pin failed: %s", esp_err_to_name(err));
120 this->mark_failed();
121 return;
122 }
123
124 err = uart_driver_install(this->uart_num_, /* UART RX ring buffer size. */ this->rx_buffer_size_,
125 /* UART TX ring buffer size. If set to zero, driver will not use TX buffer, TX function will
126 block task until all data have been sent out.*/
127 0,
128 /* UART event queue size/depth. */ 20, &(this->uart_event_queue_),
129 /* Flags used to allocate the interrupt. */ 0);
130 if (err != ESP_OK) {
131 ESP_LOGW(TAG, "uart_driver_install failed: %s", esp_err_to_name(err));
132 this->mark_failed();
133 return;
134 }
135
136 xSemaphoreGive(this->lock_);
137}
138
139void IDFUARTComponent::load_settings(bool dump_config) {
140 uart_config_t uart_config = this->get_config_();
141 esp_err_t err = uart_param_config(this->uart_num_, &uart_config);
142 if (err != ESP_OK) {
143 ESP_LOGW(TAG, "uart_param_config failed: %s", esp_err_to_name(err));
144 this->mark_failed();
145 return;
146 } else if (dump_config) {
147 ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->uart_num_);
148 this->dump_config();
149 }
150}
151
153 ESP_LOGCONFIG(TAG, "UART Bus %u:", this->uart_num_);
154 LOG_PIN(" TX Pin: ", tx_pin_);
155 LOG_PIN(" RX Pin: ", rx_pin_);
156 if (this->rx_pin_ != nullptr) {
157 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
158 }
159 ESP_LOGCONFIG(TAG,
160 " Baud Rate: %" PRIu32 " baud\n"
161 " Data Bits: %u\n"
162 " Parity: %s\n"
163 " Stop bits: %u",
164 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
165 this->check_logger_conflict();
166}
167
168void IDFUARTComponent::write_array(const uint8_t *data, size_t len) {
169 xSemaphoreTake(this->lock_, portMAX_DELAY);
170 uart_write_bytes(this->uart_num_, data, len);
171 xSemaphoreGive(this->lock_);
172#ifdef USE_UART_DEBUGGER
173 for (size_t i = 0; i < len; i++) {
174 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
175 }
176#endif
177}
178
179bool IDFUARTComponent::peek_byte(uint8_t *data) {
180 if (!this->check_read_timeout_())
181 return false;
182 xSemaphoreTake(this->lock_, portMAX_DELAY);
183 if (this->has_peek_) {
184 *data = this->peek_byte_;
185 } else {
186 int len = uart_read_bytes(this->uart_num_, data, 1, 20 / portTICK_PERIOD_MS);
187 if (len == 0) {
188 *data = 0;
189 } else {
190 this->has_peek_ = true;
191 this->peek_byte_ = *data;
192 }
193 }
194 xSemaphoreGive(this->lock_);
195 return true;
196}
197
198bool IDFUARTComponent::read_array(uint8_t *data, size_t len) {
199 size_t length_to_read = len;
200 if (!this->check_read_timeout_(len))
201 return false;
202 xSemaphoreTake(this->lock_, portMAX_DELAY);
203 if (this->has_peek_) {
204 length_to_read--;
205 *data = this->peek_byte_;
206 data++;
207 this->has_peek_ = false;
208 }
209 if (length_to_read > 0)
210 uart_read_bytes(this->uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
211 xSemaphoreGive(this->lock_);
212#ifdef USE_UART_DEBUGGER
213 for (size_t i = 0; i < len; i++) {
214 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
215 }
216#endif
217 return true;
218}
219
221 size_t available;
222
223 xSemaphoreTake(this->lock_, portMAX_DELAY);
224 uart_get_buffered_data_len(this->uart_num_, &available);
225 if (this->has_peek_)
226 available++;
227 xSemaphoreGive(this->lock_);
228
229 return available;
230}
231
233 ESP_LOGVV(TAG, " Flushing");
234 xSemaphoreTake(this->lock_, portMAX_DELAY);
235 uart_wait_tx_done(this->uart_num_, portMAX_DELAY);
236 xSemaphoreGive(this->lock_);
237}
238
240
241} // namespace uart
242} // namespace esphome
243
244#endif // USE_ESP32
virtual void mark_failed()
Mark this component as failed.
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
bool peek_byte(uint8_t *data) override
void write_array(const uint8_t *data, size_t len) override
bool read_array(uint8_t *data, size_t len) override
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
@ UART_SELECTION_USB_SERIAL_JTAG
Definition logger.h:82
@ UART_SELECTION_USB_CDC
Definition logger.h:79
Logger * global_logger
Definition logger.cpp:283
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:279