ESPHome 2025.10.3
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 if (this->rx_pin_) {
50 this->rx_pin_->setup();
51 }
52 if (this->tx_pin_ && this->rx_pin_ != this->tx_pin_) {
53 this->tx_pin_->setup();
54 }
55
56 int8_t tx_pin = tx_pin_ == nullptr ? -1 : tx_pin_->get_pin();
57 int8_t rx_pin = rx_pin_ == nullptr ? -1 : rx_pin_->get_pin();
58 bool tx_inverted = tx_pin_ != nullptr && tx_pin_->is_inverted();
59 bool rx_inverted = rx_pin_ != nullptr && rx_pin_->is_inverted();
60
61 if (false)
62 return;
63#if LT_HW_UART0
64 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL0_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL0_RX)) {
65 this->serial_ = &Serial0;
66 this->hardware_idx_ = 0;
67 }
68#endif
69#if LT_HW_UART1
70 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL1_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL1_RX)) {
71 this->serial_ = &Serial1;
72 this->hardware_idx_ = 1;
73 }
74#endif
75#if LT_HW_UART2
76 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL2_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL2_RX)) {
77 this->serial_ = &Serial2;
78 this->hardware_idx_ = 2;
79 }
80#endif
81 else {
82#if LT_ARD_HAS_SOFTSERIAL
83 this->serial_ = new SoftwareSerial(rx_pin, tx_pin, rx_inverted || tx_inverted);
84#else
85 this->serial_ = &Serial;
86 ESP_LOGE(TAG, " SoftwareSerial is not implemented for this chip. Only hardware pins are supported:");
87#if LT_HW_UART0
88 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL0_TX, PIN_SERIAL0_RX);
89#endif
90#if LT_HW_UART1
91 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL1_TX, PIN_SERIAL1_RX);
92#endif
93#if LT_HW_UART2
94 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL2_TX, PIN_SERIAL2_RX);
95#endif
96 this->mark_failed();
97 return;
98#endif
99 }
100
101 this->serial_->begin(this->baud_rate_, get_config());
102}
103
105 bool is_software = this->hardware_idx_ == -1;
106 ESP_LOGCONFIG(TAG, "UART Bus:");
107 ESP_LOGCONFIG(TAG, " Type: %s", UART_TYPE[is_software]);
108 if (!is_software) {
109 ESP_LOGCONFIG(TAG, " Port number: %d", this->hardware_idx_);
110 }
111 LOG_PIN(" TX Pin: ", tx_pin_);
112 LOG_PIN(" RX Pin: ", rx_pin_);
113 if (this->rx_pin_ != nullptr) {
114 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
115 }
116 ESP_LOGCONFIG(TAG,
117 " Baud Rate: %u baud\n"
118 " Data Bits: %u\n"
119 " Parity: %s\n"
120 " Stop bits: %u",
121 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
122 this->check_logger_conflict();
123}
124
125void LibreTinyUARTComponent::write_array(const uint8_t *data, size_t len) {
126 this->serial_->write(data, len);
127#ifdef USE_UART_DEBUGGER
128 for (size_t i = 0; i < len; i++) {
129 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
130 }
131#endif
132}
133
135 if (!this->check_read_timeout_())
136 return false;
137 *data = this->serial_->peek();
138 return true;
139}
140
141bool LibreTinyUARTComponent::read_array(uint8_t *data, size_t len) {
142 if (!this->check_read_timeout_(len))
143 return false;
144 this->serial_->readBytes(data, len);
145#ifdef USE_UART_DEBUGGER
146 for (size_t i = 0; i < len; i++) {
147 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
148 }
149#endif
150 return true;
151}
152
153int LibreTinyUARTComponent::available() { return this->serial_->available(); }
155 ESP_LOGVV(TAG, " Flushing");
156 this->serial_->flush();
157}
158
160#ifdef USE_LOGGER
161 if (this->hardware_idx_ == -1 || logger::global_logger->get_baud_rate() == 0) {
162 return;
163 }
164
166 ESP_LOGW(TAG, " You're using the same serial port for logging and the UART component. Please "
167 "disable logging over the serial port by setting logger->baud_rate to 0.");
168 }
169#endif
170}
171
172} // namespace uart
173} // namespace esphome
174
175#endif // USE_LIBRETINY
virtual void mark_failed()
Mark this component as failed.
virtual void setup()=0
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_
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