ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
rs485_frame.h
Go to the documentation of this file.
1#pragma once
2
4#include "esphome/core/hal.h"
7
8#include <span>
9
10namespace esphome::rs485_frame {
11
12// Must agree with MAX_FRAME_TYPE_LEN in __init__.py, which caps frame_type.
13static constexpr size_t MAX_FRAME_TYPE_LEN = 8;
14// Must agree with MIN_FRAME_LENGTH in __init__.py.
15static constexpr size_t MIN_FRAME_LENGTH = 2;
16static constexpr uint8_t NO_SLOT = 0xFF;
17
27constexpr uint8_t crc_length(CrcType type) {
28 return type == CRC_TYPE_NONE ? 0 : (type == CRC_TYPE_SUM8 || type == CRC_TYPE_XOR8) ? 1 : 2;
29}
34
36inline bool frame_has_prefix(std::span<const uint8_t> payload, const uint8_t *prefix, size_t len) {
37 if (payload.size() < len)
38 return false;
39 for (size_t i = 0; i < len; i++) {
40 if (payload[i] != progmem_read_byte(prefix + i))
41 return false;
42 }
43 return true;
44}
45
48 public:
50 RS485FrameHub(uint8_t dle, uint8_t stx, uint8_t etx, uint8_t escape_marker, CrcType crc_type,
51 uint16_t max_frame_length, uint8_t max_queue_size, uint8_t *rx_payload, uint8_t *tx_queue,
52 uint16_t *tx_queue_len)
53 : rx_payload_(rx_payload),
54 tx_queue_(tx_queue),
55 tx_queue_len_(tx_queue_len),
56 max_frame_length_(max_frame_length),
57 dle_(dle),
58 stx_(stx),
59 etx_(etx),
60 escape_marker_(escape_marker),
61 crc_type_(crc_type),
62 crc_len_(crc_length(crc_type)),
63 max_queue_size_(max_queue_size) {}
64
65 void setup() override;
66 void loop() override;
67 void dump_config() override;
68
69 void set_accept_header_crc(bool accept) { this->flags_.accept_header_crc = accept; }
70 void set_accept_payload_crc(bool accept) { this->flags_.accept_payload_crc = accept; }
71 void set_tx_crc_variant(CrcVariant variant) { this->tx_crc_variant_ = variant; }
73 void set_tx_gate_frame_type(const uint8_t *frame_type, size_t len) {
74 this->tx_gate_frame_type_ = frame_type;
76 }
77 void set_tx_gate_delay(uint16_t delay) { this->tx_gate_delay_ = delay; }
78 void set_tx_gate_interval(uint16_t interval) { this->tx_gate_interval_ = interval; }
79 void set_queue_policy(QueuePolicy policy) { this->queue_policy_ = policy; }
80 void set_dump_frames(bool dump_frames) { this->flags_.dump_frames = dump_frames; }
81 void set_in_frame_timeout(uint16_t ms) { this->in_frame_timeout_ms_ = ms; }
82
84 bool queue_frame(const uint8_t *data, size_t len) { return this->queue_raw_frame({data, len}, {}); }
86 bool queue_raw_frame(std::span<const uint8_t> frame_type, std::span<const uint8_t> payload);
88 template<typename F> void add_on_frame_callback(F &&callback) {
89 this->frame_callback_.add(std::forward<F>(callback));
90 }
91
92 protected:
93 void handle_rx_byte_(uint8_t byte, uint32_t now);
94 void append_rx_(uint8_t byte);
95 void process_frame_(uint32_t now);
96 void reset_rx_();
97 bool check_crc_() const;
98 bool crc_matches_(bool include_header) const;
99 void encode_crc_(const uint8_t *data, size_t len, bool include_header, uint8_t *out) const;
100 uint8_t *tx_slot_(size_t index) const { return this->tx_queue_ + index * (this->max_frame_length_ + this->crc_len_); }
101 void maybe_tx_(uint32_t now);
102 bool bus_silent_(uint32_t now) const { return now - this->last_activity_time_ >= this->tx_gate_interval_; }
103 void send_next_(uint32_t now);
104 void transmit_head_(uint32_t now);
106 template<typename F> void encode_wire_(const uint8_t *data, size_t len, F &&put) const {
107 put(this->dle_);
108 put(this->stx_);
109 for (size_t i = 0; i < len; i++) {
110 put(data[i]);
111 if (data[i] == this->dle_)
112 put(this->escape_marker_);
113 }
114 put(this->dle_);
115 put(this->etx_);
116 }
119 return this->tx_gate_mode_ == TX_GATE_FIXED_DELAY ||
120 ((this->tx_gate_mode_ != TX_GATE_IDLE_GAP || this->bus_silent_(now)) && this->available() == 0);
121 }
122 uint16_t wire_time_ms_(size_t len) const;
123 void log_frame_(const LogString *dir, const uint8_t *data, size_t len);
124
125 uint8_t *const rx_payload_;
126 // Ring of unescaped frames plus CRC; while tx_start_pending the head is waiting out the gate delay.
127 uint8_t *const tx_queue_;
128 uint16_t *const tx_queue_len_;
129 const uint8_t *tx_gate_frame_type_{nullptr};
130 LazyCallbackManager<void(std::span<const uint8_t>)> frame_callback_;
133 uint32_t last_activity_time_{0}; // RX or our own TX, seeded in setup()
134 uint32_t tx_delay_start_{0}; // When the frame waiting out the gate delay was released
135 uint16_t tx_gate_delay_{0};
136 uint16_t tx_gate_interval_{0};
139 uint16_t rx_payload_len_{0};
140 uint16_t last_tx_wire_ms_{0}; // The gates count from the end of the last frame on the wire
141
142 uint8_t dle_;
143 uint8_t stx_;
144 uint8_t etx_;
145 uint8_t escape_marker_; // Equals dle_ in doubling mode
147 uint8_t crc_len_;
153 uint8_t tx_queue_head_{0};
154 uint8_t tx_queue_count_{0};
155 uint8_t tx_sending_slot_{NO_SLOT}; // queue_raw_frame() must not overwrite it
157 struct Flags {
158 uint8_t accept_header_crc : 1;
160 uint8_t dump_frames : 1;
161 uint8_t tx_start_pending : 1;
162 uint8_t reserved : 4;
163 } flags_{.accept_header_crc = true, .accept_payload_crc = true};
164};
165
166} // namespace esphome::rs485_frame
BedjetMode mode
BedJet operating mode.
Hub for a DLE-framed RS485 bus: RX framer, CRC engine, gated TX queue and on_frame callbacks.
Definition rs485_frame.h:47
bool gate_allows_send_(uint32_t now)
Whether the released frame may go out once its gate delay has passed.
void set_tx_gate_mode(TxGateMode mode)
Definition rs485_frame.h:72
void set_queue_policy(QueuePolicy policy)
Definition rs485_frame.h:79
void set_accept_payload_crc(bool accept)
Definition rs485_frame.h:70
void handle_rx_byte_(uint8_t byte, uint32_t now)
void set_dump_frames(bool dump_frames)
Definition rs485_frame.h:80
void encode_crc_(const uint8_t *data, size_t len, bool include_header, uint8_t *out) const
void set_tx_gate_frame_type(const uint8_t *frame_type, size_t len)
Definition rs485_frame.h:73
bool queue_raw_frame(std::span< const uint8_t > frame_type, std::span< const uint8_t > payload)
Frame and queue frame_type followed by payload; both may live in PROGMEM.
uint8_t * tx_slot_(size_t index) const
bool crc_matches_(bool include_header) const
void add_on_frame_callback(F &&callback)
Called with the unescaped payload (frame_type first, CRC removed) of every valid frame.
Definition rs485_frame.h:88
void set_tx_crc_variant(CrcVariant variant)
Definition rs485_frame.h:71
void log_frame_(const LogString *dir, const uint8_t *data, size_t len)
bool bus_silent_(uint32_t now) const
uint16_t wire_time_ms_(size_t len) const
bool queue_frame(const uint8_t *data, size_t len)
Frame and queue an unframed payload; data may live in PROGMEM.
Definition rs485_frame.h:84
void set_in_frame_timeout(uint16_t ms)
Definition rs485_frame.h:81
RS485FrameHub(uint8_t dle, uint8_t stx, uint8_t etx, uint8_t escape_marker, CrcType crc_type, uint16_t max_frame_length, uint8_t max_queue_size, uint8_t *rx_payload, uint8_t *tx_queue, uint16_t *tx_queue_len)
Buffers are sized by codegen; tx_queue and tx_queue_len are nullptr on a sniffer_only hub.
Definition rs485_frame.h:50
LazyCallbackManager< void(std::span< const uint8_t >)> frame_callback_
void set_accept_header_crc(bool accept)
Definition rs485_frame.h:69
void set_tx_gate_delay(uint16_t delay)
Definition rs485_frame.h:77
void set_tx_gate_interval(uint16_t interval)
Definition rs485_frame.h:78
void encode_wire_(const uint8_t *data, size_t len, F &&put) const
Passes the wire bytes of an unescaped frame to put: DLE STX, the escaped frame, DLE ETX.
uint8_t type
std::vector< uint8_t > & out
Definition gif.cpp:49
constexpr uint8_t crc_length(CrcType type)
Definition rs485_frame.h:27
bool frame_has_prefix(std::span< const uint8_t > payload, const uint8_t *prefix, size_t len)
True when payload starts with the len-byte prefix, which may live in PROGMEM.
Definition rs485_frame.h:36
RxState
The _DLE states mean the last byte was a DLE.
Definition rs485_frame.h:33
const void size_t len
Definition hal.h:68
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t