ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
uart_component_esp32_arduino.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32_FRAMEWORK_ARDUINO
6#include "esphome/core/log.h"
7
8#ifdef USE_LOGGER
10#endif
11
12namespace esphome {
13namespace uart {
14static const char *const TAG = "uart.arduino_esp32";
15
16static const uint32_t UART_PARITY_EVEN = 0 << 0;
17static const uint32_t UART_PARITY_ODD = 1 << 0;
18static const uint32_t UART_PARITY_ENABLE = 1 << 1;
19static const uint32_t UART_NB_BIT_5 = 0 << 2;
20static const uint32_t UART_NB_BIT_6 = 1 << 2;
21static const uint32_t UART_NB_BIT_7 = 2 << 2;
22static const uint32_t UART_NB_BIT_8 = 3 << 2;
23static const uint32_t UART_NB_STOP_BIT_1 = 1 << 4;
24static const uint32_t UART_NB_STOP_BIT_2 = 3 << 4;
25static const uint32_t UART_TICK_APB_CLOCK = 1 << 27;
26
28 uint32_t config = 0;
29
30 /*
31 * All bits numbers below come from
32 * framework-arduinoespressif32/cores/esp32/esp32-hal-uart.h
33 * And more specifically conf0 union in uart_dev_t.
34 *
35 * Below is bit used from conf0 union.
36 * <name>:<bits position> <values>
37 * parity:0 0:even 1:odd
38 * parity_en:1 Set this bit to enable uart parity check.
39 * bit_num:2-4 0:5bits 1:6bits 2:7bits 3:8bits
40 * stop_bit_num:4-6 stop bit. 1:1bit 2:1.5bits 3:2bits
41 * tick_ref_always_on:27 select the clock.1:apb clock:ref_tick
42 */
43
44 if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
45 config |= UART_PARITY_EVEN | UART_PARITY_ENABLE;
46 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
47 config |= UART_PARITY_ODD | UART_PARITY_ENABLE;
48 }
49
50 switch (this->data_bits_) {
51 case 5:
52 config |= UART_NB_BIT_5;
53 break;
54 case 6:
55 config |= UART_NB_BIT_6;
56 break;
57 case 7:
58 config |= UART_NB_BIT_7;
59 break;
60 case 8:
61 config |= UART_NB_BIT_8;
62 break;
63 }
64
65 if (this->stop_bits_ == 1) {
66 config |= UART_NB_STOP_BIT_1;
67 } else {
68 config |= UART_NB_STOP_BIT_2;
69 }
70
71 config |= UART_TICK_APB_CLOCK;
72
73 return config;
74}
75
77 // Use Arduino HardwareSerial UARTs if all used pins match the ones
78 // preconfigured by the platform. For example if RX disabled but TX pin
79 // is 1 we still want to use Serial.
80 bool is_default_tx, is_default_rx;
81#ifdef CONFIG_IDF_TARGET_ESP32C3
82 is_default_tx = tx_pin_ == nullptr || tx_pin_->get_pin() == 21;
83 is_default_rx = rx_pin_ == nullptr || rx_pin_->get_pin() == 20;
84#else
85 is_default_tx = tx_pin_ == nullptr || tx_pin_->get_pin() == 1;
86 is_default_rx = rx_pin_ == nullptr || rx_pin_->get_pin() == 3;
87#endif
88 static uint8_t next_uart_num = 0;
89 if (is_default_tx && is_default_rx && next_uart_num == 0) {
90#if ARDUINO_USB_CDC_ON_BOOT
91 this->hw_serial_ = &Serial0;
92#else
93 this->hw_serial_ = &Serial;
94#endif
95 next_uart_num++;
96 } else {
97#ifdef USE_LOGGER
98 bool logger_uses_hardware_uart = true;
99
100#ifdef USE_LOGGER_USB_CDC
102 // this is not a hardware UART, ignore it
103 logger_uses_hardware_uart = false;
104 }
105#endif // USE_LOGGER_USB_CDC
106
107#ifdef USE_LOGGER_USB_SERIAL_JTAG
109 // this is not a hardware UART, ignore it
110 logger_uses_hardware_uart = false;
111 }
112#endif // USE_LOGGER_USB_SERIAL_JTAG
113
114 if (logger_uses_hardware_uart && logger::global_logger->get_baud_rate() > 0 &&
115 logger::global_logger->get_uart() == next_uart_num) {
116 next_uart_num++;
117 }
118#endif // USE_LOGGER
119
120 if (next_uart_num >= SOC_UART_NUM) {
121 ESP_LOGW(TAG, "Maximum number of UART components created already.");
122 this->mark_failed();
123 return;
124 }
125
126 this->number_ = next_uart_num;
127 this->hw_serial_ = new HardwareSerial(next_uart_num++); // NOLINT(cppcoreguidelines-owning-memory)
128 }
129
130 this->load_settings(false);
131}
132
134 int8_t tx = this->tx_pin_ != nullptr ? this->tx_pin_->get_pin() : -1;
135 int8_t rx = this->rx_pin_ != nullptr ? this->rx_pin_->get_pin() : -1;
136 bool invert = false;
137 if (tx_pin_ != nullptr && tx_pin_->is_inverted())
138 invert = true;
139 if (rx_pin_ != nullptr && rx_pin_->is_inverted())
140 invert = true;
141 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
142 this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx, invert);
143 if (dump_config) {
144 ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->number_);
145 this->dump_config();
146 }
147}
148
150 ESP_LOGCONFIG(TAG, "UART Bus %d:", this->number_);
151 LOG_PIN(" TX Pin: ", tx_pin_);
152 LOG_PIN(" RX Pin: ", rx_pin_);
153 if (this->rx_pin_ != nullptr) {
154 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
155 }
156 ESP_LOGCONFIG(TAG,
157 " Baud Rate: %u baud\n"
158 " Data Bits: %u\n"
159 " Parity: %s\n"
160 " Stop bits: %u",
161 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
162 this->check_logger_conflict();
163}
164
165void ESP32ArduinoUARTComponent::write_array(const uint8_t *data, size_t len) {
166 this->hw_serial_->write(data, len);
167#ifdef USE_UART_DEBUGGER
168 for (size_t i = 0; i < len; i++) {
169 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
170 }
171#endif
172}
173
175 if (!this->check_read_timeout_())
176 return false;
177 *data = this->hw_serial_->peek();
178 return true;
179}
180
181bool ESP32ArduinoUARTComponent::read_array(uint8_t *data, size_t len) {
182 if (!this->check_read_timeout_(len))
183 return false;
184 this->hw_serial_->readBytes(data, len);
185#ifdef USE_UART_DEBUGGER
186 for (size_t i = 0; i < len; i++) {
187 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
188 }
189#endif
190 return true;
191}
192
193int ESP32ArduinoUARTComponent::available() { return this->hw_serial_->available(); }
195 ESP_LOGVV(TAG, " Flushing");
196 this->hw_serial_->flush();
197}
198
200#ifdef USE_LOGGER
201 if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) {
202 return;
203 }
204
206 ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
207 "disable logging over the serial port by setting logger->baud_rate to 0.");
208 }
209#endif
210}
211
212} // namespace uart
213} // namespace esphome
214#endif // USE_ESP32_FRAMEWORK_ARDUINO
virtual void mark_failed()
Mark this component as failed.
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
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