ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart_mux.h
Go to the documentation of this file.
1#pragma once
2#if defined(USE_ESP32_VARIANT_ESP32P4) || defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
7
8namespace esphome::uart_mux {
9
14class UARTMux final : public uart::UARTComponent, public Component {
15 public:
16 explicit UARTMux(cdc_acm_uart::CDCACMUARTBridge *bridge) : uart_(bridge->get_uart_parent()), bridge_(bridge) {}
17
18 void setup() override;
19 void loop() override;
20 void dump_config() override;
21 // Between the hardware UART (BUS) and its consumers (modbus is BUS - 1): the
22 // mirrored framing must exist before anything reads it from us.
23 float get_setup_priority() const override { return setup_priority::BUS - 0.5f; }
24
26 void set_start_local(bool start_local) { this->start_local_ = start_local; }
27
29 void select_local();
31 void select_bridge();
32 bool is_local() const { return this->route_ == Route::ROUTE_LOCAL; }
33
34 // uart::UARTComponent: forwarded while routed locally, inert otherwise.
35 void write_array(const uint8_t *data, size_t len) override;
36 bool peek_byte(uint8_t *data) override { return this->is_local() && this->uart_->peek_byte(data); }
37 bool read_array(uint8_t *data, size_t len) override { return this->is_local() && this->uart_->read_array(data, len); }
38 size_t available() override { return this->is_local() ? this->uart_->available() : 0; }
42 bool is_connected() override { return this->is_local(); }
43 // Ignored: the bridge's tasks block inside the driver, and reinstalling it would
44 // pull it out from under them. The framing is the hardware UART's to change.
45 void load_settings(bool dump_config) override;
46 using UARTComponent::load_settings;
47
48 protected:
49 enum class Route : uint8_t {
51 ROUTE_PENDING_LOCAL, // pause() requested; the bridge may still be on the bus
53 };
54
55 // The hardware UART's settings as configured. Taken once at setup, before the
56 // bridge can overwrite the live fields with a host's line coding.
66
67 void check_logger_conflict() override {}
68 // Publish settings_ through the UARTComponent getters.
69 void apply_settings_();
70
75 bool start_local_{false};
77};
78
79} // namespace esphome::uart_mux
80#endif
ESP-IDF UART driver wrapper.
bool peek_byte(uint8_t *data) override
bool read_array(uint8_t *data, size_t len) override
void load_settings()
Load the UART settings.
Shares one hardware UART between a CDC-ACM UART bridge and local consumers.
Definition uart_mux.h:14
size_t available() override
Definition uart_mux.h:38
void check_logger_conflict() override
Definition uart_mux.h:67
UARTMux(cdc_acm_uart::CDCACMUARTBridge *bridge)
Definition uart_mux.h:16
void select_local()
Pause the bridge and route the UART to local consumers once it has stopped.
Definition uart_mux.cpp:74
float get_setup_priority() const override
Definition uart_mux.h:23
bool read_array(uint8_t *data, size_t len) override
Definition uart_mux.h:37
bool peek_byte(uint8_t *data) override
Definition uart_mux.h:36
void setup() override
Definition uart_mux.cpp:9
void set_start_local(bool start_local)
Route locally at boot instead of leaving the UART with the bridge.
Definition uart_mux.h:26
void write_array(const uint8_t *data, size_t len) override
Definition uart_mux.cpp:106
bool is_connected() override
Definition uart_mux.h:42
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
uart::UARTFlushResult flush() override
Definition uart_mux.h:39
void dump_config() override
Definition uart_mux.cpp:43
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:39
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
const void size_t len
Definition hal.h:68
static void uint32_t
uart::UARTParityOptions parity
Definition uart_mux.h:64