15PROGMEM_STRING_TABLE(CrcTypeStrings,
"none",
"sum8",
"sum16_big_endian",
"sum16_little_endian",
"xor8",
"crc16_modbus");
32 ESP_LOGV(TAG,
"Intra-frame timeout, resetting receive state");
39 if (this->flags_.tx_start_pending && now - this->tx_delay_start_ >= this->tx_gate_delay_ &&
40 this->gate_allows_send_(now)) {
41 this->flags_.tx_start_pending =
false;
47 uint8_t gate[MAX_FRAME_TYPE_LEN];
53 " Framing: DLE=0x%02x STX=0x%02x ETX=0x%02x, escape=0x%02x\n"
54 " CRC type: %s, TX CRC variant: %s, accept header CRC: %s, accept payload CRC: %s\n"
55 " TX gate: %s, gate frame: %s, gate delay: %ums\n"
56 " TX gate interval: %ums\n"
57 " Queue policy: %s, queue size: %u\n"
58 " Max frame length: %u, frame timeout: %ums\n"
59 " Sniffer only: %s, dump frames: %s",
61 LOG_STR_ARG(CrcTypeStrings::get_log_str(this->
crc_type_, 0)),
63 YESNO(this->flags_.accept_header_crc), YESNO(this->flags_.accept_payload_crc),
67 YESNO(this->
tx_queue_ ==
nullptr), YESNO(this->flags_.dump_frames));
72 ESP_LOGW(TAG,
"Ignoring frame because sniffer_only is enabled");
75 const size_t len = frame_type.size() + payload.size();
77 ESP_LOGW(TAG,
"Frame length %zu is outside %zu..max_frame_length (%u); dropping",
len, MIN_FRAME_LENGTH,
78 this->max_frame_length_);
84 ESP_LOGW(TAG,
"Frame queue full; dropping frame");
93 ESP_LOGW(TAG,
"Frame queued while the previous frame was being sent; dropping frame");
98 ESP_LOGV(TAG,
"Replacing pending frame before it was transmitted");
102 uint8_t *slot = this->
tx_slot_(index);
104 progmem_memcpy(slot + frame_type.size(), payload.data(), payload.size());
118 this->flags_.tx_start_pending =
false;
126 if (
byte == this->
dle_) {
133 if (
byte == this->
stx_) {
141 if (
byte == this->
stx_) {
144 }
else if (
byte == this->
etx_) {
159 ESP_LOGW(TAG,
"Frame exceeded max_frame_length; resetting");
172 if (this->flags_.dump_frames)
185 (this->flags_.accept_payload_crc && this->crc_matches_(
false));
195 const uint8_t header[2] = {this->
dle_, this->
stx_};
196 const size_t header_len = include_header ?
sizeof(header) : 0;
228 if (this->flags_.tx_start_pending || this->tx_queue_count_ == 0)
231 this->flags_.tx_start_pending =
true;
236 const uint8_t *frame = this->
tx_slot_(slot);
243 static constexpr size_t TX_CHUNK_SIZE = 32;
244 uint8_t chunk[TX_CHUNK_SIZE];
247 auto put = [&](uint8_t byte) {
249 if (
pos == TX_CHUNK_SIZE) {
259#if defined(USE_ESP8266) || defined(USE_RP2)
263 if (this->flags_.dump_frames)
278 return std::min<uint32_t>((
len * bits * 1000 + baud - 1) / baud, UINT16_MAX);
282#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
283 static constexpr size_t BYTES_PER_LINE = 32;
284 uint8_t
line[BYTES_PER_LINE];
287 auto log_line = [&]() {
289 ESP_LOGD(TAG,
"%s %s", LOG_STR_ARG(dir), hex);
294 if (
pos == BYTES_PER_LINE)
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
const uint8_t * tx_gate_frame_type_
void process_frame_(uint32_t now)
void handle_rx_byte_(uint8_t byte, uint32_t now)
uint16_t in_frame_timeout_ms_
void dump_config() override
void encode_crc_(const uint8_t *data, size_t len, bool include_header, uint8_t *out) const
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.
uint16_t *const tx_queue_len_
uint8_t * tx_slot_(size_t index) const
uint8_t tx_gate_frame_type_len_
bool crc_matches_(bool include_header) const
void send_next_(uint32_t now)
uint16_t last_tx_wire_ms_
void maybe_tx_(uint32_t now)
uint8_t *const rx_payload_
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
void append_rx_(uint8_t byte)
CrcVariant tx_crc_variant_
uint16_t max_frame_length_
LazyCallbackManager< void(std::span< const uint8_t >)> frame_callback_
QueuePolicy queue_policy_
void transmit_head_(uint32_t now)
uint16_t tx_gate_interval_
uint32_t last_activity_time_
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.
UARTParityOptions get_parity() const
uint8_t get_data_bits() const
uint8_t get_stop_bits() const
uint32_t get_baud_rate() const
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
void write_array(const uint8_t *data, size_t len)
std::vector< uint8_t > & out
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.
RxState
The _DLE states mean the last byte was a DLE.
@ CRC_TYPE_SUM16_LITTLE_ENDIAN
@ CRC_TYPE_SUM16_BIG_ENDIAN
ESPHOME_LOG_TAG(TAG, "rs485_frame")
PROGMEM_STRING_TABLE(CrcTypeStrings, "none", "sum8", "sum16_big_endian", "sum16_little_endian", "xor8", "crc16_modbus")
@ UART_CONFIG_PARITY_NONE
constexpr uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init=0)
XOR the bytes of data with size len, starting from init.
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
constexpr uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t sum=0)
Add up the bytes of data with size len, truncated to 8 bits: the checksum many serial and IR protocol...
void progmem_memcpy(void *dst, const void *src, size_t len)
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
uint32_t IRAM_ATTR HOT millis()
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Application App
Global storage of Application pointer - only one Application can exist.
constexpr uint16_t sum16_bytes(const uint8_t *data, size_t len, uint16_t sum=0)
Add up the bytes of data with size len, truncated to 16 bits.
constexpr uint8_t * put_le16(uint8_t *dst, uint16_t value)