[uart] Keep the swapped UART0 pins on an ESP8266 reload (#20463)

This commit is contained in:
Bascht74
2026-10-12 15:09:32 -10:00
committed by GitHub
parent afedddafe2
commit 58188305c4
3 changed files with 16 additions and 1 deletions
@@ -24,6 +24,8 @@ static constexpr uint32_t RX_TICKS_PER_SECOND = 16000000;
__attribute__((always_inline)) static inline uint32_t rx_now() { return micros() << 4; }
bool ESP8266UartComponent::serial0_in_use = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
bool ESP8266UartComponent::serial0_swapped = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
uint32_t ESP8266UartComponent::get_config() {
uint32_t config = 0;
@@ -111,6 +113,7 @@ void ESP8266UartComponent::setup() {
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
this->hw_serial_->swap();
ESP8266UartComponent::serial0_in_use = true;
ESP8266UartComponent::serial0_swapped = true;
} else
#endif // USE_ESP8266_UART_SERIAL
#ifdef USE_ESP8266_UART_SERIAL1
@@ -133,6 +136,11 @@ void ESP8266UartComponent::load_settings(bool dump_config) {
SerialConfig config = static_cast<SerialConfig>(get_config());
this->hw_serial_->begin(this->baud_rate_, config);
this->hw_serial_->setRxBufferSize(this->rx_buffer_size_);
#ifdef USE_ESP8266_UART_SERIAL
// begin() puts UART0 back on GPIO1/GPIO3, so swap it again like setup() did for GPIO15/GPIO13
if (this->hw_serial_ == &Serial && ESP8266UartComponent::serial0_swapped)
this->hw_serial_->swap();
#endif // USE_ESP8266_UART_SERIAL
} else {
this->sw_serial_->setup(this->tx_pin_, this->rx_pin_, this->baud_rate_, this->stop_bits_, this->data_bits_,
this->parity_, this->rx_buffer_size_);
@@ -92,7 +92,8 @@ class ESP8266UartComponent final : public UARTComponent, public Component {
ESP8266SoftwareSerial *sw_serial_{nullptr};
private:
static bool serial0_in_use; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static bool serial0_in_use; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static bool serial0_swapped; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
};
} // namespace esphome::uart
@@ -7,6 +7,8 @@ esphome:
- uart.write:
id: uart_id
data: [0x00, 0x20, 0x42]
- lambda: |-
id(uart_swapped).load_settings(false);
uart:
- id: uart_id
@@ -23,6 +25,10 @@ uart:
baud_rate: 115200
debug:
debug_prefix: "[UART1] "
- id: uart_swapped
tx_pin: 15
rx_pin: 13
baud_rate: 9600
switch:
- platform: uart