ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
sx126x.cpp
Go to the documentation of this file.
1#include "sx126x.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace sx126x {
7
8static const char *const TAG = "sx126x";
9static const uint16_t RAMP[8] = {10, 20, 40, 80, 200, 800, 1700, 3400};
10static const uint32_t BW_HZ[31] = {4800, 5800, 7300, 9700, 11700, 14600, 19500, 23400, 29300, 39000, 46900,
11 58600, 78200, 93800, 117300, 156200, 187200, 234300, 312000, 373600, 467000, 7810,
12 10420, 15630, 20830, 31250, 41670, 62500, 125000, 250000, 500000};
13static const uint8_t BW_LORA[10] = {LORA_BW_7810, LORA_BW_10420, LORA_BW_15630, LORA_BW_20830, LORA_BW_31250,
15static const uint8_t BW_FSK[21] = {
19
20static constexpr uint32_t RESET_DELAY_HIGH_US = 5000;
21static constexpr uint32_t RESET_DELAY_LOW_US = 2000;
22static constexpr uint32_t SWITCHING_DELAY_US = 1;
23static constexpr uint32_t TRANSMIT_TIMEOUT_MS = 4000;
24static constexpr uint32_t BUSY_TIMEOUT_MS = 20;
25
26// OCP (Over Current Protection) values
27static constexpr uint8_t OCP_80MA = 0x18; // 80 mA max current
28static constexpr uint8_t OCP_140MA = 0x38; // 140 mA max current
29
30// LoRa low data rate optimization threshold
31static constexpr float LOW_DATA_RATE_OPTIMIZE_THRESHOLD = 16.38f; // 16.38 ms
32
33uint8_t SX126x::read_fifo_(uint8_t offset, std::vector<uint8_t> &packet) {
34 this->wait_busy_();
35 this->enable();
37 this->transfer_byte(offset);
38 uint8_t status = this->transfer_byte(0x00);
39 for (uint8_t &byte : packet) {
40 byte = this->transfer_byte(0x00);
41 }
42 this->disable();
43 return status;
44}
45
46void SX126x::write_fifo_(uint8_t offset, const std::vector<uint8_t> &packet) {
47 this->wait_busy_();
48 this->enable();
50 this->transfer_byte(offset);
51 for (const uint8_t &byte : packet) {
52 this->transfer_byte(byte);
53 }
54 this->disable();
55 delayMicroseconds(SWITCHING_DELAY_US);
56}
57
58uint8_t SX126x::read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size) {
59 this->wait_busy_();
60 this->enable();
61 this->transfer_byte(opcode);
62 uint8_t status = this->transfer_byte(0x00);
63 for (int32_t i = 0; i < size; i++) {
64 data[i] = this->transfer_byte(0x00);
65 }
66 this->disable();
67 return status;
68}
69
70void SX126x::write_opcode_(uint8_t opcode, uint8_t *data, uint8_t size) {
71 this->wait_busy_();
72 this->enable();
73 this->transfer_byte(opcode);
74 for (int32_t i = 0; i < size; i++) {
75 this->transfer_byte(data[i]);
76 }
77 this->disable();
78 delayMicroseconds(SWITCHING_DELAY_US);
79}
80
81void SX126x::read_register_(uint16_t reg, uint8_t *data, uint8_t size) {
82 this->wait_busy_();
83 this->enable();
85 this->write_byte((reg >> 8) & 0xFF);
86 this->write_byte((reg >> 0) & 0xFF);
87 this->write_byte(0x00);
88 for (int32_t i = 0; i < size; i++) {
89 data[i] = this->transfer_byte(0x00);
90 }
91 this->disable();
92}
93
94void SX126x::write_register_(uint16_t reg, uint8_t *data, uint8_t size) {
95 this->wait_busy_();
96 this->enable();
98 this->write_byte((reg >> 8) & 0xFF);
99 this->write_byte((reg >> 0) & 0xFF);
100 for (int32_t i = 0; i < size; i++) {
101 this->transfer_byte(data[i]);
102 }
103 this->disable();
104 delayMicroseconds(SWITCHING_DELAY_US);
105}
106
108 // setup pins
109 this->busy_pin_->setup();
110 this->rst_pin_->setup();
111 this->dio1_pin_->setup();
112
113 // start spi
114 this->spi_setup();
115
116 // configure rf
117 this->configure();
118}
119
121 uint8_t buf[8];
122
123 // toggle chip reset
124 this->rst_pin_->digital_write(true);
125 delayMicroseconds(RESET_DELAY_HIGH_US);
126 this->rst_pin_->digital_write(false);
127 delayMicroseconds(RESET_DELAY_LOW_US);
128 this->rst_pin_->digital_write(true);
129 delayMicroseconds(RESET_DELAY_HIGH_US);
130
131 // wakeup
132 this->read_opcode_(RADIO_GET_STATUS, nullptr, 0);
133
134 // config tcxo
135 if (this->tcxo_voltage_ != TCXO_CTRL_NONE) {
136 uint32_t delay = this->tcxo_delay_ >> 6;
137 buf[0] = this->tcxo_voltage_;
138 buf[1] = (delay >> 16) & 0xFF;
139 buf[2] = (delay >> 8) & 0xFF;
140 buf[3] = (delay >> 0) & 0xFF;
141 this->write_opcode_(RADIO_SET_TCXOMODE, buf, 4);
142 buf[0] = 0x7F;
143 this->write_opcode_(RADIO_CALIBRATE, buf, 1);
144 }
145
146 // clear errors
147 buf[0] = 0x00;
148 buf[1] = 0x00;
149 this->write_opcode_(RADIO_CLR_ERROR, buf, 2);
150
151 // rf switch
152 if (this->rf_switch_) {
153 buf[0] = 0x01;
155 }
156
157 // check silicon version to make sure hw is ok
158 this->read_register_(REG_VERSION_STRING, (uint8_t *) this->version_, 16);
159 if (strncmp(this->version_, "SX126", 5) != 0 && strncmp(this->version_, "LLCC68", 6) != 0) {
160 this->mark_failed();
161 return;
162 }
163
164 // setup packet type
165 buf[0] = this->modulation_;
166 this->write_opcode_(RADIO_SET_PACKETTYPE, buf, 1);
167
168 // calibrate image
169 this->run_image_cal();
170
171 // set frequency
172 uint64_t freq = ((uint64_t) this->frequency_ << 25) / XTAL_FREQ;
173 buf[0] = (uint8_t) ((freq >> 24) & 0xFF);
174 buf[1] = (uint8_t) ((freq >> 16) & 0xFF);
175 buf[2] = (uint8_t) ((freq >> 8) & 0xFF);
176 buf[3] = (uint8_t) (freq & 0xFF);
178
179 // configure pa
180 int8_t pa_power = this->pa_power_;
181 if (this->hw_version_ == "sx1261") {
182 // the following values were taken from section 13.1.14.1 table 13-21
183 // in rev 2.1 of the datasheet
184 if (pa_power == 15) {
185 uint8_t cfg[4] = {0x06, 0x00, 0x01, 0x01};
186 this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4);
187 } else {
188 uint8_t cfg[4] = {0x04, 0x00, 0x01, 0x01};
189 this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4);
190 }
191 pa_power = std::max(pa_power, (int8_t) -3);
192 pa_power = std::min(pa_power, (int8_t) 14);
193 buf[0] = OCP_80MA;
194 this->write_register_(REG_OCP, buf, 1);
195 } else {
196 // the following values were taken from section 13.1.14.1 table 13-21
197 // in rev 2.1 of the datasheet
198 uint8_t cfg[4] = {0x04, 0x07, 0x00, 0x01};
199 this->write_opcode_(RADIO_SET_PACONFIG, cfg, 4);
200 pa_power = std::max(pa_power, (int8_t) -3);
201 pa_power = std::min(pa_power, (int8_t) 22);
202 buf[0] = OCP_140MA;
203 this->write_register_(REG_OCP, buf, 1);
204 }
205 buf[0] = pa_power;
206 buf[1] = this->pa_ramp_;
207 this->write_opcode_(RADIO_SET_TXPARAMS, buf, 2);
208
209 // configure modem
210 if (this->modulation_ == PACKET_TYPE_LORA) {
211 // set modulation params
212 float duration = 1000.0f * std::pow(2, this->spreading_factor_) / BW_HZ[this->bandwidth_];
213 buf[0] = this->spreading_factor_;
214 buf[1] = BW_LORA[this->bandwidth_ - SX126X_BW_7810];
215 buf[2] = this->coding_rate_;
216 buf[3] = (duration > LOW_DATA_RATE_OPTIMIZE_THRESHOLD) ? 0x01 : 0x00;
218
219 // set packet params and sync word
221 if (this->sync_value_.size() == 2) {
222 this->write_register_(REG_LORA_SYNCWORD, this->sync_value_.data(), this->sync_value_.size());
223 }
224 } else {
225 // set modulation params
226 uint32_t bitrate = ((uint64_t) XTAL_FREQ * 32) / this->bitrate_;
227 uint32_t fdev = ((uint64_t) this->deviation_ << 25) / XTAL_FREQ;
228 buf[0] = (bitrate >> 16) & 0xFF;
229 buf[1] = (bitrate >> 8) & 0xFF;
230 buf[2] = (bitrate >> 0) & 0xFF;
231 buf[3] = this->shaping_;
232 buf[4] = BW_FSK[this->bandwidth_ - SX126X_BW_4800];
233 buf[5] = (fdev >> 16) & 0xFF;
234 buf[6] = (fdev >> 8) & 0xFF;
235 buf[7] = (fdev >> 0) & 0xFF;
237
238 // set packet params and sync word
240 if (!this->sync_value_.empty()) {
241 this->write_register_(REG_GFSK_SYNCWORD, this->sync_value_.data(), this->sync_value_.size());
242 }
243 }
244
245 // switch to rx or sleep
246 if (this->rx_start_) {
247 this->set_mode_rx();
248 } else {
249 this->set_mode_sleep();
250 }
251}
252
254 if (this->payload_length_ > 0) {
255 return this->payload_length_;
256 }
257 return 255;
258}
259
260void SX126x::set_packet_params_(uint8_t payload_length) {
261 uint8_t buf[9];
262 if (this->modulation_ == PACKET_TYPE_LORA) {
263 buf[0] = (this->preamble_size_ >> 8) & 0xFF;
264 buf[1] = (this->preamble_size_ >> 0) & 0xFF;
265 buf[2] = (this->payload_length_ > 0) ? 0x01 : 0x00;
266 buf[3] = payload_length;
267 buf[4] = (this->crc_enable_) ? 0x01 : 0x00;
268 buf[5] = 0x00;
270 } else {
271 uint16_t preamble_size = this->preamble_size_ * 8;
272 buf[0] = (preamble_size >> 8) & 0xFF;
273 buf[1] = (preamble_size >> 0) & 0xFF;
274 buf[2] = (this->preamble_detect_ > 0) ? ((this->preamble_detect_ - 1) | 0x04) : 0x00;
275 buf[3] = this->sync_value_.size() * 8;
276 buf[4] = 0x00;
277 buf[5] = 0x00;
278 buf[6] = payload_length;
279 buf[7] = this->crc_enable_ ? 0x06 : 0x01;
280 buf[8] = 0x00;
282 }
283}
284
285SX126xError SX126x::transmit_packet(const std::vector<uint8_t> &packet) {
286 if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) {
287 ESP_LOGE(TAG, "Packet size does not match config");
289 }
290 if (packet.empty() || packet.size() > this->get_max_packet_size()) {
291 ESP_LOGE(TAG, "Packet size out of range");
293 }
294
297 if (this->payload_length_ == 0) {
298 this->set_packet_params_(packet.size());
299 }
300 this->write_fifo_(0x00, packet);
301 this->set_mode_tx();
302
303 // wait until transmit completes, typically the delay will be less than 100 ms
304 uint32_t start = millis();
305 while (!this->dio1_pin_->digital_read()) {
306 if (millis() - start > TRANSMIT_TIMEOUT_MS) {
307 ESP_LOGE(TAG, "Transmit packet failure");
309 break;
310 }
311 }
312
313 uint8_t buf[2];
314 buf[0] = 0xFF;
315 buf[1] = 0xFF;
316 this->write_opcode_(RADIO_CLR_IRQSTATUS, buf, 2);
317 if (this->rx_start_) {
318 this->set_mode_rx();
319 } else {
320 this->set_mode_sleep();
321 }
322 return ret;
323}
324
325void SX126x::call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr) {
326 for (auto &listener : this->listeners_) {
327 listener->on_packet(packet, rssi, snr);
328 }
329 this->packet_trigger_->trigger(packet, rssi, snr);
330}
331
333 if (!this->dio1_pin_->digital_read()) {
334 return;
335 }
336
337 uint16_t status;
338 uint8_t buf[3];
339 uint8_t rssi;
340 int8_t snr;
341 this->read_opcode_(RADIO_GET_IRQSTATUS, buf, 2);
342 this->write_opcode_(RADIO_CLR_IRQSTATUS, buf, 2);
343 status = (buf[0] << 8) | buf[1];
344 if ((status & IRQ_RX_DONE) == IRQ_RX_DONE) {
347 if (this->modulation_ == PACKET_TYPE_LORA) {
348 rssi = buf[0];
349 snr = buf[1];
350 } else {
351 rssi = buf[2];
352 snr = 0;
353 }
355 this->packet_.resize(buf[0]);
356 this->read_fifo_(buf[1], this->packet_);
357 this->call_listeners_(this->packet_, (float) rssi / -2.0f, (float) snr / 4.0f);
358 }
359 }
360}
361
363 // the following values were taken from section 9.2.1 table 9-2
364 // in rev 2.1 of the datasheet
365 uint8_t buf[2] = {0, 0};
366 if (this->frequency_ > 900000000) {
367 buf[0] = 0xE1;
368 buf[1] = 0xE9;
369 } else if (this->frequency_ > 850000000) {
370 buf[0] = 0xD7;
371 buf[1] = 0xD8;
372 } else if (this->frequency_ > 770000000) {
373 buf[0] = 0xC1;
374 buf[1] = 0xC5;
375 } else if (this->frequency_ > 460000000) {
376 buf[0] = 0x75;
377 buf[1] = 0x81;
378 } else if (this->frequency_ > 425000000) {
379 buf[0] = 0x6B;
380 buf[1] = 0x6F;
381 }
382 if (buf[0] > 0 && buf[1] > 0) {
383 this->write_opcode_(RADIO_CALIBRATEIMAGE, buf, 2);
384 }
385}
386
388 uint8_t buf[8];
389
390 // configure irq params
392 buf[0] = (irq >> 8) & 0xFF;
393 buf[1] = (irq >> 0) & 0xFF;
394 buf[2] = (irq >> 8) & 0xFF;
395 buf[3] = (irq >> 0) & 0xFF;
396 buf[4] = (IRQ_RADIO_NONE >> 8) & 0xFF;
397 buf[5] = (IRQ_RADIO_NONE >> 0) & 0xFF;
398 buf[6] = (IRQ_RADIO_NONE >> 8) & 0xFF;
399 buf[7] = (IRQ_RADIO_NONE >> 0) & 0xFF;
401
402 // set timeout to 0
403 buf[0] = 0x00;
405
406 // switch to continuous mode rx
407 buf[0] = 0xFF;
408 buf[1] = 0xFF;
409 buf[2] = 0xFF;
410 this->write_opcode_(RADIO_SET_RX, buf, 3);
411}
412
414 uint8_t buf[8];
415
416 // configure irq params
417 uint16_t irq = IRQ_TX_DONE | IRQ_RX_TX_TIMEOUT;
418 buf[0] = (irq >> 8) & 0xFF;
419 buf[1] = (irq >> 0) & 0xFF;
420 buf[2] = (irq >> 8) & 0xFF;
421 buf[3] = (irq >> 0) & 0xFF;
422 buf[4] = (IRQ_RADIO_NONE >> 8) & 0xFF;
423 buf[5] = (IRQ_RADIO_NONE >> 0) & 0xFF;
424 buf[6] = (IRQ_RADIO_NONE >> 8) & 0xFF;
425 buf[7] = (IRQ_RADIO_NONE >> 0) & 0xFF;
427
428 // switch to single mode tx
429 buf[0] = 0x00;
430 buf[1] = 0x00;
431 buf[2] = 0x00;
432 this->write_opcode_(RADIO_SET_TX, buf, 3);
433}
434
436 uint8_t buf[1];
437 buf[0] = 0x05;
438 this->write_opcode_(RADIO_SET_SLEEP, buf, 1);
439}
440
442 uint8_t buf[1];
443 buf[0] = mode;
444 this->write_opcode_(RADIO_SET_STANDBY, buf, 1);
445}
446
448 // wait if the device is busy, the maximum delay is only be a few ms
449 // with most commands taking only a few us
450 uint32_t start = millis();
451 while (this->busy_pin_->digital_read()) {
452 if (millis() - start > BUSY_TIMEOUT_MS) {
453 ESP_LOGE(TAG, "Wait busy timeout");
454 this->mark_failed();
455 break;
456 }
457 }
458}
459
461 ESP_LOGCONFIG(TAG, "SX126x:");
462 LOG_PIN(" CS Pin: ", this->cs_);
463 LOG_PIN(" BUSY Pin: ", this->busy_pin_);
464 LOG_PIN(" RST Pin: ", this->rst_pin_);
465 LOG_PIN(" DIO1 Pin: ", this->dio1_pin_);
466 ESP_LOGCONFIG(TAG,
467 " HW Version: %15s\n"
468 " Frequency: %" PRIu32 " Hz\n"
469 " Bandwidth: %" PRIu32 " Hz\n"
470 " PA Power: %" PRId8 " dBm\n"
471 " PA Ramp: %" PRIu16 " us\n"
472 " Payload Length: %" PRIu32 "\n"
473 " CRC Enable: %s\n"
474 " Rx Start: %s",
475 this->version_, this->frequency_, BW_HZ[this->bandwidth_], this->pa_power_, RAMP[this->pa_ramp_],
476 this->payload_length_, TRUEFALSE(this->crc_enable_), TRUEFALSE(this->rx_start_));
477 if (this->modulation_ == PACKET_TYPE_GFSK) {
478 const char *shaping = "NONE";
479 if (this->shaping_ == GAUSSIAN_BT_0_3) {
480 shaping = "GAUSSIAN_BT_0_3";
481 } else if (this->shaping_ == GAUSSIAN_BT_0_5) {
482 shaping = "GAUSSIAN_BT_0_5";
483 } else if (this->shaping_ == GAUSSIAN_BT_0_7) {
484 shaping = "GAUSSIAN_BT_0_7";
485 } else if (this->shaping_ == GAUSSIAN_BT_1_0) {
486 shaping = "GAUSSIAN_BT_1_0";
487 }
488 ESP_LOGCONFIG(TAG,
489 " Modulation: FSK\n"
490 " Deviation: %" PRIu32 " Hz\n"
491 " Shaping: %s\n"
492 " Preamble Size: %" PRIu16 "\n"
493 " Preamble Detect: %" PRIu16 "\n"
494 " Bitrate: %" PRIu32 "b/s",
495 this->deviation_, shaping, this->preamble_size_, this->preamble_detect_, this->bitrate_);
496 } else if (this->modulation_ == PACKET_TYPE_LORA) {
497 const char *cr = "4/8";
498 if (this->coding_rate_ == LORA_CR_4_5) {
499 cr = "4/5";
500 } else if (this->coding_rate_ == LORA_CR_4_6) {
501 cr = "4/6";
502 } else if (this->coding_rate_ == LORA_CR_4_7) {
503 cr = "4/7";
504 }
505 ESP_LOGCONFIG(TAG,
506 " Modulation: LORA\n"
507 " Spreading Factor: %" PRIu8 "\n"
508 " Coding Rate: %s\n"
509 " Preamble Size: %" PRIu16,
510 this->spreading_factor_, cr, this->preamble_size_);
511 }
512 if (!this->sync_value_.empty()) {
513 ESP_LOGCONFIG(TAG, " Sync Value: 0x%s", format_hex(this->sync_value_).c_str());
514 }
515 if (this->is_failed()) {
516 ESP_LOGE(TAG, "Configuring SX126x failed");
517 }
518}
519
520} // namespace sx126x
521} // namespace esphome
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
virtual void mark_failed()
Mark this component as failed.
bool is_failed() const
virtual void setup()=0
virtual void digital_write(bool value)=0
virtual bool digital_read()=0
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition automation.h:145
uint8_t read_fifo_(uint8_t offset, std::vector< uint8_t > &packet)
Definition sx126x.cpp:33
void write_opcode_(uint8_t opcode, uint8_t *data, uint8_t size)
Definition sx126x.cpp:70
std::vector< uint8_t > sync_value_
Definition sx126x.h:113
InternalGPIOPin * busy_pin_
Definition sx126x.h:114
std::vector< SX126xListener * > listeners_
Definition sx126x.h:111
size_t get_max_packet_size()
Definition sx126x.cpp:253
InternalGPIOPin * rst_pin_
Definition sx126x.h:116
void set_mode_standby(SX126xStandbyMode mode)
Definition sx126x.cpp:441
uint8_t read_opcode_(uint8_t opcode, uint8_t *data, uint8_t size)
Definition sx126x.cpp:58
void dump_config() override
Definition sx126x.cpp:460
void read_register_(uint16_t reg, uint8_t *data, uint8_t size)
Definition sx126x.cpp:81
void write_register_(uint16_t reg, uint8_t *data, uint8_t size)
Definition sx126x.cpp:94
uint16_t preamble_size_
Definition sx126x.h:126
void loop() override
Definition sx126x.cpp:332
void set_packet_params_(uint8_t payload_length)
Definition sx126x.cpp:260
void call_listeners_(const std::vector< uint8_t > &packet, float rssi, float snr)
Definition sx126x.cpp:325
std::vector< uint8_t > packet_
Definition sx126x.h:112
uint16_t preamble_detect_
Definition sx126x.h:125
void setup() override
Definition sx126x.cpp:107
std::string hw_version_
Definition sx126x.h:117
uint32_t payload_length_
Definition sx126x.h:123
Trigger< std::vector< uint8_t >, float, float > * packet_trigger_
Definition sx126x.h:110
SX126xError transmit_packet(const std::vector< uint8_t > &packet)
Definition sx126x.cpp:285
InternalGPIOPin * dio1_pin_
Definition sx126x.h:115
uint8_t spreading_factor_
Definition sx126x.h:132
void write_fifo_(uint8_t offset, const std::vector< uint8_t > &packet)
Definition sx126x.cpp:46
uint16_t preamble_size
uint8_t duration
Definition msa3xx.h:0
const char *const TAG
Definition spi.cpp:8
@ RADIO_SET_MODULATIONPARAMS
Definition sx126x_reg.h:32
@ RADIO_SET_LORASYMBTIMEOUT
Definition sx126x_reg.h:51
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
Definition helpers.cpp:249
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition core.cpp:31
void IRAM_ATTR HOT delay(uint32_t ms)
Definition core.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28