ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
software_serial_tx_frame.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
6
7namespace esphome::uart {
8
19
21ESPHOME_ALWAYS_INLINE inline SoftwareSerialTxFrame software_serial_tx_frame(uint32_t frame, uint32_t num_bits,
22 uint32_t bit_time, uint32_t min_run_cycles,
23 bool find_unlocks) {
25 tx.line = frame << 1 | (1u << (num_bits + 1));
26 // Slot 0 is the start bit, always 0, so the argument is never 0
27 tx.high_from = 31 - __builtin_clz(~tx.line & ((2u << num_bits) - 1));
28 tx.unlock_at = 0;
29 if (find_unlocks) {
30 const uint32_t edges = tx.line ^ (tx.line << 1 | 1); // the idle line before the start bit is high
31 for (uint32_t pos = 0; pos <= tx.high_from; pos++) {
32 if (((edges >> pos) & 1) && (__builtin_ctz(edges >> (pos + 1)) + 1) * bit_time >= min_run_cycles)
33 tx.unlock_at |= 1u << pos;
34 }
35 }
36 return tx;
37}
38
39} // namespace esphome::uart
ESPHOME_ALWAYS_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)
frame holds num_bits data and parity bits, LSB first. unlock_at stays 0 unless find_unlocks.
size_t size_t pos
Definition helpers.h:1333
static void uint32_t
One software serial TX byte as line levels, one bit per slot.
uint32_t unlock_at
Slots right after an edge into a run of equal bits lasting at least min_run_cycles,...
uint32_t line
The start bit at slot 0, then the frame, then a stop bit that ends the last run.
uint32_t high_from
Slot of the last 0; from the edge after it the line stays high through the stop bits.