mirror of
https://github.com/esphome/esphome.git
synced 2026-05-31 07:57:40 +08:00
[uart] Allow hardware UART with single pin on RP2040 (#14725)
This commit is contained in:
@@ -105,15 +105,34 @@ void RP2040UartComponent::setup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Determine which hardware UART to use. A pin that is not specified
|
||||||
|
// should not prevent hardware UART selection — one-way UART is valid.
|
||||||
|
// When both pins are configured, both must be HW-capable and agree on UART number.
|
||||||
|
// When only one pin is configured (nullptr other), use that pin's HW UART.
|
||||||
|
// If a pin is configured but not HW-capable (inverted/invalid), fall back to SerialPIO.
|
||||||
|
int8_t hw_uart = -1;
|
||||||
|
const bool tx_configured = (this->tx_pin_ != nullptr);
|
||||||
|
const bool rx_configured = (this->rx_pin_ != nullptr);
|
||||||
|
|
||||||
|
if (tx_configured && rx_configured) {
|
||||||
|
// Both pins configured — both must map to the same hardware UART
|
||||||
|
if (tx_hw != -1 && rx_hw != -1 && tx_hw == rx_hw) {
|
||||||
|
hw_uart = tx_hw;
|
||||||
|
}
|
||||||
|
} else if (tx_configured) {
|
||||||
|
hw_uart = tx_hw;
|
||||||
|
} else if (rx_configured) {
|
||||||
|
hw_uart = rx_hw;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef USE_LOGGER
|
#ifdef USE_LOGGER
|
||||||
if (tx_hw == rx_hw && logger::global_logger->get_uart() == tx_hw) {
|
if (hw_uart != -1 && logger::global_logger->get_uart() == hw_uart) {
|
||||||
ESP_LOGD(TAG, "Using SerialPIO as UART%d is taken by the logger", tx_hw);
|
ESP_LOGD(TAG, "Using SerialPIO as UART%d is taken by the logger", hw_uart);
|
||||||
tx_hw = -1;
|
hw_uart = -1;
|
||||||
rx_hw = -1;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (tx_hw == -1 || rx_hw == -1 || tx_hw != rx_hw) {
|
if (hw_uart == -1) {
|
||||||
ESP_LOGV(TAG, "Using SerialPIO");
|
ESP_LOGV(TAG, "Using SerialPIO");
|
||||||
pin_size_t tx = this->tx_pin_ == nullptr ? NOPIN : this->tx_pin_->get_pin();
|
pin_size_t tx = this->tx_pin_ == nullptr ? NOPIN : this->tx_pin_->get_pin();
|
||||||
pin_size_t rx = this->rx_pin_ == nullptr ? NOPIN : this->rx_pin_->get_pin();
|
pin_size_t rx = this->rx_pin_ == nullptr ? NOPIN : this->rx_pin_->get_pin();
|
||||||
@@ -127,12 +146,14 @@ void RP2040UartComponent::setup() {
|
|||||||
} else {
|
} else {
|
||||||
ESP_LOGV(TAG, "Using Hardware Serial");
|
ESP_LOGV(TAG, "Using Hardware Serial");
|
||||||
SerialUART *serial;
|
SerialUART *serial;
|
||||||
if (tx_hw == 0) {
|
if (hw_uart == 0) {
|
||||||
serial = &Serial1;
|
serial = &Serial1;
|
||||||
} else {
|
} else {
|
||||||
serial = &Serial2;
|
serial = &Serial2;
|
||||||
}
|
}
|
||||||
|
if (this->tx_pin_ != nullptr)
|
||||||
serial->setTX(this->tx_pin_->get_pin());
|
serial->setTX(this->tx_pin_->get_pin());
|
||||||
|
if (this->rx_pin_ != nullptr)
|
||||||
serial->setRX(this->rx_pin_->get_pin());
|
serial->setRX(this->rx_pin_->get_pin());
|
||||||
serial->setFIFOSize(this->rx_buffer_size_);
|
serial->setFIFOSize(this->rx_buffer_size_);
|
||||||
serial->begin(this->baud_rate_, config);
|
serial->begin(this->baud_rate_, config);
|
||||||
|
|||||||
@@ -23,3 +23,6 @@ uart:
|
|||||||
baud_rate: 115200
|
baud_rate: 115200
|
||||||
debug:
|
debug:
|
||||||
debug_prefix: "[UART1] "
|
debug_prefix: "[UART1] "
|
||||||
|
- id: uart_rx_only
|
||||||
|
rx_pin: 17
|
||||||
|
baud_rate: 1200
|
||||||
|
|||||||
Reference in New Issue
Block a user