ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
uart_component_rp2.cpp
Go to the documentation of this file.
1#ifdef USE_RP2
6#include "esphome/core/log.h"
7
8#include <hardware/uart.h>
9
10#ifdef USE_LOGGER
12#endif
13
14namespace esphome::uart {
15
16ESPHOME_LOG_TAG(TAG, "uart");
17
18// GPIO32-47 (RP2350B) and GPIO64 (CYW43) are in no mask; a 32-bit shift by them is undefined.
19static constexpr bool pin_in_mask(uint8_t pin, uint32_t mask) { return pin < 32 && (mask & (1U << pin)) != 0; }
20
22 uint16_t config = 0;
23
24 if (this->parity_ == UART_CONFIG_PARITY_NONE) {
25 config |= UART_PARITY_NONE;
26 } else if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
27 config |= UART_PARITY_EVEN;
28 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
29 config |= UART_PARITY_ODD;
30 }
31
32 switch (this->data_bits_) {
33 case 5:
34 config |= SERIAL_DATA_5;
35 break;
36 case 6:
37 config |= SERIAL_DATA_6;
38 break;
39 case 7:
40 config |= SERIAL_DATA_7;
41 break;
42 case 8:
43 config |= SERIAL_DATA_8;
44 break;
45 }
46
47 if (this->stop_bits_ == 1) {
48 config |= SERIAL_STOP_BIT_1;
49 } else {
50 config |= SERIAL_STOP_BIT_2;
51 }
52
53 return config;
54}
55
57 auto setup_pin_if_needed = [](InternalGPIOPin *pin) {
58 if (!pin) {
59 return;
60 }
62 if ((pin->get_flags() & mask) != gpio::Flags::FLAG_NONE) {
63 pin->setup();
64 }
65 };
66
67 setup_pin_if_needed(this->rx_pin_);
68 if (this->rx_pin_ != this->tx_pin_) {
69 setup_pin_if_needed(this->tx_pin_);
70 }
71
72 uint16_t config = get_config();
73
74 constexpr uint32_t valid_tx_uart_0 = __bitset({0, 12, 16, 28});
75 constexpr uint32_t valid_tx_uart_1 = __bitset({4, 8, 20, 24});
76
77 constexpr uint32_t valid_rx_uart_0 = __bitset({1, 13, 17, 29});
78 constexpr uint32_t valid_rx_uart_1 = __bitset({5, 9, 21, 25});
79
80 int8_t tx_hw = -1;
81 int8_t rx_hw = -1;
82
83 if (this->tx_pin_ != nullptr) {
84 if (this->tx_pin_->is_inverted()) {
85 ESP_LOGD(TAG, "An inverted TX pin %u can only be used with SerialPIO", this->tx_pin_->get_pin());
86 } else {
87 if (pin_in_mask(this->tx_pin_->get_pin(), valid_tx_uart_0)) {
88 tx_hw = 0;
89 } else if (pin_in_mask(this->tx_pin_->get_pin(), valid_tx_uart_1)) {
90 tx_hw = 1;
91 } else {
92 ESP_LOGD(TAG, "TX pin %u can only be used with SerialPIO", this->tx_pin_->get_pin());
93 }
94 }
95 }
96
97 if (this->rx_pin_ != nullptr) {
98 if (this->rx_pin_->is_inverted()) {
99 ESP_LOGD(TAG, "An inverted RX pin %u can only be used with SerialPIO", this->rx_pin_->get_pin());
100 } else {
101 if (pin_in_mask(this->rx_pin_->get_pin(), valid_rx_uart_0)) {
102 rx_hw = 0;
103 } else if (pin_in_mask(this->rx_pin_->get_pin(), valid_rx_uart_1)) {
104 rx_hw = 1;
105 } else {
106 ESP_LOGD(TAG, "RX pin %u can only be used with SerialPIO", this->rx_pin_->get_pin());
107 }
108 }
109 }
110
111 // Determine which hardware UART to use. A pin that is not specified
112 // should not prevent hardware UART selection — one-way UART is valid.
113 // When both pins are configured, both must be HW-capable and agree on UART number.
114 // When only one pin is configured (nullptr other), use that pin's HW UART.
115 // If a pin is configured but not HW-capable (inverted/invalid), fall back to SerialPIO.
116 int8_t hw_uart = -1;
117 const bool tx_configured = (this->tx_pin_ != nullptr);
118 const bool rx_configured = (this->rx_pin_ != nullptr);
119
120 if (tx_configured && rx_configured) {
121 // Both pins configured — both must map to the same hardware UART
122 if (tx_hw != -1 && rx_hw != -1 && tx_hw == rx_hw) {
123 hw_uart = tx_hw;
124 }
125 } else if (tx_configured) {
126 hw_uart = tx_hw;
127 } else if (rx_configured) {
128 hw_uart = rx_hw;
129 }
130
131#ifdef USE_LOGGER
132 if (hw_uart != -1 && logger::global_logger->get_uart() == hw_uart) {
133 ESP_LOGD(TAG, "Using SerialPIO as UART%d is taken by the logger", hw_uart);
134 hw_uart = -1;
135 }
136#endif
137
138 // Both Serial classes always allocate the RX queue; one entry is the smallest they take
139 const size_t rx_queue_size = rx_configured ? this->rx_buffer_size_ : 1;
140 if (hw_uart == -1) {
141 ESP_LOGV(TAG, "Using SerialPIO");
142 pin_size_t tx = this->tx_pin_ == nullptr ? NOPIN : this->tx_pin_->get_pin();
143 pin_size_t rx = this->rx_pin_ == nullptr ? NOPIN : this->rx_pin_->get_pin();
144 auto *serial = new SerialPIO(tx, rx, rx_queue_size); // NOLINT(cppcoreguidelines-owning-memory)
145 serial->begin(this->baud_rate_, config);
146 if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted())
147 gpio_set_outover(tx, GPIO_OVERRIDE_INVERT);
148 if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted())
149 gpio_set_inover(rx, GPIO_OVERRIDE_INVERT);
150 this->serial_ = serial;
151 } else {
152 ESP_LOGV(TAG, "Using Hardware Serial");
153 SerialUART *serial = hw_uart == 0 ? &Serial1 : &Serial2;
154 if (this->tx_pin_ != nullptr)
155 serial->setTX(this->tx_pin_->get_pin());
156 if (this->rx_pin_ != nullptr)
157 serial->setRX(this->rx_pin_->get_pin());
158 serial->setFIFOSize(rx_queue_size);
159 serial->begin(this->baud_rate_, config);
160 this->serial_ = serial;
161 this->hw_serial_ = true;
162 }
163}
164
165#ifdef USE_UART_TX_BREAK
167 if (this->tx_pin_ == nullptr || this->serial_ == nullptr)
168 return false;
169 // the pad override works for the hardware UART and for SerialPIO alike
170 const bool inverted = this->tx_pin_->is_inverted();
171 uint32_t value = inverted ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL;
172 if (enable)
173 value = inverted ? GPIO_OVERRIDE_HIGH : GPIO_OVERRIDE_LOW;
174 gpio_set_outover(this->tx_pin_->get_pin(), value);
175 return true;
176}
177#endif
178
180 ESP_LOGCONFIG(TAG, "UART Bus:");
181 LOG_PIN(" TX Pin: ", tx_pin_);
182 LOG_PIN(" RX Pin: ", rx_pin_);
183 if (this->rx_pin_ != nullptr) {
184 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
185 }
186 ESP_LOGCONFIG(TAG,
187 " Baud Rate: %u baud\n"
188 " Data Bits: %u\n"
189 " Parity: %s\n"
190 " Stop bits: %u",
191 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
192 if (this->hw_serial_) {
193 ESP_LOGCONFIG(TAG, " Using hardware serial");
194 } else {
195 ESP_LOGCONFIG(TAG, " Using SerialPIO");
196 }
197}
198
199void RP2UartComponent::write_array(const uint8_t *data, size_t len) {
200 this->serial_->write(data, len);
201#ifdef USE_UART_DEBUGGER
202 for (size_t i = 0; i < len; i++) {
203 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
204 }
205#endif
206}
207bool RP2UartComponent::peek_byte(uint8_t *data) {
208 if (!this->check_read_timeout_())
209 return false;
210 *data = this->serial_->peek();
211 return true;
212}
213bool RP2UartComponent::read_array(uint8_t *data, size_t len) {
214 if (!this->check_read_timeout_(len))
215 return false;
216 this->serial_->readBytes(data, len);
217#ifdef USE_UART_DEBUGGER
218 for (size_t i = 0; i < len; i++) {
219 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
220 }
221#endif
222 return true;
223}
224size_t RP2UartComponent::available() { return this->serial_->available(); }
226 ESP_LOGVV(TAG, " Flushing");
227 this->serial_->flush();
229}
230
231} // namespace esphome::uart
232#endif // USE_RP2
bool peek_byte(uint8_t *data) override
bool set_tx_break(bool enable) override
UARTFlushResult flush() override
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:468
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:43
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
static void uint32_t