ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
sx126x_commands.cpp
Go to the documentation of this file.
1#include "sx126x_commands.h"
2#include "esphome/core/hal.h"
4
5#include <cmath>
6#include <cstring>
7
8namespace esphome::sx126x {
9static constexpr uint16_t RAMP[8] PROGMEM = {10, 20, 40, 80, 200, 800, 1700, 3400};
10// Aligned 32 bit loads from flash are safe on ESP8266, so this table is read directly
11static constexpr uint32_t BW_HZ[31] PROGMEM = {4800, 5800, 7300, 9700, 11700, 14600, 19500, 23400,
12 29300, 39000, 46900, 58600, 78200, 93800, 117300, 156200,
13 187200, 234300, 312000, 373600, 467000, 7810, 10420, 15630,
14 20830, 31250, 41670, 62500, 125000, 250000, 500000};
15static constexpr uint8_t BW_LORA[10] PROGMEM = {LORA_BW_7810, LORA_BW_10420, LORA_BW_15630, LORA_BW_20830,
18static constexpr uint8_t BW_FSK[21] PROGMEM = {
22
23// OCP (Over Current Protection) values
24static constexpr uint8_t OCP_80MA = 0x18; // 80 mA max current
25static constexpr uint8_t OCP_140MA = 0x38; // 140 mA max current
26
27// LoRa low data rate optimization threshold
28static constexpr float LOW_DATA_RATE_OPTIMIZE_THRESHOLD = 16.38f; // 16.38 ms
29
30void SX126xCommands::read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size) {
31 const uint8_t header[] = {opcode, 0};
32 this->spi_device_.read(header, {data, size});
33}
34
35void SX126xCommands::write_opcode_(uint8_t opcode, const uint8_t *data, uint8_t size) {
36 this->spi_device_.write({&opcode, 1}, {data, size});
37}
38
39void SX126xCommands::read_register_(uint16_t reg, uint8_t *data, uint8_t size) {
40 const uint8_t header[] = {RADIO_READ_REGISTER, static_cast<uint8_t>(reg >> 8), static_cast<uint8_t>(reg), 0};
41 this->spi_device_.read(header, {data, size});
42}
43
44void SX126xCommands::write_register_(uint16_t reg, const uint8_t *data, uint8_t size) {
45 const uint8_t header[] = {RADIO_WRITE_REGISTER, static_cast<uint8_t>(reg >> 8), static_cast<uint8_t>(reg)};
46 this->spi_device_.write(header, {data, size});
47}
48
49ConfigurationResult SX126xCommands::configure(const RadioProfile &profile, std::span<char, 16> version,
50 AppliedProfile &applied) {
51 applied.frequency = profile.frequency;
52 applied.payload_length = profile.payload_length;
53 applied.modulation = profile.modulation;
54 applied.rx_start = profile.rx_start;
55 encode_packet_params(profile, applied);
56 uint8_t buf[8];
57 // wakeup
58 this->read_opcode_(RADIO_GET_STATUS, nullptr, 0);
59
60 // config tcxo
61 if (profile.tcxo_voltage != TCXO_CTRL_NONE) {
62 uint32_t delay = profile.tcxo_delay >> 6;
63 buf[0] = profile.tcxo_voltage;
64 put_be24(buf + 1, delay);
65 this->write_opcode_(RADIO_SET_TCXOMODE, buf, 4);
66 buf[0] = 0x7F;
67 this->write_opcode_(RADIO_CALIBRATE, buf, 1);
68 }
69
70 // clear errors
71 buf[0] = 0x00;
72 buf[1] = 0x00;
73 this->write_opcode_(RADIO_CLR_ERROR, buf, 2);
74
75 // rf switch
76 if (profile.rf_switch) {
77 buf[0] = 0x01;
78 this->write_opcode_(RADIO_SET_RFSWITCHMODE, buf, 1);
79 }
80
81 // check silicon version to make sure hw is ok
82 this->read_register_(REG_VERSION_STRING, reinterpret_cast<uint8_t *>(version.data()), version.size());
83 if (!this->spi_device_.ok()) {
85 }
86 version[version.size() - 1] = '\0';
87 if (strncmp(version.data(), "SX126", 5) != 0 && strncmp(version.data(), "LLCC68", 6) != 0) {
89 }
90
91 // setup packet type
92 buf[0] = profile.modulation;
93 this->write_opcode_(RADIO_SET_PACKETTYPE, buf, 1);
94
95 // calibrate image
96 this->calibrate_image(profile.frequency);
97
98 this->set_frequency(profile.frequency);
99
100 // configure pa
101 int8_t pa_power = profile.pa_power;
102 if (profile.sx1261) {
103 // the following values were taken from section 13.1.14.1 table 13-21
104 // in rev 2.1 of the datasheet
105 if (pa_power == 15) {
106 uint8_t cfg[4] = {0x06, 0x00, 0x01, 0x01};
107 this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4);
108 } else {
109 uint8_t cfg[4] = {0x04, 0x00, 0x01, 0x01};
110 this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4);
111 }
112 pa_power = clamp(pa_power, (int8_t) -3, (int8_t) 14);
113 buf[0] = OCP_80MA;
114 this->write_register_(REG_OCP, buf, 1);
115 } else {
116 // the following values were taken from section 13.1.14.1 table 13-21
117 // in rev 2.1 of the datasheet
118 uint8_t cfg[4] = {0x04, 0x07, 0x00, 0x01};
119 this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4);
120 pa_power = clamp(pa_power, (int8_t) -3, (int8_t) 22);
121 buf[0] = OCP_140MA;
122 this->write_register_(REG_OCP, buf, 1);
123 }
124 buf[0] = pa_power;
125 buf[1] = profile.pa_ramp;
126 this->write_opcode_(RADIO_SET_TXPARAMS, buf, 2);
127
128 // configure modem
129 if (profile.modulation == PACKET_TYPE_LORA) {
130 // set modulation params
131 float duration = 1000.0f * (1UL << profile.spreading_factor) / BW_HZ[profile.bandwidth];
132 buf[0] = profile.spreading_factor;
133 buf[1] = progmem_read_byte(&BW_LORA[profile.bandwidth - SX126X_BW_7810]);
134 buf[2] = profile.coding_rate;
135 buf[3] = (duration > LOW_DATA_RATE_OPTIMIZE_THRESHOLD) ? 0x01 : 0x00;
136 this->write_opcode_(RADIO_SET_MODULATIONPARAMS, buf, 4);
137
138 // set packet params and sync word
139 this->write_opcode_(RADIO_SET_PACKETPARAMS, applied.packet_params.data(), applied.packet_params_size);
140 if (profile.sync_size == 2) {
141 this->write_register_(REG_LORA_SYNCWORD, profile.sync_value.data(), profile.sync_size);
142 }
143 } else {
144 // set modulation params
145 uint32_t bitrate = ((uint64_t) XTAL_FREQ * 32) / profile.bitrate;
146 uint32_t fdev = ((uint64_t) profile.deviation << 25) / XTAL_FREQ;
147 put_be24(buf, bitrate);
148 buf[3] = profile.shaping;
149 buf[4] = progmem_read_byte(&BW_FSK[profile.bandwidth - SX126X_BW_4800]);
150 put_be24(buf + 5, fdev);
151 this->write_opcode_(RADIO_SET_MODULATIONPARAMS, buf, 8);
152
153 // set crc params
154 if (profile.crc_enable) {
155 put_be16(buf, profile.crc_initial);
156 this->write_register_(REG_CRC_INITIAL, buf, 2);
157 put_be16(buf, profile.crc_polynomial);
158 this->write_register_(REG_CRC_POLYNOMIAL, buf, 2);
159 }
160
161 // set whitening params
162 if (profile.whitening_enable) {
163 // according to the datasheet, section 12 table 12-1 "The user should not
164 // change the value of the 7 MSB of this register"
165 this->read_register_(REG_WHITENING_INITIAL, buf, 1);
166 buf[0] = (buf[0] & 0xFE) | ((profile.whitening_initial >> 8) & 0x01);
167 buf[1] = profile.whitening_initial & 0xFF;
168 this->write_register_(REG_WHITENING_INITIAL, buf, 2);
169 }
170
171 // set packet params and sync word
172 this->write_opcode_(RADIO_SET_PACKETPARAMS, applied.packet_params.data(), applied.packet_params_size);
173 if (profile.sync_size != 0) {
174 this->write_register_(REG_GFSK_SYNCWORD, profile.sync_value.data(), profile.sync_size);
175 }
176 }
177
178 // switch to rx or sleep
179 if (profile.rx_start) {
180 this->receive();
181 } else {
182 this->sleep();
183 }
184 return this->spi_device_.ok() ? ConfigurationResult::CONFIGURATION_RESULT_OK
186}
187
188void SX126xCommands::encode_packet_params(const RadioProfile &profile, AppliedProfile &applied) {
189 auto &buf = applied.packet_params;
190 const uint8_t payload_length = profile.max_packet_size();
191 if (profile.modulation == PACKET_TYPE_LORA) {
192 put_be16(buf.data(), profile.preamble_size);
193 buf[2] = (profile.payload_length > 0) ? 0x01 : 0x00;
194 buf[3] = payload_length;
195 buf[4] = (profile.crc_enable) ? 0x01 : 0x00;
196 buf[5] = 0x00;
197 applied.packet_params_size = 6;
198 } else {
199 uint16_t preamble_size = profile.preamble_size * 8;
200 put_be16(buf.data(), preamble_size);
201 buf[2] = (profile.preamble_detect > 0) ? ((profile.preamble_detect - 1) | 0x04) : 0x00;
202 buf[3] = profile.sync_size * 8;
203 buf[4] = 0x00;
204 buf[5] = (profile.payload_length > 0) ? 0x00 : 0x01;
205 buf[6] = payload_length;
206 if (profile.crc_enable) {
207 buf[7] = (profile.crc_inverted ? 0x04 : 0x00) + (profile.crc_size & 0x02);
208 } else {
209 buf[7] = 0x01;
210 }
211 buf[8] = (profile.whitening_enable) ? 0x01 : 0x00;
212 applied.packet_params_size = 9;
213 }
214}
215
217 applied.packet_params[applied.modulation == PACKET_TYPE_LORA ? 3 : 6] = length;
218 this->write_opcode_(RADIO_SET_PACKETPARAMS, applied.packet_params.data(), applied.packet_params_size);
219}
220
222 // the following values were taken from section 9.2.1 table 9-2
223 // in rev 2.1 of the datasheet
224 uint8_t buf[2] = {0, 0};
225 if (frequency > 900000000) {
226 buf[0] = 0xE1;
227 buf[1] = 0xE9;
228 } else if (frequency > 850000000) {
229 buf[0] = 0xD7;
230 buf[1] = 0xDB;
231 } else if (frequency > 770000000) {
232 buf[0] = 0xC1;
233 buf[1] = 0xC5;
234 } else if (frequency > 460000000) {
235 buf[0] = 0x75;
236 buf[1] = 0x81;
237 } else if (frequency > 425000000) {
238 buf[0] = 0x6B;
239 buf[1] = 0x6F;
240 }
241 if (buf[0] > 0 && buf[1] > 0) {
242 this->write_opcode_(RADIO_CALIBRATEIMAGE, buf, 2);
243 }
244}
245
246// Only the dio1 interrupts are routed to DIO1; DIO2 and DIO3 stay unused.
247void SX126xCommands::set_dio_irq_params_(uint16_t flags, uint16_t dio1) {
248 uint8_t buf[8];
249 put_be16(buf, flags);
250 put_be16(buf + 2, dio1);
251 put_be16(buf + 4, IRQ_RADIO_NONE);
252 put_be16(buf + 6, IRQ_RADIO_NONE);
253 this->write_opcode_(RADIO_SET_DIOIRQPARAMS, buf, 8);
254}
255
257 // A valid header or sync word is flagged for SX126xRadio::is_receiving(), but not signalled on DIO1
258 const uint16_t irq = IRQ_RX_DONE | IRQ_RX_TX_TIMEOUT | IRQ_CRC_ERROR;
259 this->set_dio_irq_params_(irq | IRQ_HEADER_VALID | IRQ_SYNCWORD_VALID, irq);
260
261 uint8_t buf[3];
262
263 // set timeout to 0
264 buf[0] = 0x00;
265 this->write_opcode_(RADIO_SET_LORASYMBTIMEOUT, buf, 1);
266
267 // switch to continuous mode rx
268 buf[0] = 0xFF;
269 buf[1] = 0xFF;
270 buf[2] = 0xFF;
271 this->write_opcode_(RADIO_SET_RX, buf, 3);
272}
273
275 this->set_dio_irq_params_(IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT, IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT);
276
277 uint8_t buf[3];
278
279 const uint32_t ticks = timeout_ms * 64;
280 put_be24(buf, ticks);
281 this->write_opcode_(RADIO_SET_TX, buf, 3);
282}
283
284void SX126xCommands::sleep(bool cold) {
285 // 0x04 = warm start (config retained), 0x00 = cold start (config lost, lowest power)
286 const uint8_t mode = cold ? 0x00 : 0x04;
287 this->write_opcode_(RADIO_SET_SLEEP, &mode, 1);
288}
289
291 uint8_t buf[4];
292 const uint64_t freq = ((uint64_t) frequency << 25) / XTAL_FREQ;
293 put_be32(buf, static_cast<uint32_t>(freq));
294 this->write_opcode_(RADIO_SET_RFFREQUENCY, buf, 4);
295}
296
298 const uint8_t value = mode;
299 this->write_opcode_(RADIO_SET_STANDBY, &value, 1);
300}
301
303uint16_t SX126xCommands::ramp_us(uint8_t ramp) { return progmem_read_uint16(&RAMP[ramp]); }
304
305void SX126xCommands::clear_irqs(uint16_t mask) {
306 const uint8_t data[] = {static_cast<uint8_t>(mask >> 8), static_cast<uint8_t>(mask)};
307 this->write_opcode_(RADIO_CLR_IRQSTATUS, data, 2);
308}
309
311 uint8_t data[2]{};
312 this->read_opcode_(RADIO_GET_IRQSTATUS, data, 2);
313 return (uint16_t(data[0]) << 8) | data[1];
314}
315
316void SX126xCommands::write_fifo(const uint8_t *data, size_t size) {
317 const uint8_t header[] = {RADIO_WRITE_BUFFER, 0};
318 this->spi_device_.write(header, {data, size});
319}
320
321void SX126xCommands::read_packet(uint8_t modulation, std::span<uint8_t, SX126X_MAX_PACKET_SIZE> packet, uint8_t &length,
322 float &rssi, float &snr) {
323 uint8_t data[3]{};
324 this->read_opcode_(RADIO_GET_PACKETSTATUS, data, 3);
325 rssi = (modulation == PACKET_TYPE_LORA ? data[0] : data[2]) / -2.0f;
326 // FSK packets report no SNR
327 snr = modulation == PACKET_TYPE_LORA ? static_cast<int8_t>(data[1]) / 4.0f : NAN;
328 this->read_opcode_(RADIO_GET_RXBUFFERSTATUS, data, 2);
329 if (!this->spi_device_.ok()) {
330 return; // The length and offset were not read.
331 }
332 length = data[0];
333 const uint8_t header[] = {RADIO_READ_BUFFER, data[1], 0};
334 this->spi_device_.read(header, packet.first(length));
335}
336
337} // namespace esphome::sx126x
BedjetMode mode
BedJet operating mode.
uint16_le_t frequency
Definition bl0942.h:6
void transmit(uint32_t timeout_ms=0)
ConfigurationResult configure(const RadioProfile &profile, std::span< char, 16 > version, AppliedProfile &applied)
void standby(SX126xStandbyMode mode)
void set_frequency(uint32_t frequency)
void calibrate_image(uint32_t frequency)
static uint16_t ramp_us(uint8_t ramp)
void write_fifo(const uint8_t *data, size_t size)
static uint32_t bandwidth_hz(SX126xBw bw)
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)
void write(std::span< const uint8_t > header, std::span< const uint8_t > data)
void read(std::span< const uint8_t > header, std::span< uint8_t > data)
uint16_t flags
uint16_t preamble_size
uint8_t duration
Definition msa3xx.h:0
const uint8_t STORE_YAML_DATA[] PROGMEM
The zstd YAML envelope generated by store_yaml, STORE_YAML_DATA_SIZE bytes in flash.
std::span< const uint8_t > data
@ RADIO_SET_MODULATIONPARAMS
Definition sx126x_reg.h:31
@ RADIO_SET_LORASYMBTIMEOUT
Definition sx126x_reg.h:50
uint32_t value
Definition hal.cpp:59
uint16_t size
Definition helpers.cpp:44
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition hal.h:53
constexpr uint8_t * put_be16(uint8_t *dst, uint16_t value)
Write the low 16, 24 or 32 bits of value to dst, most significant byte first (be) or least significan...
Definition helpers.h:1296
constexpr uint8_t * put_be24(uint8_t *dst, uint32_t value)
Definition helpers.h:1301
void HOT delay(uint32_t ms)
Definition hal.cpp:78
constexpr uint8_t * put_be32(uint8_t *dst, uint32_t value)
Definition helpers.h:1307
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t
std::array< uint8_t, 9 > packet_params
std::array< uint8_t, 8 > sync_value
uint16_t length
Definition tt21100.cpp:0