ESPHome 2025.12.2
Loading...
Searching...
No Matches
uart_component_rp2040.cpp
Go to the documentation of this file.
1#ifdef USE_RP2040
6#include "esphome/core/log.h"
7
8#include <hardware/uart.h>
9
10#ifdef USE_LOGGER
12#endif
13
14namespace esphome::uart {
15
16static const char *const TAG = "uart.arduino_rp2040";
17
19 uint16_t config = 0;
20
21 if (this->parity_ == UART_CONFIG_PARITY_NONE) {
22 config |= UART_PARITY_NONE;
23 } else if (this->parity_ == UART_CONFIG_PARITY_EVEN) {
24 config |= UART_PARITY_EVEN;
25 } else if (this->parity_ == UART_CONFIG_PARITY_ODD) {
26 config |= UART_PARITY_ODD;
27 }
28
29 switch (this->data_bits_) {
30 case 5:
31 config |= SERIAL_DATA_5;
32 break;
33 case 6:
34 config |= SERIAL_DATA_6;
35 break;
36 case 7:
37 config |= SERIAL_DATA_7;
38 break;
39 case 8:
40 config |= SERIAL_DATA_8;
41 break;
42 }
43
44 if (this->stop_bits_ == 1) {
45 config |= SERIAL_STOP_BIT_1;
46 } else {
47 config |= SERIAL_STOP_BIT_2;
48 }
49
50 return config;
51}
52
54 auto setup_pin_if_needed = [](InternalGPIOPin *pin) {
55 if (!pin) {
56 return;
57 }
59 if ((pin->get_flags() & mask) != gpio::Flags::FLAG_NONE) {
60 pin->setup();
61 }
62 };
63
64 setup_pin_if_needed(this->rx_pin_);
65 if (this->rx_pin_ != this->tx_pin_) {
66 setup_pin_if_needed(this->tx_pin_);
67 }
68
69 uint16_t config = get_config();
70
71 constexpr uint32_t valid_tx_uart_0 = __bitset({0, 12, 16, 28});
72 constexpr uint32_t valid_tx_uart_1 = __bitset({4, 8, 20, 24});
73
74 constexpr uint32_t valid_rx_uart_0 = __bitset({1, 13, 17, 29});
75 constexpr uint32_t valid_rx_uart_1 = __bitset({5, 9, 21, 25});
76
77 int8_t tx_hw = -1;
78 int8_t rx_hw = -1;
79
80 if (this->tx_pin_ != nullptr) {
81 if (this->tx_pin_->is_inverted()) {
82 ESP_LOGD(TAG, "An inverted TX pin %u can only be used with SerialPIO", this->tx_pin_->get_pin());
83 } else {
84 if (((1 << this->tx_pin_->get_pin()) & valid_tx_uart_0) != 0) {
85 tx_hw = 0;
86 } else if (((1 << this->tx_pin_->get_pin()) & valid_tx_uart_1) != 0) {
87 tx_hw = 1;
88 } else {
89 ESP_LOGD(TAG, "TX pin %u can only be used with SerialPIO", this->tx_pin_->get_pin());
90 }
91 }
92 }
93
94 if (this->rx_pin_ != nullptr) {
95 if (this->rx_pin_->is_inverted()) {
96 ESP_LOGD(TAG, "An inverted RX pin %u can only be used with SerialPIO", this->rx_pin_->get_pin());
97 } else {
98 if (((1 << this->rx_pin_->get_pin()) & valid_rx_uart_0) != 0) {
99 rx_hw = 0;
100 } else if (((1 << this->rx_pin_->get_pin()) & valid_rx_uart_1) != 0) {
101 rx_hw = 1;
102 } else {
103 ESP_LOGD(TAG, "RX pin %u can only be used with SerialPIO", this->rx_pin_->get_pin());
104 }
105 }
106 }
107
108#ifdef USE_LOGGER
109 if (tx_hw == rx_hw && logger::global_logger->get_uart() == tx_hw) {
110 ESP_LOGD(TAG, "Using SerialPIO as UART%d is taken by the logger", tx_hw);
111 tx_hw = -1;
112 rx_hw = -1;
113 }
114#endif
115
116 if (tx_hw == -1 || rx_hw == -1 || tx_hw != rx_hw) {
117 ESP_LOGV(TAG, "Using SerialPIO");
118 pin_size_t tx = this->tx_pin_ == nullptr ? SerialPIO::NOPIN : this->tx_pin_->get_pin();
119 pin_size_t rx = this->rx_pin_ == nullptr ? SerialPIO::NOPIN : this->rx_pin_->get_pin();
120 auto *serial = new SerialPIO(tx, rx, this->rx_buffer_size_); // NOLINT(cppcoreguidelines-owning-memory)
121 serial->begin(this->baud_rate_, config);
122 if (this->tx_pin_ != nullptr && this->tx_pin_->is_inverted())
123 gpio_set_outover(tx, GPIO_OVERRIDE_INVERT);
124 if (this->rx_pin_ != nullptr && this->rx_pin_->is_inverted())
125 gpio_set_inover(rx, GPIO_OVERRIDE_INVERT);
126 this->serial_ = serial;
127 } else {
128 ESP_LOGV(TAG, "Using Hardware Serial");
129 SerialUART *serial;
130 if (tx_hw == 0) {
131 serial = &Serial1;
132 } else {
133 serial = &Serial2;
134 }
135 serial->setTX(this->tx_pin_->get_pin());
136 serial->setRX(this->rx_pin_->get_pin());
137 serial->setFIFOSize(this->rx_buffer_size_);
138 serial->begin(this->baud_rate_, config);
139 this->serial_ = serial;
140 this->hw_serial_ = true;
141 }
142}
143
145 ESP_LOGCONFIG(TAG, "UART Bus:");
146 LOG_PIN(" TX Pin: ", tx_pin_);
147 LOG_PIN(" RX Pin: ", rx_pin_);
148 if (this->rx_pin_ != nullptr) {
149 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", this->rx_buffer_size_);
150 }
151 ESP_LOGCONFIG(TAG,
152 " Baud Rate: %u baud\n"
153 " Data Bits: %u\n"
154 " Parity: %s\n"
155 " Stop bits: %u",
156 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
157 if (this->hw_serial_) {
158 ESP_LOGCONFIG(TAG, " Using hardware serial");
159 } else {
160 ESP_LOGCONFIG(TAG, " Using SerialPIO");
161 }
162}
163
164void RP2040UartComponent::write_array(const uint8_t *data, size_t len) {
165 this->serial_->write(data, len);
166#ifdef USE_UART_DEBUGGER
167 for (size_t i = 0; i < len; i++) {
168 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
169 }
170#endif
171}
173 if (!this->check_read_timeout_())
174 return false;
175 *data = this->serial_->peek();
176 return true;
177}
178bool RP2040UartComponent::read_array(uint8_t *data, size_t len) {
179 if (!this->check_read_timeout_(len))
180 return false;
181 this->serial_->readBytes(data, len);
182#ifdef USE_UART_DEBUGGER
183 for (size_t i = 0; i < len; i++) {
184 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
185 }
186#endif
187 return true;
188}
189int RP2040UartComponent::available() { return this->serial_->available(); }
191 ESP_LOGVV(TAG, " Flushing");
192 this->serial_->flush();
193}
194
195} // namespace esphome::uart
196#endif // USE_RP2040
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:297
const char *const TAG
Definition spi.cpp:8
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:32
std::string size_t len
Definition helpers.h:503