ESPHome 2025.9.0-dev
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 if (false)
55 return;
56#if LT_HW_UART0
57 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL0_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL0_RX)) {
58 this->serial_ = &Serial0;
59 this->hardware_idx_ = 0;
60 }
61#endif
62#if LT_HW_UART1
63 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL1_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL1_RX)) {
64 this->serial_ = &Serial1;
65 this->hardware_idx_ = 1;
66 }
67#endif
68#if LT_HW_UART2
69 else if ((tx_pin == -1 || tx_pin == PIN_SERIAL2_TX) && (rx_pin == -1 || rx_pin == PIN_SERIAL2_RX)) {
70 this->serial_ = &Serial2;
71 this->hardware_idx_ = 2;
72 }
73#endif
74 else {
75#if LT_ARD_HAS_SOFTSERIAL
76 this->serial_ = new SoftwareSerial(rx_pin, tx_pin, rx_inverted || tx_inverted);
77#else
78 this->serial_ = &Serial;
79 ESP_LOGE(TAG, " SoftwareSerial is not implemented for this chip. Only hardware pins are supported:");
80#if LT_HW_UART0
81 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL0_TX, PIN_SERIAL0_RX);
82#endif
83#if LT_HW_UART1
84 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL1_TX, PIN_SERIAL1_RX);
85#endif
86#if LT_HW_UART2
87 ESP_LOGE(TAG, " TX=%u, RX=%u", PIN_SERIAL2_TX, PIN_SERIAL2_RX);
88#endif
89 this->mark_failed();
90 return;
91#endif
92 }
93
94 this->serial_->begin(this->baud_rate_, get_config());
95}
96
98 bool is_software = this->hardware_idx_ == -1;
99 ESP_LOGCONFIG(TAG, "UART Bus:");
100 ESP_LOGCONFIG(TAG, " Type: %s", UART_TYPE[is_software]);
101 if (!is_software) {
102 ESP_LOGCONFIG(TAG, " Port number: %d", this->hardware_idx_);
103 }
104 LOG_PIN(" TX Pin: ", tx_pin_);
105 LOG_PIN(" RX Pin: ", rx_pin_);
106 if (this->rx_pin_ != nullptr) {
107 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
108 }
109 ESP_LOGCONFIG(TAG,
110 " Baud Rate: %u baud\n"
111 " Data Bits: %u\n"
112 " Parity: %s\n"
113 " Stop bits: %u",
114 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
115 this->check_logger_conflict();
116}
117
118void 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
134bool 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
146int 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
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
virtual void mark_failed()
Mark this component as failed.
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:283
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:279