Update the patched UART component to ESPHome 2026.11.0

ESPHome 2026.11's tuya component calls uart.new_uart_component(), which the
2026.10.2 copy didn't have, so the build failed. Copy the UART component from
2026.11.0, reapply the PA13/PA14 patch, regenerate the patch file and point
the refs at v1.0.6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-19 00:31:45 +01:00
1 parent bcc6b3a42b
commit 1457e25d98
39 files changed
+907 -304

No files matched your search

+99 -62
View File
@@ -1,6 +1,9 @@
from collections.abc import Coroutine
from dataclasses import dataclass, field
from logging import getLogger
import math
import re
from typing import Any
from esphome import automation, pins
import esphome.codegen as cg
@@ -54,6 +57,7 @@ from esphome.const import (
)
from esphome.core import CORE, ID, CoroPriority, coroutine_with_priority
import esphome.final_validate as fv
from esphome.types import ConfigType
from esphome.yaml_util import make_data_base
_LOGGER = getLogger(__name__)
@@ -117,13 +121,18 @@ HOST_BAUD_RATES = [
]
UARTDevice = uart_ns.class_("UARTDevice")
UARTWriteAction = uart_ns.class_("UARTWriteAction", automation.Action)
UARTDebugger = uart_ns.class_("UARTDebugger", cg.Component, automation.Action)
UARTDummyReceiver = uart_ns.class_("UARTDummyReceiver", cg.Component)
MULTI_CONF = True
MULTI_CONF_NO_DEFAULT = True
def is_host_uart(uart_id: ID, full_config: ConfigType) -> bool:
return CORE.is_host and any(
conf[CONF_ID] == uart_id for conf in full_config.get(DOMAIN) or []
)
def validate_raw_data(value):
if isinstance(value, str):
return value.encode("utf-8")
@@ -136,6 +145,10 @@ def validate_raw_data(value):
)
# Lengths are stored as uint16_t; reject longer payloads at validation.
validate_raw_payload = cv.All(validate_raw_data, cv.Length(max=65535))
def validate_rx_pin(value):
value = pins.internal_gpio_input_pin_schema(value)
if CORE.is_esp8266 and value[CONF_NUMBER] >= 16:
@@ -187,11 +200,9 @@ def _uart_declare_type(value):
UARTParityOptions = uart_ns.enum("UARTParityOptions")
UART_PARITY_OPTIONS = {
"NONE": UARTParityOptions.UART_CONFIG_PARITY_NONE,
"EVEN": UARTParityOptions.UART_CONFIG_PARITY_EVEN,
"ODD": UARTParityOptions.UART_CONFIG_PARITY_ODD,
}
UART_PARITY_OPTIONS = UARTParityOptions.options(
"NONE", "EVEN", "ODD", prefix="UART_CONFIG_PARITY_"
)
CONF_FLUSH_TIMEOUT = "flush_timeout"
CONF_RX_FULL_THRESHOLD = "rx_full_threshold"
@@ -199,13 +210,9 @@ CONF_RX_TIMEOUT = "rx_timeout"
CONF_CLOCK_SOURCE = "clock_source"
UARTClockSource = cg.global_ns.enum("uart_sclk_t")
UART_CLOCK_SOURCES = {
"DEFAULT": UARTClockSource.UART_SCLK_DEFAULT,
"APB": UARTClockSource.UART_SCLK_APB,
"XTAL": UARTClockSource.UART_SCLK_XTAL,
"RTC": UARTClockSource.UART_SCLK_RTC,
"REF_TICK": UARTClockSource.UART_SCLK_REF_TICK,
}
UART_CLOCK_SOURCES = UARTClockSource.options(
"DEFAULT", "APB", "XTAL", "RTC", "REF_TICK", prefix="UART_SCLK_"
)
# Keep in sync with SOC_UART_SUPPORT_* in ESP-IDF's per-variant soc_caps.h.
UART_CLOCK_SOURCES_BY_VARIANT = {
@@ -225,11 +232,7 @@ UART_CLOCK_SOURCES_BY_VARIANT = {
}
UARTDirection = uart_ns.enum("UARTDirection")
UART_DIRECTIONS = {
"RX": UARTDirection.UART_DIRECTION_RX,
"TX": UARTDirection.UART_DIRECTION_TX,
"BOTH": UARTDirection.UART_DIRECTION_BOTH,
}
UART_DIRECTIONS = UARTDirection.options("RX", "TX", "BOTH", prefix="UART_DIRECTION_")
# The reason for having CONF_BYTES at 150 by default:
#
@@ -274,7 +277,9 @@ DEBUG_SCHEMA = cv.Schema(
cv.Optional(
CONF_TIMEOUT, default=AFTER_DEFAULTS[CONF_TIMEOUT]
): cv.positive_time_period_milliseconds,
cv.Optional(CONF_DELIMITER): cv.templatable(validate_raw_data),
cv.Optional(CONF_DELIMITER): cv.All(
validate_raw_data, cv.Length(max=255)
),
}
),
cv.Optional(
@@ -345,12 +350,11 @@ async def debug_to_code(config, parent):
after = config[CONF_AFTER]
cg.add(trigger.set_after_bytes(after[CONF_BYTES]))
cg.add(trigger.set_after_timeout(after[CONF_TIMEOUT]))
if CONF_DELIMITER in after:
data = after[CONF_DELIMITER]
if isinstance(data, bytes):
data = list(data)
for byte in after[CONF_DELIMITER]:
cg.add(trigger.add_delimiter_byte(byte))
if delimiter := after.get(CONF_DELIMITER):
table = cg.shared_progmem_array(
"uart_debug_delimiter", cg.uint8, [int(b) for b in delimiter]
)
cg.add(trigger.set_after_delimiter(table, len(delimiter)))
if config[CONF_DUMMY_RECEIVER]:
dummy = cg.new_Pvariable(config[CONF_DUMMY_RECEIVER_ID], parent)
await cg.register_component(dummy, {})
@@ -366,17 +370,10 @@ async def to_code(config):
cg.add(var.set_baud_rate(config[CONF_BAUD_RATE]))
if CONF_TX_PIN in config:
tx_pin = await cg.gpio_pin_expression(config[CONF_TX_PIN])
cg.add(var.set_tx_pin(tx_pin))
if CONF_RX_PIN in config:
rx_pin = await cg.gpio_pin_expression(config[CONF_RX_PIN])
cg.add(var.set_rx_pin(rx_pin))
if CONF_FLOW_CONTROL_PIN in config:
flow_control_pin = await cg.gpio_pin_expression(config[CONF_FLOW_CONTROL_PIN])
cg.add(var.set_flow_control_pin(flow_control_pin))
if CONF_PORT in config:
cg.add(var.set_name(config[CONF_PORT]))
await cg.add_pin(var.set_tx_pin, config.get(CONF_TX_PIN))
await cg.add_pin(var.set_rx_pin, config.get(CONF_RX_PIN))
await cg.add_pin(var.set_flow_control_pin, config.get(CONF_FLOW_CONTROL_PIN))
cg.add_optional(var.set_name, config.get(CONF_PORT))
cg.add(var.set_rx_buffer_size(config[CONF_RX_BUFFER_SIZE]))
if CORE.is_esp32:
if CONF_RX_FULL_THRESHOLD not in config:
@@ -393,10 +390,8 @@ async def to_code(config):
)
cg.add(var.set_rx_full_threshold(config[CONF_RX_FULL_THRESHOLD]))
cg.add(var.set_rx_timeout(config[CONF_RX_TIMEOUT]))
if CONF_FLUSH_TIMEOUT in config:
cg.add(var.set_flush_timeout(config[CONF_FLUSH_TIMEOUT]))
if (tx_buffer_size := config.get(CONF_TX_BUFFER_SIZE)) is not None:
cg.add(var.set_tx_buffer_size(tx_buffer_size))
cg.add_optional(var.set_flush_timeout, config.get(CONF_FLUSH_TIMEOUT))
cg.add_optional(var.set_tx_buffer_size, config.get(CONF_TX_BUFFER_SIZE))
# The member already defaults to UART_SCLK_DEFAULT, so only emit a real choice
if (clock_source := config.get(CONF_CLOCK_SOURCE, "DEFAULT")) != "DEFAULT":
cg.add(var.set_clock_source(UART_CLOCK_SOURCES[clock_source]))
@@ -444,6 +439,34 @@ UART_DEVICE_SCHEMA = cv.Schema(
KEY_UART_DEVICES = "uart_devices"
@dataclass
class UARTData:
# UARTs whose received bytes do not keep the timing of a serial line.
unclocked: list[ID] = field(default_factory=list)
def _get_data() -> UARTData:
if DOMAIN not in CORE.data:
CORE.data[DOMAIN] = UARTData()
return CORE.data[DOMAIN]
def mark_unclocked(uart_id: ID) -> ID:
"""Record that the bytes of uart_id do not arrive with the timing of a serial line, e.g. over TCP or USB.
A gap between them says nothing about where a frame ends. Use it on the UART's declared id,
cv.All(cv.declare_id(MyUart), uart.mark_unclocked), so that every to_code sees it.
"""
_get_data().unclocked.append(uart_id)
return uart_id
def is_unclocked(uart_id: ID) -> bool:
"""Return True if uart_id was passed to mark_unclocked()."""
# By name: a generated id is named only after the schemas ran.
return any(str(uart) == str(uart_id) for uart in _get_data().unclocked)
def final_validate_device_schema(
name: str,
*,
@@ -533,43 +556,57 @@ def final_validate_device_schema(
)
def subtree_references_uart(
node: object, uart_id: str, conf_key: str = CONF_UART_ID
) -> bool:
"""Return True if any dict in the subtree has a conf_key entry naming this bus.
For the final validation of a component that needs a UART for itself. Bare
`id:` references (a uart.write action) and lambdas are not found.
"""
if isinstance(node, dict):
return any(
(key == conf_key and str(value) == uart_id)
or subtree_references_uart(value, uart_id, conf_key)
for key, value in node.items()
)
if isinstance(node, list):
return any(subtree_references_uart(item, uart_id, conf_key) for item in node)
return False
async def register_uart_device(var, config):
"""Register a UART device, setting up all the internal values.
This is a coroutine, you need to await it with an 'await' expression!
"""
parent = await cg.get_variable(config[CONF_UART_ID])
cg.add(var.set_uart_parent(parent))
await cg.add_variable(var.set_uart_parent, config[CONF_UART_ID])
@automation.register_action(
def new_uart_component(
config: ConfigType, *args: Any
) -> Coroutine[Any, Any, cg.MockObj]:
"""Create a component on a UART bus: construct, register, attach to the bus.
Extra args go to the constructor.
"""
return cg.new_bus_component(register_uart_device, config, *args)
automation.register_apply_action(
"uart.write",
UARTWriteAction,
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(UARTComponent),
cv.Required(CONF_DATA): cv.templatable(validate_raw_data),
cv.Required(CONF_DATA): cv.templatable(validate_raw_payload),
},
key=CONF_DATA,
),
synchronous=True,
# A constant stays in flash: write_array_progmem copies it out in chunks on ESP8266
automation.ApplyBytes(
CONF_DATA, "write_array({})", progmem_target="write_array_progmem({})"
),
)
async def uart_write_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
data = config[CONF_DATA]
if isinstance(data, bytes):
data = list(data)
if cg.is_template(data):
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
cg.add(var.set_data_template(templ))
else:
# Generate static array in flash to avoid RAM copy
arr_id = ID(f"{action_id}_data", is_declaration=True, type=cg.uint8)
arr = cg.static_const_array(arr_id, cg.ArrayInitializer(*data))
cg.add(var.set_data_static(arr, len(data)))
return var
@coroutine_with_priority(CoroPriority.FINAL)
-43
View File
@@ -1,43 +0,0 @@
#pragma once
#include "uart.h"
#include "esphome/core/automation.h"
#include <vector>
namespace esphome::uart {
template<typename... Ts> class UARTWriteAction final : public Action<Ts...>, public Parented<UARTComponent> {
public:
void set_data_template(std::vector<uint8_t> (*func)(Ts...)) {
// Stateless lambdas (generated by ESPHome) implicitly convert to function pointers
this->code_.func = func;
this->len_ = -1; // Sentinel value indicates template mode
}
// Store pointer to static data in flash (no RAM copy)
void set_data_static(const uint8_t *data, size_t len) {
this->code_.data = data;
this->len_ = len; // Length >= 0 indicates static mode
}
void play(const Ts &...x) override {
if (this->len_ >= 0) {
// Static mode: use pointer and length
this->parent_->write_array(this->code_.data, static_cast<size_t>(this->len_));
} else {
// Template mode: call function
auto val = this->code_.func(x...);
this->parent_->write_array(val);
}
}
protected:
ssize_t len_{-1}; // -1 = template mode, >=0 = static mode with length
union Code {
std::vector<uint8_t> (*func)(Ts...); // Function pointer (stateless lambdas)
const uint8_t *data; // Pointer to static data in flash
} code_;
};
} // namespace esphome::uart
+100
View File
@@ -0,0 +1,100 @@
from esphome import automation
import esphome.codegen as cg
from esphome.components import binary_sensor, uart
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_UART_ID
from esphome.core import CORE, ID
import esphome.final_validate as fv
from esphome.types import ConfigType
from .. import uart_ns, validate_raw_data
DEPENDENCIES = ["uart"]
DOMAIN = "uart_binary_sensor"
CONF_HUB_ID = "hub_id"
UARTBinarySensorHub = uart_ns.class_(
"UARTBinarySensorHub", uart.UARTDevice, cg.Component
)
# Every sensor on a bus is one matcher of that bus's hub; the define is the largest hub
_request_matcher_slot = cg.slot_counter("UART_BINARY_SENSOR_COUNT")
CONFIG_SCHEMA = (
# Initially off, so the first sequence after boot is an OFF to ON edge for on_press
binary_sensor.binary_sensor_schema(binary_sensor.BinarySensorInitiallyOff)
.extend(
{
cv.GenerateID(CONF_HUB_ID): cv.declare_id(UARTBinarySensorHub),
# An empty pattern would match every byte; the hub's buffer index is a byte
cv.Required(CONF_DATA): cv.All(
validate_raw_data, cv.Length(min=1, max=255)
),
}
)
.extend(uart.UART_DEVICE_SCHEMA)
)
_validate_bus = uart.final_validate_device_schema("uart binary_sensor", require_rx=True)
def _one_hub_per_bus(config: ConfigType) -> ConfigType:
"""The first matching sensor takes the bytes and clears them, so a sequence that contains
another on the same bus would leave one of them dead; and the sensors of a bus share the hub
the first one creates, so only that one can name it and claim the bus's receive side."""
data = bytes(config[CONF_DATA])
first = True
for other in fv.platform_entries(
domain="binary_sensor",
platform="uart",
match={CONF_UART_ID: config[CONF_UART_ID]},
):
if other is config:
break # Every sensor before this one on the bus has been seen
first = False
other_data = bytes(other[CONF_DATA])
if other_data in data or data in other_data:
raise cv.Invalid(
f"Another uart binary_sensor on '{config[CONF_UART_ID]}' has data this pattern "
"would match as well"
)
if config[CONF_HUB_ID].is_manual:
raise cv.Invalid(
f"Set hub_id on the first uart binary_sensor of '{config[CONF_UART_ID]}'; "
"the others share its hub"
)
if first:
_validate_bus(config)
return config
FINAL_VALIDATE_SCHEMA = _one_hub_per_bus
def _hubs() -> dict[ID, cg.MockObj]:
"""The hub of each bus, keyed by the bus id."""
return CORE.data.setdefault(DOMAIN, {})
async def _hub_for(config: ConfigType) -> cg.MockObj:
"""The one hub that reads the bus for every sensor on it, created by the first sensor."""
hubs = _hubs()
if (hub := hubs.get(config[CONF_UART_ID])) is None:
hub = hubs[config[CONF_UART_ID]] = cg.new_Pvariable(config[CONF_HUB_ID])
await cg.register_component(hub, config)
await uart.register_uart_device(hub, config)
return hub
async def to_code(config: ConfigType) -> None:
hub = await _hub_for(config)
var = await binary_sensor.new_binary_sensor(config)
data = config[CONF_DATA]
_request_matcher_slot(str(hub))
cg.add(
hub.add_event_matcher(
var, automation.progmem_bytes("uart_binary_sensor_data", data), len(data)
)
)
@@ -0,0 +1,47 @@
#include "uart_binary_sensor.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
namespace esphome::uart {
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor");
void UARTBinarySensorHub::dump_config() {
ESP_LOGCONFIG(TAG, "UART Binary Sensor:");
for (const auto &matcher : this->matchers_) {
LOG_BINARY_SENSOR(" ", "Pattern", matcher.sensor);
}
}
void UARTBinarySensorHub::loop() {
this->read_available([this](uint8_t byte) {
this->buffer_.push_overwrite(byte);
for (const auto &matcher : this->matchers_) {
// The byte just read has to be the pattern's last one before the rest is compared
if (progmem_read_byte(&matcher.data[matcher.data_len - 1]) != byte || !this->matches_(matcher)) {
continue;
}
// A momentary press, as the remote receiver binary sensors publish theirs
matcher.sensor->publish_state(true);
matcher.sensor->publish_state(false);
this->buffer_.clear();
break;
}
});
}
// Whether the pattern, apart from its last byte, ends the buffer
bool UARTBinarySensorHub::matches_(const EventMatcher &matcher) const {
if (this->buffer_.size() < matcher.data_len) {
return false;
}
const size_t start = this->buffer_.size() - matcher.data_len;
for (size_t i = 0; i + 1 < matcher.data_len; i++) {
if (this->buffer_[start + i] != progmem_read_byte(&matcher.data[i])) {
return false;
}
}
return true;
}
} // namespace esphome::uart
@@ -0,0 +1,37 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/uart/uart.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
namespace esphome::uart {
// One per bus: reads the bytes once and presses each sensor whose pattern ends the stream
class UARTBinarySensorHub : public UARTDevice, public Component {
public:
void setup() override { this->buffer_.init(this->max_len_); }
void loop() override;
void dump_config() override;
// The pattern lives in flash; called once per sensor by the generated code
void add_event_matcher(binary_sensor::BinarySensor *sensor, const uint8_t *data, uint8_t data_len) {
this->matchers_.push_back({sensor, data, data_len});
this->max_len_ = std::max(this->max_len_, data_len);
}
protected:
struct EventMatcher {
binary_sensor::BinarySensor *sensor;
const uint8_t *data;
uint8_t data_len;
};
bool matches_(const EventMatcher &matcher) const;
StaticVector<EventMatcher, UART_BINARY_SENSOR_COUNT> matchers_;
FixedRingBuffer<uint8_t, 255> buffer_;
uint8_t max_len_{0};
};
} // namespace esphome::uart
+11 -18
View File
@@ -1,36 +1,29 @@
import esphome.codegen as cg
from esphome import automation
from esphome.components import button, uart
import esphome.config_validation as cv
from esphome.const import CONF_DATA
from esphome.core import HexInt
from esphome.const import CONF_DATA, CONF_UART_ID
from esphome.types import ConfigType
from .. import uart_ns, validate_raw_data
from .. import validate_raw_payload
CODEOWNERS = ["@ssieb"]
DEPENDENCIES = ["uart"]
UARTButton = uart_ns.class_("UARTButton", button.Button, uart.UARTDevice, cg.Component)
CONFIG_SCHEMA = (
button.button_schema(UARTButton)
button.button_schema(button.ApplyButton)
.extend(
{
cv.Required(CONF_DATA): validate_raw_data,
cv.Required(CONF_DATA): validate_raw_payload,
}
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
async def to_code(config):
var = await button.new_button(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
async def to_code(config: ConfigType) -> None:
data = config[CONF_DATA]
if isinstance(data, bytes):
data = [HexInt(x) for x in data]
cg.add(var.set_data(cg.ArrayInitializer(*data)))
table = automation.progmem_bytes("uart_data", data)
await button.sub_buttons(config, parent=config[CONF_UART_ID]).create(
config, None, apply=f"write_array_progmem({table}, {len(data)})"
)
-15
View File
@@ -1,15 +0,0 @@
#include "uart_button.h"
#include "esphome/core/log.h"
namespace esphome::uart {
static const char *const TAG = "uart.button";
void UARTButton::press_action() {
ESP_LOGD(TAG, "'%s': Sending data", this->get_name().c_str());
this->write_array(this->data_.data(), this->data_.size());
}
void UARTButton::dump_config() { LOG_BUTTON("", "UART Button", this); }
} // namespace esphome::uart
-23
View File
@@ -1,23 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include "esphome/components/button/button.h"
#include <vector>
namespace esphome::uart {
class UARTButton final : public button::Button, public UARTDevice, public Component {
public:
void set_data(std::vector<uint8_t> &&data) { this->data_ = std::move(data); }
void set_data(std::initializer_list<uint8_t> data) { this->data_ = std::vector<uint8_t>(data); }
void dump_config() override;
protected:
void press_action() override;
std::vector<uint8_t> data_;
};
} // namespace esphome::uart
+26 -13
View File
@@ -1,8 +1,7 @@
import esphome.codegen as cg
from esphome.components import event, uart
import esphome.config_validation as cv
from esphome.const import CONF_EVENT_TYPES, CONF_ID
from esphome.core import ID
from esphome.const import CONF_EVENT_TYPES
from esphome.types import ConfigType
from .. import uart_ns
@@ -12,9 +11,10 @@ CODEOWNERS = ["@eoasmxd"]
DEPENDENCIES = ["uart"]
UARTEvent = uart_ns.class_("UARTEvent", event.Event, uart.UARTDevice, cg.Component)
UARTEventMatcher = uart_ns.struct("UARTEventMatcher")
def validate_event_types(value) -> list[tuple[str, str | list[int]]]:
def validate_event_types(value) -> list[tuple[str, list[int]]]:
if not isinstance(value, list):
raise cv.Invalid("Event type must be a list of key-value mappings.")
@@ -44,7 +44,7 @@ def validate_event_types(value) -> list[tuple[str, str | list[int]]]:
try:
# Try to validate as string
match_data_str = cv.string_strict(match_data)
processed.append((event_name, match_data_str))
processed.append((event_name, list(match_data_str.encode("utf-8"))))
continue
except cv.Invalid:
pass # Not string either
@@ -56,6 +56,10 @@ def validate_event_types(value) -> list[tuple[str, str | list[int]]]:
if not processed:
raise cv.Invalid("event_types must contain at least one event mapping.")
if len(processed) > 0xFFFF:
raise cv.Invalid("event_types can have at most 65535 entries.")
if any(len(data) > 0xFFFF for _, data in processed):
raise cv.Invalid("Event match data can be at most 65535 bytes.")
return processed
@@ -77,14 +81,23 @@ async def to_code(config: ConfigType) -> None:
var = await event.new_event(config, event_types=event_names)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
for i, (event_name, match_data) in enumerate(config[CONF_EVENT_TYPES]):
if isinstance(match_data, str):
match_data = [ord(c) for c in match_data]
match_data_var_id = ID(
f"match_data_{config[CONF_ID]}_{i}", is_declaration=True, type=cg.uint8
matchers = []
max_len = 0
for event_name, match_data in config[CONF_EVENT_TYPES]:
match_data = [int(b) for b in match_data]
data = (
cg.shared_progmem_array("uart_event_match", cg.uint8, match_data)
if match_data
else cg.nullptr
)
match_data_var = cg.static_const_array(
match_data_var_id, cg.ArrayInitializer(*match_data)
matchers.append(
cg.StructInitializer(
UARTEventMatcher,
("event_name", event_name),
("data", data),
("data_len", len(match_data)),
)
)
cg.add(var.add_event_matcher(event_name, match_data_var, len(match_data)))
max_len = max(max_len, len(match_data))
table = cg.shared_progmem_array("uart_event_matchers", UARTEventMatcher, matchers)
cg.add(var.set_matchers(table, len(matchers), max_len))
+12 -15
View File
@@ -1,10 +1,10 @@
#include "uart_event.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include <algorithm>
namespace esphome::uart {
static const char *const TAG = "uart.event";
ESPHOME_LOG_TAG(TAG, "uart.event");
void UARTEvent::setup() {}
@@ -12,26 +12,23 @@ void UARTEvent::dump_config() { LOG_EVENT("", "UART Event", this); }
void UARTEvent::loop() { this->read_data_(); }
void UARTEvent::add_event_matcher(const char *event_name, const uint8_t *match_data, size_t match_data_len) {
this->matchers_.push_back({event_name, match_data, match_data_len});
if (match_data_len > this->max_matcher_len_) {
this->max_matcher_len_ = match_data_len;
}
}
void UARTEvent::read_data_() {
while (this->available()) {
uint8_t data;
this->read_byte(&data);
this->read_available([this](uint8_t data) {
this->buffer_.push_back(data);
bool match_found = false;
for (const auto &matcher : this->matchers_) {
for (uint16_t i = 0; i < this->matcher_count_; i++) {
const UARTEventMatcher &matcher = this->matchers_[i];
if (this->buffer_.size() < matcher.data_len) {
continue;
}
if (std::equal(matcher.data, matcher.data + matcher.data_len, this->buffer_.end() - matcher.data_len)) {
const uint8_t *tail = this->buffer_.data() + this->buffer_.size() - matcher.data_len;
size_t pos = 0;
// The pattern is in flash; ESP8266 only allows word loads there, so read it with progmem_read_byte
while (pos < matcher.data_len && progmem_read_byte(matcher.data + pos) == tail[pos])
pos++;
if (pos == matcher.data_len) {
this->trigger(matcher.event_name);
this->buffer_.clear();
match_found = true;
@@ -42,7 +39,7 @@ void UARTEvent::read_data_() {
if (!match_found && this->max_matcher_len_ > 0 && this->buffer_.size() > this->max_matcher_len_) {
this->buffer_.erase(this->buffer_.begin());
}
}
});
}
} // namespace esphome::uart
+28 -9
View File
@@ -1,31 +1,50 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/components/event/event.h"
#include "esphome/components/uart/uart.h"
#include <type_traits>
#include <vector>
namespace esphome::uart {
/// An event type and the byte pattern that fires it; `data` may point to PROGMEM.
struct UARTEventMatcher {
const char *event_name;
const uint8_t *data;
size_t data_len;
};
// Codegen keeps the matchers in a flash table read with plain loads, which ESP8266 only allows for whole words.
static_assert(std::is_same_v<decltype(UARTEventMatcher::event_name), const char *>,
"UARTEventMatcher fields must stay word sized");
static_assert(std::is_same_v<decltype(UARTEventMatcher::data), const uint8_t *>,
"UARTEventMatcher fields must stay word sized");
static_assert(std::is_same_v<decltype(UARTEventMatcher::data_len), size_t>,
"UARTEventMatcher fields must stay word sized");
#ifdef USE_ESP8266
static_assert(sizeof(UARTEventMatcher) == 3 * sizeof(uint32_t), "UARTEventMatcher is read from flash with word loads");
#endif
class UARTEvent final : public event::Event, public UARTDevice, public Component {
public:
void setup() override;
void loop() override;
void dump_config() override;
void add_event_matcher(const char *event_name, const uint8_t *match_data, size_t match_data_len);
/// `matchers` is a codegen PROGMEM table that must outlive the component.
void set_matchers(const UARTEventMatcher *matchers, uint16_t count, uint16_t max_len) {
this->matchers_ = matchers;
this->matcher_count_ = count;
this->max_matcher_len_ = max_len;
}
protected:
struct EventMatcher {
const char *event_name;
const uint8_t *data;
size_t data_len;
};
void read_data_();
std::vector<EventMatcher> matchers_;
const UARTEventMatcher *matchers_{nullptr};
std::vector<uint8_t> buffer_;
size_t max_matcher_len_ = 0;
uint16_t matcher_count_{0};
uint16_t max_matcher_len_{0};
};
} // namespace esphome::uart
@@ -0,0 +1,36 @@
import esphome.codegen as cg
from esphome.components import packet_interface, uart
from esphome.components.packet_interface import (
CONF_MAX_PACKET_SIZE,
packet_interface_schema,
)
from esphome.const import CONF_ID
from esphome.types import ConfigType
from .. import uart_ns
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["uart"]
UartPacketInterface = uart_ns.class_(
"UartPacketInterface",
packet_interface.PacketInterface,
uart.UARTDevice,
)
CONFIG_SCHEMA = packet_interface_schema(UartPacketInterface, 508).extend(
uart.UART_DEVICE_SCHEMA
)
async def to_code(config: ConfigType) -> None:
# The receive buffer's size is known here, so it is a static array rather than allocated at run time
rx_buffer = f"{config[CONF_ID].id}__rx_buffer"
cg.add_global(
cg.RawStatement(f"static uint8_t {rx_buffer}[{config[CONF_MAX_PACKET_SIZE]}];")
)
var = await packet_interface.new_packet_interface(
config, cg.RawExpression(rx_buffer)
)
await uart.register_uart_device(var, config)
@@ -0,0 +1,82 @@
#include "esphome/core/log.h"
#include "uart_packet_interface.h"
namespace esphome::uart {
using packet_interface::PacketBuffer;
using packet_interface::PacketMetaData;
ESPHOME_LOG_TAG(TAG, "uart.packet_interface");
static constexpr uint8_t FLAG_BYTE = 0x7E;
static constexpr uint8_t CONTROL_BYTE = 0x7D;
static constexpr uint8_t ESCAPE_XOR = 0x20;
// Raw bytes escaped and written per UART write
static constexpr size_t TX_CHUNK_SIZE = 32;
void UartPacketInterface::loop() {
this->read_available([this](uint8_t byte) { this->handle_rx_byte_(byte); });
}
void UartPacketInterface::handle_rx_byte_(uint8_t byte) {
if (byte == FLAG_BYTE) {
// A flag that ends a frame also starts the next, so an unfinished frame cannot hide the one after it
if (this->rx_started_ && this->rx_length_ != 0)
this->on_receive_from_interface_(PacketBuffer(this->rx_buffer_.data(), this->rx_length_), {});
this->rx_started_ = true;
this->rx_length_ = 0;
this->rx_control_ = false;
return;
}
if (!this->rx_started_)
return;
if (byte == CONTROL_BYTE) {
this->rx_control_ = true;
return;
}
if (this->rx_control_) {
byte ^= ESCAPE_XOR;
this->rx_control_ = false;
}
if (this->rx_length_ >= this->rx_buffer_.size()) {
ESP_LOGD(TAG, "Packet too large, discarding");
this->rx_started_ = false;
this->rx_length_ = 0;
return;
}
this->rx_buffer_[this->rx_length_++] = byte;
}
bool UartPacketInterface::send_to_interface(const PacketBuffer &data, const PacketMetaData &meta_data) {
size_t size = data.size();
if (size == 0)
return true;
if (size > this->max_packet_size_) {
ESP_LOGW(TAG, "Packet of %zu bytes is larger than %zu", size, this->max_packet_size_);
return false;
}
uint8_t raw[TX_CHUNK_SIZE]{};
// Every byte may be escaped, plus a flag byte at each end
uint8_t out[TX_CHUNK_SIZE * 2 + 2];
size_t out_len = 0;
out[out_len++] = FLAG_BYTE;
for (size_t offset = 0; offset < size;) {
size_t count = data.copy_to(raw, TX_CHUNK_SIZE, offset);
offset += count;
for (size_t i = 0; i != count; i++) {
uint8_t byte = raw[i];
if (byte == FLAG_BYTE || byte == CONTROL_BYTE) {
out[out_len++] = CONTROL_BYTE;
byte ^= ESCAPE_XOR;
}
out[out_len++] = byte;
}
if (offset == size)
out[out_len++] = FLAG_BYTE;
this->write_array(out, out_len);
out_len = 0;
}
return true;
}
} // namespace esphome::uart
@@ -0,0 +1,36 @@
#pragma once
#include <span>
#include "esphome/components/packet_interface/packet_interface.h"
#include "esphome/components/uart/uart.h"
namespace esphome::uart {
/**
* A packet interface for sending and receiving framed packets over a UART connection.
* Uses simple byte-stuffing framing with FLAG_BYTE and CONTROL_BYTE.
* The protocol wraps data between FLAG_BYTEs.
* Any occurrence of FLAG_BYTE or CONTROL_BYTE in the data is escaped by emitting CONTROL_BYTE followed by the byte
* XORed with 0x20.
*/
class UartPacketInterface : public packet_interface::PacketInterface, public UARTDevice {
public:
/// @param rx_buffer Holds the unescaped data being received; its size is the largest packet, and it must outlive
/// the link
explicit UartPacketInterface(std::span<uint8_t> rx_buffer)
: packet_interface::PacketInterface(rx_buffer.size()), rx_buffer_(rx_buffer) {}
void loop() override;
bool send_to_interface(const packet_interface::PacketBuffer &data,
const packet_interface::PacketMetaData &meta_data) override;
protected:
void handle_rx_byte_(uint8_t byte);
const std::span<uint8_t> rx_buffer_;
size_t rx_length_{0};
bool rx_started_{};
bool rx_control_{};
};
} // namespace esphome::uart
@@ -4,7 +4,11 @@
namespace esphome::uart {
static const char *const TAG = "uart_transport";
ESPHOME_LOG_TAG(TAG, "uart_transport");
static constexpr uint16_t MAX_PACKET_SIZE = 508;
static constexpr uint8_t FLAG_BYTE = 0x7E;
static constexpr uint8_t CONTROL_BYTE = 0x7D;
void UARTTransport::loop() {
PacketTransport::loop();
@@ -78,4 +82,5 @@ void UARTTransport::send_packet(const std::vector<uint8_t> &buf) const {
this->parent_->write_byte(FLAG_BYTE);
}
size_t UARTTransport::get_max_packet_size() { return MAX_PACKET_SIZE; }
} // namespace esphome::uart
@@ -16,9 +16,6 @@ namespace esphome::uart {
* Any occurrence of FLAG_BYTE or CONTROL_BYTE in the data is escaped by emitting CONTROL_BYTE followed by the byte
* XORed with 0x20.
*/
static const uint16_t MAX_PACKET_SIZE = 508;
static const uint8_t FLAG_BYTE = 0x7E;
static const uint8_t CONTROL_BYTE = 0x7D;
class UARTTransport final : public packet_transport::PacketTransport, public UARTDevice {
public:
@@ -29,7 +26,7 @@ class UARTTransport final : public packet_transport::PacketTransport, public UAR
void write_byte_(uint8_t byte) const;
void send_packet(const std::vector<uint8_t> &buf) const override;
bool should_send() override { return true; };
size_t get_max_packet_size() override { return MAX_PACKET_SIZE; }
size_t get_max_packet_size() override;
std::vector<uint8_t> receive_buffer_{};
bool rx_started_{};
bool rx_control_{};
@@ -0,0 +1,39 @@
#pragma once
#include <cstdint>
#include "esphome/core/helpers.h"
namespace esphome::uart {
/// One software serial TX byte as line levels, one bit per slot.
struct SoftwareSerialTxFrame {
/// The start bit at slot 0, then the frame, then a stop bit that ends the last run
uint32_t line;
/// Slot of the last 0; from the edge after it the line stays high through the stop bits
uint32_t high_from;
/// Slots right after an edge into a run of equal bits lasting at least `min_run_cycles`, where a pending
/// interrupt only delays bits that repeat the level
uint32_t unlock_at;
};
/// `frame` holds `num_bits` data and parity bits, LSB first. `unlock_at` stays 0 unless `find_unlocks`.
ESPHOME_ALWAYS_INLINE inline SoftwareSerialTxFrame software_serial_tx_frame(uint32_t frame, uint32_t num_bits,
uint32_t bit_time, uint32_t min_run_cycles,
bool find_unlocks) {
SoftwareSerialTxFrame tx;
tx.line = frame << 1 | (1u << (num_bits + 1));
// Slot 0 is the start bit, always 0, so the argument is never 0
tx.high_from = 31 - __builtin_clz(~tx.line & ((2u << num_bits) - 1));
tx.unlock_at = 0;
if (find_unlocks) {
const uint32_t edges = tx.line ^ (tx.line << 1 | 1); // the idle line before the start bit is high
for (uint32_t pos = 0; pos <= tx.high_from; pos++) {
if (((edges >> pos) & 1) && (__builtin_ctz(edges >> (pos + 1)) + 1) * bit_time >= min_run_cycles)
tx.unlock_at |= 1u << pos;
}
}
return tx;
}
} // namespace esphome::uart
+16 -21
View File
@@ -1,10 +1,10 @@
from esphome import automation
import esphome.codegen as cg
from esphome.components import switch, uart
import esphome.config_validation as cv
from esphome.const import CONF_DATA, CONF_SEND_EVERY
from esphome.core import HexInt
from .. import uart_ns, validate_raw_data
from .. import uart_ns, validate_raw_payload
DEPENDENCIES = ["uart"]
@@ -18,11 +18,11 @@ CONFIG_SCHEMA = (
.extend(
{
cv.Required(CONF_DATA): cv.Any(
validate_raw_data,
validate_raw_payload,
cv.Schema(
{
cv.Optional(CONF_TURN_OFF): validate_raw_data,
cv.Optional(CONF_TURN_ON): validate_raw_data,
cv.Optional(CONF_TURN_OFF): validate_raw_payload,
cv.Optional(CONF_TURN_ON): validate_raw_payload,
}
),
),
@@ -40,20 +40,15 @@ async def to_code(config):
await uart.register_uart_device(var, config)
data = config[CONF_DATA]
if isinstance(data, dict):
if data_on := data.get(CONF_TURN_ON):
if isinstance(data_on, bytes):
data_on = [HexInt(x) for x in data_on]
cg.add(var.set_data_on(cg.ArrayInitializer(*data_on)))
if data_off := data.get(CONF_TURN_OFF):
if isinstance(data_off, bytes):
data_off = [HexInt(x) for x in data_off]
cg.add(var.set_data_off(cg.ArrayInitializer(*data_off)))
else:
data = config[CONF_DATA]
if isinstance(data, bytes):
data = [HexInt(x) for x in data]
cg.add(var.set_data_on(cg.ArrayInitializer(*data)))
single_state = not isinstance(data, dict)
if single_state:
data = {CONF_TURN_ON: data}
for key, setter in (
(CONF_TURN_ON, var.set_data_on),
(CONF_TURN_OFF, var.set_data_off),
):
if payload := data.get(key):
cg.add(setter(automation.progmem_bytes("uart_data", payload), len(payload)))
if single_state:
cg.add(var.set_single_state(True))
if CONF_SEND_EVERY in config:
cg.add(var.set_send_every(config[CONF_SEND_EVERY]))
cg.add_optional(var.set_send_every, config.get(CONF_SEND_EVERY))
+7 -7
View File
@@ -4,7 +4,7 @@
namespace esphome::uart {
static const char *const TAG = "uart.switch";
ESPHOME_LOG_TAG(TAG, "uart.switch");
void UARTSwitch::loop() {
if (this->send_every_) {
@@ -17,13 +17,13 @@ void UARTSwitch::loop() {
}
void UARTSwitch::write_command_(bool state) {
if (state && !this->data_on_.empty()) {
ESP_LOGD(TAG, "'%s': Sending on data", this->get_name().c_str());
this->write_array(this->data_on_.data(), this->data_on_.size());
if (state && this->data_on_len_ != 0) {
ESP_LOGD(TAG, "'%s': Sending on data", LOG_STR_ARG(this->get_log_name()));
this->write_array_progmem(this->data_on_, this->data_on_len_);
}
if (!state && !this->data_off_.empty()) {
ESP_LOGD(TAG, "'%s': Sending off data", this->get_name().c_str());
this->write_array(this->data_off_.data(), this->data_off_.size());
if (!state && this->data_off_len_ != 0) {
ESP_LOGD(TAG, "'%s': Sending off data", LOG_STR_ARG(this->get_log_name()));
this->write_array_progmem(this->data_off_, this->data_off_len_);
}
}
+12 -7
View File
@@ -5,7 +5,6 @@
#include "esphome/components/switch/switch.h"
#include <cinttypes>
#include <vector>
namespace esphome::uart {
@@ -13,10 +12,14 @@ class UARTSwitch final : public switch_::Switch, public UARTDevice, public Compo
public:
void loop() override;
void set_data_on(std::vector<uint8_t> &&data) { this->data_on_ = std::move(data); }
void set_data_on(std::initializer_list<uint8_t> data) { this->data_on_ = std::vector<uint8_t>(data); }
void set_data_off(std::vector<uint8_t> &&data) { this->data_off_ = std::move(data); }
void set_data_off(std::initializer_list<uint8_t> data) { this->data_off_ = std::vector<uint8_t>(data); }
void set_data_on(const uint8_t *data, uint16_t len) {
this->data_on_ = data;
this->data_on_len_ = len;
}
void set_data_off(const uint8_t *data, uint16_t len) {
this->data_off_ = data;
this->data_off_len_ = len;
}
void set_send_every(uint32_t send_every) { this->send_every_ = send_every; }
void set_single_state(bool single) { this->single_state_ = single; }
@@ -25,8 +28,10 @@ class UARTSwitch final : public switch_::Switch, public UARTDevice, public Compo
protected:
void write_command_(bool state);
void write_state(bool state) override;
std::vector<uint8_t> data_on_;
std::vector<uint8_t> data_off_;
const uint8_t *data_on_{nullptr};
const uint8_t *data_off_{nullptr};
uint16_t data_on_len_{0};
uint16_t data_off_len_{0};
bool single_state_{false};
uint32_t send_every_;
uint32_t last_transmission_;
+8 -1
View File
@@ -8,7 +8,7 @@
namespace esphome::uart {
static const char *const TAG = "uart";
ESPHOME_LOG_TAG(TAG, "uart");
// UART parity strings indexed by UARTParityOptions enum (0-4): NONE, EVEN, ODD, MARK, SPACE
PROGMEM_STRING_TABLE(UARTParityStrings, "NONE", "EVEN", "ODD", "MARK", "SPACE", "UNKNOWN");
@@ -33,6 +33,13 @@ void UARTDevice::check_uart_settings(uint32_t baud_rate, uint8_t stop_bits, UART
}
}
void UARTDevice::discard_available() {
UARTComponent *parent = this->parent_;
uint8_t byte;
while (parent->available() && parent->read_byte(&byte)) {
}
}
const LogString *parity_to_str(UARTParityOptions parity) {
return UARTParityStrings::get_log_str(static_cast<uint8_t>(parity), UARTParityStrings::LAST_INDEX);
}
+58
View File
@@ -1,5 +1,8 @@
#pragma once
#include <algorithm>
#include <cstdint>
#include <type_traits>
#include <vector>
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
@@ -25,6 +28,7 @@ class UARTDevice {
size_t get_rx_timeout() { return this->parent_->get_rx_timeout(); }
void write_array(const uint8_t *data, size_t len) { this->parent_->write_array(data, len); }
void write_array_progmem(const uint8_t *data, size_t len) { this->parent_->write_array_progmem(data, len); }
void write_array(const std::vector<uint8_t> &data) { this->parent_->write_array(data); }
template<size_t N> void write_array(const std::array<uint8_t, N> &data) {
this->parent_->write_array(data.data(), data.size());
@@ -47,6 +51,48 @@ class UARTDevice {
size_t available() { return this->parent_->available(); }
/// Pass the bytes available now to on_chunk(data, len) in N byte chunks; returning false stops.
/// Returns false only on a read error.
template<size_t N = 64, typename F> bool read_available_chunks(F &&on_chunk) {
return this->read_available_chunks<N>(on_chunk, this->available());
}
/// Same, reading exactly count bytes; count must not exceed available().
template<size_t N = 64, typename F> bool read_available_chunks(F &&on_chunk, size_t count) {
static_assert(N > 0, "read buffer size must be non-zero");
uint8_t buf[N];
while (count > 0) {
const size_t len = std::min(count, N);
if (!this->read_array(buf, len)) {
return false;
}
count -= len;
if (!continue_after(on_chunk, static_cast<const uint8_t *>(buf), len)) {
break;
}
}
return true;
}
/// Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
template<size_t N = 64, typename F> bool read_available(F &&on_byte) {
return this->read_available<N>(on_byte, this->available());
}
template<size_t N = 64, typename F> bool read_available(F &&on_byte, size_t count) {
return this->read_available_chunks<N>(
[&on_byte](const uint8_t *data, size_t len) {
for (const uint8_t *p = data; p != data + len; p++) {
if (!continue_after(on_byte, *p)) {
return false;
}
}
return true;
},
count);
}
/// Read and drop every byte waiting in the receive buffer.
void discard_available();
UARTFlushResult flush() { return this->parent_->flush(); }
// Compat APIs
@@ -73,6 +119,18 @@ class UARTDevice {
UARTParityOptions parity = UART_CONFIG_PARITY_NONE, uint8_t data_bits = 8);
protected:
/// Call f(args...) and return its result; a void callback always continues.
template<typename F, typename... A> static bool continue_after(F &f, A... args) {
using R = std::invoke_result_t<F &, A...>;
if constexpr (std::is_void_v<R>) {
f(args...);
return true;
} else {
static_assert(std::is_same_v<std::remove_cvref_t<R>, bool>, "callback must return void or bool");
return f(args...);
}
}
UARTComponent *parent_{nullptr};
};
+33 -1
View File
@@ -1,8 +1,14 @@
#include "uart_component.h"
#include "esphome/core/application.h"
#include <algorithm>
namespace esphome::uart {
static const char *const TAG = "uart";
ESPHOME_LOG_TAG(TAG, "uart");
// Keeps the pacing product in 32 bits up to about 10 Mbaud.
static constexpr uint32_t MAX_PACE_SPAN_MS = 4000;
bool UARTComponent::check_read_timeout_(size_t len) {
if (this->available() >= len)
@@ -19,6 +25,19 @@ bool UARTComponent::check_read_timeout_(size_t len) {
return true;
}
size_t UARTComponent::paced_write_room(uint32_t last_write_ms) {
size_t room = this->available_for_write();
if (room != SIZE_MAX) {
return room;
}
// A pass woken early writes little.
uint32_t span =
std::min({App.get_loop_component_start_time() - last_write_ms, App.get_loop_interval(), MAX_PACE_SPAN_MS});
// 10 bits per byte on the line.
uint32_t paced = this->baud_rate_ / 10 * span / 1000;
return std::max<size_t>(1, paced);
}
void UARTComponent::set_rx_full_threshold_ms(uint8_t time) {
uint8_t bytelength = this->data_bits_ + this->stop_bits_ + 1;
if (this->parity_ != UARTParityOptions::UART_CONFIG_PARITY_NONE)
@@ -27,4 +46,17 @@ void UARTComponent::set_rx_full_threshold_ms(uint8_t time) {
this->set_rx_full_threshold(val);
}
#ifdef USE_ESP8266
void UARTComponent::write_array_progmem(const uint8_t *data, size_t len) {
uint8_t buf[32];
while (len > 0) {
const size_t chunk = std::min(len, sizeof(buf));
progmem_memcpy(buf, data, chunk);
this->write_array(buf, chunk);
data += chunk;
len -= chunk;
}
}
#endif
} // namespace esphome::uart
+29
View File
@@ -23,6 +23,14 @@ enum UARTParityOptions {
UART_CONFIG_PARITY_SPACE,
};
// A runtime setting like CRTSCTS and IXON/IXOFF in termios. A backend without the lines
// keeps the value and ignores it, as a Linux driver does.
enum UARTFlowControl : uint8_t {
UART_FLOW_CONTROL_NONE,
UART_FLOW_CONTROL_RTS_CTS,
UART_FLOW_CONTROL_XON_XOFF,
};
#ifdef USE_UART_DEBUGGER
enum UARTDirection {
UART_DIRECTION_RX,
@@ -65,6 +73,13 @@ class UARTComponent {
// @param len Length of the array.
virtual void write_array(const uint8_t *data, size_t len) = 0;
// Writes bytes that may live in PROGMEM; ESP8266 cannot read flash bytewise, so it copies chunks.
#ifdef USE_ESP8266
void write_array_progmem(const uint8_t *data, size_t len);
#else
void write_array_progmem(const uint8_t *data, size_t len) { this->write_array(data, len); }
#endif
// Reads a single byte from the UART bus.
// @param data Pointer to the byte where the read data will be stored.
// @return True if a byte was successfully read, false otherwise.
@@ -89,6 +104,10 @@ class UARTComponent {
// Platforms that cannot tell return SIZE_MAX: write_array() takes everything and may block.
virtual size_t available_for_write() { return SIZE_MAX; }
// Bytes a write can take now. Where available_for_write() cannot tell (SIZE_MAX), paces to the line time since
// last_write_ms, a loop start time: at most one loop interval and 4 s, at least one byte.
size_t paced_write_room(uint32_t last_write_ms);
// Pure virtual method to block until all bytes have been written to the UART bus.
// @return UARTFlushResult indicating whether the flush was confirmed, timed out, failed, or assumed successful.
virtual UARTFlushResult flush() = 0;
@@ -174,6 +193,15 @@ class UARTComponent {
// @return Baud rate in bits per second.
uint32_t get_baud_rate() const { return baud_rate_; }
// Set the line handshaking (RTS/CTS or XON/XOFF) used in UART communication. Unrelated to
// flow_control_pin, the RS485 driver enable; backends without handshaking ignore it.
// @param flow_control Flow control option.
void set_flow_control(UARTFlowControl flow_control) { this->flow_control_ = flow_control; }
// Get the flow control used in UART communication.
// @return Flow control option.
UARTFlowControl get_flow_control() const { return this->flow_control_; }
#if defined(USE_ESP8266) || defined(USE_ESP32)
/**
* Load the UART settings.
@@ -220,6 +248,7 @@ class UARTComponent {
uint32_t baud_rate_{0};
uint8_t stop_bits_{0};
uint8_t data_bits_{0};
UARTFlowControl flow_control_{UART_FLOW_CONTROL_NONE};
UARTParityOptions parity_{UART_CONFIG_PARITY_NONE}; // Must match the parity check in __init__.py
#ifdef USE_UART_DEBUGGER
CallbackManager<void(UARTDirection, uint8_t)> debug_callback_{};
+79 -22
View File
@@ -1,6 +1,7 @@
#ifdef USE_ESP8266
#include "uart_component_esp8266.h"
#include <esp8266_peri.h>
#include <optional>
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
@@ -18,12 +19,23 @@ namespace esphome::uart {
ESPHOME_LOG_TAG(TAG, "uart");
// Edge decoder up to this baud rate; at 38400 WiFi interrupt latency corrupts bytes.
static constexpr uint32_t SW_SERIAL_EDGE_MODE_MAX_BAUD = 19200;
// TX locks interrupts per bit up to this baud rate and per byte above it. At 38400 byte locks still trip the Wi-Fi
// watchdog when every byte is below 0x80, while an interrupt between bits leaves the bytes intact; at 115200 it
// stretches bits enough to corrupt them.
static constexpr uint32_t SW_SERIAL_TX_BIT_LOCK_MAX_BAUD = 38400;
// Edge decoder time base: 1/16 us from the 1 MHz system timer, which keeps its rate during a
// CpuFrequencyBoost; the cycle counter does not.
static constexpr uint32_t RX_TICKS_PER_SECOND = 16000000;
// A byte lock lets pending interrupts run right after an edge when the next edge is at least this far away, so their
// delay lands inside a run of equal bits. Locks longer than about 6 bits at 57600 trip the Wi-Fi watchdog.
static constexpr uint32_t TX_UNLOCK_MIN_RUN_CYCLES = F_CPU / 1000000 * 30;
// Longest per-bit TX interrupt lock; the Wi-Fi interrupt handler asserts (hardware WDT) when held off about 1 ms
static constexpr uint32_t MAX_TX_LOCK_CYCLES = F_CPU / 1000000 * 200;
__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 +123,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 +146,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_);
@@ -169,6 +187,13 @@ void ESP8266UartComponent::dump_config() {
this->baud_rate_ <= SW_SERIAL_EDGE_MODE_MAX_BAUD ? LOG_STR_LITERAL("edge")
: LOG_STR_LITERAL("start bit sampler"));
}
if (this->tx_pin_ != nullptr && this->baud_rate_ <= SW_SERIAL_TX_BIT_LOCK_MAX_BAUD &&
this->sw_serial_->locks_whole_frame()) {
ESP_LOGW(TAG,
" TX locks interrupts for whole bytes because a software UART receives above %u baud; Wi-Fi "
"watchdog resets are possible under load",
(unsigned) SW_SERIAL_EDGE_MODE_MAX_BAUD);
}
}
this->check_logger_conflict();
}
@@ -253,10 +278,19 @@ void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_p
// load_settings() re-enters here
if (this->gpio_rx_pin_ != nullptr)
this->gpio_rx_pin_->detach_interrupt();
// This instance counted itself if its RX ran the whole-byte interrupt
if (this->blocking_rx_ && this->gpio_rx_pin_ != nullptr)
blocking_rx_count--;
// RX disabled: forget the old pin, so a later reload does not remove this instance from the count again
if (rx_pin == nullptr)
this->gpio_rx_pin_ = nullptr;
this->bit_time_ = F_CPU / baud_rate;
this->stop_bits_ = stop_bits;
this->data_bits_ = data_bits;
this->parity_ = parity;
const bool edge_mode = baud_rate <= SW_SERIAL_EDGE_MODE_MAX_BAUD;
this->blocking_rx_ = !edge_mode;
this->lock_frame_ = baud_rate > SW_SERIAL_TX_BIT_LOCK_MAX_BAUD;
// to_isr() allocates, so call it only for a new or changed pin
if (tx_pin != nullptr) {
if (tx_pin != this->gpio_tx_pin_)
@@ -278,10 +312,11 @@ void ESP8266SoftwareSerial::setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_p
this->rx_.setup((RX_TICKS_PER_SECOND + baud_rate / 2) / baud_rate, data_bits, parity != UART_CONFIG_PARITY_NONE,
stop_bits, buffer, rx_buffer_size);
this->rx_.reset(rx_now(), this->rx_pin_.digital_read());
if (baud_rate <= SW_SERIAL_EDGE_MODE_MAX_BAUD) {
if (edge_mode) {
gpio_rx_pin_->attach_interrupt(ESP8266SoftwareSerial::gpio_intr_edge, this, gpio::INTERRUPT_ANY_EDGE);
} else {
gpio_rx_pin_->attach_interrupt(ESP8266SoftwareSerial::gpio_intr, this, gpio::INTERRUPT_FALLING_EDGE);
blocking_rx_count++;
}
}
}
@@ -361,27 +396,44 @@ void IRAM_ATTR HOT ESP8266SoftwareSerial::write_byte(uint8_t data) {
need_parity_bit = false;
}
{
// Transmit runs from the main loop and never overlaps a CpuFrequencyBoost
InterruptLock lock;
const uint32_t bit_time = this->bit_time_;
uint32_t wait = bit_time;
const uint32_t start = arch_get_cpu_cycle_count();
// Start bit
this->write_bit_(false, &wait, start, bit_time);
for (int i = 0; i < this->data_bits_; i++) {
bool bit = data & (1 << i);
this->write_bit_(bit, &wait, start, bit_time);
if (need_parity_bit)
parity_bit ^= bit;
}
if (need_parity_bit)
this->write_bit_(parity_bit, &wait, start, bit_time);
// Stop bit
this->write_bit_(true, &wait, start, bit_time);
if (this->stop_bits_ == 2)
this->wait_(&wait, start, bit_time);
// Transmit runs from the main loop and never overlaps a CpuFrequencyBoost
const uint32_t bit_time = this->bit_time_;
// Data bits LSB first, then the parity bit
uint32_t frame = data & ((1u << this->data_bits_) - 1);
uint32_t num_bits = this->data_bits_;
if (need_parity_bit) {
parity_bit ^= __builtin_parity(frame);
frame |= uint32_t(parity_bit) << num_bits++;
}
// Decided before the start bit so the timed loop only tests a bit
const SoftwareSerialTxFrame tx = software_serial_tx_frame(frame, num_bits, bit_time, TX_UNLOCK_MIN_RUN_CYCLES,
this->lock_frame_ && blocking_rx_count == 0);
const uint32_t line = tx.line;
const uint32_t high_from = tx.high_from;
const uint32_t unlock_at = tx.unlock_at;
std::optional<InterruptLock> frame_lock;
if (this->locks_whole_frame())
frame_lock.emplace();
uint32_t start;
{
InterruptLock lock;
// Start bit
this->tx_pin_.digital_write(false);
start = arch_get_cpu_cycle_count();
}
uint32_t wait = bit_time;
for (uint32_t pos = 0; pos <= high_from; pos++) {
if (pos != 0)
this->write_bit_((line >> pos) & 1, &wait, start, bit_time);
if ((unlock_at >> pos) & 1)
frame_lock.emplace(); // drops the held lock first, so pending interrupts run
}
this->write_bit_(true, &wait, start, bit_time);
// Hold that high run with interrupts on: one that runs past it only delays the next start bit, and back-to-back
// byte locks with no gap between them trip the Wi-Fi watchdog
frame_lock.reset();
for (uint32_t i = high_from; i < num_bits + this->stop_bits_; i++)
this->wait_(&wait, start, bit_time);
}
void IRAM_ATTR ESP8266SoftwareSerial::wait_(uint32_t *wait, const uint32_t &start, uint32_t bit_time) {
while (arch_get_cpu_cycle_count() - start < *wait)
@@ -392,9 +444,14 @@ bool IRAM_ATTR ESP8266SoftwareSerial::read_bit_(uint32_t *wait, const uint32_t &
this->wait_(wait, start, bit_time);
return this->rx_pin_.digital_read();
}
// Locks only from MAX_TX_LOCK_CYCLES before the edge to the edge, so a pending interrupt runs right after it; a bit
// longer than that (below about 5000 baud) spins with interrupts on first. Nests inside a frame lock.
void IRAM_ATTR ESP8266SoftwareSerial::write_bit_(bool bit, uint32_t *wait, const uint32_t &start, uint32_t bit_time) {
this->tx_pin_.digital_write(bit);
while (arch_get_cpu_cycle_count() - start + MAX_TX_LOCK_CYCLES < *wait)
;
InterruptLock lock;
this->wait_(wait, start, bit_time);
this->tx_pin_.digital_write(bit);
}
uint8_t ESP8266SoftwareSerial::read_byte() {
this->rx_sync_();
+10 -1
View File
@@ -8,6 +8,7 @@
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "software_serial_rx_decoder.h"
#include "software_serial_tx_frame.h"
#include "uart_component.h"
namespace esphome::uart {
@@ -26,6 +27,10 @@ class ESP8266SoftwareSerial {
size_t available();
/// TX holds interrupts off for each byte up to the edge into its trailing run of 1 bits, rather than per bit: above
/// the per-bit lock limit, or while any instance's RX interrupt reads whole bytes (it must not run between TX edges).
bool locks_whole_frame() const { return this->lock_frame_ || blocking_rx_count != 0; }
protected:
/// Above SW_SERIAL_EDGE_MODE_MAX_BAUD; reads the whole byte in the ISR.
static void gpio_intr(ESP8266SoftwareSerial *arg);
@@ -51,6 +56,9 @@ class ESP8266SoftwareSerial {
UARTParityOptions parity_;
uint8_t stop_bits_;
uint8_t data_bits_;
bool lock_frame_{false};
bool blocking_rx_{false}; // this instance's RX reads whole bytes in its interrupt
static inline uint8_t blocking_rx_count = 0; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
};
class ESP8266UartComponent final : public UARTComponent, public Component {
@@ -92,7 +100,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
+18 -1
View File
@@ -23,7 +23,7 @@
namespace esphome::uart {
static const char *const TAG = "uart";
ESPHOME_LOG_TAG(TAG, "uart");
/// Check if a pin number matches one of the default UART0 GPIO pins.
/// These pins may have residual IOMUX state from the ROM bootloader that
@@ -238,6 +238,10 @@ void IDFUARTComponent::load_settings(bool dump_config) {
this->mark_failed();
return;
}
#ifdef USE_GPIO_HOLD
// Release held pins so the UART peripheral can drive them
this->set_pins_hold_(false);
#endif
#ifdef USE_UART_WAKE_LOOP_ON_RX
// Register ISR callback to wake the main loop when UART data arrives.
@@ -514,7 +518,20 @@ void IDFUARTComponent::on_shutdown() {
if (err != ESP_OK) {
ESP_LOGW(TAG, "uart_driver_delete failed: %s", esp_err_to_name(err));
}
#ifdef USE_GPIO_HOLD
// Hold the pins again so they keep their state through the reset
this->set_pins_hold_(true);
#endif
}
#ifdef USE_GPIO_HOLD
void IDFUARTComponent::set_pins_hold_(bool hold) {
for (auto *pin : {this->tx_pin_, this->rx_pin_, this->flow_control_pin_}) {
if (pin != nullptr)
pin->set_hold(hold);
}
}
#endif
} // namespace esphome::uart
#endif // USE_ESP32
+3
View File
@@ -90,6 +90,9 @@ class IDFUARTComponent final : public UARTComponent, public Component {
protected:
void check_logger_conflict() override;
#ifdef USE_GPIO_HOLD
void set_pins_hold_(bool hold);
#endif
uint32_t line_inversion_mask_();
// Re-applies what uart_param_config() resets: inversion, RX threshold/timeout, mode.
esp_err_t apply_line_settings_();
+35 -10
View File
@@ -9,11 +9,11 @@
#error This HostUartComponent implementation is not supported on this host OS
#endif
#include <stdio.h>
#include <string.h>
#include <cerrno>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
@@ -98,7 +98,7 @@ speed_t get_baud(int baud) {
namespace esphome::uart {
static const char *const TAG = "uart";
ESPHOME_LOG_TAG(TAG, "uart");
HostUartComponent::~HostUartComponent() {
if (this->file_descriptor_ != -1) {
@@ -162,11 +162,7 @@ void HostUartComponent::setup() {
break;
};
// Set stop bits
if (this->stop_bits_ == 2) {
options.c_cflag |= CSTOPB;
} else {
options.c_cflag &= ~CSTOPB;
}
assign_bits(options.c_cflag, CSTOPB, this->stop_bits_ == 2);
cfsetispeed(&options, baud);
cfsetospeed(&options, baud);
tcsetattr(this->file_descriptor_, TCSANOW, &options);
@@ -210,7 +206,6 @@ void HostUartComponent::write_array(const uint8_t *data, size_t len) {
this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
}
#endif
return;
}
bool HostUartComponent::peek_byte(uint8_t *data) {
@@ -286,6 +281,36 @@ UARTFlushResult HostUartComponent::flush() {
return UARTFlushResult::UART_FLUSH_RESULT_SUCCESS;
}
bool HostUartComponent::set_modem_control(bool dtr, bool rts) {
if (this->file_descriptor_ == -1) {
return false;
}
// One TIOCMSET so the device never sees a state in between the old and the new one
int bits;
if (ioctl(this->file_descriptor_, TIOCMGET, &bits) == -1) {
this->update_error_(strerror(errno));
return false;
}
bits = (bits & ~(TIOCM_DTR | TIOCM_RTS)) | (dtr ? TIOCM_DTR : 0) | (rts ? TIOCM_RTS : 0);
if (ioctl(this->file_descriptor_, TIOCMSET, &bits) == -1) {
this->update_error_(strerror(errno));
return false;
}
return true;
}
bool HostUartComponent::get_dtr() const { return (this->get_modem_bits_() & TIOCM_DTR) != 0; }
bool HostUartComponent::get_rts() const { return (this->get_modem_bits_() & TIOCM_RTS) != 0; }
int HostUartComponent::get_modem_bits_() const {
int bits = 0;
if (this->file_descriptor_ != -1) {
ioctl(this->file_descriptor_, TIOCMGET, &bits);
}
return bits;
}
void HostUartComponent::update_error_(const std::string &error) {
if (this->first_error_.empty()) {
this->first_error_ = error;
+9 -2
View File
@@ -2,6 +2,8 @@
#ifdef USE_HOST
#include <utility>
#include "esphome/core/component.h"
#include "esphome/core/log.h"
#include "uart_component.h"
@@ -10,7 +12,7 @@ namespace esphome::uart {
class HostUartComponent final : public UARTComponent, public Component {
public:
virtual ~HostUartComponent();
~HostUartComponent();
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::BUS; }
@@ -19,10 +21,15 @@ class HostUartComponent final : public UARTComponent, public Component {
bool read_array(uint8_t *data, size_t len) override;
size_t available() override;
UARTFlushResult flush() override;
void set_name(std::string port_name) { port_name_ = port_name; };
/// Drive DTR/RTS. Returns false when the port is closed or the ioctl fails.
bool set_modem_control(bool dtr, bool rts);
bool get_dtr() const;
bool get_rts() const;
void set_name(std::string port_name) { this->port_name_ = std::move(port_name); }
protected:
void update_error_(const std::string &error);
int get_modem_bits_() const;
void check_logger_conflict() override {}
std::string port_name_;
std::string first_error_;
+1 -1
View File
@@ -18,7 +18,7 @@
namespace esphome::uart {
static const char *const TAG = "uart";
ESPHOME_LOG_TAG(TAG, "uart");
static const char *const UART_TYPE[] = {
"hardware",
+1 -1
View File
@@ -13,7 +13,7 @@
namespace esphome::uart {
static const char *const TAG = "uart";
ESPHOME_LOG_TAG(TAG, "uart");
uint16_t RP2UartComponent::get_config() {
uint16_t config = 0;
+5 -4
View File
@@ -3,12 +3,13 @@
#include <vector>
#include "uart_debugger.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome::uart {
static const char *const TAG = "uart_debug";
ESPHOME_LOG_TAG(TAG, "uart_debug");
UARTDebugger::UARTDebugger(UARTComponent *parent) {
parent->add_debug_callback([this](UARTDirection direction, uint8_t byte) {
@@ -44,15 +45,15 @@ void UARTDebugger::store_byte_(UARTDirection direction, uint8_t byte) {
}
void UARTDebugger::trigger_after_delimiter_(uint8_t byte) {
if (this->after_delimiter_.empty() || !this->has_buffered_bytes_()) {
if (this->after_delimiter_len_ == 0 || !this->has_buffered_bytes_()) {
return;
}
if (this->after_delimiter_[this->after_delimiter_pos_] != byte) {
if (progmem_read_byte(this->after_delimiter_ + this->after_delimiter_pos_) != byte) {
this->after_delimiter_pos_ = 0;
return;
}
this->after_delimiter_pos_++;
if (this->after_delimiter_pos_ == this->after_delimiter_.size()) {
if (this->after_delimiter_pos_ == this->after_delimiter_len_) {
this->fire_trigger_();
this->after_delimiter_pos_ = 0;
}
+10 -7
View File
@@ -36,11 +36,13 @@ class UARTDebugger final : public Component, public Trigger<UARTDirection, std::
/// this timeout, logging will be triggered.
void set_after_timeout(uint32_t timeout) { this->after_timeout_ = timeout; }
/// Add a delimiter byte. This can be called multiple times to setup a
/// multi-byte delimiter (a typical example would be '\r\n').
/// When the constructed byte sequence is found in the data stream,
/// logging will be triggered.
void add_delimiter_byte(uint8_t byte) { this->after_delimiter_.push_back(byte); }
/// Set a delimiter byte sequence (a typical example would be '\r\n').
/// When the sequence is found in the data stream, logging will be triggered.
/// `delimiter` is a codegen PROGMEM table that must outlive the debugger.
void set_after_delimiter(const uint8_t *delimiter, uint8_t len) {
this->after_delimiter_ = delimiter;
this->after_delimiter_len_ = len;
}
void set_debug_prefix(const char *prefix) { this->debug_prefix_ = StringRef(prefix); }
@@ -51,8 +53,9 @@ class UARTDebugger final : public Component, public Trigger<UARTDirection, std::
size_t after_bytes_;
uint32_t after_timeout_;
uint32_t last_time_{};
std::vector<uint8_t> after_delimiter_{};
size_t after_delimiter_pos_{};
const uint8_t *after_delimiter_{nullptr};
uint8_t after_delimiter_len_{0};
uint8_t after_delimiter_pos_{0};
bool is_triggering_{false};
StringRef debug_prefix_{};