ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart_mux.cpp
Go to the documentation of this file.
1#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
2#include "uart_mux.h"
3#include "esphome/core/log.h"
4
6
7ESPHOME_LOG_TAG(TAG, "uart_mux");
8
10 // A failed UART never assigned its port; nothing behind the mux can work.
11 if (this->uart_->is_failed()) {
12 ESP_LOGE(TAG, "UART parent failed; aborting");
13 this->mark_failed();
14 return;
15 }
16
17 this->settings_ = {
20 this->uart_->get_parity(),
21 };
22 this->apply_settings_();
23
24 if (this->start_local_) {
25 this->select_local();
26 } else {
27 // loop() only completes hand-offs; the bridge keeps the UART until an action.
28 this->disable_loop();
29 }
30}
31
33 if (!this->bridge_->is_paused()) {
34 return;
35 }
36 // Bytes that arrived during the hand-off belong to neither owner.
37 this->uart_->flush_input();
39 ESP_LOGD(TAG, "UART routed to local consumers");
40 this->disable_loop();
41}
42
44 ESP_LOGCONFIG(TAG,
45 "UART Mux:\n"
46 " Start local: %s\n"
47 " Route: %s",
48 YESNO(this->start_local_),
49 this->route_ == Route::ROUTE_LOCAL ? LOG_STR_LITERAL("local")
50 : this->route_ == Route::ROUTE_PENDING_LOCAL ? LOG_STR_LITERAL("pending local")
51 : LOG_STR_LITERAL("bridge"));
52}
53
54void UARTMux::load_settings(bool dump_config) {
55 if (!this->load_settings_warned_) {
56 this->load_settings_warned_ = true;
57 ESP_LOGW(TAG, "load_settings() ignored; change the framing on the hardware UART instead");
58 }
59 // Undo whatever the caller set on us. Not re-sampled from the live UART, whose
60 // fields carry the host's line coding while the bridge owns the bus.
61 this->apply_settings_();
62}
63
73
75 if (this->route_ != Route::ROUTE_BRIDGE) {
76 return;
77 }
78 ESP_LOGD(TAG, "Pausing bridge to route UART locally");
79 this->bridge_->pause();
81 this->enable_loop();
82}
83
85 if (this->route_ == Route::ROUTE_BRIDGE) {
86 return;
87 }
88 // A bridge that failed setup() has no worker tasks; handing it the bus would kill
89 // the UART in both directions.
90 if (this->bridge_->is_failed()) {
91 ESP_LOGW(TAG, "Bridge failed; keeping the UART routed locally");
92 return;
93 }
94 // While the pause is still pending the bridge's RX task may be inside
95 // uart_read_bytes() on this port, and nothing local has run, so flush only a
96 // completed hand-off.
97 if (this->route_ == Route::ROUTE_LOCAL) {
98 this->uart_->flush_input();
99 }
101 ESP_LOGD(TAG, "UART routed to bridge");
102 this->bridge_->resume();
103 this->disable_loop();
104}
105
106void UARTMux::write_array(const uint8_t *data, size_t len) {
107 if (!this->is_local()) {
108 ESP_LOGV(TAG, "Dropping %zu bytes: UART routed to bridge", len);
109 return;
110 }
111 this->uart_->write_array(data, len);
112}
113
114} // namespace esphome::uart_mux
115#endif
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:274
void enable_loop()
Enable this component's loop.
Definition component.h:248
void disable_loop()
Disable this component's loop.
void resume()
Re-apply the host's line coding and line state, then resume forwarding.
bool is_paused() const
True once both worker tasks are off the bus and the configured framing is restored.
void pause()
Stop forwarding in both directions and hand the UART back to its configured framing,...
void flush_input()
Discard everything received so far: the peek cache and the driver's RX buffer.
void write_array(const uint8_t *data, size_t len) override
UARTParityOptions get_parity() const
void load_settings()
Load the UART settings.
void select_local()
Pause the bridge and route the UART to local consumers once it has stopped.
Definition uart_mux.cpp:74
void setup() override
Definition uart_mux.cpp:9
void write_array(const uint8_t *data, size_t len) override
Definition uart_mux.cpp:106
void select_bridge()
Route the UART back to the bridge.
Definition uart_mux.cpp:84
cdc_acm_uart::CDCACMUARTBridge * bridge_
Definition uart_mux.h:72
uart::IDFUARTComponent * uart_
Definition uart_mux.h:71
void dump_config() override
Definition uart_mux.cpp:43
ESPHOME_LOG_TAG(TAG, "uart_mux")
const void size_t len
Definition hal.h:68
uart::UARTParityOptions parity
Definition uart_mux.h:64