ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
sx127x.cpp
Go to the documentation of this file.
1#include "sx127x.h"
2#include "esphome/core/hal.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace sx127x {
7
8static const char *const TAG = "sx127x";
9static const uint32_t FXOSC = 32000000u;
10static const uint16_t RAMP[16] = {3400, 2000, 1000, 500, 250, 125, 100, 62, 50, 40, 31, 25, 20, 15, 12, 10};
11static const uint32_t BW_HZ[22] = {2604, 3125, 3906, 5208, 6250, 7812, 10416, 12500, 15625, 20833, 25000,
12 31250, 41666, 50000, 62500, 83333, 100000, 125000, 166666, 200000, 250000, 500000};
13static const uint8_t BW_LORA[22] = {BW_7_8, BW_7_8, BW_7_8, BW_7_8, BW_7_8, BW_7_8, BW_10_4, BW_15_6,
16static const uint8_t BW_FSK_OOK[22] = {RX_BW_2_6, RX_BW_3_1, RX_BW_3_9, RX_BW_5_2, RX_BW_6_3, RX_BW_7_8,
20static const int32_t RSSI_OFFSET_HF = 157;
21static const int32_t RSSI_OFFSET_LF = 164;
22
23uint8_t SX127x::read_register_(uint8_t reg) {
24 this->enable();
25 this->write_byte(reg & 0x7F);
26 uint8_t value = this->read_byte();
27 this->disable();
28 return value;
29}
30
31void SX127x::write_register_(uint8_t reg, uint8_t value) {
32 this->enable();
33 this->write_byte(reg | 0x80);
34 this->write_byte(value);
35 this->disable();
36}
37
38void SX127x::read_fifo_(std::vector<uint8_t> &packet) {
39 this->enable();
40 this->write_byte(REG_FIFO & 0x7F);
41 this->read_array(packet.data(), packet.size());
42 this->disable();
43}
44
45void SX127x::write_fifo_(const std::vector<uint8_t> &packet) {
46 this->enable();
47 this->write_byte(REG_FIFO | 0x80);
48 this->write_array(packet.data(), packet.size());
49 this->disable();
50}
51
53 // setup reset
54 this->rst_pin_->setup();
55
56 // setup dio0
57 if (this->dio0_pin_) {
58 this->dio0_pin_->setup();
59 }
60
61 // start spi
62 this->spi_setup();
63
64 // configure rf
65 this->configure();
66}
67
69 // toggle chip reset
70 this->rst_pin_->digital_write(false);
72 this->rst_pin_->digital_write(true);
73 delayMicroseconds(10000);
74
75 // check silicon version to make sure hw is ok
76 if (this->read_register_(REG_VERSION) != 0x12) {
77 this->mark_failed();
78 return;
79 }
80
81 // enter sleep mode
83
84 // set freq
85 uint64_t frf = ((uint64_t) this->frequency_ << 19) / FXOSC;
86 this->write_register_(REG_FRF_MSB, (uint8_t) ((frf >> 16) & 0xFF));
87 this->write_register_(REG_FRF_MID, (uint8_t) ((frf >> 8) & 0xFF));
88 this->write_register_(REG_FRF_LSB, (uint8_t) ((frf >> 0) & 0xFF));
89
90 // enter standby mode
92
93 // run image cal
94 this->run_image_cal();
95
96 // go back to sleep
97 this->set_mode_sleep();
98
99 // config pa
100 if (this->pa_pin_ == PA_PIN_BOOST) {
101 this->pa_power_ = std::max(this->pa_power_, (uint8_t) 2);
102 this->pa_power_ = std::min(this->pa_power_, (uint8_t) 17);
103 this->write_register_(REG_PA_CONFIG, (this->pa_power_ - 2) | this->pa_pin_ | PA_MAX_POWER);
104 } else {
105 this->pa_power_ = std::min(this->pa_power_, (uint8_t) 14);
106 this->write_register_(REG_PA_CONFIG, (this->pa_power_ - 0) | this->pa_pin_ | PA_MAX_POWER);
107 }
108 if (this->modulation_ != MOD_LORA) {
109 this->write_register_(REG_PA_RAMP, this->pa_ramp_ | this->shaping_);
110 } else {
112 }
113
114 // configure modem
115 if (this->modulation_ != MOD_LORA) {
116 this->configure_fsk_ook_();
117 } else {
118 this->configure_lora_();
119 }
120
121 // switch to rx or sleep
122 if (this->rx_start_) {
123 this->set_mode_rx();
124 } else {
125 this->set_mode_sleep();
126 }
127}
128
130 // set the channel bw
131 this->write_register_(REG_RX_BW, BW_FSK_OOK[this->bandwidth_]);
132
133 // set fdev
134 uint32_t fdev = std::min((this->deviation_ * 4096) / 250000, (uint32_t) 0x3FFF);
135 this->write_register_(REG_FDEV_MSB, (uint8_t) ((fdev >> 8) & 0xFF));
136 this->write_register_(REG_FDEV_LSB, (uint8_t) ((fdev >> 0) & 0xFF));
137
138 // set bitrate
139 uint64_t bitrate = (FXOSC + this->bitrate_ / 2) / this->bitrate_; // round up
140 this->write_register_(REG_BITRATE_MSB, (uint8_t) ((bitrate >> 8) & 0xFF));
141 this->write_register_(REG_BITRATE_LSB, (uint8_t) ((bitrate >> 0) & 0xFF));
142
143 // configure rx and afc
144 uint8_t trigger = (this->preamble_detect_ > 0) ? TRIGGER_PREAMBLE : TRIGGER_RSSI;
146 if (this->modulation_ == MOD_FSK) {
148 } else {
149 this->write_register_(REG_RX_CONFIG, AGC_AUTO_ON | trigger);
150 }
151
152 // configure packet mode
153 if (this->packet_mode_) {
154 uint8_t crc_mode = (this->crc_enable_) ? CRC_ON : CRC_OFF;
156 if (this->payload_length_ > 0) {
159 } else {
162 }
164 } else {
166 }
168
169 // config bit synchronizer
170 uint8_t polarity = (this->preamble_polarity_ == 0xAA) ? PREAMBLE_AA : PREAMBLE_55;
171 if (!this->sync_value_.empty()) {
172 uint8_t size = this->sync_value_.size() - 1;
174 for (uint32_t i = 0; i < this->sync_value_.size(); i++) {
175 this->write_register_(REG_SYNC_VALUE1 + i, this->sync_value_[i]);
176 }
177 } else {
179 }
180
181 // config preamble detector
182 if (this->preamble_detect_ > 0) {
183 uint8_t size = (this->preamble_detect_ - 1) << PREAMBLE_DETECTOR_SIZE_SHIFT;
184 uint8_t tol = this->preamble_errors_ << PREAMBLE_DETECTOR_TOL_SHIFT;
186 } else {
188 }
191
192 // config sync generation and setup ook threshold
193 uint8_t bitsync = this->bitsync_ ? BIT_SYNC_ON : BIT_SYNC_OFF;
196
197 // set rx floor
198 this->write_register_(REG_OOK_FIX, 256 + int(this->rx_floor_ * 2.0));
199 this->write_register_(REG_RSSI_THRESH, std::abs(int(this->rx_floor_ * 2.0)));
200}
201
203 // config modem
204 uint8_t header_mode = this->payload_length_ > 0 ? IMPLICIT_HEADER : EXPLICIT_HEADER;
205 uint8_t crc_mode = (this->crc_enable_) ? RX_PAYLOAD_CRC_ON : RX_PAYLOAD_CRC_OFF;
206 uint8_t spreading_factor = this->spreading_factor_ << SPREADING_FACTOR_SHIFT;
207 this->write_register_(REG_MODEM_CONFIG1, BW_LORA[this->bandwidth_] | this->coding_rate_ | header_mode);
208 this->write_register_(REG_MODEM_CONFIG2, spreading_factor | crc_mode);
209
210 // config fifo and payload length
213 this->write_register_(REG_PAYLOAD_LENGTH, std::max(this->payload_length_, (uint32_t) 1));
214
215 // config preamble
216 if (this->preamble_size_ >= 6) {
219 }
220
221 // optimize detection
222 float duration = 1000.0f * std::pow(2, this->spreading_factor_) / BW_HZ[this->bandwidth_];
223 if (duration > 16) {
225 } else {
227 }
228 if (this->spreading_factor_ == 6) {
231 } else {
234 }
235
236 // config sync word
237 if (!this->sync_value_.empty()) {
239 }
240}
241
243 if (this->payload_length_ > 0) {
244 return this->payload_length_;
245 }
246 if (this->modulation_ == MOD_LORA) {
247 return 256;
248 } else {
249 return 64;
250 }
251}
252
253SX127xError SX127x::transmit_packet(const std::vector<uint8_t> &packet) {
254 if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) {
255 ESP_LOGE(TAG, "Packet size does not match config");
257 }
258 if (packet.empty() || packet.size() > this->get_max_packet_size()) {
259 ESP_LOGE(TAG, "Packet size out of range");
261 }
262
264 if (this->modulation_ == MOD_LORA) {
265 this->set_mode_standby();
266 if (this->payload_length_ == 0) {
267 this->write_register_(REG_PAYLOAD_LENGTH, packet.size());
268 }
269 this->write_register_(REG_IRQ_FLAGS, 0xFF);
271 this->write_fifo_(packet);
272 this->set_mode_tx();
273 } else {
274 this->set_mode_standby();
275 if (this->payload_length_ == 0) {
276 this->write_register_(REG_FIFO, packet.size());
277 }
278 this->write_fifo_(packet);
279 this->set_mode_tx();
280 }
281
282 // wait until transmit completes, typically the delay will be less than 100 ms
283 uint32_t start = millis();
284 while (!this->dio0_pin_->digital_read()) {
285 if (millis() - start > 4000) {
286 ESP_LOGE(TAG, "Transmit packet failure");
288 break;
289 }
290 }
291 if (this->rx_start_) {
292 this->set_mode_rx();
293 } else {
294 this->set_mode_sleep();
295 }
296 return ret;
297}
298
299void SX127x::call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr) {
300 for (auto &listener : this->listeners_) {
301 listener->on_packet(packet, rssi, snr);
302 }
303 this->packet_trigger_->trigger(packet, rssi, snr);
304}
305
307 if (this->dio0_pin_ == nullptr || !this->dio0_pin_->digital_read()) {
308 return;
309 }
310
311 if (this->modulation_ == MOD_LORA) {
312 uint8_t status = this->read_register_(REG_IRQ_FLAGS);
313 this->write_register_(REG_IRQ_FLAGS, 0xFF);
314 if ((status & PAYLOAD_CRC_ERROR) == 0) {
315 uint8_t bytes = this->read_register_(REG_NB_RX_BYTES);
316 uint8_t addr = this->read_register_(REG_FIFO_RX_CURR_ADDR);
317 uint8_t rssi = this->read_register_(REG_PKT_RSSI_VALUE);
318 int8_t snr = (int8_t) this->read_register_(REG_PKT_SNR_VALUE);
319 this->packet_.resize(bytes);
321 this->read_fifo_(this->packet_);
322 if (this->frequency_ > 700000000) {
323 this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_HF, (float) snr / 4);
324 } else {
325 this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_LF, (float) snr / 4);
326 }
327 }
328 } else if (this->packet_mode_) {
329 uint8_t payload_length = this->payload_length_;
330 if (payload_length == 0) {
331 payload_length = this->read_register_(REG_FIFO);
332 }
333 this->packet_.resize(payload_length);
334 this->read_fifo_(this->packet_);
335 this->call_listeners_(this->packet_, 0.0f, 0.0f);
336 }
337}
338
340 if (this->modulation_ == MOD_LORA) {
343 }
344 if (this->auto_cal_) {
346 } else {
348 }
349 uint32_t start = millis();
351 if (millis() - start > 20) {
352 ESP_LOGE(TAG, "Image cal failure");
353 this->mark_failed();
354 break;
355 }
356 }
357 if (this->modulation_ == MOD_LORA) {
358 this->set_mode_(this->modulation_, MODE_SLEEP);
359 this->set_mode_(this->modulation_, MODE_STDBY);
360 }
361}
362
363void SX127x::set_mode_(uint8_t modulation, uint8_t mode) {
364 uint32_t start = millis();
365 this->write_register_(REG_OP_MODE, modulation | mode);
366 while (true) {
367 uint8_t curr = this->read_register_(REG_OP_MODE) & MODE_MASK;
368 if ((curr == mode) || (mode == MODE_RX && curr == MODE_RX_FS)) {
369 if (mode == MODE_SLEEP) {
370 this->write_register_(REG_OP_MODE, modulation | mode);
371 }
372 break;
373 }
374 if (millis() - start > 20) {
375 ESP_LOGE(TAG, "Set mode failure");
376 this->mark_failed();
377 break;
378 }
379 }
380}
381
383 this->set_mode_(this->modulation_, MODE_RX);
384 if (this->modulation_ == MOD_LORA) {
387 }
388}
389
391 this->set_mode_(this->modulation_, MODE_TX);
392 if (this->modulation_ == MOD_LORA) {
395 }
396}
397
399
401
403 ESP_LOGCONFIG(TAG, "SX127x:");
404 LOG_PIN(" CS Pin: ", this->cs_);
405 LOG_PIN(" RST Pin: ", this->rst_pin_);
406 LOG_PIN(" DIO0 Pin: ", this->dio0_pin_);
407 const char *pa_pin = "RFO";
408 if (this->pa_pin_ == PA_PIN_BOOST) {
409 pa_pin = "BOOST";
410 }
411 ESP_LOGCONFIG(TAG,
412 " Auto Cal: %s\n"
413 " Frequency: %" PRIu32 " Hz\n"
414 " Bandwidth: %" PRIu32 " Hz\n"
415 " PA Pin: %s\n"
416 " PA Power: %" PRIu8 " dBm\n"
417 " PA Ramp: %" PRIu16 " us",
418 TRUEFALSE(this->auto_cal_), this->frequency_, BW_HZ[this->bandwidth_], pa_pin, this->pa_power_,
419 RAMP[this->pa_ramp_]);
420 if (this->modulation_ == MOD_FSK) {
421 ESP_LOGCONFIG(TAG, " Deviation: %" PRIu32 " Hz", this->deviation_);
422 }
423 if (this->modulation_ == MOD_LORA) {
424 const char *cr = "4/8";
425 if (this->coding_rate_ == CODING_RATE_4_5) {
426 cr = "4/5";
427 } else if (this->coding_rate_ == CODING_RATE_4_6) {
428 cr = "4/6";
429 } else if (this->coding_rate_ == CODING_RATE_4_7) {
430 cr = "4/7";
431 }
432 ESP_LOGCONFIG(TAG,
433 " Modulation: LORA\n"
434 " Preamble Size: %" PRIu16 "\n"
435 " Spreading Factor: %" PRIu8 "\n"
436 " Coding Rate: %s\n"
437 " CRC Enable: %s",
438 this->preamble_size_, this->spreading_factor_, cr, TRUEFALSE(this->crc_enable_));
439 if (this->payload_length_ > 0) {
440 ESP_LOGCONFIG(TAG, " Payload Length: %" PRIu32, this->payload_length_);
441 }
442 if (!this->sync_value_.empty()) {
443 ESP_LOGCONFIG(TAG, " Sync Value: 0x%02x", this->sync_value_[0]);
444 }
445 } else {
446 const char *shaping = "NONE";
447 if (this->modulation_ == MOD_FSK) {
448 if (this->shaping_ == GAUSSIAN_BT_0_3) {
449 shaping = "GAUSSIAN_BT_0_3";
450 } else if (this->shaping_ == GAUSSIAN_BT_0_5) {
451 shaping = "GAUSSIAN_BT_0_5";
452 } else if (this->shaping_ == GAUSSIAN_BT_1_0) {
453 shaping = "GAUSSIAN_BT_1_0";
454 }
455 } else {
456 if (this->shaping_ == CUTOFF_BR_X_2) {
457 shaping = "CUTOFF_BR_X_2";
458 } else if (this->shaping_ == CUTOFF_BR_X_1) {
459 shaping = "CUTOFF_BR_X_1";
460 }
461 }
462 ESP_LOGCONFIG(TAG,
463 " Shaping: %s\n"
464 " Modulation: %s\n"
465 " Bitrate: %" PRIu32 "b/s\n"
466 " Bitsync: %s\n"
467 " Rx Start: %s\n"
468 " Rx Floor: %.1f dBm\n"
469 " Packet Mode: %s",
470 shaping, this->modulation_ == MOD_FSK ? "FSK" : "OOK", this->bitrate_, TRUEFALSE(this->bitsync_),
471 TRUEFALSE(this->rx_start_), this->rx_floor_, TRUEFALSE(this->packet_mode_));
472 if (this->packet_mode_) {
473 ESP_LOGCONFIG(TAG, " CRC Enable: %s", TRUEFALSE(this->crc_enable_));
474 }
475 if (this->payload_length_ > 0) {
476 ESP_LOGCONFIG(TAG, " Payload Length: %" PRIu32, this->payload_length_);
477 }
478 if (!this->sync_value_.empty()) {
479 ESP_LOGCONFIG(TAG, " Sync Value: 0x%s", format_hex(this->sync_value_).c_str());
480 }
481 if (this->preamble_size_ > 0 || this->preamble_detect_ > 0) {
482 ESP_LOGCONFIG(TAG,
483 " Preamble Polarity: 0x%X\n"
484 " Preamble Size: %" PRIu16 "\n"
485 " Preamble Detect: %" PRIu8 "\n"
486 " Preamble Errors: %" PRIu8,
488 }
489 }
490 if (this->is_failed()) {
491 ESP_LOGE(TAG, "Configuring SX127x failed");
492 }
493}
494
495} // namespace sx127x
496} // 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
InternalGPIOPin * rst_pin_
Definition sx127x.h:102
uint8_t preamble_polarity_
Definition sx127x.h:116
InternalGPIOPin * dio0_pin_
Definition sx127x.h:101
Trigger< std::vector< uint8_t >, float, float > * packet_trigger_
Definition sx127x.h:97
std::vector< SX127xListener * > listeners_
Definition sx127x.h:98
void write_fifo_(const std::vector< uint8_t > &packet)
Definition sx127x.cpp:45
SX127xError transmit_packet(const std::vector< uint8_t > &packet)
Definition sx127x.cpp:253
std::vector< uint8_t > sync_value_
Definition sx127x.h:100
uint32_t payload_length_
Definition sx127x.h:107
void dump_config() override
Definition sx127x.cpp:402
uint8_t read_register_(uint8_t reg)
Definition sx127x.cpp:23
void loop() override
Definition sx127x.cpp:306
void set_mode_(uint8_t modulation, uint8_t mode)
Definition sx127x.cpp:363
void read_fifo_(std::vector< uint8_t > &packet)
Definition sx127x.cpp:38
uint16_t preamble_size_
Definition sx127x.h:108
std::vector< uint8_t > packet_
Definition sx127x.h:99
size_t get_max_packet_size()
Definition sx127x.cpp:242
void write_register_(uint8_t reg, uint8_t value)
Definition sx127x.cpp:31
void call_listeners_(const std::vector< uint8_t > &packet, float rssi, float snr)
Definition sx127x.cpp:299
void setup() override
Definition sx127x.cpp:52
uint8_t spreading_factor_
Definition sx127x.h:118
uint8_t duration
Definition msa3xx.h:0
const char *const TAG
Definition spi.cpp:8
@ PREAMBLE_DETECTOR_TOL_SHIFT
Definition sx127x_reg.h:209
@ PREAMBLE_DETECTOR_SIZE_SHIFT
Definition sx127x_reg.h:208
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
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:28