ESPHome 2025.6.0
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 ESP_LOGCONFIG(TAG, "Running setup");
60 // Use Arduino HardwareSerial UARTs if all used pins match the ones
61 // preconfigured by the platform. For example if RX disabled but TX pin
62 // is 1 we still want to use Serial.
63 SerialConfig config = static_cast<SerialConfig>(get_config());
64
65 if (!ESP8266UartComponent::serial0_in_use && (tx_pin_ == nullptr || tx_pin_->get_pin() == 1) &&
66 (rx_pin_ == nullptr || rx_pin_->get_pin() == 3)
67#ifdef USE_LOGGER
68 // we will use UART0 if logger isn't using it in swapped mode
69 && (logger::global_logger->get_hw_serial() == nullptr ||
71#endif
72 ) {
73 this->hw_serial_ = &Serial;
74 this->hw_serial_->begin(this->baud_rate_, config);
75 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
76 ESP8266UartComponent::serial0_in_use = true;
77 } else if (!ESP8266UartComponent::serial0_in_use && (tx_pin_ == nullptr || tx_pin_->get_pin() == 15) &&
78 (rx_pin_ == nullptr || rx_pin_->get_pin() == 13)
79#ifdef USE_LOGGER
80 // we will use UART0 swapped if logger isn't using it in regular mode
81 && (logger::global_logger->get_hw_serial() == nullptr ||
83#endif
84 ) {
85 this->hw_serial_ = &Serial;
86 this->hw_serial_->begin(this->baud_rate_, config);
87 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
88 this->hw_serial_->swap();
89 ESP8266UartComponent::serial0_in_use = true;
90 } else if ((tx_pin_ == nullptr || tx_pin_->get_pin() == 2) && (rx_pin_ == nullptr || rx_pin_->get_pin() == 8)) {
91 this->hw_serial_ = &Serial1;
92 this->hw_serial_->begin(this->baud_rate_, config);
93 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
94 } else {
95 this->sw_serial_ = new ESP8266SoftwareSerial(); // NOLINT
96 this->sw_serial_->setup(tx_pin_, rx_pin_, this->baud_rate_, this->stop_bits_, this->data_bits_, this->parity_,
97 this->rx_buffer_size_);
98 }
99}
100
102 ESP_LOGCONFIG(TAG, "Loading UART bus settings");
103 if (this->hw_serial_ != nullptr) {
104 SerialConfig config = static_cast<SerialConfig>(get_config());
105 this->hw_serial_->begin(this->baud_rate_, config);
106 this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
107 } else {
108 this->sw_serial_->setup(this->tx_pin_, this->rx_pin_, this->baud_rate_, this->stop_bits_, this->data_bits_,
109 this->parity_, this->rx_buffer_size_);
110 }
111 if (dump_config) {
112 ESP_LOGCONFIG(TAG, "UART bus was reloaded.");
113 this->dump_config();
114 }
115}
116
118 ESP_LOGCONFIG(TAG, "UART Bus:");
119 LOG_PIN(" TX Pin: ", this->tx_pin_);
120 LOG_PIN(" RX Pin: ", this->rx_pin_);
121 if (this->rx_pin_ != nullptr) {
122 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_); // NOLINT
123 }
124 ESP_LOGCONFIG(TAG,
125 " Baud Rate: %u baud\n"
126 " Data Bits: %u\n"
127 " Parity: %s\n"
128 " Stop bits: %u",
129 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
130 if (this->hw_serial_ != nullptr) {
131 ESP_LOGCONFIG(TAG, " Using hardware serial interface.");
132 } else {
133 ESP_LOGCONFIG(TAG, " Using software serial");
134 }
135 this->check_logger_conflict();
136}
137
139#ifdef USE_LOGGER
140 if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) {
141 return;
142 }
143
144 if (this->hw_serial_ == logger::global_logger->get_hw_serial()) {
145 ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
146 "disable logging over the serial port by setting logger->baud_rate to 0.");
147 }
148#endif
149}
150
151void ESP8266UartComponent::write_array(const uint8_t *data, size_t len) {
152 if (this->hw_serial_ != nullptr) {
153 this->hw_serial_->write(data, len);
154 } else {
155 for (size_t i = 0; i < len; i++)
156 this->sw_serial_->write_byte(data[i]);
157 }
158#ifdef USE_UART_DEBUGGER
159 for (size_t i = 0; i < len; i++) {
160 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
161 }
162#endif
163}
165 if (!this->check_read_timeout_())
166 return false;
167 if (this->hw_serial_ != nullptr) {
168 *data = this->hw_serial_->peek();
169 } else {
170 *data = this->sw_serial_->peek_byte();
171 }
172 return true;
173}
174bool ESP8266UartComponent::read_array(uint8_t *data, size_t len) {
175 if (!this->check_read_timeout_(len))
176 return false;
177 if (this->hw_serial_ != nullptr) {
178 this->hw_serial_->readBytes(data, len);
179 } else {
180 for (size_t i = 0; i < len; i++)
181 data[i] = this->sw_serial_->read_byte();
182 }
183#ifdef USE_UART_DEBUGGER
184 for (size_t i = 0; i < len; i++) {
185 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
186 }
187#endif
188 return true;
189}
191 if (this->hw_serial_ != nullptr) {
192 return this->hw_serial_->available();
193 } else {
194 return this->sw_serial_->available();
195 }
196}
198 ESP_LOGVV(TAG, " Flushing");
199 if (this->hw_serial_ != nullptr) {
200 this->hw_serial_->flush();
201 } else {
202 this->sw_serial_->flush();
203 }
204}
205void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_pin, uint32_t baud_rate,
206 uint8_t stop_bits, uint32_t data_bits, UARTParityOptions parity,
207 size_t rx_buffer_size) {
208 this->bit_time_ = F_CPU / baud_rate;
209 this->rx_buffer_size_ = rx_buffer_size;
210 this->stop_bits_ = stop_bits;
211 this->data_bits_ = data_bits;
212 this->parity_ = parity;
213 if (tx_pin != nullptr) {
214 gpio_tx_pin_ = tx_pin;
218 }
219 if (rx_pin != nullptr) {
220 gpio_rx_pin_ = rx_pin;
223 rx_buffer_ = new uint8_t[this->rx_buffer_size_]; // NOLINT
225 }
226}
228 uint32_t wait = arg->bit_time_ + arg->bit_time_ / 3 - 500;
229 const uint32_t start = arch_get_cpu_cycle_count();
230 uint8_t rec = 0;
231 // Manually unroll the loop
232 for (int i = 0; i < arg->data_bits_; i++)
233 rec |= arg->read_bit_(&wait, start) << i;
234
235 /* If parity is enabled, just read it and ignore it. */
236 /* TODO: Should we check parity? Or is it too slow for nothing added..*/
238 arg->read_bit_(&wait, start);
239
240 // Stop bit
241 arg->wait_(&wait, start);
242 if (arg->stop_bits_ == 2)
243 arg->wait_(&wait, start);
244
245 arg->rx_buffer_[arg->rx_in_pos_] = rec;
246 arg->rx_in_pos_ = (arg->rx_in_pos_ + 1) % arg->rx_buffer_size_;
247 // Clear RX pin so that the interrupt doesn't re-trigger right away again.
249}
250void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
251 if (this->gpio_tx_pin_ == nullptr) {
252 ESP_LOGE(TAG, "UART doesn't have TX pins set!");
253 return;
254 }
255 bool parity_bit = false;
256 bool need_parity_bit = true;
257 if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
258 parity_bit = false;
259 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
260 parity_bit = true;
261 } else {
262 need_parity_bit = false;
263 }
264
265 {
266 InterruptLock lock;
267 uint32_t wait = this->bit_time_;
268 const uint32_t start = arch_get_cpu_cycle_count();
269 // Start bit
270 this->write_bit_(false, &wait, start);
271 for (int i = 0; i < this->data_bits_; i++) {
272 bool bit = data & (1 << i);
273 this->write_bit_(bit, &wait, start);
274 if (need_parity_bit)
275 parity_bit ^= bit;
276 }
277 if (need_parity_bit)
278 this->write_bit_(parity_bit, &wait, start);
279 // Stop bit
280 this->write_bit_(true, &wait, start);
281 if (this->stop_bits_ == 2)
282 this->wait_(&wait, start);
283 }
284}
285void IRAM_ATTR ESP8266SoftwareSerial::wait_(uint32_t *wait, const uint32_t &start) {
286 while (arch_get_cpu_cycle_count() - start < *wait)
287 ;
288 *wait += this->bit_time_;
289}
290bool IRAM_ATTR ESP8266SoftwareSerial::read_bit_(uint32_t *wait, const uint32_t &start) {
291 this->wait_(wait, start);
292 return this->rx_pin_.digital_read();
293}
294void IRAM_ATTR ESP8266SoftwareSerial::write_bit_(bool bit, uint32_t *wait, const uint32_t &start) {
295 this->tx_pin_.digital_write(bit);
296 this->wait_(wait, start);
297}
299 if (this->rx_in_pos_ == this->rx_out_pos_)
300 return 0;
301 uint8_t data = this->rx_buffer_[this->rx_out_pos_];
302 this->rx_out_pos_ = (this->rx_out_pos_ + 1) % this->rx_buffer_size_;
303 return data;
304}
306 if (this->rx_in_pos_ == this->rx_out_pos_)
307 return 0;
308 return this->rx_buffer_[this->rx_out_pos_];
309}
311 // Flush is a NO-OP with software serial, all bytes are written immediately.
312}
314 int avail = int(this->rx_in_pos_) - int(this->rx_out_pos_);
315 if (avail < 0)
316 return avail + this->rx_buffer_size_;
317 return avail;
318}
319
320} // namespace uart
321} // namespace esphome
322#endif // USE_ESP8266
virtual void setup()=0
void digital_write(bool value)
Definition gpio.cpp:147
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:615
Stream * get_hw_serial() const
Definition logger.h:117
UARTSelection get_uart() const
Get the UART used by the logger.
Definition logger.cpp:201
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:82
@ UART_SELECTION_UART0
Definition logger.h:67
Logger * global_logger
Definition logger.cpp:242
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:60
std::string size_t len
Definition helpers.h:302