ESPHome  2024.11.1
uart_component_libretiny.cpp
Go to the documentation of this file.
1 #ifdef USE_LIBRETINY
2 
4 #include "esphome/core/defines.h"
5 #include "esphome/core/helpers.h"
6 #include "esphome/core/log.h"
8 
9 #ifdef USE_LOGGER
11 #endif
12 
13 #if LT_ARD_HAS_SOFTSERIAL
14 #include <SoftwareSerial.h>
15 #endif
16 
17 namespace esphome {
18 namespace uart {
19 
20 static const char *const TAG = "uart.lt";
21 
22 static const char *UART_TYPE[] = {
23  "hardware",
24  "software",
25 };
26 
28  uint16_t config = 0;
29 
30  switch (this->parity_) {
32  config |= SERIAL_PARITY_NONE;
33  break;
35  config |= SERIAL_PARITY_EVEN;
36  break;
38  config |= SERIAL_PARITY_ODD;
39  break;
40  }
41 
42  config |= (this->data_bits_ - 4) << 8;
43  config |= 0x10 + (this->stop_bits_ - 1) * 0x20;
44 
45  return config;
46 }
47 
49  ESP_LOGCONFIG(TAG, "Setting up UART...");
50 
51  int8_t tx_pin = tx_pin_ == nullptr ? -1 : tx_pin_->get_pin();
52  int8_t rx_pin = rx_pin_ == nullptr ? -1 : rx_pin_->get_pin();
53  bool tx_inverted = tx_pin_ != nullptr && tx_pin_->is_inverted();
54  bool rx_inverted = rx_pin_ != nullptr && rx_pin_->is_inverted();
55 
56  if (false)
57  return;
58 #if LT_HW_UART0
59  else if ((tx_pin == -1 || tx_pin == PIN_SERIAL0_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL0_RX)) {
60  this->serial_ = &Serial0;
61  this->hardware_idx_ = 0;
62  }
63 #endif
64 #if LT_HW_UART1
65  else if ((tx_pin == -1 || tx_pin == PIN_SERIAL1_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL1_RX)) {
66  this->serial_ = &Serial1;
67  this->hardware_idx_ = 1;
68  }
69 #endif
70 #if LT_HW_UART2
71  else if ((tx_pin == -1 || tx_pin == PIN_SERIAL2_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL2_RX)) {
72  this->serial_ = &Serial2;
73  this->hardware_idx_ = 2;
74  }
75 #endif
76  else {
77 #if LT_ARD_HAS_SOFTSERIAL
78  this->serial_ = new SoftwareSerial(rx_pin, tx_pin, rx_inverted || tx_inverted);
79 #else
80  this->serial_ = &Serial;
81  ESP_LOGE(TAG, " SoftwareSerial is not implemented for this chip. Only hardware pins are supported:");
82 #if LT_HW_UART0
83  ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL0_TX, PIN_SERIAL0_RX);
84 #endif
85 #if LT_HW_UART1
86  ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL1_TX, PIN_SERIAL1_RX);
87 #endif
88 #if LT_HW_UART2
89  ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL2_TX, PIN_SERIAL2_RX);
90 #endif
91  this->mark_failed();
92  return;
93 #endif
94  }
95 
96  this->serial_->begin(this->baud_rate_, get_config());
97 }
98 
100  bool is_software = this->hardware_idx_ == -1;
101  ESP_LOGCONFIG(TAG, "UART Bus:");
102  ESP_LOGCONFIG(TAG, " Type: %s", UART_TYPE[is_software]);
103  if (!is_software) {
104  ESP_LOGCONFIG(TAG, " Port number: %d", this->hardware_idx_);
105  }
106  LOG_PIN(" TX Pin: ", tx_pin_);
107  LOG_PIN(" RX Pin: ", rx_pin_);
108  if (this->rx_pin_ != nullptr) {
109  ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
110  }
111  ESP_LOGCONFIG(TAG, " Baud Rate: %u baud", this->baud_rate_);
112  ESP_LOGCONFIG(TAG, " Data Bits: %u", this->data_bits_);
113  ESP_LOGCONFIG(TAG, " Parity: %s", LOG_STR_ARG(parity_to_str(this->parity_)));
114  ESP_LOGCONFIG(TAG, " Stop bits: %u", this->stop_bits_);
115  this->check_logger_conflict();
116 }
117 
118 void LibreTinyUARTComponent::write_array(const uint8_t *data, size_t len) {
119  this->serial_->write(data, len);
120 #ifdef USE_UART_DEBUGGER
121  for (size_t i = 0; i < len; i++) {
122  this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
123  }
124 #endif
125 }
126 
128  if (!this->check_read_timeout_())
129  return false;
130  *data = this->serial_->peek();
131  return true;
132 }
133 
134 bool LibreTinyUARTComponent::read_array(uint8_t *data, size_t len) {
135  if (!this->check_read_timeout_(len))
136  return false;
137  this->serial_->readBytes(data, len);
138 #ifdef USE_UART_DEBUGGER
139  for (size_t i = 0; i < len; i++) {
140  this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
141  }
142 #endif
143  return true;
144 }
145 
146 int LibreTinyUARTComponent::available() { return this->serial_->available(); }
148  ESP_LOGVV(TAG, " Flushing...");
149  this->serial_->flush();
150 }
151 
153 #ifdef USE_LOGGER
154  if (this->hardware_idx_ == -1 || logger::global_logger->get_baud_rate() == 0) {
155  return;
156  }
157 
158  if (this->serial_ == logger::global_logger->get_hw_serial()) {
159  ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
160  "disable logging over the serial port by setting logger->baud_rate to 0.");
161  }
162 #endif
163 }
164 
165 } // namespace uart
166 } // namespace esphome
167 
168 #endif // USE_LIBRETINY
uint32_t get_baud_rate() const
void write_array(const uint8_t *data, size_t len) override
virtual uint8_t get_pin() const =0
Logger * global_logger
Definition: logger.cpp:198
const char *const TAG
Definition: spi.cpp:8
bool read_array(uint8_t *data, size_t len) override
std::string size_t len
Definition: helpers.h:293
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
Implementation of SPI Controller mode.
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