Home IO Control
ESPHome add-on for IO-Homecontrol devices
Loading...
Searching...
No Matches
radio_sx1262.h
Go to the documentation of this file.
1#pragma once
2
3/// @file radio_sx1262.h
4/// @brief SX1262 radio driver for IO-Homecontrol.
5///
6/// Implements the RadioDriver interface for the Semtech SX1262 transceiver.
7/// Unlike the SX1276, the SX1262 uses opcode-based SPI commands and requires a
8/// BUSY pin check before every SPI transaction. The capture path preserves the
9/// radio-reported RX metadata so offline analysis can work from trustworthy data.
10
11#include "radio_interface.h"
12#include "esphome/core/hal.h"
13
14namespace esphome {
15namespace home_io_control {
16
17// ============================================================================
18// SX1262 Opcode Constants
19// ============================================================================
20
21static constexpr uint8_t SX1262_SET_STANDBY = 0x80;
22static constexpr uint8_t SX1262_SET_RX = 0x82;
23static constexpr uint8_t SX1262_SET_TX = 0x83;
24static constexpr uint8_t SX1262_SET_RF_FREQUENCY = 0x86;
25static constexpr uint8_t SX1262_SET_RX_TX_FALLBACK_MODE = 0x93;
26static constexpr uint8_t SX1262_WRITE_BUFFER = 0x0E;
27static constexpr uint8_t SX1262_READ_BUFFER = 0x1E;
28static constexpr uint8_t SX1262_SET_DIO_IRQ_PARAMS = 0x08;
29static constexpr uint8_t SX1262_GET_IRQ_STATUS = 0x12;
30static constexpr uint8_t SX1262_GET_PACKET_STATUS = 0x14;
31static constexpr uint8_t SX1262_GET_DEVICE_ERRORS = 0x17;
32static constexpr uint8_t SX1262_CLEAR_IRQ_STATUS = 0x02;
33static constexpr uint8_t SX1262_CLEAR_DEVICE_ERRORS = 0x07;
34static constexpr uint8_t SX1262_SET_PACKET_TYPE = 0x8A;
35static constexpr uint8_t SX1262_SET_MODULATION_PARAMS = 0x8B;
36static constexpr uint8_t SX1262_SET_PACKET_PARAMS = 0x8C;
37static constexpr uint8_t SX1262_SET_BUFFER_BASE_ADDRESS = 0x8F;
38static constexpr uint8_t SX1262_SET_PA_CONFIG = 0x95;
39static constexpr uint8_t SX1262_SET_TX_PARAMS = 0x8E;
40static constexpr uint8_t SX1262_SET_DIO2_AS_RF_SWITCH_CTRL = 0x9D;
41static constexpr uint8_t SX1262_SET_DIO3_AS_TCXO_CTRL = 0x97;
42static constexpr uint8_t SX1262_CALIBRATE = 0x89;
43static constexpr uint8_t SX1262_CALIBRATE_IMAGE = 0x98;
44static constexpr uint8_t SX1262_GET_RX_BUFFER_STATUS = 0x13;
45static constexpr uint8_t SX1262_GET_RSSI_INST = 0x15;
46static constexpr uint8_t SX1262_SET_REGULATOR_MODE = 0x96;
47static constexpr uint8_t SX1262_GET_STATUS = 0xC0;
48
49// SPI register access opcodes
50static constexpr uint8_t SX1262_WRITE_REGISTER = 0x0D;
51static constexpr uint8_t SX1262_READ_REGISTER = 0x1D;
52
53// ============================================================================
54// SX1262 IRQ Bit Masks
55// ============================================================================
56
57static constexpr uint16_t SX1262_IRQ_TX_DONE = 0x0001;
58static constexpr uint16_t SX1262_IRQ_RX_DONE = 0x0002;
59static constexpr uint16_t SX1262_IRQ_SYNC_WORD_VALID = 0x0008;
60static constexpr uint16_t SX1262_IRQ_CRC_ERR = 0x0040;
61// Sync word register base address
62static constexpr uint16_t SX1262_REG_SYNC_WORD = 0x06C0;
63static constexpr uint16_t SX1262_REG_RX_GAIN = 0x08AC;
64static constexpr uint16_t SX1262_REG_TX_CLAMP_CONFIG = 0x08D8;
65
66static constexpr uint8_t SX1262_GFSK_PACKET_TYPE_KNOWN_LENGTH = 0x00;
67static constexpr uint8_t SX1262_GFSK_CRC_OFF = 0x01;
68static constexpr uint8_t SX1262_FALLBACK_STDBY_XOSC = 0x30;
69
70// ============================================================================
71// SX1262 Radio Driver
72// ============================================================================
73
74/// @brief SX1262 implementation of RadioDriver.
75///
76/// Manages the SX1262 via opcode‑based SPI using the SpiAccess interface.
77/// Configures the chip in FSK mode with software CRC‑CCITT to match the
78/// IO‑Homecontrol protocol (the SX1262 lacks the SX1276's IoHomeOn mode).
79class RadioSX1262 : public RadioDriver {
80 public:
81 RadioSX1262(SpiAccess *spi, InternalGPIOPin *rst_pin, InternalGPIOPin *dio1_pin, InternalGPIOPin *busy_pin,
82 uint8_t tx_power, uint8_t tcxo_voltage, InternalGPIOPin *fem_en_pin = nullptr,
83 InternalGPIOPin *vfem_pin = nullptr, InternalGPIOPin *fem_pa_pin = nullptr)
84 : RadioDriver(rst_pin),
85 spi_(spi),
86 dio1_pin_(dio1_pin),
87 busy_pin_(busy_pin),
88 tx_power_(tx_power),
89 tcxo_voltage_(tcxo_voltage),
90 fem_en_pin_(fem_en_pin),
91 vfem_pin_(vfem_pin),
92 fem_pa_pin_(fem_pa_pin) {}
93
94 /// @copydoc RadioDriver::init
95 bool init() override;
96 /// @copydoc RadioDriver::send_packet
97 bool send_packet(const uint8_t *data, uint8_t len, const RadioTxConfig &tx_config) override;
98 /// @copydoc RadioDriver::wait_for_packet
99 bool wait_for_packet(RadioRxPacket &packet, uint32_t timeout_ms) override;
100 /// @copydoc RadioDriver::check_for_packet
101 bool check_for_packet(RadioRxPacket &packet) override;
102 /// @copydoc RadioDriver::change_frequency
103 void change_frequency(uint32_t freq_hz) override;
104 /// @copydoc RadioDriver::read_rssi
105 int16_t read_rssi() override;
106 /// @copydoc RadioDriver::set_mode_rx
107 void set_mode_rx() override;
108 /// @copydoc RadioDriver::set_mode_standby
109 void set_mode_standby() override;
110 [[nodiscard]] bool is_failed() const override { return this->failed_; }
111 [[nodiscard]] const char *chip_name() const override { return "sx1262"; }
112 /// @brief Dump SX1262‑specific debug info.
113 void dump_debug() override;
114
115 protected:
116 // --- SPI communication (opcode‑based) ---
117 /// Wait until BUSY pin is low before any SPI transaction.
118 void wait_busy_();
119 /// Write an opcode with optional parameter bytes.
120 /// @param opcode SX1262 opcode.
121 /// @param params Pointer to parameter buffer (may be nullptr).
122 /// @param len Parameter length.
123 void write_opcode_(uint8_t opcode, const uint8_t *params, uint8_t len);
124 /// Read response from an opcode.
125 /// @param opcode Opcode that was previously written.
126 /// @param data Output buffer.
127 /// @param len Expected number of bytes to read.
128 void read_opcode_(uint8_t opcode, uint8_t *data, uint8_t len);
129 /// Write to a register (SX1262 uses opcodes for register access).
130 /// @param addr 16‑bit register address.
131 /// @param data Pointer to data bytes.
132 /// @param len Number of bytes.
133 void write_register_(uint16_t addr, const uint8_t *data, uint8_t len);
134 /// Read from a register.
135 /// @param addr 16‑bit register address.
136 /// @param data Output buffer.
137 /// @param len Number of bytes to read.
138 void read_register_(uint16_t addr, uint8_t *data, uint8_t len);
139 /// Write into the SX1262 TX/RX buffer at a given offset.
140 /// @param offset Buffer offset.
141 /// @param data Payload bytes.
142 /// @param len Payload length.
143 void write_buffer_(uint8_t offset, const uint8_t *data, uint8_t len);
144 /// Read from the SX1262 RX buffer.
145 /// @param offset Buffer offset.
146 /// @param data Output buffer.
147 /// @param len Number of bytes to read.
148 void read_buffer_(uint8_t offset, uint8_t *data, uint8_t len);
149
150 // --- Radio configuration ---
151 /// Full radio initialization (called from init()).
152 void configure_radio_();
153 /// Set RF frequency via the frequency register.
154 /// @param freq_hz Frequency in Hz.
155 void set_frequency_register_(uint32_t freq_hz);
156 /// Configure packet parameters (preamble, payload length, CRC).
157 /// @param preamble_len Preamble length in symbols.
158 /// @param payload_len Expected payload length.
159 /// @param packet_type Fixed for GFSK.
160 /// @param crc_type CRC configuration (off or on).
161 void set_packet_params_(uint16_t preamble_len, uint8_t payload_len, uint8_t packet_type, uint8_t crc_type);
162 /// Apply RX‑specific packet parameters (calls set_packet_params_ for RX).
164 /// Clear all IRQ status bits.
165 /// @param irq_mask Bitmask of IRQs to clear.
166 void clear_irq_status_(uint16_t irq_mask);
167 /// @brief Read device error flags (and clear them).
168 /// @return Error bitmask.
169 uint16_t get_device_errors_();
170 /// @brief Clear device error flags.
172 /// Reset RX state machine and buffer. Optionally force standby first.
173 /// @param force_standby If true, switch to standby before reset.
174 void reset_rx_state_(bool force_standby = true);
175 /// Populate the RadioCaptureInfo from SX1262‑specific telemetry.
176 /// @param blocking_wait true if this was a blocking wait.
177 /// @param irq_status Raw IRQ status.
178 /// @param rx_offset Reported RX buffer offset.
179 /// @param reported_len Length reported by the radio.
180 /// @param raw Pointer to raw buffer bytes (may be nullptr).
181 /// @param raw_len Length of raw buffer.
182 /// @param frame Pointer to parsed frame bytes (may be nullptr).
183 /// @param frame_len Length of parsed frame.
184 void fill_capture_info_(bool blocking_wait, uint16_t irq_status, uint8_t rx_offset, uint8_t reported_len,
185 const uint8_t *raw, uint8_t raw_len, const uint8_t *frame, uint8_t frame_len);
186
187 /// Read a received packet from the buffer and return the raw bytes reported by the chip.
188 /// This is virtual to allow test doubles.
189 /// @param packet Output RadioRxPacket.
190 /// @param blocking_wait true if called from a blocking wait path.
191 /// @param irq_status Raw IRQ status word.
192 /// @return true if a valid packet was extracted; false otherwise.
193 virtual bool read_rx_packet(RadioRxPacket &packet, bool blocking_wait, uint16_t irq_status);
194
195 /// Software CRC helper kept for transmit framing parity with the current implementation.
196 /// @return Number of encoded bytes, or 0 if buffer too small.
197 static uint8_t uart_encode_packet(const uint8_t *data, uint8_t len, uint8_t *encoded, uint8_t encoded_max_len);
198
199 /// DIO1 ISR — sets dio_fired flag. Runs in interrupt context.
200 static void gpio_intr(RadioSX1262 *arg);
201
202 /// @brief Read the raw IRQ status from the radio.
203 /// @return 16‑bit IRQ status word.
204 virtual uint16_t read_irq_status_raw();
205
206 private:
207 // === wait_for_packet helpers (private) ===
208 /// Poll until any radio activity (DIO1) or timeout.
209 /// @param start Starting timestamp.
210 /// @param timeout_ms Maximum wait.
211 /// @param saw_dio1 Output: true if DIO1 fired.
212 /// @param irq Output: captured IRQ status.
213 /// @return true if activity detected before timeout; false otherwise.
214 bool poll_until_activity_(uint32_t start, uint32_t timeout_ms, bool &saw_dio1, uint16_t &irq);
215 /// Resolve the race between SYNC word detection and payload‑ready by peeking
216 /// the RX buffer status and potentially restarting RX.
217 /// @param start Starting timestamp.
218 /// @param timeout_ms Maximum wait.
219 /// @param irq Output: final IRQ status when success is determined.
220 /// @return true if a frame boundary is resolved; false on timeout or error.
221 bool resolve_sync_race_(uint32_t start, uint32_t timeout_ms, uint16_t &irq);
222 /// Finalize receive: populate packet from the RX buffer and telemetry.
223 /// @param packet Output RadioRxPacket.
224 /// @param irq IRQ status at completion.
225 /// @return true if packet extraction succeeded; false otherwise.
226 bool finalize_receive_(RadioRxPacket &packet, uint16_t irq);
227
228 SpiAccess *spi_;
229 InternalGPIOPin *dio1_pin_;
230 InternalGPIOPin *busy_pin_;
231 InternalGPIOPin *fem_en_pin_;
232 InternalGPIOPin *vfem_pin_;
233 InternalGPIOPin *fem_pa_pin_;
234 uint8_t tx_power_;
235 uint8_t tcxo_voltage_;
236 bool failed_{false};
237};
238
239} // namespace home_io_control
240} // namespace esphome
RadioDriver(InternalGPIOPin *rst_pin=nullptr)
SX1262 implementation of RadioDriver.
void wait_busy_()
Wait until BUSY pin is low before any SPI transaction.
void set_mode_rx() override
Switch to continuous receive mode.
int16_t read_rssi() override
Read instantaneous RSSI (in dBm) while in RX mode.
void write_opcode_(uint8_t opcode, const uint8_t *params, uint8_t len)
Write an opcode with optional parameter bytes.
void set_mode_standby() override
Switch to standby mode.
virtual bool read_rx_packet(RadioRxPacket &packet, bool blocking_wait, uint16_t irq_status)
Read a received packet from the buffer and return the raw bytes reported by the chip.
RadioSX1262(SpiAccess *spi, InternalGPIOPin *rst_pin, InternalGPIOPin *dio1_pin, InternalGPIOPin *busy_pin, uint8_t tx_power, uint8_t tcxo_voltage, InternalGPIOPin *fem_en_pin=nullptr, InternalGPIOPin *vfem_pin=nullptr, InternalGPIOPin *fem_pa_pin=nullptr)
void change_frequency(uint32_t freq_hz) override
Change the carrier frequency using fast hop (no standby transition needed).
void write_buffer_(uint8_t offset, const uint8_t *data, uint8_t len)
Write into the SX1262 TX/RX buffer at a given offset.
const char * chip_name() const override
Get a human‑readable chip name.
void reset_rx_state_(bool force_standby=true)
Reset RX state machine and buffer.
void fill_capture_info_(bool blocking_wait, uint16_t irq_status, uint8_t rx_offset, uint8_t reported_len, const uint8_t *raw, uint8_t raw_len, const uint8_t *frame, uint8_t frame_len)
Populate the RadioCaptureInfo from SX1262‑specific telemetry.
void set_frequency_register_(uint32_t freq_hz)
Set RF frequency via the frequency register.
void clear_device_errors_()
Clear device error flags.
void write_register_(uint16_t addr, const uint8_t *data, uint8_t len)
Write to a register (SX1262 uses opcodes for register access).
void set_packet_params_(uint16_t preamble_len, uint8_t payload_len, uint8_t packet_type, uint8_t crc_type)
Configure packet parameters (preamble, payload length, CRC).
void dump_debug() override
Dump SX1262‑specific debug info.
void configure_radio_()
Full radio initialization (called from init()).
virtual uint16_t read_irq_status_raw()
Read the raw IRQ status from the radio.
static void gpio_intr(RadioSX1262 *arg)
DIO1 ISR — sets dio_fired flag. Runs in interrupt context.
bool check_for_packet(RadioRxPacket &packet) override
Non-blocking check for a received packet.
static uint8_t uart_encode_packet(const uint8_t *data, uint8_t len, uint8_t *encoded, uint8_t encoded_max_len)
Software CRC helper kept for transmit framing parity with the current implementation.
void set_rx_packet_params_()
Apply RX‑specific packet parameters (calls set_packet_params_ for RX).
void read_opcode_(uint8_t opcode, uint8_t *data, uint8_t len)
Read response from an opcode.
bool send_packet(const uint8_t *data, uint8_t len, const RadioTxConfig &tx_config) override
Send a packet using the specified carrier frequency and preamble settings.
bool init() override
Initialize the radio hardware. Returns true on success.
void clear_irq_status_(uint16_t irq_mask)
Clear all IRQ status bits.
void read_buffer_(uint8_t offset, uint8_t *data, uint8_t len)
Read from the SX1262 RX buffer.
void read_register_(uint16_t addr, uint8_t *data, uint8_t len)
Read from a register.
bool is_failed() const override
Returns true if the radio failed to initialize or encountered a fatal error.
uint16_t get_device_errors_()
Read device error flags (and clear them).
bool wait_for_packet(RadioRxPacket &packet, uint32_t timeout_ms) override
Wait (blocking) for a packet with timeout.
Interface for SPI bus access.
static constexpr uint8_t SX1262_SET_DIO3_AS_TCXO_CTRL
static constexpr uint8_t SX1262_CALIBRATE
static constexpr uint16_t SX1262_REG_RX_GAIN
static constexpr uint8_t SX1262_SET_BUFFER_BASE_ADDRESS
static constexpr uint8_t SX1262_CLEAR_DEVICE_ERRORS
static constexpr uint8_t SX1262_GFSK_CRC_OFF
static constexpr uint8_t SX1262_READ_REGISTER
static constexpr uint8_t SX1262_SET_PACKET_TYPE
static constexpr uint16_t SX1262_IRQ_SYNC_WORD_VALID
static constexpr uint8_t SX1262_GET_DEVICE_ERRORS
static constexpr uint16_t SX1262_IRQ_CRC_ERR
static constexpr uint8_t SX1262_SET_MODULATION_PARAMS
static constexpr uint8_t SX1262_GET_RX_BUFFER_STATUS
static constexpr uint8_t SX1262_CLEAR_IRQ_STATUS
static constexpr uint8_t SX1262_SET_RX
static constexpr uint8_t SX1262_SET_TX
static constexpr uint8_t SX1262_GFSK_PACKET_TYPE_KNOWN_LENGTH
static constexpr uint8_t SX1262_CALIBRATE_IMAGE
static constexpr uint8_t SX1262_SET_DIO2_AS_RF_SWITCH_CTRL
static constexpr uint8_t SX1262_WRITE_BUFFER
static constexpr uint8_t SX1262_READ_BUFFER
static constexpr uint16_t SX1262_IRQ_RX_DONE
static constexpr uint8_t SX1262_SET_PA_CONFIG
static constexpr uint8_t SX1262_SET_TX_PARAMS
static constexpr uint16_t SX1262_IRQ_TX_DONE
static constexpr uint8_t SX1262_SET_DIO_IRQ_PARAMS
static constexpr uint8_t SX1262_SET_PACKET_PARAMS
static constexpr uint8_t SX1262_GET_STATUS
static constexpr uint16_t SX1262_REG_TX_CLAMP_CONFIG
static constexpr uint8_t SX1262_SET_RX_TX_FALLBACK_MODE
static constexpr uint8_t SX1262_GET_IRQ_STATUS
static constexpr uint8_t SX1262_SET_REGULATOR_MODE
static constexpr uint8_t SX1262_GET_PACKET_STATUS
static constexpr uint16_t SX1262_REG_SYNC_WORD
static constexpr uint8_t SX1262_FALLBACK_STDBY_XOSC
static constexpr uint8_t SX1262_SET_RF_FREQUENCY
static constexpr uint8_t SX1262_SET_STANDBY
static constexpr uint8_t SX1262_WRITE_REGISTER
static constexpr uint8_t SX1262_GET_RSSI_INST
Radio abstraction layer for IO-Homecontrol.
Raw packet received from the radio.
Configuration for transmitting a packet: carrier frequency and preamble length.