ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sx126x_radio.h
Go to the documentation of this file.
1#pragma once
2
3#include "radio_profile.h"
4#include "sx126x_commands.h"
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7#include <span>
8#include <vector>
9
10namespace esphome::sx126x {
11
21
23 public:
25};
26
27// Who may use the radio between operations. This is driver state, not the chip's mode.
28enum class RadioState : uint8_t {
29 // Startup, cold sleep, or a failed configuration, mode command or restoration. Only
30 // configure() is accepted; sends return NOT_READY and the loop sleeps. SX126x configures
31 // again every second after a failure, and on the next mode command after cold sleep.
33 // Available. The chip is receiving or sleeping, as configured, or after the TX mode command
34 // sending whatever the FIFO holds. That send is not tracked: a send or mode command may cut
35 // it short, and poll() clears its TX_DONE as a flag without a packet.
37 // A send is on air. SPI is free between polls; only polling and cancellation are accepted.
39 // Standby with a received packet left in the FIFO, found when a send entered standby.
40 // The send was rejected with BUSY; the next poll() delivers the packet and restores the
41 // radio. configure() may discard it to recover from repeated read failures.
43};
44
53
54// What a radio operation leaves for its caller to do. The radio has already committed its
55// state; the caller reports status first, then calls the listener or delivers the packet.
58 // The completion to notify when finished is set; null for a blocking send.
60 // With report set: the warning to set, or null to clear it.
61 const LogString *warning{nullptr};
62 float rssi{0};
63 float snr{0};
64 // With received set: the length of the packet in the buffer.
65 uint8_t packet_length{0};
66 bool report{false};
67 // A send ended, and result is its outcome.
68 bool finished{false};
69 // The packet buffer holds a packet to deliver.
70 bool received{false};
71 // Nothing to poll until the next DIO1 edge or configuration.
72 bool sleep_loop{false};
73};
74
75// Owns the radio state and every SPI command sequence.
76//
77// Status reports, completions and packet listeners can run user code, which may call back
78// into the driver. Every operation therefore does its SPI work with busy() set, commits the
79// next state, and returns RadioEffects; SX126x::apply_() then reports status and calls the
80// listeners. This class has no access to component status or listeners, so callbacks only
81// ever see a committed state. Do not log here either: log listeners are user code. busy()
82// covers re-entry during a sequence, for example from a GPIO pin that logs.
83//
84// Each sequence starts with bus().begin(); after a BUSY timeout the rest is skipped and
85// bus().ok() is false. A failed send preparation restores the radio in a new sequence. A
86// failed restoration needs configuration, and its result overrides the send result. A
87// successful send start reports nothing, so no user code runs before the caller has the
88// result. Every send ends in finish_transmit_(), exactly once.
89//
90// A failed receive read keeps the packet and retries after RECEIVE_RETRY_INTERVAL_MS, so a
91// stuck BUSY line does not cost a 20 ms wait on every loop.
93 public:
94 SX126xRadio() = default;
95 SX126xRadio(const SX126xRadio &) = delete;
96 SX126xRadio &operator=(const SX126xRadio &) = delete;
97 SX126xSpiDevice &bus() { return this->commands_.bus(); }
98 void set_dio1_pin(GPIOPin *pin) { this->dio1_pin_ = pin; }
99 void set_rst_pin(GPIOPin *pin) { this->rst_pin_ = pin; }
100 GPIOPin *get_dio1_pin() const { return this->dio1_pin_; }
101 GPIOPin *get_rst_pin() const { return this->rst_pin_; }
102 RadioState state() const { return this->state_; }
103 bool busy() const { return this->busy_; }
104 bool is_transmitting() const { return this->state_ == RadioState::RADIO_STATE_TRANSMITTING; }
105 // A held packet may be discarded: explicit reconfiguration is the recovery path.
106 bool can_configure() const { return !this->busy_ && !this->is_transmitting(); }
107 bool can_change_mode() const { return !this->busy_ && this->state_ == RadioState::RADIO_STATE_IDLE; }
108 size_t max_packet_size() const { return this->applied_.max_packet_size(); }
109 // True while a packet is being received, or has been received and not yet read, so a send would lose it.
110 // Reads the chip only when the radio is idle and listening; any other state reports false, a failed read true.
111 bool is_receiving();
112
113 // Requires can_configure(). Resets the chip and applies the requested profile.
114 RadioEffects configure(const RadioProfile &requested, std::span<char, 16> version);
115 // Requires can_change_mode(). Moves to another frequency in the band the image was calibrated for, without a reset,
116 // then receives or sleeps as the applied profile says.
118 // Requires can_change_mode(). Cold sleep and failed commands need configuration afterwards.
119 RadioEffects change_mode(ModeCommand command, uint8_t arg);
120 // Result SX126X_ERROR_NONE starts a send that finishes exactly once, through poll_transmit() or
121 // cancel_transmit(). Any other result owes no completion.
122 RadioEffects start_transmit(SX126xTransmitListener *listener, const uint8_t *data, size_t len, uint32_t timeout_ms);
123 // Sets finished only when it ended the current send.
125 // Sets finished only if listener owns the current send.
127 // One loop step: polls a send, or reads a received packet into packet.
128 RadioEffects poll(uint32_t now, std::span<uint8_t, SX126X_MAX_PACKET_SIZE> packet);
129
130 private:
131 struct TxOperation {
132 SX126xTransmitListener *listener{nullptr};
133 uint32_t started_ms{0};
134 uint32_t timeout_ms{0};
135 };
136 void commit_(RadioState state) {
137 this->state_ = state;
138 this->busy_ = false;
139 }
140 static RadioEffects comm_report(bool ok);
141 const LogString *apply_configuration_(const RadioProfile &requested, std::span<char, 16> version);
142 bool restore_();
143 // A received packet that passed its CRC is in the FIFO
144 bool packet_waiting_();
145 RadioEffects finish_transmit_(SX126xError result);
146 RadioEffects receive_(bool holding, uint32_t now, std::span<uint8_t, SX126X_MAX_PACKET_SIZE> packet);
147 SX126xCommands commands_;
148 AppliedProfile applied_;
149 TxOperation tx_;
150 uint32_t rx_retry_started_ms_{0};
151 uint16_t pending_irq_{0}; // flags of a received packet whose read failed after they were cleared
152 GPIOPin *dio1_pin_{nullptr};
153 GPIOPin *rst_pin_{nullptr};
155 bool busy_{false};
156 bool rx_retry_pending_{false};
157};
158
159} // namespace esphome::sx126x
uint16_le_t frequency
Definition bl0942.h:6
RadioEffects configure(const RadioProfile &requested, std::span< char, 16 > version)
GPIOPin * get_rst_pin() const
SX126xSpiDevice & bus()
SX126xRadio(const SX126xRadio &)=delete
GPIOPin * get_dio1_pin() const
RadioEffects retune(uint32_t frequency)
void set_dio1_pin(GPIOPin *pin)
RadioEffects cancel_transmit(SX126xTransmitListener *listener)
RadioEffects poll(uint32_t now, std::span< uint8_t, SX126X_MAX_PACKET_SIZE > packet)
void set_rst_pin(GPIOPin *pin)
RadioEffects poll_transmit(uint32_t now)
RadioEffects change_mode(ModeCommand command, uint8_t arg)
RadioEffects start_transmit(SX126xTransmitListener *listener, const uint8_t *data, size_t len, uint32_t timeout_ms)
SX126xRadio & operator=(const SX126xRadio &)=delete
virtual void on_transmit_complete(SX126xError result)=0
const void size_t len
Definition hal.h:68
static void uint32_t
SX126xTransmitListener * listener
esp_err_t result