ESPHome  2024.4.0
uart_component_esp32_arduino.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
3 #include "esphome/core/defines.h"
4 #include "esphome/core/helpers.h"
5 #include "esphome/core/log.h"
7 
8 #ifdef USE_LOGGER
10 #endif
11 
12 namespace esphome {
13 namespace uart {
14 static const char *const TAG = "uart.arduino_esp32";
15 
16 static const uint32_t UART_PARITY_EVEN = 0 << 0;
17 static const uint32_t UART_PARITY_ODD = 1 << 0;
18 static const uint32_t UART_PARITY_ENABLE = 1 << 1;
19 static const uint32_t UART_NB_BIT_5 = 0 << 2;
20 static const uint32_t UART_NB_BIT_6 = 1 << 2;
21 static const uint32_t UART_NB_BIT_7 = 2 << 2;
22 static const uint32_t UART_NB_BIT_8 = 3 << 2;
23 static const uint32_t UART_NB_STOP_BIT_1 = 1 << 4;
24 static const uint32_t UART_NB_STOP_BIT_2 = 3 << 4;
25 static 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  ESP_LOGCONFIG(TAG, "Setting up UART...");
78  // Use Arduino HardwareSerial UARTs if all used pins match the ones
79  // preconfigured by the platform. For example if RX disabled but TX pin
80  // is 1 we still want to use Serial.
81  bool is_default_tx, is_default_rx;
82 #ifdef CONFIG_IDF_TARGET_ESP32C3
83  is_default_tx = tx_pin_ == nullptr || tx_pin_->get_pin() == 21;
84  is_default_rx = rx_pin_ == nullptr || rx_pin_->get_pin() == 20;
85 #else
86  is_default_tx = tx_pin_ == nullptr || tx_pin_->get_pin() == 1;
87  is_default_rx = rx_pin_ == nullptr || rx_pin_->get_pin() == 3;
88 #endif
89  static uint8_t next_uart_num = 0;
90  if (is_default_tx && is_default_rx && next_uart_num == 0) {
91 #if ARDUINO_USB_CDC_ON_BOOT
92  this->hw_serial_ = &Serial0;
93 #else
94  this->hw_serial_ = &Serial;
95 #endif
96  next_uart_num++;
97  } else {
98 #ifdef USE_LOGGER
99  // The logger doesn't use this UART component, instead it targets the UARTs
100  // directly (i.e. Serial/Serial0, Serial1, and Serial2). If the logger is
101  // enabled, skip the UART that it is configured to use.
102  if (logger::global_logger->get_baud_rate() > 0 && logger::global_logger->get_uart() == next_uart_num) {
103  next_uart_num++;
104  }
105 #endif // USE_LOGGER
106 
107  if (next_uart_num >= UART_NUM_MAX) {
108  ESP_LOGW(TAG, "Maximum number of UART components created already.");
109  this->mark_failed();
110  return;
111  }
112 
113  this->number_ = next_uart_num;
114  this->hw_serial_ = new HardwareSerial(next_uart_num++); // NOLINT(cppcoreguidelines-owning-memory)
115  }
116 
117  this->load_settings(false);
118 }
119 
121  int8_t tx = this->tx_pin_ != nullptr ? this->tx_pin_->get_pin() : -1;
122  int8_t rx = this->rx_pin_ != nullptr ? this->rx_pin_->get_pin() : -1;
123  bool invert = false;
124  if (tx_pin_ != nullptr && tx_pin_->is_inverted())
125  invert = true;
126  if (rx_pin_ != nullptr && rx_pin_->is_inverted())
127  invert = true;
128  this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
129  this->hw_serial_->begin(this->baud_rate_, get_config(), rx, tx, invert);
130  if (dump_config) {
131  ESP_LOGCONFIG(TAG, "UART %u was reloaded.", this->number_);
132  this->dump_config();
133  }
134 }
135 
137  ESP_LOGCONFIG(TAG, "UART Bus %d:", this->number_);
138  LOG_PIN(" TX Pin: ", tx_pin_);
139  LOG_PIN(" RX Pin: ", rx_pin_);
140  if (this->rx_pin_ != nullptr) {
141  ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
142  }
143  ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
144  ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_);
145  ESP_LOGCONFIG(TAG, " Parity: %s", LOG_STR_ARG(parity_to_str(this->parity_)));
146  ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
147  this->check_logger_conflict();
148 }
149 
150 void ESP32ArduinoUARTComponent::write_array(const uint8_t *data, size_t len) {
151  this->hw_serial_->write(data, len);
152 #ifdef USE_UART_DEBUGGER
153  for (size_t i = 0; i < len; i++) {
154  this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
155  }
156 #endif
157 }
158 
160  if (!this->check_read_timeout_())
161  return false;
162  *data = this->hw_serial_->peek();
163  return true;
164 }
165 
166 bool ESP32ArduinoUARTComponent::read_array(uint8_t *data, size_t len) {
167  if (!this->check_read_timeout_(len))
168  return false;
169  this->hw_serial_->readBytes(data, len);
170 #ifdef USE_UART_DEBUGGER
171  for (size_t i = 0; i < len; i++) {
172  this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
173  }
174 #endif
175  return true;
176 }
177 
178 int ESP32ArduinoUARTComponent::available() { return this->hw_serial_->available(); }
180  ESP_LOGVV(TAG, " Flushing...");
181  this->hw_serial_->flush();
182 }
183 
185 #ifdef USE_LOGGER
186  if (this->hw_serial_ == nullptr || logger::global_logger->get_baud_rate() == 0) {
187  return;
188  }
189 
191  ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
192  "disable logging over the serial port by setting logger->baud_rate to 0.");
193  }
194 #endif
195 }
196 
197 } // namespace uart
198 } // namespace esphome
199 #endif // USE_ESP32_FRAMEWORK_ARDUINO
uint32_t get_baud_rate() const
bool read_array(uint8_t *data, size_t len) override
virtual uint8_t get_pin() const =0
Logger * global_logger
Definition: logger.cpp:179
const char *const TAG
Definition: spi.cpp:8
void write_array(const uint8_t *data, size_t len) override
std::string size_t len
Definition: helpers.h:292
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
const LogString * parity_to_str(UARTParityOptions parity)
Definition: uart.cpp:33
virtual bool is_inverted() const =0