ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
uart_component_esp8266.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP8266
4
5#include <HardwareSerial.h>
6#include <vector>
8#include "esphome/core/hal.h"
9#include "esphome/core/log.h"
12#include "uart_component.h"
13
14namespace esphome::uart {
15
17 public:
18 void setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_pin, uint32_t baud_rate, uint8_t stop_bits,
19 uint32_t data_bits, UARTParityOptions parity, size_t rx_buffer_size);
20
21 uint8_t read_byte();
22 uint8_t peek_byte();
23
24 void flush();
25
26 void write_byte(uint8_t data);
27
28 size_t available();
29
32 bool locks_whole_frame() const { return this->lock_frame_ || blocking_rx_count != 0; }
33
34 protected:
36 static void gpio_intr(ESP8266SoftwareSerial *arg);
37 static void gpio_intr_edge(ESP8266SoftwareSerial *arg);
38
39 void wait_(uint32_t *wait, const uint32_t &start, uint32_t bit_time);
40 bool read_bit_(uint32_t *wait, const uint32_t &start, uint32_t bit_time);
41 void write_bit_(bool bit, uint32_t *wait, const uint32_t &start, uint32_t bit_time);
42
44 void ESPHOME_ALWAYS_INLINE rx_sync_() {
45 if (this->rx_.pending())
47 }
48
49 // Members ordered largest to smallest to minimize padding
57 uint8_t stop_bits_;
58 uint8_t data_bits_;
59 bool lock_frame_{false};
60 bool blocking_rx_{false}; // this instance's RX reads whole bytes in its interrupt
61 static inline uint8_t blocking_rx_count = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
62};
63
64class ESP8266UartComponent final : public UARTComponent, public Component {
65 public:
66 void setup() override;
67 void dump_config() override;
68 float get_setup_priority() const override { return setup_priority::BUS; }
69
70 void write_array(const uint8_t *data, size_t len) override;
71
72 bool peek_byte(uint8_t *data) override;
73 bool read_array(uint8_t *data, size_t len) override;
74
75 size_t available() override;
76 size_t available_for_write() override;
77 UARTFlushResult flush() override;
78
80
93 void load_settings(bool dump_config) override;
94 using UARTComponent::load_settings; // also bring in the no-arg overload for convenience
95
96 protected:
97 void check_logger_conflict() override;
98
99 HardwareSerial *hw_serial_{nullptr};
101
102 private:
103 static bool serial0_in_use; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
104 static bool serial0_swapped; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
105};
106
107} // namespace esphome::uart
108#endif // USE_ESP8266
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio_pin.h:98
void write_bit_(bool bit, uint32_t *wait, const uint32_t &start, uint32_t bit_time)
static void gpio_intr_edge(ESP8266SoftwareSerial *arg)
bool read_bit_(uint32_t *wait, const uint32_t &start, uint32_t bit_time)
void wait_(uint32_t *wait, const uint32_t &start, uint32_t bit_time)
bool locks_whole_frame() const
TX holds interrupts off for each byte up to the edge into its trailing run of 1 bits,...
static void gpio_intr(ESP8266SoftwareSerial *arg)
Above SW_SERIAL_EDGE_MODE_MAX_BAUD; reads the whole byte in the ISR.
void load_settings()
Load the UART settings.
bool read_array(uint8_t *data, size_t len) override
void write_array(const uint8_t *data, size_t len) override
Software serial RX decoder: on_edge() counts bits from the time between edges; finalize() completes a...
void load_settings()
Load the UART settings.
void setup()
constexpr float BUS
For communication buses like i2c/spi.
Definition component.h:39
UARTFlushResult
Result of a flush() call.
const void size_t len
Definition hal.h:68
static void uint32_t