[uart] Add IDFUARTComponent::flush_input() and use it in uart_mux (#19336)

Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Keith Burzinski
2026-09-15 21:57:06 -05:00
committed by GitHub
co-authored by Claude Fable 5.1
parent 47be8c811f
commit a264093d86
3 changed files with 8 additions and 15 deletions
@@ -37,6 +37,12 @@ class IDFUARTComponent final : public UARTComponent, public Component {
uint8_t get_hw_serial_number() { return this->uart_num_; }
/// Discard everything received so far: the peek cache and the driver's RX buffer.
void flush_input() {
this->has_peek_ = false;
uart_flush_input(this->uart_num_);
}
/**
* Load the UART with the current settings.
* @param dump_config (Optional, default `true`): True for displaying new settings or
+2 -14
View File
@@ -2,8 +2,6 @@
#include "uart_mux.h"
#include "esphome/core/log.h"
#include "driver/uart.h"
namespace esphome::uart_mux {
static const char *const TAG = "uart_mux";
@@ -36,7 +34,7 @@ void UARTMux::loop() {
return;
}
// Bytes that arrived during the hand-off belong to neither owner.
this->flush_input_();
this->uart_->flush_input();
this->route_ = Route::ROUTE_LOCAL;
ESP_LOGD(TAG, "UART routed to local consumers");
this->disable_loop();
@@ -97,7 +95,7 @@ void UARTMux::select_bridge() {
// uart_read_bytes() on this port, and nothing local has run, so flush only a
// completed hand-off.
if (this->route_ == Route::ROUTE_LOCAL) {
this->flush_input_();
this->uart_->flush_input();
}
this->route_ = Route::ROUTE_BRIDGE;
ESP_LOGD(TAG, "UART routed to bridge");
@@ -105,16 +103,6 @@ void UARTMux::select_bridge() {
this->disable_loop();
}
void UARTMux::flush_input_() {
// Drain the UART component's one-byte peek cache first: the driver flush does not
// clear it, and draining afterwards could discard a freshly arrived byte instead.
uint8_t discard;
if (this->uart_->available() > 0) {
this->uart_->read_byte(&discard);
}
uart_flush_input(static_cast<uart_port_t>(this->uart_->get_hw_serial_number()));
}
void UARTMux::write_array(const uint8_t *data, size_t len) {
if (!this->is_local()) {
ESP_LOGV(TAG, "Dropping %zu bytes: UART routed to bridge", len);
-1
View File
@@ -66,7 +66,6 @@ class UARTMux final : public uart::UARTComponent, public Component {
};
void check_logger_conflict() override {}
void flush_input_();
// Publish settings_ through the UARTComponent getters.
void apply_settings_();