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