9static constexpr uint32_t RESET_DELAY_HIGH_US = 5000;
10static constexpr uint32_t RESET_DELAY_LOW_US = 2000;
11static constexpr uint32_t RECEIVE_RETRY_INTERVAL_MS = 200;
13RadioEffects SX126xRadio::comm_report(
bool ok) {
15 effects.report =
true;
16 effects.warning = ok ? nullptr : LOG_STR(
"Wait busy timeout");
23 this->rx_retry_pending_ =
false;
24 this->pending_irq_ = 0;
27 effects.
warning = this->apply_configuration_(requested, version);
33const LogString *SX126xRadio::apply_configuration_(
const RadioProfile &requested, std::span<char, 16> version) {
35 return LOG_STR(
"Invalid sync value");
39 pulse_pin(this->rst_pin_,
false, RESET_DELAY_LOW_US);
42 AppliedProfile applied;
43 switch (this->commands_.
configure(requested, version, applied)) {
45 this->applied_ = applied;
48 return LOG_STR(
"Unsupported chip version");
50 return LOG_STR(
"Wait busy timeout");
64 const bool hold = !this->applied_.
rx_start && this->packet_waiting_();
70 this->pending_irq_ = 0;
71 this->commands_.
sleep();
73 const bool ok = this->
bus().
ok();
80 return comm_report(ok);
83bool SX126xRadio::packet_waiting_() {
84 const uint16_t irq = this->commands_.
read_irq() | this->pending_irq_;
110 const bool ok = this->
bus().
ok();
114 return comm_report(ok);
139 if (this->packet_waiting_()) {
145 this->pending_irq_ = 0;
146 if (fixed_length == 0) {
150 this->commands_.
transmit(timeout_ms);
151 if (!this->
bus().ok()) {
152 const bool restored = this->restore_();
154 effects = comm_report(restored);
158 this->tx_ = {listener,
millis(), timeout_ms};
177 if (!this->
bus().ok()) {
188 if (!
done &&
static_cast<int32_t
>(now - this->tx_.started_ms) >=
static_cast<int32_t
>(this->tx_.timeout_ms)) {
196 if (this->busy_ || !this->
is_transmitting() || listener ==
nullptr || listener != this->tx_.listener) {
205 const bool restored = this->restore_();
209 effects.
listener = std::exchange(this->tx_.listener,
nullptr);
214bool SX126xRadio::restore_() {
224 this->commands_.
sleep();
226 return this->
bus().
ok();
238 if (this->pending_irq_ != 0 || this->dio1_pin_->
digital_read()) {
261 if (this->rx_retry_pending_ && now - this->rx_retry_started_ms_ < RECEIVE_RETRY_INTERVAL_MS) {
265 if (!holding && this->pending_irq_ == 0 && !this->dio1_pin_->
digital_read()) {
269 return this->receive_(holding, now, packet);
272RadioEffects SX126xRadio::receive_(
bool holding,
uint32_t now, std::span<uint8_t, SX126X_MAX_PACKET_SIZE> packet) {
276 const uint16_t
status = this->commands_.
read_irq() | this->pending_irq_;
283 float rssi = 0, snr = 0;
288 const bool read = this->
bus().
ok();
289 this->pending_irq_ = read ? 0 :
status;
291 this->rx_retry_pending_ = !read;
292 this->rx_retry_started_ms_ = now;
294 if (read && holding) {
299 effects.received = read && received;
302 effects.packet_length =
length;
virtual void digital_write(bool value)=0
virtual bool is_internal()
Only the platform pin class behind the InternalGPIOPin alias may return true: callers static_cast to ...
virtual bool digital_read()=0
void transmit(uint32_t timeout_ms=0)
ConfigurationResult configure(const RadioProfile &profile, std::span< char, 16 > version, AppliedProfile &applied)
void standby(SX126xStandbyMode mode)
void sleep(bool cold=false)
void set_frequency(uint32_t frequency)
void calibrate_image(uint32_t frequency)
void write_fifo(const uint8_t *data, size_t size)
void set_packet_length(AppliedProfile &applied, uint8_t length)
void read_packet(uint8_t modulation, std::span< uint8_t, SX126X_MAX_PACKET_SIZE > packet, uint8_t &length, float &rssi, float &snr)
void clear_irqs(uint16_t mask=0xFFFF)
RadioEffects configure(const RadioProfile &requested, std::span< char, 16 > version)
bool can_configure() const
size_t max_packet_size() const
bool can_change_mode() const
RadioEffects retune(uint32_t frequency)
RadioEffects cancel_transmit(SX126xTransmitListener *listener)
RadioEffects poll(uint32_t now, std::span< uint8_t, SX126X_MAX_PACKET_SIZE > packet)
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)
bool is_transmitting() const
@ SX126X_ERROR_INVALID_PARAMS
@ SX126X_ERROR_BUSY_TIMEOUT
@ CONFIGURATION_RESULT_OK
@ CONFIGURATION_RESULT_UNSUPPORTED
@ RADIO_STATE_NEEDS_CONFIGURATION
@ RADIO_STATE_TRANSMITTING
@ MODE_COMMAND_COLD_SLEEP
void pulse_pin(GPIOPin *pin, bool level, uint32_t width_us)
Drive pin to level for width_us microseconds, then to the opposite level.
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
uint32_t IRAM_ATTR HOT millis()
const LogString * warning
SX126xTransmitListener * listener