ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
sx127x.cpp
Go to the documentation of this file.
1#include "sx127x.h"
3#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6namespace esphome::sx127x {
7
8ESPHOME_LOG_TAG(TAG, "sx127x");
9static const uint32_t FXOSC = 32000000u;
10static constexpr uint16_t RAMP[16] PROGMEM = {3400, 2000, 1000, 500, 250, 125, 100, 62, 50, 40, 31, 25, 20, 15, 12, 10};
11// Aligned 32 bit loads from flash are safe on ESP8266, so this table is read directly
12static constexpr uint32_t BW_HZ[22] PROGMEM = {2604, 3125, 3906, 5208, 6250, 7812, 10416, 12500,
13 15625, 20833, 25000, 31250, 41666, 50000, 62500, 83333,
14 100000, 125000, 166666, 200000, 250000, 500000};
15static constexpr uint8_t BW_LORA[22] PROGMEM = {
18static constexpr uint8_t BW_FSK_OOK[22] PROGMEM = {
22
23static const int32_t RSSI_OFFSET_HF = 157;
24static const int32_t RSSI_OFFSET_LF = 164;
25
26uint8_t SX127x::read_register_(uint8_t reg) {
27 this->enable();
28 this->write_byte(reg & 0x7F);
29 uint8_t value = this->read_byte();
30 this->disable();
31 return value;
32}
33
34void SX127x::write_register_(uint8_t reg, uint8_t value) {
35 this->enable();
36 this->write_byte(reg | 0x80);
37 this->write_byte(value);
38 this->disable();
39}
40
41void SX127x::read_fifo_(std::vector<uint8_t> &packet) {
42 this->enable();
43 this->write_byte(REG_FIFO & 0x7F);
44 for (auto &byte : packet) {
45 byte = this->transfer_byte(0x00);
46 }
47 this->disable();
48}
49
50void SX127x::write_fifo_(const uint8_t *data, size_t len) {
51 this->enable();
52 this->write_byte(REG_FIFO | 0x80);
53 for (size_t i = 0; i < len; i++) {
54 this->transfer_byte(data[i]);
55 }
56 this->disable();
57}
58
60
62 // setup reset
63 this->rst_pin_->setup();
64
65 // setup dio0
66 if (this->dio0_pin_) {
67 this->dio0_pin_->setup();
69 }
70
71 // start spi
72 this->spi_setup();
73
74 // configure rf
75 this->configure();
76}
77
79 // toggle chip reset
80 this->rst_pin_->digital_write(false);
82 this->rst_pin_->digital_write(true);
83 delayMicroseconds(10000);
84
85 // check silicon version to make sure hw is ok
86 if (this->read_register_(REG_VERSION) != 0x12) {
87 this->mark_failed(LOG_STR("Unsupported chip version"));
88 return;
89 }
90
91 // enter sleep mode
93
94 // set freq
95 uint64_t frf = ((uint64_t) this->frequency_ << 19) / FXOSC;
96 this->write_register_(REG_FRF_MSB, (uint8_t) ((frf >> 16) & 0xFF));
97 this->write_register_(REG_FRF_MID, (uint8_t) ((frf >> 8) & 0xFF));
98 this->write_register_(REG_FRF_LSB, (uint8_t) ((frf >> 0) & 0xFF));
99
100 // enter standby mode
102
103 // run image cal
104 this->run_image_cal();
105
106 // go back to sleep
107 this->set_mode_sleep();
108
109 // config pa
110 if (this->pa_pin_ == PA_PIN_BOOST) {
111 this->pa_power_ = std::max(this->pa_power_, (uint8_t) 2);
112 this->pa_power_ = std::min(this->pa_power_, (uint8_t) 17);
113 this->write_register_(REG_PA_CONFIG, (this->pa_power_ - 2) | this->pa_pin_ | PA_MAX_POWER);
114 } else {
115 this->pa_power_ = std::min(this->pa_power_, (uint8_t) 14);
116 this->write_register_(REG_PA_CONFIG, (this->pa_power_ - 0) | this->pa_pin_ | PA_MAX_POWER);
117 }
118 if (this->modulation_ != MOD_LORA) {
119 this->write_register_(REG_PA_RAMP, this->pa_ramp_ | this->shaping_);
120 } else {
122 }
123
124 // configure modem
125 if (this->modulation_ != MOD_LORA) {
126 this->configure_fsk_ook_();
127 } else {
128 this->configure_lora_();
129 }
130
131 // switch to rx or sleep
132 if (this->rx_start_) {
133 this->set_mode_rx();
134 } else {
135 this->set_mode_sleep();
136 }
137}
138
140 // set the channel bw
141 this->write_register_(REG_RX_BW, progmem_read_byte(&BW_FSK_OOK[this->bandwidth_]));
142
143 // set fdev
144 uint32_t fdev = std::min((this->deviation_ * 4096) / 250000, (uint32_t) 0x3FFF);
145 this->write_register_(REG_FDEV_MSB, (uint8_t) ((fdev >> 8) & 0xFF));
146 this->write_register_(REG_FDEV_LSB, (uint8_t) ((fdev >> 0) & 0xFF));
147
148 // set bitrate
149 uint64_t bitrate = (FXOSC + this->bitrate_ / 2) / this->bitrate_; // round up
150 this->write_register_(REG_BITRATE_MSB, (uint8_t) ((bitrate >> 8) & 0xFF));
151 this->write_register_(REG_BITRATE_LSB, (uint8_t) ((bitrate >> 0) & 0xFF));
152
153 // configure rx and afc
154 uint8_t trigger = (this->preamble_detect_ > 0) ? TRIGGER_PREAMBLE : TRIGGER_RSSI;
156 if (this->modulation_ == MOD_FSK) {
158 } else {
159 this->write_register_(REG_RX_CONFIG, AGC_AUTO_ON | trigger);
160 }
161
162 // configure packet mode
163 if (this->packet_mode_) {
164 uint8_t crc_mode = (this->crc_enable_) ? CRC_ON : CRC_OFF;
166 if (this->payload_length_ > 0) {
169 } else {
172 }
174 } else {
176 }
178
179 // config bit synchronizer
180 uint8_t polarity = (this->preamble_polarity_ == 0xAA) ? PREAMBLE_AA : PREAMBLE_55;
181 if (!this->sync_value_.empty()) {
182 uint8_t size = this->sync_value_.size() - 1;
184 for (uint32_t i = 0; i < this->sync_value_.size(); i++) {
185 this->write_register_(REG_SYNC_VALUE1 + i, this->sync_value_[i]);
186 }
187 } else {
189 }
190
191 // config preamble detector
192 if (this->preamble_detect_ > 0) {
193 uint8_t size = (this->preamble_detect_ - 1) << PREAMBLE_DETECTOR_SIZE_SHIFT;
194 uint8_t tol = this->preamble_errors_ << PREAMBLE_DETECTOR_TOL_SHIFT;
196 } else {
198 }
201
202 // config sync generation and setup ook threshold
203 uint8_t bitsync = this->bitsync_ ? BIT_SYNC_ON : BIT_SYNC_OFF;
206
207 // set rx floor
208 this->write_register_(REG_OOK_FIX, 256 + int(this->rx_floor_ * 2.0f));
209 this->write_register_(REG_RSSI_THRESH, std::abs(int(this->rx_floor_ * 2.0f)));
210}
211
213 // config modem
214 uint8_t header_mode = this->payload_length_ > 0 ? IMPLICIT_HEADER : EXPLICIT_HEADER;
215 uint8_t crc_mode = (this->crc_enable_) ? RX_PAYLOAD_CRC_ON : RX_PAYLOAD_CRC_OFF;
216 uint8_t spreading_factor = this->spreading_factor_ << SPREADING_FACTOR_SHIFT;
218 progmem_read_byte(&BW_LORA[this->bandwidth_]) | this->coding_rate_ | header_mode);
219 this->write_register_(REG_MODEM_CONFIG2, spreading_factor | crc_mode);
220
221 // config fifo and payload length
224 this->write_register_(REG_PAYLOAD_LENGTH, std::max(this->payload_length_, (uint32_t) 1));
225
226 // config preamble
227 if (this->preamble_size_ >= 6) {
230 }
231
232 // optimize detection
233 float duration = 1000.0f * (1UL << this->spreading_factor_) / BW_HZ[this->bandwidth_];
234 if (duration > 16) {
236 } else {
238 }
239 if (this->spreading_factor_ == 6) {
242 } else {
245 }
246
247 // config sync word
248 if (!this->sync_value_.empty()) {
250 }
251}
252
254 if (this->payload_length_ > 0) {
255 return this->payload_length_;
256 }
257 if (this->modulation_ == MOD_LORA) {
258 return SX127X_MAX_PACKET_SIZE;
259 }
260 return 64;
261}
262
263SX127xError SX127x::transmit_packet(const uint8_t *data, size_t len) {
264 // Callers such as a template number's on_value can run before setup() or after a failed setup
265 if (!this->is_ready()) {
266 ESP_LOGW(TAG, "Radio not ready, packet dropped");
268 }
269 if (this->payload_length_ > 0 && this->payload_length_ != len) {
270 ESP_LOGE(TAG, "Packet size does not match config");
272 }
273 if (len == 0 || len > this->get_max_packet_size()) {
274 ESP_LOGE(TAG, "Packet size out of range");
276 }
277
278 if (this->dio0_pin_ == nullptr) {
279 ESP_LOGE(TAG, "DIO0 pin not configured, cannot wait for transmit completion");
281 }
282
284 if (this->modulation_ == MOD_LORA) {
285 this->set_mode_standby();
286 if (this->payload_length_ == 0) {
288 }
289 this->write_register_(REG_IRQ_FLAGS, 0xFF);
291 this->write_fifo_(data, len);
292 this->set_mode_tx();
293 } else {
294 this->set_mode_standby();
295 if (this->payload_length_ == 0) {
296 this->write_register_(REG_FIFO, len);
297 }
298 this->write_fifo_(data, len);
299 this->set_mode_tx();
300 }
301
302 // wait until transmit completes, typically the delay will be less than 100 ms
303 uint32_t start = millis();
304 while (!this->dio0_pin_->digital_read()) {
305 App.feed_wdt();
306 if (millis() - start > 4000) {
307 ESP_LOGE(TAG, "Transmit packet failure");
309 break;
310 }
311 }
312 if (this->rx_start_) {
313 this->set_mode_rx();
314 } else {
315 this->set_mode_sleep();
316 }
317 return ret;
318}
319
320void SX127x::call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr) {
321 for (auto &listener : this->listeners_) {
322 listener->on_packet(packet, rssi, snr);
323 }
324 this->packet_trigger_.trigger(packet, rssi, snr);
325}
326
328 this->disable_loop();
329 if (this->dio0_pin_ == nullptr || !this->dio0_pin_->digital_read()) {
330 return;
331 }
332
333 if (this->modulation_ == MOD_LORA) {
334 uint8_t status = this->read_register_(REG_IRQ_FLAGS);
335 this->write_register_(REG_IRQ_FLAGS, 0xFF);
336 if ((status & PAYLOAD_CRC_ERROR) == 0) {
337 uint8_t bytes = this->read_register_(REG_NB_RX_BYTES);
338 uint8_t addr = this->read_register_(REG_FIFO_RX_CURR_ADDR);
339 uint8_t rssi = this->read_register_(REG_PKT_RSSI_VALUE);
340 int8_t snr = (int8_t) this->read_register_(REG_PKT_SNR_VALUE);
341 this->packet_.resize(bytes);
343 this->read_fifo_(this->packet_);
344 if (this->frequency_ > 700000000) {
345 this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_HF, (float) snr / 4);
346 } else {
347 this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_LF, (float) snr / 4);
348 }
349 }
350 } else if (this->packet_mode_) {
351 uint8_t payload_length = this->payload_length_;
352 if (payload_length == 0) {
353 payload_length = this->read_register_(REG_FIFO);
354 }
355 this->packet_.resize(payload_length);
356 this->read_fifo_(this->packet_);
357 this->call_listeners_(this->packet_, 0.0f, 0.0f);
358 }
359}
360
362 if (this->modulation_ == MOD_LORA) {
365 }
366 if (this->auto_cal_) {
368 } else {
370 }
371 uint32_t start = millis();
373 if (millis() - start > 20) {
374 this->mark_failed(LOG_STR("Image cal failure"));
375 break;
376 }
377 }
378 if (this->modulation_ == MOD_LORA) {
379 this->set_mode_(this->modulation_, MODE_SLEEP);
380 this->set_mode_(this->modulation_, MODE_STDBY);
381 }
382}
383
384void SX127x::set_mode_(uint8_t modulation, uint8_t mode) {
385 uint32_t start = millis();
386 this->write_register_(REG_OP_MODE, modulation | mode);
387 while (true) {
388 uint8_t curr = this->read_register_(REG_OP_MODE) & MODE_MASK;
389 if ((curr == mode) || (mode == MODE_RX && curr == MODE_RX_FS)) {
390 if (mode == MODE_SLEEP) {
391 this->write_register_(REG_OP_MODE, modulation | mode);
392 }
393 break;
394 }
395 if (millis() - start > 20) {
396 this->mark_failed(LOG_STR("Set mode failure"));
397 return;
398 }
399 }
400}
401
403 this->set_mode_(this->modulation_, MODE_RX);
404 if (this->modulation_ == MOD_LORA) {
407 }
408}
409
411 this->set_mode_(this->modulation_, MODE_TX);
412 if (this->modulation_ == MOD_LORA) {
415 }
416}
417
419
421
423 ESP_LOGCONFIG(TAG, "SX127x:");
424 LOG_PIN(" CS Pin: ", this->cs_);
425 LOG_PIN(" RST Pin: ", this->rst_pin_);
426 LOG_PIN(" DIO0 Pin: ", this->dio0_pin_);
427 const char *pa_pin = "RFO";
428 if (this->pa_pin_ == PA_PIN_BOOST) {
429 pa_pin = "BOOST";
430 }
431 ESP_LOGCONFIG(TAG,
432 " Auto Cal: %s\n"
433 " Frequency: %" PRIu32 " Hz\n"
434 " Bandwidth: %" PRIu32 " Hz\n"
435 " PA Pin: %s\n"
436 " PA Power: %" PRIu8 " dBm\n"
437 " PA Ramp: %" PRIu16 " us",
438 TRUEFALSE(this->auto_cal_), this->frequency_, BW_HZ[this->bandwidth_], pa_pin, this->pa_power_,
439 progmem_read_uint16(&RAMP[this->pa_ramp_]));
440 if (this->modulation_ == MOD_FSK) {
441 ESP_LOGCONFIG(TAG, " Deviation: %" PRIu32 " Hz", this->deviation_);
442 }
443 if (this->modulation_ == MOD_LORA) {
444 const char *cr = "4/8";
445 if (this->coding_rate_ == CODING_RATE_4_5) {
446 cr = "4/5";
447 } else if (this->coding_rate_ == CODING_RATE_4_6) {
448 cr = "4/6";
449 } else if (this->coding_rate_ == CODING_RATE_4_7) {
450 cr = "4/7";
451 }
452 ESP_LOGCONFIG(TAG,
453 " Modulation: LORA\n"
454 " Preamble Size: %" PRIu16 "\n"
455 " Spreading Factor: %" PRIu8 "\n"
456 " Coding Rate: %s\n"
457 " CRC Enable: %s",
458 this->preamble_size_, this->spreading_factor_, cr, TRUEFALSE(this->crc_enable_));
459 if (this->payload_length_ > 0) {
460 ESP_LOGCONFIG(TAG, " Payload Length: %" PRIu32, this->payload_length_);
461 }
462 if (!this->sync_value_.empty()) {
463 ESP_LOGCONFIG(TAG, " Sync Value: 0x%02x", this->sync_value_[0]);
464 }
465 } else {
466 const char *shaping = "NONE";
467 if (this->modulation_ == MOD_FSK) {
468 if (this->shaping_ == GAUSSIAN_BT_0_3) {
469 shaping = "GAUSSIAN_BT_0_3";
470 } else if (this->shaping_ == GAUSSIAN_BT_0_5) {
471 shaping = "GAUSSIAN_BT_0_5";
472 } else if (this->shaping_ == GAUSSIAN_BT_1_0) {
473 shaping = "GAUSSIAN_BT_1_0";
474 }
475 } else {
476 if (this->shaping_ == CUTOFF_BR_X_2) {
477 shaping = "CUTOFF_BR_X_2";
478 } else if (this->shaping_ == CUTOFF_BR_X_1) {
479 shaping = "CUTOFF_BR_X_1";
480 }
481 }
482 ESP_LOGCONFIG(TAG,
483 " Shaping: %s\n"
484 " Modulation: %s\n"
485 " Bitrate: %" PRIu32 "b/s\n"
486 " Bitsync: %s\n"
487 " Rx Start: %s\n"
488 " Rx Floor: %.1f dBm\n"
489 " Packet Mode: %s",
490 shaping, this->modulation_ == MOD_FSK ? LOG_STR_LITERAL("FSK") : LOG_STR_LITERAL("OOK"),
491 this->bitrate_, TRUEFALSE(this->bitsync_), TRUEFALSE(this->rx_start_), this->rx_floor_,
492 TRUEFALSE(this->packet_mode_));
493 if (this->packet_mode_) {
494 ESP_LOGCONFIG(TAG, " CRC Enable: %s", TRUEFALSE(this->crc_enable_));
495 }
496 if (this->payload_length_ > 0) {
497 ESP_LOGCONFIG(TAG, " Payload Length: %" PRIu32, this->payload_length_);
498 }
499 if (!this->sync_value_.empty()) {
500 char hex_buf[17]; // 8 bytes max = 16 hex chars + null
501 ESP_LOGCONFIG(TAG, " Sync Value: 0x%s",
502 format_hex_to(hex_buf, this->sync_value_.data(), this->sync_value_.size()));
503 }
504 if (this->preamble_size_ > 0 || this->preamble_detect_ > 0) {
505 ESP_LOGCONFIG(TAG,
506 " Preamble Polarity: 0x%X\n"
507 " Preamble Size: %" PRIu16 "\n"
508 " Preamble Detect: %" PRIu8 "\n"
509 " Preamble Errors: %" PRIu8,
511 }
512 }
513}
514
515} // namespace esphome::sx127x
BedjetMode mode
BedJet operating mode.
uint8_t status
Definition bl0942.h:8
void feed_wdt()
Feed the task watchdog.
void mark_failed()
Mark this component as failed.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
bool is_ready() const
void disable_loop()
Disable this component's loop.
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Inform the parent automation that the event has triggered.
Definition automation.h:557
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:29
void digital_write(bool value) override
Definition gpio.cpp:215
InternalGPIOPin * rst_pin_
Definition sx127x.h:108
uint8_t preamble_polarity_
Definition sx127x.h:122
InternalGPIOPin * dio0_pin_
Definition sx127x.h:107
Trigger< std::vector< uint8_t >, float, float > packet_trigger_
Definition sx127x.h:103
std::vector< SX127xListener * > listeners_
Definition sx127x.h:104
void write_fifo_(const uint8_t *data, size_t len)
Definition sx127x.cpp:50
std::vector< uint8_t > sync_value_
Definition sx127x.h:106
uint32_t payload_length_
Definition sx127x.h:113
void dump_config() override
Definition sx127x.cpp:422
uint8_t read_register_(uint8_t reg)
Definition sx127x.cpp:26
static void gpio_intr(SX127x *arg)
Definition sx127x.cpp:59
void loop() override
Definition sx127x.cpp:327
void set_mode_(uint8_t modulation, uint8_t mode)
Definition sx127x.cpp:384
void read_fifo_(std::vector< uint8_t > &packet)
Definition sx127x.cpp:41
uint16_t preamble_size_
Definition sx127x.h:114
std::vector< uint8_t > packet_
Definition sx127x.h:105
size_t get_max_packet_size()
Definition sx127x.cpp:253
void write_register_(uint8_t reg, uint8_t value)
Definition sx127x.cpp:34
void call_listeners_(const std::vector< uint8_t > &packet, float rssi, float snr)
Definition sx127x.cpp:320
void setup() override
Definition sx127x.cpp:61
uint8_t spreading_factor_
Definition sx127x.h:124
SX127xError transmit_packet(const uint8_t *data, size_t len)
Definition sx127x.cpp:263
int ret
uint8_t duration
Definition msa3xx.h:0
constexpr float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM
Definition bme680.cpp:28
@ INTERRUPT_RISING_EDGE
Definition gpio_pin.h:52
@ PREAMBLE_DETECTOR_TOL_SHIFT
Definition sx127x_reg.h:208
@ PREAMBLE_DETECTOR_SIZE_SHIFT
Definition sx127x_reg.h:207
ESPHOME_LOG_TAG(TAG, "sx127x_transport")
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint16_t size
Definition helpers.cpp:34
uint16_t progmem_read_uint16(const uint16_t *addr)
Definition hal.h:53
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:428
Application App
Global storage of Application pointer - only one Application can exist.
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
static void uint32_t