ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
uart_component_esp8266.cpp
Go to the documentation of this file.
1#ifdef USE_ESP8266
6#include "esphome/core/log.h"
7
8#ifdef USE_LOGGER
10#endif
11
12namespace esphome {
13namespace uart {
14
15static const char *const TAG = "uart.arduino_esp8266";
16bool ESP8266UartComponent::serial0_in_use = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
17
19 uint32_t config = 0;
20
21 if (this->parity_ == UART_CONFIG_PARITY_NONE) {
22 config |= UART_PARITY_NONE;
23 } else if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
24 config |= UART_PARITY_EVEN;
25 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
26 config |= UART_PARITY_ODD;
27 }
28
29 switch (this->data_bits_) {
30 case 5:
31 config |= UART_NB_BIT_5;
32 break;
33 case 6:
34 config |= UART_NB_BIT_6;
35 break;
36 case 7:
37 config |= UART_NB_BIT_7;
38 break;
39 case 8:
40 config |= UART_NB_BIT_8;
41 break;
42 }
43
44 if (this->stop_bits_ == 1) {
45 config |= UART_NB_STOP_BIT_1;
46 } else {
47 config |= UART_NB_STOP_BIT_2;
48 }
49
50 if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted())
51 config |= BIT(22);
52 if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted())
53 config |= BIT(19);
54
55 return config;
56}
57
59 // Use Arduino HardwareSerial UARTs if all used pins match the ones
60 // preconfigured by the platform. For example if RX disabled but TX pin
61 // is 1 we still want to use Serial.
62 SerialConfig config = static_cast<SerialConfig>(get_config());
63
64 if (!ESP8266UartComponent::serial0_in_use && (tx_pin_ == nullptr || tx_pin_->get_pin() == 1) &&
65 (rx_pin_ == nullptr || rx_pin_->get_pin() == 3)
66#ifdef USE_LOGGER
67 // we will use UART0 if logger isn't using it in swapped mode
68 && (logger::global_logger->get_hw_serial() == nullptr ||
70#endif
71 ) {
72 this->hw_serial_ = &Serial;
73 this->hw_serial_->begin(this->baud_rate_, config);
74 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
75 ESP8266UartComponent::serial0_in_use = true;
76 } else if (!ESP8266UartComponent::serial0_in_use && (tx_pin_ == nullptr || tx_pin_->get_pin() == 15) &&
77 (rx_pin_ == nullptr || rx_pin_->get_pin() == 13)
78#ifdef USE_LOGGER
79 // we will use UART0 swapped if logger isn't using it in regular mode
80 && (logger::global_logger->get_hw_serial() == nullptr ||
82#endif
83 ) {
84 this->hw_serial_ = &Serial;
85 this->hw_serial_->begin(this->baud_rate_, config);
86 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
87 this->hw_serial_->swap();
88 ESP8266UartComponent::serial0_in_use = true;
89 } else if ((tx_pin_ == nullptr || tx_pin_->get_pin() == 2) && (rx_pin_ == nullptr || rx_pin_->get_pin() == 8)) {
90 this->hw_serial_ = &Serial1;
91 this->hw_serial_->begin(this->baud_rate_, config);
92 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
93 } else {
94 this->sw_serial_ = new ESP8266SoftwareSerial(); // NOLINT
95 this->sw_serial_->setup(tx_pin_, rx_pin_, this->baud_rate_, this->stop_bits_, this->data_bits_, this->parity_,
96 this->rx_buffer_size_);
97 }
98}
99
101 ESP_LOGCONFIG(TAG, "Loading UART bus settings");
102 if (this->hw_serial_ != nullptr) {
103 SerialConfig config = static_cast<SerialConfig>(get_config());
104 this->hw_serial_->begin(this->baud_rate_, config);
105 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
106 } else {
107 this->sw_serial_->setup(this->tx_pin_, this->rx_pin_, this->baud_rate_, this->stop_bits_, this->data_bits_,
108 this->parity_, this->rx_buffer_size_);
109 }
110 if (dump_config) {
111 ESP_LOGCONFIG(TAG, "UART bus was reloaded.");
112 this->dump_config();
113 }
114}
115
117 ESP_LOGCONFIG(TAG, "UART Bus:");
118 LOG_PIN(" TX Pin: ", this->tx_pin_);
119 LOG_PIN(" RX Pin: ", this->rx_pin_);
120 if (this->rx_pin_ != nullptr) {
121 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_); // NOLINT
122 }
123 ESP_LOGCONFIG(TAG,
124 " Baud Rate: %u baud\n"
125 " Data Bits: %u\n"
126 " Parity: %s\n"
127 " Stop bits: %u",
128 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
129 if (this->hw_serial_ != nullptr) {
130 ESP_LOGCONFIG(TAG, " Using hardware serial interface.");
131 } else {
132 ESP_LOGCONFIG(TAG, " Using software serial");
133 }
134 this->check_logger_conflict();
135}
136
138#ifdef USE_LOGGER
139 if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) {
140 return;
141 }
142
143 if (this->hw_serial_ == logger::global_logger->get_hw_serial()) {
144 ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
145 "disable logging over the serial port by setting logger->baud_rate to 0.");
146 }
147#endif
148}
149
150void ESP8266UartComponent::write_array(const uint8_t *data, size_t len) {
151 if (this->hw_serial_ != nullptr) {
152 this->hw_serial_->write(data, len);
153 } else {
154 for (size_t i = 0; i < len; i++)
155 this->sw_serial_->write_byte(data[i]);
156 }
157#ifdef USE_UART_DEBUGGER
158 for (size_t i = 0; i < len; i++) {
159 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
160 }
161#endif
162}
164 if (!this->check_read_timeout_())
165 return false;
166 if (this->hw_serial_ != nullptr) {
167 *data = this->hw_serial_->peek();
168 } else {
169 *data = this->sw_serial_->peek_byte();
170 }
171 return true;
172}
173bool ESP8266UartComponent::read_array(uint8_t *data, size_t len) {
174 if (!this->check_read_timeout_(len))
175 return false;
176 if (this->hw_serial_ != nullptr) {
177 this->hw_serial_->readBytes(data, len);
178 } else {
179 for (size_t i = 0; i < len; i++)
180 data[i] = this->sw_serial_->read_byte();
181 }
182#ifdef USE_UART_DEBUGGER
183 for (size_t i = 0; i < len; i++) {
184 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
185 }
186#endif
187 return true;
188}
190 if (this->hw_serial_ != nullptr) {
191 return this->hw_serial_->available();
192 } else {
193 return this->sw_serial_->available();
194 }
195}
197 ESP_LOGVV(TAG, " Flushing");
198 if (this->hw_serial_ != nullptr) {
199 this->hw_serial_->flush();
200 } else {
201 this->sw_serial_->flush();
202 }
203}
204void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_pin, uint32_t baud_rate,
205 uint8_t stop_bits, uint32_t data_bits, UARTParityOptions parity,
206 size_t rx_buffer_size) {
207 this->bit_time_ = F_CPU / baud_rate;
208 this->rx_buffer_size_ = rx_buffer_size;
209 this->stop_bits_ = stop_bits;
210 this->data_bits_ = data_bits;
211 this->parity_ = parity;
212 if (tx_pin != nullptr) {
213 gpio_tx_pin_ = tx_pin;
217 }
218 if (rx_pin != nullptr) {
219 gpio_rx_pin_ = rx_pin;
222 rx_buffer_ = new uint8_t[this->rx_buffer_size_]; // NOLINT
224 }
225}
227 uint32_t wait = arg->bit_time_ + arg->bit_time_ / 3 - 500;
228 const uint32_t start = arch_get_cpu_cycle_count();
229 uint8_t rec = 0;
230 // Manually unroll the loop
231 for (int i = 0; i < arg->data_bits_; i++)
232 rec |= arg->read_bit_(&wait, start) << i;
233
234 /* If parity is enabled, just read it and ignore it. */
235 /* TODO: Should we check parity? Or is it too slow for nothing added..*/
237 arg->read_bit_(&wait, start);
238
239 // Stop bit
240 arg->wait_(&wait, start);
241 if (arg->stop_bits_ == 2)
242 arg->wait_(&wait, start);
243
244 arg->rx_buffer_[arg->rx_in_pos_] = rec;
245 arg->rx_in_pos_ = (arg->rx_in_pos_ + 1) % arg->rx_buffer_size_;
246 // Clear RX pin so that the interrupt doesn't re-trigger right away again.
248}
249void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
250 if (this->gpio_tx_pin_ == nullptr) {
251 ESP_LOGE(TAG, "UART doesn't have TX pins set!");
252 return;
253 }
254 bool parity_bit = false;
255 bool need_parity_bit = true;
256 if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
257 parity_bit = false;
258 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
259 parity_bit = true;
260 } else {
261 need_parity_bit = false;
262 }
263
264 {
265 InterruptLock lock;
266 uint32_t wait = this->bit_time_;
267 const uint32_t start = arch_get_cpu_cycle_count();
268 // Start bit
269 this->write_bit_(false, &wait, start);
270 for (int i = 0; i < this->data_bits_; i++) {
271 bool bit = data & (1 << i);
272 this->write_bit_(bit, &wait, start);
273 if (need_parity_bit)
274 parity_bit ^= bit;
275 }
276 if (need_parity_bit)
277 this->write_bit_(parity_bit, &wait, start);
278 // Stop bit
279 this->write_bit_(true, &wait, start);
280 if (this->stop_bits_ == 2)
281 this->wait_(&wait, start);
282 }
283}
284void IRAM_ATTR ESP8266SoftwareSerial::wait_(uint32_t *wait, const uint32_t &start) {
285 while (arch_get_cpu_cycle_count() - start < *wait)
286 ;
287 *wait += this->bit_time_;
288}
289bool IRAM_ATTR ESP8266SoftwareSerial::read_bit_(uint32_t *wait, const uint32_t &start) {
290 this->wait_(wait, start);
291 return this->rx_pin_.digital_read();
292}
293void IRAM_ATTR ESP8266SoftwareSerial::write_bit_(bool bit, uint32_t *wait, const uint32_t &start) {
294 this->tx_pin_.digital_write(bit);
295 this->wait_(wait, start);
296}
298 if (this->rx_in_pos_ == this->rx_out_pos_)
299 return 0;
300 uint8_t data = this->rx_buffer_[this->rx_out_pos_];
301 this->rx_out_pos_ = (this->rx_out_pos_ + 1) % this->rx_buffer_size_;
302 return data;
303}
305 if (this->rx_in_pos_ == this->rx_out_pos_)
306 return 0;
307 return this->rx_buffer_[this->rx_out_pos_];
308}
310 // Flush is a NO-OP with software serial, all bytes are written immediately.
311}
313 int avail = int(this->rx_in_pos_) - int(this->rx_out_pos_);
314 if (avail < 0)
315 return avail + this->rx_buffer_size_;
316 return avail;
317}
318
319} // namespace uart
320} // namespace esphome
321#endif // USE_ESP8266
virtual void setup()=0
void digital_write(bool value)
Definition gpio.cpp:146
virtual uint8_t get_pin() const =0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:88
virtual bool is_inverted() const =0
virtual ISRInternalGPIOPin to_isr() const =0
Helper class to disable interrupts.
Definition helpers.h:732
Stream * get_hw_serial() const
Definition logger.h:120
UARTSelection get_uart() const
Get the UART used by the logger.
Definition logger.cpp:242
void wait_(uint32_t *wait, const uint32_t &start)
void setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t data_bits, UARTParityOptions parity, size_t rx_buffer_size)
void write_bit_(bool bit, uint32_t *wait, const uint32_t &start)
bool read_bit_(uint32_t *wait, const uint32_t &start)
static void gpio_intr(ESP8266SoftwareSerial *arg)
bool read_array(uint8_t *data, size_t len) override
void write_array(const uint8_t *data, size_t len) override
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ UART_SELECTION_UART0_SWAP
Definition logger.h:85
@ UART_SELECTION_UART0
Definition logger.h:70
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
uint32_t arch_get_cpu_cycle_count()
Definition core.cpp:59
std::string size_t len
Definition helpers.h:279