ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sx1231.h
Go to the documentation of this file.
1#pragma once
2
3#include "sx1231_reg.h"
7#include "esphome/core/hal.h"
9#include <span>
10#include <vector>
11
12namespace esphome::sx1231 {
13
14static constexpr size_t SX1231_MAX_PACKET_SIZE = 64; // the 66 byte FIFO also holds the length byte
15static constexpr size_t SX1231_MAX_SYNC_SIZE = 8;
16
24
25// Driver state, not the chip's mode
26enum class RadioState : uint8_t {
27 // Before setup or after a failed mode change; configure() is retried every second
29 // Receiving or sleeping as rx_start says, or in the mode set by the last mode action (untracked)
31};
32
34 public:
35 // Called from loop() with 1 to SX1231_MAX_PACKET_SIZE bytes, which are only valid until this returns.
36 // snr is always NAN.
37 virtual void on_packet(std::span<const uint8_t> packet, float rssi, float snr) = 0;
38};
39
40class SX1231 final : public Component,
41 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
42 spi::DATA_RATE_8MHZ> {
43 public:
44 size_t get_max_packet_size();
45 float get_setup_priority() const override { return setup_priority::PROCESSOR; }
46 void setup() override;
47 void loop() override;
48 void dump_config() override;
49 // The RegRxBw code for FSK; the OOK code is worked out from it
50 void set_bandwidth(uint8_t bandwidth) { this->bandwidth_ = bandwidth; }
51 void set_bitrate(uint32_t bitrate) { this->bitrate_ = bitrate; }
52 void set_bitsync(bool bitsync) { this->bitsync_ = bitsync; }
53 void set_crc_enable(bool crc_enable) { this->crc_enable_ = crc_enable; }
54 void set_deviation(uint32_t deviation) { this->deviation_ = deviation; }
55 void set_dio0_pin(InternalGPIOPin *dio0_pin) { this->dio0_pin_ = dio0_pin; }
57 // Ignored unless the radio is idle
58 void set_mode_rx();
59 void set_mode_tx();
60 void set_mode_standby();
61 void set_mode_sleep();
62 void set_modulation(uint8_t modulation) { this->modulation_ = modulation; }
63 void set_pa_pin(uint8_t pin) { this->pa_pin_ = pin; }
64 void set_pa_power(int8_t power) { this->pa_power_ = power; }
65 // Masked to the four bits of the register, as it is also an index into a table
66 void set_pa_ramp(uint8_t ramp) { this->pa_ramp_ = ramp & 0x0F; }
67 void set_packet_mode(bool packet_mode) { this->packet_mode_ = packet_mode; }
68 void set_payload_length(uint8_t payload_length) { this->payload_length_ = payload_length; }
70 void set_rst_pin(GPIOPin *rst_pin) { this->rst_pin_ = rst_pin; }
71 void set_rx_floor(float floor) { this->rx_floor_ = floor; }
72 void set_rx_start(bool start) { this->rx_start_ = start; }
73 void set_shaping(uint8_t shaping) { this->shaping_ = shaping; }
74 void set_sync_value(const StaticVector<uint8_t, SX1231_MAX_SYNC_SIZE> &sync_value) { this->sync_value_ = sync_value; }
75 void configure();
76 SX1231Error transmit_packet(const uint8_t *data, size_t len);
77 SX1231Error transmit_packet(const std::vector<uint8_t> &packet) {
78 return this->transmit_packet(packet.data(), packet.size());
79 }
80#ifdef SX1231_LISTENER_COUNT
82 void register_listener(SX1231Listener *listener);
83#endif
84 // True while a packet is being received, or has been received and not yet read; sending would lose it.
85 bool is_receiving();
86 template<typename F> void add_on_packet_callback(F &&callback) {
87 this->packet_callback_.add(std::forward<F>(callback));
88 }
89
90 protected:
91 // Settings as last applied by configure(), used by sends and reception
93 uint8_t payload_length{0};
94 bool packet_mode{false};
95 bool high_power{false};
96 bool rx_start{false};
97 };
98 // Returns the failure reason, or nullptr
99 const LogString *configure_radio_();
100 // False if the chip did not enter the mode
101 bool set_mode_(uint8_t mode);
102 void change_mode_(uint8_t mode);
103 // After a send or configure(): RX if rx_start, else sleep
104 bool restore_();
105 // Retries configure() every second
106 void fail_(const LogString *reason);
107 bool packet_waiting_();
108 void receive_();
109 void restart_stalled_rx_();
110 SX1231Error start_transmit_(const uint8_t *data, size_t len);
111 void write_registers_(uint8_t reg, const uint8_t *data, size_t len);
112 void write_register16_(uint8_t msb_reg, uint16_t value);
113 void write_fifo_(const uint8_t *data, size_t len);
114 void write_register_(uint8_t reg, uint8_t value);
115 void call_listeners_(std::span<const uint8_t> packet, float rssi, float snr);
116 uint8_t read_register_(uint8_t reg);
118#ifdef SX1231_LISTENER_COUNT
120#endif
122 GPIOPin *rst_pin_{nullptr};
130 uint8_t bandwidth_;
131 uint8_t modulation_;
132 uint8_t pa_pin_;
133 int8_t pa_power_;
134 uint8_t pa_ramp_;
136 uint8_t shaping_;
137 bool bitsync_{false};
138 bool crc_enable_{false};
139 bool packet_mode_{false};
140 bool rx_start_{false};
143};
144
145} // namespace esphome::sx1231
BedjetMode mode
BedJet operating mode.
uint16_le_t frequency
Definition bl0942.h:6
Read-only view of bytes in a buffer owned elsewhere, valid only until that buffer is written again.
Definition helpers.h:149
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:335
The SPIDevice is what components using the SPI will create.
Definition spi.h:445
void set_frequency(uint32_t frequency)
Definition sx1231.h:56
void change_mode_(uint8_t mode)
Definition sx1231.cpp:385
void set_pa_ramp(uint8_t ramp)
Definition sx1231.h:66
void set_rst_pin(GPIOPin *rst_pin)
Definition sx1231.h:70
void set_payload_length(uint8_t payload_length)
Definition sx1231.h:68
void set_bitsync(bool bitsync)
Definition sx1231.h:52
void set_shaping(uint8_t shaping)
Definition sx1231.h:73
SX1231Error start_transmit_(const uint8_t *data, size_t len)
Definition sx1231.cpp:241
void fail_(const LogString *reason)
Definition sx1231.cpp:93
void set_bitrate(uint32_t bitrate)
Definition sx1231.h:51
void write_fifo_(const uint8_t *data, size_t len)
Definition sx1231.cpp:43
void set_pa_pin(uint8_t pin)
Definition sx1231.h:63
SX1231Error transmit_packet(const uint8_t *data, size_t len)
Definition sx1231.cpp:219
void set_packet_mode(bool packet_mode)
Definition sx1231.h:67
void dump_config() override
Definition sx1231.cpp:403
void set_deviation(uint32_t deviation)
Definition sx1231.h:54
SX1231Error transmit_packet(const std::vector< uint8_t > &packet)
Definition sx1231.h:77
uint8_t read_register_(uint8_t reg)
Definition sx1231.cpp:34
void set_rx_start(bool start)
Definition sx1231.h:72
void call_listeners_(std::span< const uint8_t > packet, float rssi, float snr)
Definition sx1231.cpp:311
uint16_t preamble_size_
Definition sx1231.h:128
void set_bandwidth(uint8_t bandwidth)
Definition sx1231.h:50
void set_preamble_size(uint16_t preamble_size)
Definition sx1231.h:69
void add_on_packet_callback(F &&callback)
Definition sx1231.h:86
void write_register_(uint8_t reg, uint8_t value)
Definition sx1231.cpp:36
StaticVector< SX1231Listener *, SX1231_LISTENER_COUNT > listeners_
Definition sx1231.h:119
void set_crc_enable(bool crc_enable)
Definition sx1231.h:53
void set_pa_power(int8_t power)
Definition sx1231.h:64
size_t get_max_packet_size()
Definition sx1231.cpp:212
void set_sync_value(const StaticVector< uint8_t, SX1231_MAX_SYNC_SIZE > &sync_value)
Definition sx1231.h:74
void setup() override
Definition sx1231.cpp:59
const LogString * configure_radio_()
Definition sx1231.cpp:104
void set_modulation(uint8_t modulation)
Definition sx1231.h:62
LazyCallbackManager< void(ByteView, float, float)> packet_callback_
Definition sx1231.h:117
float get_setup_priority() const override
Definition sx1231.h:45
void set_rx_floor(float floor)
Definition sx1231.h:71
void loop() override
Definition sx1231.cpp:320
StaticVector< uint8_t, SX1231_MAX_SYNC_SIZE > sync_value_
Definition sx1231.h:127
void register_listener(SX1231Listener *listener)
Listeners must request a slot from their to_code: sx1231.request_listener_slot(radio_id).
Definition sx1231.cpp:302
void write_register16_(uint8_t msb_reg, uint16_t value)
Definition sx1231.cpp:53
void write_registers_(uint8_t reg, const uint8_t *data, size_t len)
Definition sx1231.cpp:39
InternalGPIOPin * dio0_pin_
Definition sx1231.h:121
AppliedSettings applied_
Definition sx1231.h:129
void set_dio0_pin(InternalGPIOPin *dio0_pin)
Definition sx1231.h:55
bool set_mode_(uint8_t mode)
Definition sx1231.cpp:362
virtual void on_packet(std::span< const uint8_t > packet, float rssi, float snr)=0
uint16_t preamble_size
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:48
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
static void uint32_t