ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sx126x_spi_device.h
Go to the documentation of this file.
1#pragma once
2
4#include <span>
5
6namespace esphome::sx126x {
7
8// Persistent radio device. read()/write() use exchange_() for one complete selected interval.
9// No raw SPI access escapes. Call only from the main loop; spans are borrowed until return.
10// Failures stick: after a BUSY timeout every exchange is skipped until the next begin(),
11// so a command sequence checks ok() once instead of after every command.
13 public:
14 SX126xSpiDevice() = default;
17 void set_spi_parent(spi::SPIComponent *parent) { this->device_.set_spi_parent(parent); }
18 void set_cs_pin(GPIOPin *pin) { this->device_.set_cs_pin(pin); }
19 void set_data_rate(uint32_t rate) { this->device_.set_data_rate(rate); }
20 void set_mode(spi::SPIMode mode) { this->device_.set_mode(mode); }
21 void set_bit_order(spi::SPIBitOrder order) { this->device_.set_bit_order(order); }
22 void set_release_device(bool release) { this->device_.set_release_device(release); }
23 void set_write_only(bool write_only) { this->device_.set_write_only(write_only); }
24#ifdef USE_SPI_PSRAM_DMA
25 void set_psram_dma(bool enable) { this->device_.set_psram_dma(enable); }
26#endif
27
28 void set_busy_pin(GPIOPin *pin) { this->busy_pin_ = pin; }
29 GPIOPin *get_busy_pin() const { return this->busy_pin_; }
30 GPIOPin *get_cs_pin() const { return this->device_.get_cs_pin(); }
31 void setup() { this->device_.spi_setup(); }
32 void teardown() { this->device_.spi_teardown(); }
33 // Starts a command sequence with no recorded failure.
34 void begin() { this->ok_ = true; }
35 // False once any exchange since begin() timed out waiting for BUSY.
36 bool ok() const { return this->ok_; }
37 void write(std::span<const uint8_t> header, std::span<const uint8_t> data);
38 // Leaves data unchanged when the exchange is skipped or times out.
39 void read(std::span<const uint8_t> header, std::span<uint8_t> data);
40
41 private:
42 class Device final : public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW,
43 spi::CLOCK_PHASE_LEADING, spi::DATA_RATE_8MHZ> {
44 public:
45 GPIOPin *get_cs_pin() const { return this->cs_; }
46 };
47 class Selection {
48 public:
49 explicit Selection(Device &device) : device_(device) { this->device_.enable(); }
50 ~Selection() { this->device_.disable(); }
51 Selection(const Selection &) = delete;
52 Selection &operator=(const Selection &) = delete;
53
54 private:
55 Device &device_;
56 };
57 void exchange_(std::span<const uint8_t> header, std::span<const uint8_t> tx, std::span<uint8_t> rx);
58 Device device_;
59 GPIOPin *busy_pin_{nullptr};
60 bool ok_{true};
61};
62
63} // namespace esphome::sx126x
BedjetMode mode
BedJet operating mode.
void set_release_device(bool release)
Definition spi.h:416
void set_write_only(bool write_only)
Definition spi.h:417
void set_psram_dma(bool enable)
Definition spi.h:419
The SPIDevice is what components using the SPI will create.
Definition spi.h:445
void set_data_rate(uint32_t data_rate)
Definition spi.h:462
void spi_setup() override
Definition spi.h:454
void set_cs_pin(GPIOPin *cs)
Definition spi.h:460
void set_mode(SPIMode mode)
Definition spi.h:466
void set_bit_order(SPIBitOrder order)
Definition spi.h:464
void set_spi_parent(SPIComponent *parent)
Definition spi.h:458
void spi_teardown() override
Definition spi.h:456
void write(std::span< const uint8_t > header, std::span< const uint8_t > data)
void set_spi_parent(spi::SPIComponent *parent)
SX126xSpiDevice & operator=(const SX126xSpiDevice &)=delete
void set_write_only(bool write_only)
void read(std::span< const uint8_t > header, std::span< uint8_t > data)
void set_bit_order(spi::SPIBitOrder order)
SX126xSpiDevice(const SX126xSpiDevice &)=delete
void set_mode(spi::SPIMode mode)
SPIMode
Modes mapping to clock phase and polarity.
Definition spi.h:84
SPIBitOrder
The bit-order for SPI devices. This defines how the data read from and written to the device is inter...
Definition spi.h:46
static void uint32_t