ESPHome  2022.12.8
uart_component_esp8266.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP8266
4 #include "esphome/core/defines.h"
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/log.h"
7 
8 #ifdef USE_LOGGER
10 #endif
11 
12 namespace esphome {
13 namespace uart {
14 
15 static const char *const TAG = "uart.arduino_esp8266";
16 bool 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, "Setting up UART bus...");
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, "UART Bus:");
103  LOG_PIN(" TX Pin: ", tx_pin_);
104  LOG_PIN(" RX Pin: ", rx_pin_);
105  if (this->rx_pin_ != nullptr) {
106  ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_); // NOLINT
107  }
108  ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
109  ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_);
110  ESP_LOGCONFIG(TAG, " Parity: %s", LOG_STR_ARG(parity_to_str(this->parity_)));
111  ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
112  if (this->hw_serial_ != nullptr) {
113  ESP_LOGCONFIG(TAG, " Using hardware serial interface.");
114  } else {
115  ESP_LOGCONFIG(TAG, " Using software serial");
116  }
117  this->check_logger_conflict();
118 }
119 
121 #ifdef USE_LOGGER
122  if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) {
123  return;
124  }
125 
126  if (this->hw_serial_ == logger::global_logger->get_hw_serial()) {
127  ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
128  "disable logging over the serial port by setting logger->baud_rate to 0.");
129  }
130 #endif
131 }
132 
133 void ESP8266UartComponent::write_array(const uint8_t *data, size_t len) {
134  if (this->hw_serial_ != nullptr) {
135  this->hw_serial_->write(data, len);
136  } else {
137  for (size_t i = 0; i < len; i++)
138  this->sw_serial_->write_byte(data[i]);
139  }
140 #ifdef USE_UART_DEBUGGER
141  for (size_t i = 0; i < len; i++) {
142  this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
143  }
144 #endif
145 }
146 bool ESP8266UartComponent::peek_byte(uint8_t *data) {
147  if (!this->check_read_timeout_())
148  return false;
149  if (this->hw_serial_ != nullptr) {
150  *data = this->hw_serial_->peek();
151  } else {
152  *data = this->sw_serial_->peek_byte();
153  }
154  return true;
155 }
156 bool ESP8266UartComponent::read_array(uint8_t *data, size_t len) {
157  if (!this->check_read_timeout_(len))
158  return false;
159  if (this->hw_serial_ != nullptr) {
160  this->hw_serial_->readBytes(data, len);
161  } else {
162  for (size_t i = 0; i < len; i++)
163  data[i] = this->sw_serial_->read_byte();
164  }
165 #ifdef USE_UART_DEBUGGER
166  for (size_t i = 0; i < len; i++) {
167  this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
168  }
169 #endif
170  return true;
171 }
173  if (this->hw_serial_ != nullptr) {
174  return this->hw_serial_->available();
175  } else {
176  return this->sw_serial_->available();
177  }
178 }
180  ESP_LOGVV(TAG, " Flushing...");
181  if (this->hw_serial_ != nullptr) {
182  this->hw_serial_->flush();
183  } else {
184  this->sw_serial_->flush();
185  }
186 }
187 void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_pin, uint32_t baud_rate,
188  uint8_t stop_bits, uint32_t data_bits, UARTParityOptions parity,
189  size_t rx_buffer_size) {
190  this->bit_time_ = F_CPU / baud_rate;
191  this->rx_buffer_size_ = rx_buffer_size;
192  this->stop_bits_ = stop_bits;
193  this->data_bits_ = data_bits;
194  this->parity_ = parity;
195  if (tx_pin != nullptr) {
196  gpio_tx_pin_ = tx_pin;
197  gpio_tx_pin_->setup();
198  tx_pin_ = gpio_tx_pin_->to_isr();
199  tx_pin_.digital_write(true);
200  }
201  if (rx_pin != nullptr) {
202  gpio_rx_pin_ = rx_pin;
203  gpio_rx_pin_->setup();
204  rx_pin_ = gpio_rx_pin_->to_isr();
205  rx_buffer_ = new uint8_t[this->rx_buffer_size_]; // NOLINT
206  gpio_rx_pin_->attach_interrupt(ESP8266SoftwareSerial::gpio_intr, this, gpio::INTERRUPT_FALLING_EDGE);
207  }
208 }
210  uint32_t wait = arg->bit_time_ + arg->bit_time_ / 3 - 500;
211  const uint32_t start = arch_get_cpu_cycle_count();
212  uint8_t rec = 0;
213  // Manually unroll the loop
214  for (int i = 0; i < arg->data_bits_; i++)
215  rec |= arg->read_bit_(&wait, start) << i;
216 
217  /* If parity is enabled, just read it and ignore it. */
218  /* TODO: Should we check parity? Or is it too slow for nothing added..*/
220  arg->read_bit_(&wait, start);
221 
222  // Stop bit
223  arg->wait_(&wait, start);
224  if (arg->stop_bits_ == 2)
225  arg->wait_(&wait, start);
226 
227  arg->rx_buffer_[arg->rx_in_pos_] = rec;
228  arg->rx_in_pos_ = (arg->rx_in_pos_ + 1) % arg->rx_buffer_size_;
229  // Clear RX pin so that the interrupt doesn't re-trigger right away again.
230  arg->rx_pin_.clear_interrupt();
231 }
232 void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
233  if (this->gpio_tx_pin_ == nullptr) {
234  ESP_LOGE(TAG, "UART doesn't have TX pins set!");
235  return;
236  }
237  bool parity_bit = false;
238  bool need_parity_bit = true;
239  if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
240  parity_bit = false;
241  } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
242  parity_bit = true;
243  } else {
244  need_parity_bit = false;
245  }
246 
247  {
248  InterruptLock lock;
249  uint32_t wait = this->bit_time_;
250  const uint32_t start = arch_get_cpu_cycle_count();
251  // Start bit
252  this->write_bit_(false, &wait, start);
253  for (int i = 0; i < this->data_bits_; i++) {
254  bool bit = data & (1 << i);
255  this->write_bit_(bit, &wait, start);
256  if (need_parity_bit)
257  parity_bit ^= bit;
258  }
259  if (need_parity_bit)
260  this->write_bit_(parity_bit, &wait, start);
261  // Stop bit
262  this->write_bit_(true, &wait, start);
263  if (this->stop_bits_ == 2)
264  this->wait_(&wait, start);
265  }
266 }
267 void IRAM_ATTR ESP8266SoftwareSerial::wait_(uint32_t *wait, const uint32_t &start) {
268  while (arch_get_cpu_cycle_count() - start < *wait)
269  ;
270  *wait += this->bit_time_;
271 }
272 bool IRAM_ATTR ESP8266SoftwareSerial::read_bit_(uint32_t *wait, const uint32_t &start) {
273  this->wait_(wait, start);
274  return this->rx_pin_.digital_read();
275 }
276 void IRAM_ATTR ESP8266SoftwareSerial::write_bit_(bool bit, uint32_t *wait, const uint32_t &start) {
277  this->tx_pin_.digital_write(bit);
278  this->wait_(wait, start);
279 }
281  if (this->rx_in_pos_ == this->rx_out_pos_)
282  return 0;
283  uint8_t data = this->rx_buffer_[this->rx_out_pos_];
284  this->rx_out_pos_ = (this->rx_out_pos_ + 1) % this->rx_buffer_size_;
285  return data;
286 }
288  if (this->rx_in_pos_ == this->rx_out_pos_)
289  return 0;
290  return this->rx_buffer_[this->rx_out_pos_];
291 }
293  // Flush is a NO-OP with software serial, all bytes are written immediately.
294 }
296  int avail = int(this->rx_in_pos_) - int(this->rx_out_pos_);
297  if (avail < 0)
298  return avail + this->rx_buffer_size_;
299  return avail;
300 }
301 
302 } // namespace uart
303 } // namespace esphome
304 #endif // USE_ESP8266
virtual void digital_write(bool value)=0
void write_bit_(bool bit, 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)
uint32_t get_baud_rate() const
bool read_array(uint8_t *data, size_t len) override
void write_array(const uint8_t *data, size_t len) override
virtual void setup()=0
bool read_bit_(uint32_t *wait, const uint32_t &start)
virtual uint8_t get_pin() const =0
Logger * global_logger
Definition: logger.cpp:303
UARTSelection get_uart() const
Get the UART used by the logger.
Definition: logger.cpp:264
static void gpio_intr(ESP8266SoftwareSerial *arg)
virtual bool digital_read()=0
virtual ISRInternalGPIOPin to_isr() const =0
Helper class to disable interrupts.
Definition: helpers.h:539
std::string size_t len
Definition: helpers.h:281
Definition: a4988.cpp:4
void wait_(uint32_t *wait, const uint32_t &start)
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
uint32_t arch_get_cpu_cycle_count()
Definition: core.cpp:57
const LogString * parity_to_str(UARTParityOptions parity)
Definition: uart.cpp:32
virtual bool is_inverted() const =0