ESPHome 2025.10.4
Loading...
Searching...
No Matches
uart_component_libretiny.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
2
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
17namespace esphome {
18namespace uart {
19
20static const char *const TAG = "uart.lt";
21
22static 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 int8_t tx_pin = tx_pin_ == nullptr ? -1 : tx_pin_->get_pin();
50 int8_t rx_pin = rx_pin_ == nullptr ? -1 : rx_pin_->get_pin();
51 bool tx_inverted = tx_pin_ != nullptr && tx_pin_->is_inverted();
52 bool rx_inverted = rx_pin_ != nullptr && rx_pin_->is_inverted();
53
54 auto shouldFallbackToSoftwareSerial = [&]() -> bool {
55 auto hasFlags = [](InternalGPIOPin *pin, const gpio::Flags mask) -> bool {
56 return pin && pin->get_flags() & mask != gpio::Flags::FLAG_NONE;
57 };
60#if LT_ARD_HAS_SOFTSERIAL
61 ESP_LOGI(TAG, "Pins has flags set. Using Software Serial");
62 return true;
63#else
64 ESP_LOGW(TAG, "Pin flags are set but not supported for hardware serial. Ignoring");
65#endif
66 }
67 return false;
68 };
69
70 if (false)
71 return;
72#if LT_HW_UART0
73 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL0_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL0_RX) &&
74 !shouldFallbackToSoftwareSerial()) {
75 this->serial_ = &Serial0;
76 this->hardware_idx_ = 0;
77 }
78#endif
79#if LT_HW_UART1
80 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL1_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL1_RX) &&
81 !shouldFallbackToSoftwareSerial()) {
82 this->serial_ = &Serial1;
83 this->hardware_idx_ = 1;
84 }
85#endif
86#if LT_HW_UART2
87 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL2_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL2_RX) &&
88 !shouldFallbackToSoftwareSerial()) {
89 this->serial_ = &Serial2;
90 this->hardware_idx_ = 2;
91 }
92#endif
93 else {
94#if LT_ARD_HAS_SOFTSERIAL
95 if (this->rx_pin_) {
96 this->rx_pin_->setup();
97 }
98 if (this->tx_pin_ && this->rx_pin_ != this->tx_pin_) {
99 this->tx_pin_->setup();
100 }
101 this->serial_ = new SoftwareSerial(rx_pin, tx_pin, rx_inverted || tx_inverted);
102#else
103 this->serial_ = &Serial;
104 ESP_LOGE(TAG, " SoftwareSerial is not implemented for this chip. Only hardware pins are supported:");
105#if LT_HW_UART0
106 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL0_TX, PIN_SERIAL0_RX);
107#endif
108#if LT_HW_UART1
109 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL1_TX, PIN_SERIAL1_RX);
110#endif
111#if LT_HW_UART2
112 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL2_TX, PIN_SERIAL2_RX);
113#endif
114 this->mark_failed();
115 return;
116#endif
117 }
118
119 this->serial_->begin(this->baud_rate_, get_config());
120}
121
123 bool is_software = this->hardware_idx_ == -1;
124 ESP_LOGCONFIG(TAG, "UART Bus:");
125 ESP_LOGCONFIG(TAG, " Type: %s", UART_TYPE[is_software]);
126 if (!is_software) {
127 ESP_LOGCONFIG(TAG, " Port number: %d", this->hardware_idx_);
128 }
129 LOG_PIN(" TX Pin: ", tx_pin_);
130 LOG_PIN(" RX Pin: ", rx_pin_);
131 if (this->rx_pin_ != nullptr) {
132 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
133 }
134 ESP_LOGCONFIG(TAG,
135 " Baud Rate: %u baud\n"
136 " Data Bits: %u\n"
137 " Parity: %s\n"
138 " Stop bits: %u",
139 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
140 this->check_logger_conflict();
141}
142
143void LibreTinyUARTComponent::write_array(const uint8_t *data, size_t len) {
144 this->serial_->write(data, len);
145#ifdef USE_UART_DEBUGGER
146 for (size_t i = 0; i < len; i++) {
147 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
148 }
149#endif
150}
151
153 if (!this->check_read_timeout_())
154 return false;
155 *data = this->serial_->peek();
156 return true;
157}
158
159bool LibreTinyUARTComponent::read_array(uint8_t *data, size_t len) {
160 if (!this->check_read_timeout_(len))
161 return false;
162 this->serial_->readBytes(data, len);
163#ifdef USE_UART_DEBUGGER
164 for (size_t i = 0; i < len; i++) {
165 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
166 }
167#endif
168 return true;
169}
170
171int LibreTinyUARTComponent::available() { return this->serial_->available(); }
173 ESP_LOGVV(TAG, " Flushing");
174 this->serial_->flush();
175}
176
178#ifdef USE_LOGGER
179 if (this->hardware_idx_ == -1 || logger::global_logger->get_baud_rate() == 0) {
180 return;
181 }
182
184 ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
185 "disable logging over the serial port by setting logger->baud_rate to 0.");
186 }
187#endif
188}
189
190} // namespace uart
191} // namespace esphome
192
193#endif // USE_LIBRETINY
virtual void mark_failed()
Mark this component as failed.
virtual void setup()=0
virtual gpio::Flags get_flags() const =0
Retrieve GPIO pin flags.
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
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_
@ FLAG_OPEN_DRAIN
Definition gpio.h:20
@ FLAG_NONE
Definition gpio.h:17
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_PULLDOWN
Definition gpio.h:22
Logger * global_logger
Definition logger.cpp:294
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
std::string size_t len
Definition helpers.h:304