Line data Source code
1 : #include "SX126x.h"
2 : #include <string.h>
3 : #include <math.h>
4 : #if !RADIOLIB_EXCLUDE_SX126X
5 :
6 18 : SX126x::SX126x(Module* mod) : PhysicalLayer() {
7 18 : this->freqStep = RADIOLIB_SX126X_FREQUENCY_STEP_SIZE;
8 18 : this->maxPacketLength = RADIOLIB_SX126X_MAX_PACKET_LENGTH;
9 18 : this->mod = mod;
10 18 : this->standbyXOSC = false;
11 18 : this->irqMap[RADIOLIB_IRQ_TX_DONE] = RADIOLIB_SX126X_IRQ_TX_DONE;
12 18 : this->irqMap[RADIOLIB_IRQ_RX_DONE] = RADIOLIB_SX126X_IRQ_RX_DONE;
13 18 : this->irqMap[RADIOLIB_IRQ_PREAMBLE_DETECTED] = RADIOLIB_SX126X_IRQ_PREAMBLE_DETECTED;
14 18 : this->irqMap[RADIOLIB_IRQ_SYNC_WORD_VALID] = RADIOLIB_SX126X_IRQ_SYNC_WORD_VALID;
15 18 : this->irqMap[RADIOLIB_IRQ_HEADER_VALID] = RADIOLIB_SX126X_IRQ_HEADER_VALID;
16 18 : this->irqMap[RADIOLIB_IRQ_HEADER_ERR] = RADIOLIB_SX126X_IRQ_HEADER_ERR;
17 18 : this->irqMap[RADIOLIB_IRQ_CRC_ERR] = RADIOLIB_SX126X_IRQ_CRC_ERR;
18 18 : this->irqMap[RADIOLIB_IRQ_CAD_DONE] = RADIOLIB_SX126X_IRQ_CAD_DONE;
19 18 : this->irqMap[RADIOLIB_IRQ_CAD_DETECTED] = RADIOLIB_SX126X_IRQ_CAD_DETECTED;
20 18 : this->irqMap[RADIOLIB_IRQ_TIMEOUT] = RADIOLIB_SX126X_IRQ_TIMEOUT;
21 18 : }
22 :
23 0 : int16_t SX126x::begin(uint8_t cr, uint8_t syncWord, uint16_t preambleLength) {
24 : // BW in kHz and SF are required in order to calculate LDRO for setModulationParams
25 : // set the defaults, this will get overwritten later anyway
26 0 : this->bandwidthKhz = 500.0;
27 0 : this->spreadingFactor = 9;
28 :
29 : // initialize configuration variables (will be overwritten during public settings configuration)
30 0 : this->bandwidth = RADIOLIB_SX126X_LORA_BW_500_0; // initialized to 500 kHz, since lower values will interfere with LLCC68
31 0 : this->codingRate = RADIOLIB_SX126X_LORA_CR_4_7;
32 0 : this->ldrOptimize = 0x00;
33 0 : this->crcTypeLoRa = RADIOLIB_SX126X_LORA_CRC_ON;
34 0 : this->preambleLengthLoRa = preambleLength;
35 0 : this->tcxoDelay = 0;
36 0 : this->headerType = RADIOLIB_SX126X_LORA_HEADER_EXPLICIT;
37 0 : this->implicitLen = 0xFF;
38 :
39 : // set module properties and perform initial setup
40 0 : int16_t state = this->modSetup(RADIOLIB_SX126X_PACKET_TYPE_LORA);
41 0 : RADIOLIB_ASSERT(state);
42 :
43 : // configure publicly accessible settings
44 0 : state = setCodingRate(cr);
45 0 : RADIOLIB_ASSERT(state);
46 :
47 0 : state = setSyncWord(syncWord);
48 0 : RADIOLIB_ASSERT(state);
49 :
50 0 : state = setPreambleLength(preambleLength);
51 0 : RADIOLIB_ASSERT(state);
52 :
53 : // set publicly accessible settings that are not a part of begin method
54 0 : state = setCurrentLimit(60.0);
55 0 : RADIOLIB_ASSERT(state);
56 :
57 0 : state = setDio2AsRfSwitch(true);
58 0 : RADIOLIB_ASSERT(state);
59 :
60 0 : state = setCRC(2);
61 0 : RADIOLIB_ASSERT(state);
62 :
63 0 : state = invertIQ(false);
64 0 : RADIOLIB_ASSERT(state);
65 :
66 0 : return(state);
67 : }
68 :
69 0 : int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, uint16_t preambleLength) {
70 : // initialize configuration variables (will be overwritten during public settings configuration)
71 0 : this->bitRate = 21333; // 48.0 kbps
72 0 : this->frequencyDev = 52428; // 50.0 kHz
73 0 : this->rxBandwidth = RADIOLIB_SX126X_GFSK_RX_BW_156_2;
74 0 : this->rxBandwidthKhz = 156.2;
75 0 : this->pulseShape = RADIOLIB_SX126X_GFSK_FILTER_GAUSS_0_5;
76 0 : this->crcTypeFSK = RADIOLIB_SX126X_GFSK_CRC_2_BYTE_INV; // CCITT CRC configuration
77 0 : this->preambleLengthFSK = preambleLength;
78 :
79 : // set module properties and perform initial setup
80 0 : int16_t state = this->modSetup(RADIOLIB_SX126X_PACKET_TYPE_GFSK);
81 0 : RADIOLIB_ASSERT(state);
82 :
83 : // configure publicly accessible settings
84 0 : state = setBitRate(br);
85 0 : RADIOLIB_ASSERT(state);
86 :
87 0 : state = setFrequencyDeviation(freqDev);
88 0 : RADIOLIB_ASSERT(state);
89 :
90 0 : state = setRxBandwidth(rxBw);
91 0 : RADIOLIB_ASSERT(state);
92 :
93 0 : state = setCurrentLimit(60.0);
94 0 : RADIOLIB_ASSERT(state);
95 :
96 0 : state = setPreambleLength(preambleLength);
97 0 : RADIOLIB_ASSERT(state);
98 :
99 : // set publicly accessible settings that are not a part of begin method
100 0 : uint8_t sync[] = {0x12, 0xAD};
101 0 : state = setSyncWord(sync, 2);
102 0 : RADIOLIB_ASSERT(state);
103 :
104 0 : state = setDataShaping(RADIOLIB_SHAPING_NONE);
105 0 : RADIOLIB_ASSERT(state);
106 :
107 0 : state = setEncoding(RADIOLIB_ENCODING_NRZ);
108 0 : RADIOLIB_ASSERT(state);
109 :
110 0 : state = variablePacketLengthMode(RADIOLIB_SX126X_MAX_PACKET_LENGTH);
111 0 : RADIOLIB_ASSERT(state);
112 :
113 0 : state = setCRC(2);
114 0 : RADIOLIB_ASSERT(state);
115 :
116 0 : state = setDio2AsRfSwitch(true);
117 0 : RADIOLIB_ASSERT(state);
118 :
119 0 : return(state);
120 : }
121 :
122 0 : int16_t SX126x::beginBPSK(float br) {
123 : // set module properties and perform initial setup
124 0 : int16_t state = this->modSetup(RADIOLIB_SX126X_PACKET_TYPE_BPSK);
125 0 : RADIOLIB_ASSERT(state);
126 :
127 : // configure publicly accessible settings
128 0 : state = setBitRate(br);
129 0 : RADIOLIB_ASSERT(state);
130 :
131 : // set publicly accessible settings that are not a part of begin method
132 0 : state = setDio2AsRfSwitch(true);
133 0 : RADIOLIB_ASSERT(state);
134 :
135 0 : return(state);
136 : }
137 :
138 0 : int16_t SX126x::beginLRFHSS(uint8_t bw, uint8_t cr, bool narrowGrid) {
139 0 : this->lrFhssGridNonFcc = narrowGrid;
140 :
141 : // set module properties and perform initial setup
142 0 : int16_t state = this->modSetup(RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS);
143 0 : RADIOLIB_ASSERT(state);
144 :
145 : // set publicly accessible settings that are not a part of begin method
146 0 : state = setCurrentLimit(60.0);
147 0 : RADIOLIB_ASSERT(state);
148 :
149 0 : state = setDio2AsRfSwitch(true);
150 0 : RADIOLIB_ASSERT(state);
151 :
152 : // set all packet params to 0 (packet engine is disabled in LR-FHSS mode)
153 0 : state = setPacketParamsFSK(0, 0, 0, 0, 0, 0, 0, 0);
154 0 : RADIOLIB_ASSERT(state);
155 :
156 : // set bit rate
157 0 : this->rxBandwidth = 0;
158 0 : this->frequencyDev = 0;
159 0 : this->pulseShape = RADIOLIB_SX126X_GFSK_FILTER_GAUSS_1;
160 0 : state = setBitRate(0.48828125f);
161 0 : RADIOLIB_ASSERT(state);
162 :
163 0 : return(setLrFhssConfig(bw, cr));
164 : }
165 :
166 0 : int16_t SX126x::setLrFhssConfig(uint8_t bw, uint8_t cr, uint8_t hdrCount, uint16_t hopSeqId) {
167 : // check and cache all parameters
168 0 : RADIOLIB_CHECK_RANGE((int8_t)cr, (int8_t)RADIOLIB_SX126X_LR_FHSS_CR_5_6, (int8_t)RADIOLIB_SX126X_LR_FHSS_CR_1_3, RADIOLIB_ERR_INVALID_CODING_RATE);
169 0 : this->lrFhssCr = cr;
170 0 : RADIOLIB_CHECK_RANGE((int8_t)bw, (int8_t)RADIOLIB_SX126X_LR_FHSS_BW_39_06, (int8_t)RADIOLIB_SX126X_LR_FHSS_BW_1574_2, RADIOLIB_ERR_INVALID_BANDWIDTH);
171 0 : this->lrFhssBw = bw;
172 0 : RADIOLIB_CHECK_RANGE(hdrCount, 1, 4, RADIOLIB_ERR_INVALID_BIT_RANGE);
173 0 : this->lrFhssHdrCount = hdrCount;
174 0 : RADIOLIB_CHECK_RANGE((int16_t)hopSeqId, (int16_t)0x000, (int16_t)0x1FF, RADIOLIB_ERR_INVALID_DATA_SHAPING);
175 0 : this->lrFhssHopSeqId = hopSeqId;
176 0 : return(RADIOLIB_ERR_NONE);
177 : }
178 :
179 0 : int16_t SX126x::reset(bool verify) {
180 : // run the reset sequence
181 0 : this->mod->hal->pinMode(this->mod->getRst(), this->mod->hal->GpioModeOutput);
182 0 : this->mod->hal->digitalWrite(this->mod->getRst(), this->mod->hal->GpioLevelLow);
183 0 : this->mod->hal->delay(1);
184 0 : this->mod->hal->digitalWrite(this->mod->getRst(), this->mod->hal->GpioLevelHigh);
185 :
186 : // return immediately when verification is disabled
187 0 : if(!verify) {
188 0 : return(RADIOLIB_ERR_NONE);
189 : }
190 :
191 : // set mode to standby - SX126x often refuses first few commands after reset
192 0 : RadioLibTime_t start = this->mod->hal->millis();
193 : while(true) {
194 : // try to set mode to standby
195 0 : int16_t state = standby();
196 0 : if(state == RADIOLIB_ERR_NONE) {
197 : // standby command successful
198 0 : return(RADIOLIB_ERR_NONE);
199 : }
200 :
201 : // standby command failed, check timeout and try again
202 0 : if(this->mod->hal->millis() - start >= 1000) {
203 : // timed out, possibly incorrect wiring
204 0 : return(state);
205 : }
206 :
207 : // wait a bit to not spam the module
208 0 : this->mod->hal->delay(10);
209 0 : }
210 : }
211 :
212 3 : int16_t SX126x::transmit(const uint8_t* data, size_t len, uint8_t addr) {
213 : // set mode to standby
214 3 : int16_t state = standby();
215 3 : RADIOLIB_ASSERT(state);
216 :
217 : // check packet length
218 0 : if(this->codingRate > RADIOLIB_SX126X_LORA_CR_4_8) {
219 : // Long Interleaver needs at least 8 bytes
220 0 : if(len < 8) {
221 0 : return(RADIOLIB_ERR_PACKET_TOO_SHORT);
222 : }
223 :
224 : // Long Interleaver supports up to 253 bytes if CRC is enabled
225 0 : if(this->crcTypeLoRa == RADIOLIB_SX126X_LORA_CRC_ON && (len > RADIOLIB_SX126X_MAX_PACKET_LENGTH - 2)) {
226 0 : return(RADIOLIB_ERR_PACKET_TOO_LONG);
227 : }
228 : }
229 0 : if(len > RADIOLIB_SX126X_MAX_PACKET_LENGTH) {
230 0 : return(RADIOLIB_ERR_PACKET_TOO_LONG);
231 : }
232 :
233 : // calculate timeout in ms (5ms + 500 % of expected time-on-air)
234 0 : RadioLibTime_t timeout = 5 + (getTimeOnAir(len) * 5) / 1000;
235 : RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeout);
236 :
237 : // start transmission
238 0 : state = startTransmit(data, len, addr);
239 0 : RADIOLIB_ASSERT(state);
240 :
241 : // wait for packet transmission or timeout
242 0 : uint8_t modem = getPacketType();
243 0 : RadioLibTime_t start = this->mod->hal->millis();
244 : while(true) {
245 : // yield for multi-threaded platforms
246 0 : this->mod->hal->yield();
247 :
248 : // check timeout
249 0 : if(this->mod->hal->millis() - start > timeout) {
250 0 : finishTransmit();
251 0 : return(RADIOLIB_ERR_TX_TIMEOUT);
252 : }
253 :
254 : // poll the interrupt pin
255 0 : if(this->mod->hal->digitalRead(this->mod->getIrq())) {
256 : // in LoRa or GFSK, only Tx done interrupt is enabled
257 0 : if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
258 0 : break;
259 : }
260 :
261 : // in LR-FHSS, IRQ signals both Tx done as frequency hop request
262 0 : if(this->getIrqFlags() & RADIOLIB_SX126X_IRQ_TX_DONE) {
263 0 : break;
264 : } else {
265 : // handle frequency hop
266 0 : this->hopLRFHSS();
267 : }
268 : }
269 : }
270 :
271 0 : return(finishTransmit());
272 : }
273 :
274 3 : int16_t SX126x::receive(uint8_t* data, size_t len, RadioLibTime_t timeout) {
275 : // set mode to standby
276 3 : int16_t state = standby();
277 3 : RADIOLIB_ASSERT(state);
278 :
279 0 : RadioLibTime_t timeoutInternal = timeout;
280 0 : if(!timeoutInternal) {
281 : // calculate timeout (500 % of expected time-one-air)
282 0 : size_t maxLen = len;
283 0 : if(len == 0) { maxLen = RADIOLIB_SX126X_MAX_PACKET_LENGTH; }
284 0 : timeoutInternal = (getTimeOnAir(maxLen) * 5) / 1000;
285 : }
286 :
287 : RADIOLIB_DEBUG_BASIC_PRINTLN("Timeout in %lu ms", timeoutInternal);
288 :
289 : // start reception
290 0 : uint32_t timeoutValue = (uint32_t)(((float)timeoutInternal * 1000.0f) / 15.625f);
291 0 : state = startReceive(timeoutValue);
292 0 : RADIOLIB_ASSERT(state);
293 :
294 : // wait for packet reception or timeout
295 0 : bool softTimeout = false;
296 0 : RadioLibTime_t start = this->mod->hal->millis();
297 0 : while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
298 0 : this->mod->hal->yield();
299 : // safety check, the timeout should be done by the radio
300 0 : if(this->mod->hal->millis() - start > timeoutInternal) {
301 0 : softTimeout = true;
302 0 : break;
303 : }
304 : }
305 :
306 : // if it was a timeout, this will return an error code
307 0 : state = standby();
308 0 : if((state != RADIOLIB_ERR_NONE) && (state != RADIOLIB_ERR_SPI_CMD_TIMEOUT)) {
309 0 : return(state);
310 : }
311 :
312 : // check whether this was a timeout or not
313 0 : if(softTimeout || (getIrqFlags() & this->irqMap[RADIOLIB_IRQ_TIMEOUT])) {
314 0 : (void)finishReceive();
315 0 : return(RADIOLIB_ERR_RX_TIMEOUT);
316 : }
317 :
318 : // read the received data
319 0 : return(readData(data, len));
320 : }
321 :
322 3 : int16_t SX126x::transmitDirect(uint32_t frf) {
323 : // set RF switch (if present)
324 3 : this->mod->setRfSwitchState(this->txMode);
325 :
326 : // user requested to start transmitting immediately (required for RTTY)
327 3 : int16_t state = RADIOLIB_ERR_NONE;
328 3 : if(frf != 0) {
329 0 : state = setRfFrequency(frf);
330 : }
331 3 : RADIOLIB_ASSERT(state);
332 :
333 : // direct mode activation intentionally skipped here, as it seems to lead to much worse results
334 3 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX_CONTINUOUS_WAVE, NULL, 0));
335 : }
336 :
337 3 : int16_t SX126x::receiveDirect() {
338 : // set RF switch (if present)
339 3 : this->mod->setRfSwitchState(Module::MODE_RX);
340 :
341 : // SX126x is unable to output received data directly
342 3 : return(RADIOLIB_ERR_UNKNOWN);
343 : }
344 :
345 0 : int16_t SX126x::directMode() {
346 : // check modem
347 0 : if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
348 0 : return(RADIOLIB_ERR_WRONG_MODEM);
349 : }
350 :
351 : // set mode to standby
352 0 : int16_t state = standby();
353 0 : RADIOLIB_ASSERT(state);
354 :
355 : // disable DIO2 RF switch
356 0 : state = setDio2AsRfSwitch(false);
357 0 : RADIOLIB_ASSERT(state);
358 :
359 : // set DIO2 to clock output and DIO3 to data input
360 : // this is done exclusively by writing magic values to even more magic registers
361 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_1, RADIOLIB_SX126X_TX_BITBANG_1_ENABLED, 6, 4);
362 0 : RADIOLIB_ASSERT(state);
363 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_0, RADIOLIB_SX126X_TX_BITBANG_0_ENABLED, 3, 0);
364 0 : RADIOLIB_ASSERT(state);
365 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_OUT_ENABLE, RADIOLIB_SX126X_DIO3_OUT_DISABLED, 3, 3);
366 0 : RADIOLIB_ASSERT(state);
367 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_IN_ENABLE, RADIOLIB_SX126X_DIO3_IN_ENABLED, 3, 3);
368 0 : RADIOLIB_ASSERT(state);
369 :
370 : // enable TxDone interrupt
371 0 : state = setDioIrqParams(RADIOLIB_SX126X_IRQ_TX_DONE, RADIOLIB_SX126X_IRQ_TX_DONE);
372 0 : RADIOLIB_ASSERT(state);
373 :
374 : // set preamble length to the maximum to prevent SX126x from exiting Tx mode for a while
375 0 : state = setPreambleLength(0xFFFF);
376 0 : RADIOLIB_ASSERT(state);
377 :
378 0 : return(state);
379 : }
380 :
381 0 : int16_t SX126x::packetMode() {
382 : // set mode to standby
383 0 : int16_t state = standby();
384 0 : RADIOLIB_ASSERT(state);
385 :
386 : // set preamble length to the default
387 0 : state = setPreambleLength(16);
388 0 : RADIOLIB_ASSERT(state);
389 :
390 : // disable TxDone interrupt
391 0 : state = setDioIrqParams(RADIOLIB_SX126X_IRQ_NONE, RADIOLIB_SX126X_IRQ_NONE);
392 0 : RADIOLIB_ASSERT(state);
393 :
394 : // restore the magic registers
395 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_IN_ENABLE, RADIOLIB_SX126X_DIO3_IN_DISABLED, 3, 3);
396 0 : RADIOLIB_ASSERT(state);
397 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_DIOX_OUT_ENABLE, RADIOLIB_SX126X_DIO3_OUT_ENABLED, 3, 3);
398 0 : RADIOLIB_ASSERT(state);
399 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_0, RADIOLIB_SX126X_TX_BITBANG_0_DISABLED, 3, 0);
400 0 : RADIOLIB_ASSERT(state);
401 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_TX_BITBANG_ENABLE_1, RADIOLIB_SX126X_TX_BITBANG_1_DISABLED, 6, 4);
402 0 : RADIOLIB_ASSERT(state);
403 :
404 : // enable DIO2 RF switch
405 0 : state = setDio2AsRfSwitch(true);
406 0 : RADIOLIB_ASSERT(state);
407 :
408 0 : return(state);
409 : }
410 :
411 3 : int16_t SX126x::scanChannel() {
412 3 : ChannelScanConfig_t cfg = {
413 : .cad = {
414 : .symNum = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
415 : .detPeak = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
416 : .detMin = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
417 : .exitMode = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
418 : .timeout = 0,
419 : .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS,
420 : .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK,
421 : },
422 : };
423 6 : return(this->scanChannel(cfg));
424 : }
425 :
426 6 : int16_t SX126x::scanChannel(const ChannelScanConfig_t &cfg) {
427 : // set mode to CAD
428 6 : int state = startChannelScan(cfg);
429 6 : RADIOLIB_ASSERT(state);
430 :
431 : // wait for channel activity detected or timeout
432 0 : while(!this->mod->hal->digitalRead(this->mod->getIrq())) {
433 0 : this->mod->hal->yield();
434 : }
435 :
436 : // check CAD result
437 0 : return(getChannelScanResult());
438 : }
439 :
440 0 : int16_t SX126x::hopLRFHSS() {
441 0 : if(!(this->getIrqFlags() & RADIOLIB_SX126X_IRQ_LR_FHSS_HOP)) {
442 0 : return(RADIOLIB_ERR_TX_TIMEOUT);
443 : }
444 :
445 0 : int16_t state = this->setLRFHSSHop(this->lrFhssHopNum % 16);
446 0 : RADIOLIB_ASSERT(state);
447 0 : return(clearIrqStatus());
448 : }
449 :
450 3 : int16_t SX126x::finishTransmit() {
451 : // clear interrupt flags
452 3 : int16_t state = clearIrqStatus();
453 3 : RADIOLIB_ASSERT(state);
454 :
455 : // set mode to standby to disable transmitter/RF switch
456 0 : return(standby());
457 : }
458 :
459 3 : int16_t SX126x::finishReceive() {
460 : // set mode to standby to disable RF switch
461 3 : int16_t state = standby();
462 3 : RADIOLIB_ASSERT(state);
463 :
464 : // try to fix timeout error in implicit header mode
465 : // check for modem type and header mode is done in fixImplicitTimeout()
466 0 : state = fixImplicitTimeout();
467 0 : RADIOLIB_ASSERT(state);
468 :
469 : // clear interrupt flags
470 0 : return(clearIrqStatus());
471 : }
472 :
473 3 : int16_t SX126x::startReceive() {
474 3 : return(this->startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF, RADIOLIB_IRQ_RX_DEFAULT_FLAGS, RADIOLIB_IRQ_RX_DEFAULT_MASK, 0));
475 : }
476 :
477 0 : int16_t SX126x::startReceiveDutyCycle(uint32_t rxPeriod, uint32_t sleepPeriod, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask) {
478 : // datasheet claims time to go to sleep is ~500us, same to wake up, compensate for that with 1 ms + TCXO delay
479 0 : uint32_t transitionTime = this->tcxoDelay + 1000;
480 0 : sleepPeriod -= transitionTime;
481 :
482 : // divide by 15.625
483 0 : uint32_t rxPeriodRaw = (rxPeriod * 8) / 125;
484 0 : uint32_t sleepPeriodRaw = (sleepPeriod * 8) / 125;
485 :
486 : // check 24 bit limit and zero value (likely not intended)
487 0 : if((rxPeriodRaw & 0xFF000000) || (rxPeriodRaw == 0)) {
488 0 : return(RADIOLIB_ERR_INVALID_RX_PERIOD);
489 : }
490 :
491 : // this check of the high byte also catches underflow when we subtracted transitionTime
492 0 : if((sleepPeriodRaw & 0xFF000000) || (sleepPeriodRaw == 0)) {
493 0 : return(RADIOLIB_ERR_INVALID_SLEEP_PERIOD);
494 : }
495 :
496 : // set up Rx mode
497 0 : RadioModeConfig_t cfg = {
498 : .receive = {
499 : .timeout = RADIOLIB_SX126X_RX_TIMEOUT_INF,
500 : .irqFlags = irqFlags,
501 : .irqMask = irqMask,
502 : .len = 0,
503 : }
504 0 : };
505 0 : int16_t state = this->stageMode(RADIOLIB_RADIO_MODE_RX, &cfg);
506 0 : RADIOLIB_ASSERT(state);
507 :
508 0 : const uint8_t data[6] = {(uint8_t)((rxPeriodRaw >> 16) & 0xFF), (uint8_t)((rxPeriodRaw >> 8) & 0xFF), (uint8_t)(rxPeriodRaw & 0xFF),
509 0 : (uint8_t)((sleepPeriodRaw >> 16) & 0xFF), (uint8_t)((sleepPeriodRaw >> 8) & 0xFF), (uint8_t)(sleepPeriodRaw & 0xFF)};
510 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_RX_DUTY_CYCLE, data, 6));
511 : }
512 :
513 0 : int16_t SX126x::startReceiveDutyCycleAuto(uint16_t senderPreambleLength, uint16_t minSymbols, RadioLibIrqFlags_t irqFlags, RadioLibIrqFlags_t irqMask) {
514 : // calculate the sleep and wake periods
515 0 : uint32_t wakePeriod = 0;
516 0 : uint32_t sleepPeriod = 0;
517 : DataRate_t dr = {
518 : .lora = {
519 0 : .spreadingFactor = this->spreadingFactor,
520 0 : .bandwidth = this->bandwidthKhz,
521 0 : .codingRate = this->codingRate,
522 : }
523 0 : };
524 0 : int16_t state = calculateRxDutyCycle(senderPreambleLength, this->preambleLengthLoRa, minSymbols, &dr, &wakePeriod, &sleepPeriod);
525 0 : RADIOLIB_ASSERT(state);
526 :
527 : // If our sleep period is shorter than our transition time, just use the standard startReceive
528 0 : if(sleepPeriod < this->tcxoDelay + 1016) {
529 0 : return(startReceive(RADIOLIB_SX126X_RX_TIMEOUT_INF, irqFlags, irqMask));
530 : }
531 :
532 0 : return(startReceiveDutyCycle(wakePeriod, sleepPeriod, irqFlags, irqMask));
533 : }
534 :
535 3 : int16_t SX126x::readData(uint8_t* data, size_t len) {
536 : // this method may get called from receive() after Rx timeout
537 : // if that's the case, the first call will return "SPI command timeout error"
538 : // check the IRQ to be sure this really originated from timeout event
539 3 : int16_t state = this->mod->SPIcheckStream();
540 3 : uint16_t irq = getIrqFlags();
541 3 : if((state == RADIOLIB_ERR_SPI_CMD_TIMEOUT) && (irq & RADIOLIB_SX126X_IRQ_TIMEOUT)) {
542 : // this is definitely Rx timeout
543 0 : return(RADIOLIB_ERR_RX_TIMEOUT);
544 : }
545 3 : RADIOLIB_ASSERT(state);
546 :
547 : // check integrity CRC
548 0 : int16_t crcState = RADIOLIB_ERR_NONE;
549 : // Report CRC mismatch when there's a payload CRC error, or a header error and no valid header (to avoid false alarm from previous packet)
550 0 : if((irq & RADIOLIB_SX126X_IRQ_CRC_ERR) || ((irq & RADIOLIB_SX126X_IRQ_HEADER_ERR) && !(irq & RADIOLIB_SX126X_IRQ_HEADER_VALID))) {
551 0 : crcState = RADIOLIB_ERR_CRC_MISMATCH;
552 : }
553 :
554 : // get packet length and Rx buffer offset
555 0 : uint8_t offset = 0;
556 0 : size_t length = getPacketLength(true, &offset);
557 0 : if((len != 0) && (len < length)) {
558 : // user requested less data than we got, only return what was requested
559 0 : length = len;
560 : }
561 :
562 : // read packet data starting at offset
563 0 : state = readBuffer(data, length, offset);
564 0 : RADIOLIB_ASSERT(state);
565 :
566 : // clear interrupt flags
567 0 : state = clearIrqStatus();
568 :
569 : // check if CRC failed - this is done after reading data to give user the option to keep them
570 0 : RADIOLIB_ASSERT(crcState);
571 :
572 0 : return(state);
573 : }
574 :
575 3 : int16_t SX126x::startChannelScan() {
576 3 : ChannelScanConfig_t cfg = {
577 : .cad = {
578 : .symNum = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
579 : .detPeak = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
580 : .detMin = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
581 : .exitMode = RADIOLIB_SX126X_CAD_PARAM_DEFAULT,
582 : .timeout = 0,
583 : .irqFlags = RADIOLIB_IRQ_CAD_DEFAULT_FLAGS,
584 : .irqMask = RADIOLIB_IRQ_CAD_DEFAULT_MASK,
585 : },
586 : };
587 6 : return(this->startChannelScan(cfg));
588 : }
589 :
590 12 : int16_t SX126x::startChannelScan(const ChannelScanConfig_t &cfg) {
591 : // check active modem
592 12 : if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
593 12 : return(RADIOLIB_ERR_WRONG_MODEM);
594 : }
595 :
596 : // set mode to standby
597 0 : int16_t state = standby();
598 0 : RADIOLIB_ASSERT(state);
599 :
600 : // set RF switch (if present)
601 0 : this->mod->setRfSwitchState(Module::MODE_RX);
602 :
603 : // set DIO pin mapping
604 0 : state = setDioIrqParams(getIrqMapped(cfg.cad.irqFlags), getIrqMapped(cfg.cad.irqMask));
605 0 : RADIOLIB_ASSERT(state);
606 :
607 : // clear interrupt flags
608 0 : state = clearIrqStatus();
609 0 : RADIOLIB_ASSERT(state);
610 :
611 : // set mode to CAD
612 0 : state = setCad(cfg.cad.symNum, cfg.cad.detPeak, cfg.cad.detMin, cfg.cad.exitMode, cfg.cad.timeout);
613 0 : return(state);
614 : }
615 :
616 3 : int16_t SX126x::getChannelScanResult() {
617 : // check active modem
618 3 : if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
619 3 : return(RADIOLIB_ERR_WRONG_MODEM);
620 : }
621 :
622 : // check CAD result
623 0 : uint16_t cadResult = getIrqFlags();
624 0 : if(cadResult & RADIOLIB_SX126X_IRQ_CAD_DETECTED) {
625 : // detected some LoRa activity
626 0 : return(RADIOLIB_LORA_DETECTED);
627 0 : } else if(cadResult & RADIOLIB_SX126X_IRQ_CAD_DONE) {
628 : // channel is free
629 0 : return(RADIOLIB_CHANNEL_FREE);
630 : }
631 :
632 0 : return(RADIOLIB_ERR_UNKNOWN);
633 : }
634 :
635 3 : float SX126x::getRSSI() {
636 3 : return(this->getRSSI(true));
637 : }
638 :
639 3 : float SX126x::getRSSI(bool packet) {
640 3 : if(packet) {
641 : // get last packet RSSI from packet status
642 3 : uint32_t packetStatus = getPacketStatus();
643 3 : uint8_t rssiPkt = packetStatus & 0xFF;
644 3 : return(-1.0 * rssiPkt/2.0);
645 : } else {
646 : // get instantaneous RSSI value
647 0 : uint8_t rssiRaw = 0;
648 0 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RSSI_INST, &rssiRaw, 1);
649 0 : return((float)rssiRaw / (-2.0f));
650 : }
651 : }
652 :
653 3 : float SX126x::getSNR() {
654 : // check active modem
655 3 : if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
656 3 : return(RADIOLIB_ERR_WRONG_MODEM);
657 : }
658 :
659 : // get last packet SNR from packet status
660 0 : uint32_t packetStatus = getPacketStatus();
661 0 : uint8_t snrPkt = (packetStatus >> 8) & 0xFF;
662 0 : if(snrPkt < 128) {
663 0 : return(snrPkt/4.0);
664 : } else {
665 0 : return((snrPkt - 256)/4.0);
666 : }
667 : }
668 :
669 0 : float SX126x::getFrequencyError() {
670 : // check active modem
671 0 : uint8_t modem = getPacketType();
672 0 : if(modem != RADIOLIB_SX126X_PACKET_TYPE_LORA) {
673 0 : return(0.0);
674 : }
675 :
676 : // read the raw frequency error register values
677 0 : uint8_t efeRaw[3] = {0};
678 0 : int16_t state = readRegister(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC, &efeRaw[0], 1);
679 0 : RADIOLIB_ASSERT(state);
680 0 : state = readRegister(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC + 1, &efeRaw[1], 1);
681 0 : RADIOLIB_ASSERT(state);
682 0 : state = readRegister(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC + 2, &efeRaw[2], 1);
683 0 : RADIOLIB_ASSERT(state);
684 0 : uint32_t efe = ((uint32_t) efeRaw[0] << 16) | ((uint32_t) efeRaw[1] << 8) | efeRaw[2];
685 0 : efe &= 0x0FFFFF;
686 :
687 0 : float error = 0;
688 :
689 : // check the first bit
690 0 : if (efe & 0x80000) {
691 : // frequency error is negative
692 0 : efe |= (uint32_t) 0xFFF00000;
693 0 : efe = ~efe + 1;
694 0 : error = 1.55f * (float) efe / (1600.0f / (float) this->bandwidthKhz) * -1.0f;
695 : } else {
696 0 : error = 1.55f * (float) efe / (1600.0f / (float) this->bandwidthKhz);
697 : }
698 :
699 0 : return(error);
700 : }
701 :
702 3 : size_t SX126x::getPacketLength(bool update) {
703 3 : return(this->getPacketLength(update, NULL));
704 : }
705 :
706 3 : size_t SX126x::getPacketLength(bool update, uint8_t* offset) {
707 : (void)update;
708 :
709 : // in implicit mode, return the cached value if the offset was not requested
710 3 : if((getPacketType() == RADIOLIB_SX126X_PACKET_TYPE_LORA) && (this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) && (!offset)) {
711 0 : return(this->implicitLen);
712 : }
713 :
714 : // if offset was requested, or in explicit mode, we always have to perform the SPI transaction
715 3 : uint8_t rxBufStatus[2] = {0, 0};
716 3 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2);
717 :
718 3 : if(offset) { *offset = rxBufStatus[1]; }
719 :
720 3 : return((size_t)rxBufStatus[0]);
721 : }
722 :
723 0 : int16_t SX126x::getLoRaRxHeaderInfo(uint8_t* cr, bool* hasCRC) {
724 0 : int16_t state = RADIOLIB_ERR_NONE;
725 :
726 : // check if in explicit header mode
727 0 : if(this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) {
728 0 : return(RADIOLIB_ERR_WRONG_MODEM);
729 : }
730 :
731 0 : if(cr) { *cr = this->mod->SPIgetRegValue(RADIOLIB_SX126X_REG_LORA_RX_CODING_RATE, 6, 4) >> 4; }
732 0 : if(hasCRC) { *hasCRC = (this->mod->SPIgetRegValue(RADIOLIB_SX126X_REG_FREQ_ERROR_RX_CRC, 4, 4) != 0); }
733 :
734 0 : return(state);
735 : }
736 :
737 18 : RadioLibTime_t SX126x::calculateTimeOnAir(ModemType_t modem, DataRate_t dr, PacketConfig_t pc, size_t len) {
738 : // everything is in microseconds to allow integer arithmetic
739 : // some constants have .25, these are multiplied by 4, and have _x4 postfix to indicate that fact
740 18 : switch (modem) {
741 8 : case RADIOLIB_MODEM_LORA: {
742 8 : uint32_t symbolLength_us = ((uint32_t)(1000 * 10) << dr.lora.spreadingFactor) / (dr.lora.bandwidth * 10) ;
743 8 : uint8_t sfCoeff1_x4 = 17; // (4.25 * 4)
744 8 : uint8_t sfCoeff2 = 8;
745 8 : if(dr.lora.spreadingFactor == 5 || dr.lora.spreadingFactor == 6) {
746 0 : sfCoeff1_x4 = 25; // 6.25 * 4
747 0 : sfCoeff2 = 0;
748 : }
749 8 : uint8_t sfDivisor = 4*dr.lora.spreadingFactor;
750 8 : if(pc.lora.ldrOptimize) {
751 4 : sfDivisor = 4*(dr.lora.spreadingFactor - 2);
752 : }
753 8 : const int8_t bitsPerCrc = 16;
754 8 : const int8_t N_symbol_header = pc.lora.implicitHeader ? 0 : 20;
755 :
756 : // numerator of equation in section 6.1.4 of SX1268 datasheet v1.1 (might not actually be bitcount, but it has len * 8)
757 8 : int16_t bitCount = (int16_t) 8 * len + pc.lora.crcEnabled * bitsPerCrc - 4 * dr.lora.spreadingFactor + sfCoeff2 + N_symbol_header;
758 8 : if(bitCount < 0) {
759 0 : bitCount = 0;
760 : }
761 : // add (sfDivisor) - 1 to the numerator to give integer CEIL(...)
762 8 : uint16_t nPreCodedSymbols = (bitCount + (sfDivisor - 1)) / (sfDivisor);
763 :
764 : // preamble can be 65k, therefore nSymbol_x4 needs to be 32 bit
765 8 : uint32_t nSymbol_x4 = (pc.lora.preambleLength + 8) * 4 + sfCoeff1_x4 + nPreCodedSymbols * dr.lora.codingRate * 4;
766 :
767 8 : return((symbolLength_us * nSymbol_x4) / 4);
768 : }
769 4 : case RADIOLIB_MODEM_FSK: {
770 4 : return((((float)(pc.fsk.crcLength * 8) + pc.fsk.syncWordLength + pc.fsk.preambleLength + (uint32_t)len * 8) / (dr.fsk.bitRate / 1000.0f)));
771 : }
772 3 : case RADIOLIB_MODEM_LRFHSS: {
773 : // calculate the number of bits based on coding rate
774 : uint16_t N_bits;
775 3 : switch(dr.lrFhss.cr) {
776 0 : case RADIOLIB_SX126X_LR_FHSS_CR_5_6:
777 0 : N_bits = ((len * 6) + 4) / 5; // this is from the official LR11xx driver, but why the extra +4?
778 0 : break;
779 3 : case RADIOLIB_SX126X_LR_FHSS_CR_2_3:
780 3 : N_bits = (len * 3) / 2;
781 3 : break;
782 0 : case RADIOLIB_SX126X_LR_FHSS_CR_1_2:
783 0 : N_bits = len * 2;
784 0 : break;
785 0 : case RADIOLIB_SX126X_LR_FHSS_CR_1_3:
786 0 : N_bits = len * 3;
787 0 : break;
788 0 : default:
789 0 : return(RADIOLIB_ERR_INVALID_CODING_RATE);
790 : }
791 :
792 : // calculate number of bits when accounting for unaligned last block
793 3 : uint16_t N_payBits = (N_bits / RADIOLIB_SX126X_LR_FHSS_FRAG_BITS) * RADIOLIB_SX126X_LR_FHSS_BLOCK_BITS;
794 3 : uint16_t N_lastBlockBits = N_bits % RADIOLIB_SX126X_LR_FHSS_FRAG_BITS;
795 3 : if(N_lastBlockBits) {
796 3 : N_payBits += N_lastBlockBits + 2;
797 : }
798 :
799 : // add header bits
800 3 : uint16_t N_totalBits = (RADIOLIB_SX126X_LR_FHSS_HEADER_BITS * pc.lrFhss.hdrCount) + N_payBits;
801 3 : return(((uint32_t)N_totalBits * 8 * 1000000UL) / 488.28215f);
802 : }
803 3 : default:
804 3 : return(RADIOLIB_ERR_WRONG_MODEM);
805 : }
806 :
807 : return(RADIOLIB_ERR_UNKNOWN);
808 : }
809 :
810 3 : RadioLibTime_t SX126x::getTimeOnAir(size_t len) {
811 3 : uint8_t type = getPacketType();
812 3 : ModemType_t modem = RADIOLIB_MODEM_LORA;
813 3 : DataRate_t dataRate = {};
814 3 : PacketConfig_t packetConfig = {};
815 :
816 3 : if(type == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
817 0 : uint8_t cr = this->codingRate;
818 : // We assume same calculation for short and long interleaving, so map CR values 0-4 and 5-7 to the same values
819 0 : if (cr < 5) {
820 0 : cr = cr + 4;
821 0 : } else if (cr == 7) {
822 0 : cr = cr + 1;
823 : }
824 :
825 0 : dataRate.lora.codingRate = cr;
826 0 : dataRate.lora.spreadingFactor = this->spreadingFactor;
827 0 : dataRate.lora.bandwidth = this->bandwidthKhz;
828 :
829 0 : packetConfig.lora.preambleLength = this->preambleLengthLoRa;
830 0 : packetConfig.lora.crcEnabled = (bool)this->crcTypeLoRa;
831 0 : packetConfig.lora.implicitHeader = this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT;
832 0 : packetConfig.lora.ldrOptimize = (bool)this->ldrOptimize;
833 3 : } else if(type == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
834 0 : modem = RADIOLIB_MODEM_FSK;
835 :
836 0 : dataRate.fsk.bitRate = RADIOLIB_SX126X_CRYSTAL_FREQ * 32.0f * 1000.0f / (float)this->bitRate;
837 0 : dataRate.fsk.freqDev = (float)this->frequencyDev;
838 :
839 0 : uint8_t crcLen = 0;
840 0 : if(this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_1_BYTE || this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_1_BYTE_INV) {
841 0 : crcLen = 1;
842 0 : } else if(this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_2_BYTE || this->crcTypeFSK == RADIOLIB_SX126X_GFSK_CRC_2_BYTE_INV) {
843 0 : crcLen = 2;
844 : }
845 :
846 0 : packetConfig.fsk.preambleLength = this->preambleLengthFSK;
847 0 : packetConfig.fsk.syncWordLength = this->syncWordLength;
848 0 : packetConfig.fsk.crcLength = crcLen;
849 3 : } else if(type == RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
850 0 : modem = RADIOLIB_MODEM_LRFHSS;
851 :
852 0 : dataRate.lrFhss.bw = this->lrFhssBw;
853 0 : dataRate.lrFhss.cr = this->lrFhssCr;
854 0 : dataRate.lrFhss.narrowGrid = this->lrFhssGridNonFcc;
855 :
856 0 : packetConfig.lrFhss.hdrCount = this->lrFhssHdrCount;
857 3 : } else if(type == RADIOLIB_SX126X_PACKET_TYPE_BPSK) {
858 : // BPSK is so experimental it does not have a specific data rate structure
859 : // so just reuse FSK
860 0 : modem = RADIOLIB_MODEM_FSK;
861 :
862 0 : dataRate.fsk.bitRate = RADIOLIB_SX126X_CRYSTAL_FREQ * 32.0f * 1000.0f / (float)this->bitRate;
863 0 : dataRate.fsk.freqDev = 0;
864 :
865 0 : packetConfig.fsk.preambleLength = 0;
866 0 : packetConfig.fsk.syncWordLength = 0;
867 0 : packetConfig.fsk.crcLength = 0;
868 :
869 : } else {
870 3 : return(RADIOLIB_ERR_WRONG_MODEM);
871 :
872 : }
873 :
874 0 : return(calculateTimeOnAir(modem, dataRate, packetConfig, len));
875 : }
876 :
877 3 : RadioLibTime_t SX126x::calculateRxTimeout(RadioLibTime_t timeoutUs) {
878 : // the timeout value is given in units of 15.625 microseconds
879 : // the calling function should provide some extra width, as this number of units is truncated to integer
880 3 : RadioLibTime_t timeout = timeoutUs / 15.625f;
881 3 : return(timeout);
882 : }
883 :
884 6 : uint32_t SX126x::getIrqFlags() {
885 6 : uint8_t data[] = { 0x00, 0x00 };
886 6 : this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_IRQ_STATUS, data, 2);
887 6 : return(((uint32_t)(data[0]) << 8) | data[1]);
888 : }
889 :
890 3 : int16_t SX126x::setIrqFlags(uint32_t irq) {
891 3 : return(this->setDioIrqParams(irq, irq));
892 : }
893 :
894 3 : int16_t SX126x::clearIrqFlags(uint32_t irq) {
895 3 : return(this->clearIrqStatus(irq));
896 : }
897 :
898 3 : uint8_t SX126x::randomByte() {
899 : // set some magic registers
900 3 : this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_LNA, RADIOLIB_SX126X_LNA_RNG_ENABLED, 0, 0);
901 3 : this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_MIXER, RADIOLIB_SX126X_MIXER_RNG_ENABLED, 0, 0);
902 :
903 : // set mode to Rx
904 3 : setRx(RADIOLIB_SX126X_RX_TIMEOUT_INF);
905 :
906 : // wait a bit for the RSSI reading to stabilise
907 3 : this->mod->hal->delay(10);
908 :
909 : // read RSSI value 8 times, always keep just the least significant bit
910 3 : uint8_t randByte = 0x00;
911 27 : for(uint8_t i = 0; i < 8; i++) {
912 24 : uint8_t val = 0x00;
913 24 : readRegister(RADIOLIB_SX126X_REG_RANDOM_NUMBER_0, &val, sizeof(uint8_t));
914 24 : randByte |= ((val & 0x01) << i);
915 : }
916 :
917 : // set mode to standby
918 3 : standby();
919 :
920 : // restore the magic registers
921 3 : this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_LNA, RADIOLIB_SX126X_LNA_RNG_DISABLED, 0, 0);
922 3 : this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_ANA_MIXER, RADIOLIB_SX126X_MIXER_RNG_DISABLED, 0, 0);
923 :
924 3 : return(randByte);
925 : }
926 :
927 9 : int16_t SX126x::getModem(ModemType_t* modem) {
928 9 : RADIOLIB_ASSERT_PTR(modem);
929 :
930 9 : uint8_t type = getPacketType();
931 9 : switch(type) {
932 0 : case(RADIOLIB_SX126X_PACKET_TYPE_LORA):
933 0 : *modem = ModemType_t::RADIOLIB_MODEM_LORA;
934 0 : return(RADIOLIB_ERR_NONE);
935 0 : case(RADIOLIB_SX126X_PACKET_TYPE_GFSK):
936 0 : *modem = ModemType_t::RADIOLIB_MODEM_FSK;
937 0 : return(RADIOLIB_ERR_NONE);
938 0 : case(RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS):
939 0 : *modem = ModemType_t::RADIOLIB_MODEM_LRFHSS;
940 0 : return(RADIOLIB_ERR_NONE);
941 : }
942 :
943 9 : return(RADIOLIB_ERR_WRONG_MODEM);
944 : }
945 :
946 12 : int16_t SX126x::stageMode(RadioModeType_t mode, RadioModeConfig_t* cfg) {
947 : int16_t state;
948 :
949 12 : switch(mode) {
950 9 : case(RADIOLIB_RADIO_MODE_RX): {
951 : // in implicit header mode, use the provided length if it is nonzero
952 : // otherwise we trust the user has previously set the payload length manually
953 9 : if((this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) && (cfg->receive.len != 0)) {
954 0 : this->implicitLen = cfg->receive.len;
955 : }
956 :
957 : // ensure we are in standby
958 9 : state = standby();
959 9 : RADIOLIB_ASSERT(state);
960 :
961 : // set DIO mapping
962 0 : if(cfg->receive.timeout != RADIOLIB_SX126X_RX_TIMEOUT_INF) {
963 0 : cfg->receive.irqMask |= (1UL << RADIOLIB_IRQ_TIMEOUT);
964 : }
965 0 : state = setDioIrqParams(getIrqMapped(cfg->receive.irqFlags), getIrqMapped(cfg->receive.irqMask));
966 0 : RADIOLIB_ASSERT(state);
967 :
968 : // set buffer pointers
969 0 : state = setBufferBaseAddress();
970 0 : RADIOLIB_ASSERT(state);
971 :
972 : // clear interrupt flags
973 0 : state = clearIrqStatus();
974 0 : RADIOLIB_ASSERT(state);
975 :
976 : // restore original packet length
977 0 : uint8_t modem = getPacketType();
978 0 : if(modem == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
979 0 : state = setPacketParams(this->preambleLengthLoRa, this->crcTypeLoRa, this->implicitLen, this->headerType, this->invertIQEnabled);
980 0 : } else if(modem == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
981 0 : state = setPacketParamsFSK(this->preambleLengthFSK, this->preambleDetLength, this->crcTypeFSK, this->syncWordLength, RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF, this->whitening,
982 0 : this->packetType, (this->packetType == RADIOLIB_SX126X_GFSK_PACKET_FIXED) ? this->implicitLen : RADIOLIB_SX126X_MAX_PACKET_LENGTH);
983 : } else {
984 0 : return(RADIOLIB_ERR_UNKNOWN);
985 : }
986 :
987 : // if max(uint32_t) is used, revert to RxContinuous
988 0 : if(cfg->receive.timeout == 0xFFFFFFFF) {
989 0 : cfg->receive.timeout = 0xFFFFFF;
990 : }
991 0 : this->rxTimeout = cfg->receive.timeout;
992 0 : } break;
993 :
994 3 : case(RADIOLIB_RADIO_MODE_TX): {
995 : // check packet length
996 3 : if(cfg->transmit.len > RADIOLIB_SX126X_MAX_PACKET_LENGTH) {
997 0 : return(RADIOLIB_ERR_PACKET_TOO_LONG);
998 : }
999 :
1000 : // maximum packet length is decreased by 1 when address filtering is active
1001 : if((RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF != RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF) &&
1002 : (cfg->transmit.len > RADIOLIB_SX126X_MAX_PACKET_LENGTH - 1)) {
1003 : return(RADIOLIB_ERR_PACKET_TOO_LONG);
1004 : }
1005 :
1006 : // set packet Length
1007 3 : state = RADIOLIB_ERR_NONE;
1008 3 : uint8_t modem = getPacketType();
1009 3 : if(modem == RADIOLIB_SX126X_PACKET_TYPE_LORA) {
1010 0 : state = setPacketParams(this->preambleLengthLoRa, this->crcTypeLoRa, cfg->transmit.len, this->headerType, this->invertIQEnabled);
1011 :
1012 3 : } else if(modem == RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
1013 0 : state = setPacketParamsFSK(this->preambleLengthFSK, this->preambleDetLength, this->crcTypeFSK, this->syncWordLength, RADIOLIB_SX126X_GFSK_ADDRESS_FILT_OFF, this->whitening, this->packetType, cfg->transmit.len);
1014 :
1015 3 : } else if(modem == RADIOLIB_SX126X_PACKET_TYPE_BPSK) {
1016 0 : uint16_t rampUp = RADIOLIB_SX126X_BPSK_RAMP_UP_TIME_600_BPS;
1017 0 : uint16_t rampDown = RADIOLIB_SX126X_BPSK_RAMP_DOWN_TIME_600_BPS;
1018 0 : if(this->bitRate == 100) {
1019 0 : rampUp = RADIOLIB_SX126X_BPSK_RAMP_UP_TIME_100_BPS;
1020 0 : rampDown = RADIOLIB_SX126X_BPSK_RAMP_DOWN_TIME_100_BPS;
1021 : }
1022 0 : state = setPacketParamsBPSK(cfg->transmit.len, rampUp, rampDown, 8*cfg->transmit.len);
1023 :
1024 3 : } else if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
1025 3 : return(RADIOLIB_ERR_UNKNOWN);
1026 :
1027 : }
1028 0 : RADIOLIB_ASSERT(state);
1029 :
1030 : // set DIO mapping
1031 0 : if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
1032 0 : state = setDioIrqParams(RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_TIMEOUT, RADIOLIB_SX126X_IRQ_TX_DONE);
1033 : } else {
1034 0 : state = setDioIrqParams(RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_LR_FHSS_HOP, RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_LR_FHSS_HOP);
1035 : }
1036 0 : RADIOLIB_ASSERT(state);
1037 :
1038 : // set buffer pointers
1039 0 : state = setBufferBaseAddress();
1040 0 : RADIOLIB_ASSERT(state);
1041 :
1042 : // write packet to buffer
1043 0 : if(modem != RADIOLIB_SX126X_PACKET_TYPE_LR_FHSS) {
1044 0 : state = writeBuffer(cfg->transmit.data, cfg->transmit.len);
1045 :
1046 : } else {
1047 : // first, reset the LR-FHSS state machine
1048 0 : state = resetLRFHSS();
1049 0 : RADIOLIB_ASSERT(state);
1050 :
1051 : // skip hopping for the first 4 - lrFhssHdrCount blocks
1052 0 : for(int i = 0; i < 4 - this->lrFhssHdrCount; ++i ) {
1053 0 : stepLRFHSS();
1054 : }
1055 :
1056 : // in LR-FHSS mode, we need to build the entire packet manually
1057 0 : uint8_t frame[RADIOLIB_SX126X_MAX_PACKET_LENGTH] = { 0 };
1058 0 : size_t frameLen = 0;
1059 0 : this->lrFhssFrameBitsRem = 0;
1060 0 : this->lrFhssFrameHopsRem = 0;
1061 0 : this->lrFhssHopNum = 0;
1062 0 : state = buildLRFHSSPacket(cfg->transmit.data, cfg->transmit.len, frame, &frameLen, &this->lrFhssFrameBitsRem, &this->lrFhssFrameHopsRem);
1063 0 : RADIOLIB_ASSERT(state);
1064 :
1065 : // FIXME check max len for FHSS
1066 0 : state = writeBuffer(frame, frameLen);
1067 0 : RADIOLIB_ASSERT(state);
1068 :
1069 : // activate hopping
1070 0 : const uint8_t hopCfg[] = { RADIOLIB_SX126X_HOPPING_ENABLED, (uint8_t)frameLen, (uint8_t)this->lrFhssFrameHopsRem };
1071 0 : state = writeRegister(RADIOLIB_SX126X_REG_HOPPING_ENABLE, hopCfg, 3);
1072 0 : RADIOLIB_ASSERT(state);
1073 :
1074 : // write the initial hopping table
1075 0 : uint8_t initHops = this->lrFhssFrameHopsRem;
1076 0 : if(initHops > 16) {
1077 0 : initHops = 16;
1078 : };
1079 0 : for(size_t i = 0; i < initHops; i++) {
1080 : // set the hop frequency and symbols
1081 0 : state = this->setLRFHSSHop(i);
1082 0 : RADIOLIB_ASSERT(state);
1083 : }
1084 :
1085 : }
1086 0 : RADIOLIB_ASSERT(state);
1087 :
1088 : // clear interrupt flags
1089 0 : state = clearIrqStatus();
1090 0 : RADIOLIB_ASSERT(state);
1091 :
1092 : // fix sensitivity
1093 0 : state = fixSensitivity();
1094 0 : RADIOLIB_ASSERT(state);
1095 0 : } break;
1096 :
1097 0 : default:
1098 0 : return(RADIOLIB_ERR_UNSUPPORTED);
1099 : }
1100 :
1101 0 : this->stagedMode = mode;
1102 0 : return(state);
1103 : }
1104 :
1105 3 : int16_t SX126x::launchMode() {
1106 : int16_t state;
1107 3 : switch(this->stagedMode) {
1108 3 : case(RADIOLIB_RADIO_MODE_RX): {
1109 3 : this->mod->setRfSwitchState(Module::MODE_RX);
1110 3 : state = setRx(this->rxTimeout);
1111 3 : } break;
1112 :
1113 0 : case(RADIOLIB_RADIO_MODE_TX): {
1114 0 : this->mod->setRfSwitchState(this->txMode);
1115 0 : state = setTx(RADIOLIB_SX126X_TX_TIMEOUT_NONE);
1116 0 : RADIOLIB_ASSERT(state);
1117 :
1118 : // wait for BUSY to go low (= PA ramp up done)
1119 0 : while(this->mod->hal->digitalRead(this->mod->getGpio())) {
1120 0 : this->mod->hal->yield();
1121 : }
1122 0 : } break;
1123 :
1124 0 : default:
1125 0 : return(RADIOLIB_ERR_UNSUPPORTED);
1126 : }
1127 :
1128 3 : this->stagedMode = RADIOLIB_RADIO_MODE_NONE;
1129 3 : return(state);
1130 : }
1131 :
1132 : #if !RADIOLIB_EXCLUDE_DIRECT_RECEIVE
1133 0 : void SX126x::setDirectAction(void (*func)(void)) {
1134 0 : setDio1Action(func);
1135 0 : }
1136 :
1137 0 : void SX126x::readBit(uint32_t pin) {
1138 0 : updateDirectBuffer((uint8_t)this->mod->hal->digitalRead(pin));
1139 0 : }
1140 : #endif
1141 :
1142 0 : int16_t SX126x::uploadPatch(const uint32_t* patch, size_t len, bool nonvolatile) {
1143 : // set to standby RC mode
1144 0 : int16_t state = standby(RADIOLIB_SX126X_STANDBY_RC);
1145 0 : RADIOLIB_ASSERT(state);
1146 :
1147 : // check the version
1148 : #if RADIOLIB_DEBUG_BASIC
1149 : char ver_pre[16];
1150 : this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_VERSION_STRING, 16, reinterpret_cast<uint8_t*>(ver_pre));
1151 : RADIOLIB_DEBUG_BASIC_PRINTLN("Pre-update version string: %s", ver_pre);
1152 : #endif
1153 :
1154 : // enable patch update
1155 0 : this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_PATCH_UPDATE_ENABLE, RADIOLIB_SX126X_PATCH_UPDATE_ENABLED);
1156 :
1157 : // upload the patch
1158 : uint8_t data[4];
1159 0 : for(uint32_t i = 0; i < len / sizeof(uint32_t); i++) {
1160 0 : uint32_t bin = 0;
1161 0 : if(nonvolatile) {
1162 0 : uint32_t* ptr = const_cast<uint32_t*>(patch) + i;
1163 0 : bin = RADIOLIB_NONVOLATILE_READ_DWORD(ptr);
1164 : } else {
1165 0 : bin = patch[i];
1166 : }
1167 0 : data[0] = (bin >> 24) & 0xFF;
1168 0 : data[1] = (bin >> 16) & 0xFF;
1169 0 : data[2] = (bin >> 8) & 0xFF;
1170 0 : data[3] = bin & 0xFF;
1171 0 : this->mod->SPIwriteRegisterBurst(RADIOLIB_SX126X_REG_PATCH_MEMORY_BASE + i*sizeof(uint32_t), data, sizeof(uint32_t));
1172 : }
1173 :
1174 : // disable patch update
1175 0 : this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_PATCH_UPDATE_ENABLE, RADIOLIB_SX126X_PATCH_UPDATE_DISABLED);
1176 :
1177 : // update
1178 0 : this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_PRAM_UPDATE, NULL, 0);
1179 :
1180 : // check the version again
1181 : #if RADIOLIB_DEBUG_BASIC
1182 : char ver_post[16];
1183 : this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_VERSION_STRING, 16, reinterpret_cast<uint8_t*>(ver_post));
1184 : RADIOLIB_DEBUG_BASIC_PRINTLN("Post-update version string: %s", ver_post);
1185 : #endif
1186 :
1187 0 : return(state);
1188 : }
1189 :
1190 0 : int16_t SX126x::spectralScanStart(uint16_t numSamples, uint8_t window, uint8_t interval) {
1191 : // abort first - not sure if this is strictly needed, but the example code does this
1192 0 : spectralScanAbort();
1193 :
1194 : // set the RSSI window size
1195 0 : this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, window);
1196 :
1197 : // start Rx with infinite timeout
1198 0 : int16_t state = setRx(RADIOLIB_SX126X_RX_TIMEOUT_INF);
1199 0 : RADIOLIB_ASSERT(state);
1200 :
1201 : // now set the actual spectral scan parameters
1202 0 : const uint8_t data[3] = { (uint8_t)((numSamples >> 8) & 0xFF), (uint8_t)(numSamples & 0xFF), interval };
1203 0 : return(this->mod->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_SPECTR_SCAN_PARAMS, data, 3));
1204 : }
1205 :
1206 0 : void SX126x::spectralScanAbort() {
1207 0 : this->mod->SPIwriteRegister(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, 0x00);
1208 0 : }
1209 :
1210 0 : int16_t SX126x::spectralScanGetStatus() {
1211 0 : uint8_t status = this->mod->SPIreadRegister(RADIOLIB_SX126X_REG_SPECTRAL_SCAN_STATUS);
1212 0 : if(status == RADIOLIB_SX126X_SPECTRAL_SCAN_COMPLETED) {
1213 0 : return(RADIOLIB_ERR_NONE);
1214 : }
1215 0 : return(RADIOLIB_ERR_RANGING_TIMEOUT);
1216 : }
1217 :
1218 0 : int16_t SX126x::spectralScanGetResult(uint16_t* results) {
1219 : // read the raw results
1220 : uint8_t data[2*RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE];
1221 0 : this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_SPECTRAL_SCAN_RESULT, 2*RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE, data);
1222 :
1223 : // convert it
1224 0 : for(uint8_t i = 0; i < RADIOLIB_SX126X_SPECTRAL_SCAN_RES_SIZE; i++) {
1225 0 : results[i] = ((uint16_t)data[i*2] << 8) | ((uint16_t)data[i*2 + 1]);
1226 : }
1227 0 : return(RADIOLIB_ERR_NONE);
1228 : }
1229 :
1230 0 : int16_t SX126x::calibrateImage(float freq) {
1231 0 : uint8_t data[2] = { 0, 0 };
1232 :
1233 : // try to match the frequency ranges
1234 0 : int freqBand = (int)freq;
1235 0 : if((freqBand >= 902) && (freqBand <= 928)) {
1236 0 : data[0] = RADIOLIB_SX126X_CAL_IMG_902_MHZ_1;
1237 0 : data[1] = RADIOLIB_SX126X_CAL_IMG_902_MHZ_2;
1238 0 : } else if((freqBand >= 863) && (freqBand <= 870)) {
1239 0 : data[0] = RADIOLIB_SX126X_CAL_IMG_863_MHZ_1;
1240 0 : data[1] = RADIOLIB_SX126X_CAL_IMG_863_MHZ_2;
1241 0 : } else if((freqBand >= 779) && (freqBand <= 787)) {
1242 0 : data[0] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_1;
1243 0 : data[1] = RADIOLIB_SX126X_CAL_IMG_779_MHZ_2;
1244 0 : } else if((freqBand >= 470) && (freqBand <= 510)) {
1245 0 : data[0] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_1;
1246 0 : data[1] = RADIOLIB_SX126X_CAL_IMG_470_MHZ_2;
1247 0 : } else if((freqBand >= 430) && (freqBand <= 440)) {
1248 0 : data[0] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_1;
1249 0 : data[1] = RADIOLIB_SX126X_CAL_IMG_430_MHZ_2;
1250 : }
1251 :
1252 : int16_t state;
1253 0 : if(data[0]) {
1254 : // matched with predefined ranges, do the calibration
1255 0 : state = SX126x::calibrateImage(data);
1256 :
1257 : } else {
1258 : // if nothing matched, try custom calibration - the may or may not work
1259 : RADIOLIB_DEBUG_BASIC_PRINTLN("Failed to match predefined frequency range, trying custom");
1260 0 : state = SX126x::calibrateImageRejection(freq - 4.0f, freq + 4.0f);
1261 :
1262 : }
1263 :
1264 0 : return(state);
1265 : }
1266 :
1267 0 : int16_t SX126x::calibrateImageRejection(float freqMin, float freqMax) {
1268 : // calculate the calibration coefficients and calibrate image
1269 0 : uint8_t data[] = { (uint8_t)floor((freqMin - 1.0f) / 4.0f), (uint8_t)ceil((freqMax + 1.0f) / 4.0f) };
1270 0 : data[0] = (data[0] % 2) ? data[0] : data[0] - 1;
1271 0 : data[1] = (data[1] % 2) ? data[1] : data[1] + 1;
1272 0 : return(this->calibrateImage(data));
1273 : }
1274 :
1275 0 : int16_t SX126x::fixSensitivity() {
1276 : // fix receiver sensitivity for 500 kHz LoRa
1277 : // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.1 for details
1278 :
1279 : // read current sensitivity configuration
1280 0 : uint8_t sensitivityConfig = 0;
1281 0 : int16_t state = readRegister(RADIOLIB_SX126X_REG_SENSITIVITY_CONFIG, &sensitivityConfig, 1);
1282 0 : RADIOLIB_ASSERT(state);
1283 :
1284 : // fix the value for LoRa with 500 kHz bandwidth
1285 0 : if((getPacketType() == RADIOLIB_SX126X_PACKET_TYPE_LORA) && (fabsf(this->bandwidthKhz - 500.0f) <= 0.001f)) {
1286 0 : sensitivityConfig &= 0xFB;
1287 : } else {
1288 0 : sensitivityConfig |= 0x04;
1289 : }
1290 0 : return(writeRegister(RADIOLIB_SX126X_REG_SENSITIVITY_CONFIG, &sensitivityConfig, 1));
1291 : }
1292 :
1293 0 : int16_t SX126x::fixPaClamping(bool enable) {
1294 : // fixes overly eager PA clamping
1295 : // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.2 for details
1296 :
1297 : // read current clamping configuration
1298 0 : uint8_t clampConfig = 0;
1299 0 : int16_t state = readRegister(RADIOLIB_SX126X_REG_TX_CLAMP_CONFIG, &clampConfig, 1);
1300 0 : RADIOLIB_ASSERT(state);
1301 :
1302 : // apply or undo workaround
1303 0 : if (enable)
1304 0 : clampConfig |= 0x1E;
1305 : else
1306 0 : clampConfig = (clampConfig & ~0x1E) | 0x08;
1307 :
1308 0 : return(writeRegister(RADIOLIB_SX126X_REG_TX_CLAMP_CONFIG, &clampConfig, 1));
1309 : }
1310 :
1311 0 : int16_t SX126x::fixImplicitTimeout() {
1312 : // fixes timeout in implicit header mode
1313 : // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.3 for details
1314 :
1315 : //check if we're in implicit LoRa mode
1316 0 : if(!((this->headerType == RADIOLIB_SX126X_LORA_HEADER_IMPLICIT) && (getPacketType() == RADIOLIB_SX126X_PACKET_TYPE_LORA))) {
1317 : // not in the correct mode, nothing to do here
1318 0 : return(RADIOLIB_ERR_NONE);
1319 : }
1320 :
1321 : // stop RTC counter
1322 0 : uint8_t rtcStop = 0x00;
1323 0 : int16_t state = writeRegister(RADIOLIB_SX126X_REG_RTC_CTRL, &rtcStop, 1);
1324 0 : RADIOLIB_ASSERT(state);
1325 :
1326 : // read currently active event
1327 0 : uint8_t rtcEvent = 0;
1328 0 : state = readRegister(RADIOLIB_SX126X_REG_EVENT_MASK, &rtcEvent, 1);
1329 0 : RADIOLIB_ASSERT(state);
1330 :
1331 : // clear events
1332 0 : rtcEvent |= 0x02;
1333 0 : return(writeRegister(RADIOLIB_SX126X_REG_EVENT_MASK, &rtcEvent, 1));
1334 : }
1335 :
1336 0 : int16_t SX126x::fixInvertedIQ(uint8_t iqConfig) {
1337 : // fixes IQ configuration for inverted IQ
1338 : // see SX1262/SX1268 datasheet, chapter 15 Known Limitations, section 15.4 for details
1339 :
1340 : // read current IQ configuration
1341 0 : uint8_t iqConfigCurrent = 0;
1342 0 : int16_t state = readRegister(RADIOLIB_SX126X_REG_IQ_CONFIG, &iqConfigCurrent, 1);
1343 0 : RADIOLIB_ASSERT(state);
1344 :
1345 : // set correct IQ configuration
1346 0 : if(iqConfig == RADIOLIB_SX126X_LORA_IQ_INVERTED) {
1347 0 : iqConfigCurrent &= 0xFB;
1348 : } else {
1349 0 : iqConfigCurrent |= 0x04;
1350 : }
1351 :
1352 : // update with the new value
1353 0 : return(writeRegister(RADIOLIB_SX126X_REG_IQ_CONFIG, &iqConfigCurrent, 1));
1354 : }
1355 :
1356 0 : int16_t SX126x::fixGFSK() {
1357 : // method that applies some magic workaround for specific bitrate, frequency deviation,
1358 : // receiver bandwidth and carrier frequencies for GFSK (and resets it in all other cases)
1359 : // this is not documented in the datasheet, only in Semtech repositories for SX126x and LR11xx
1360 :
1361 : // first, check we are using GFSK modem
1362 0 : if(getPacketType() != RADIOLIB_SX126X_PACKET_TYPE_GFSK) {
1363 : // not in GFSK, nothing to do here
1364 0 : return(RADIOLIB_ERR_NONE);
1365 : }
1366 :
1367 : // next, decide what to change based on modulation properties
1368 0 : int16_t state = RADIOLIB_ERR_UNKNOWN;
1369 0 : if(this->bitRate == 1200) {
1370 : // workaround for 1.2 kbps
1371 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_3, 0x00, 4, 4);
1372 :
1373 0 : } else if(this->bitRate == 600) {
1374 : // workaround for 0.6 kbps
1375 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_1, 0x18, 4, 3);
1376 0 : RADIOLIB_ASSERT(state);
1377 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, 0x04, 4, 2);
1378 0 : RADIOLIB_ASSERT(state);
1379 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_3, 0x00, 4, 4);
1380 0 : RADIOLIB_ASSERT(state);
1381 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_4, 0x50, 6, 4);
1382 :
1383 : } else {
1384 : // reset
1385 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_1, 0x08, 4, 3);
1386 0 : RADIOLIB_ASSERT(state);
1387 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_RSSI_AVG_WINDOW, 0x00, 4, 2);
1388 0 : RADIOLIB_ASSERT(state);
1389 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_3, 0x10, 4, 4);
1390 0 : RADIOLIB_ASSERT(state);
1391 0 : state = this->mod->SPIsetRegValue(RADIOLIB_SX126X_REG_GFSK_FIX_4, 0x00, 6, 4);
1392 :
1393 : }
1394 :
1395 0 : return(state);
1396 : }
1397 :
1398 0 : Module* SX126x::getMod() {
1399 0 : return(this->mod);
1400 : }
1401 :
1402 0 : int16_t SX126x::modSetup(uint8_t modem) {
1403 : // set module properties
1404 0 : this->mod->init();
1405 0 : this->mod->hal->pinMode(this->mod->getIrq(), this->mod->hal->GpioModeInput);
1406 0 : this->mod->hal->pinMode(this->mod->getGpio(), this->mod->hal->GpioModeInput);
1407 0 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_ADDR] = Module::BITS_16;
1408 0 : this->mod->spiConfig.widths[RADIOLIB_MODULE_SPI_WIDTH_CMD] = Module::BITS_8;
1409 0 : this->mod->spiConfig.statusPos = 1;
1410 0 : this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_READ] = RADIOLIB_SX126X_CMD_READ_REGISTER;
1411 0 : this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_WRITE] = RADIOLIB_SX126X_CMD_WRITE_REGISTER;
1412 0 : this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_NOP] = RADIOLIB_SX126X_CMD_NOP;
1413 0 : this->mod->spiConfig.cmds[RADIOLIB_MODULE_SPI_COMMAND_STATUS] = RADIOLIB_SX126X_CMD_GET_STATUS;
1414 0 : this->mod->spiConfig.stream = true;
1415 0 : this->mod->spiConfig.parseStatusCb = SPIparseStatus;
1416 :
1417 : // find the SX126x chip - this will also reset the module and verify the module
1418 0 : if(!SX126x::findChip(this->chipType)) {
1419 : RADIOLIB_DEBUG_BASIC_PRINTLN("No SX126x found!");
1420 0 : this->mod->term();
1421 0 : return(RADIOLIB_ERR_CHIP_NOT_FOUND);
1422 : }
1423 : RADIOLIB_DEBUG_BASIC_PRINTLN("M\tSX126x");
1424 :
1425 0 : int16_t state = RADIOLIB_ERR_NONE;
1426 :
1427 : // set TCXO control, if requested
1428 0 : if(this->tcxoVoltage > 0.0f) {
1429 0 : state = setTCXO(this->tcxoVoltage);
1430 0 : RADIOLIB_ASSERT(state);
1431 : }
1432 :
1433 : // configure settings not accessible by API
1434 0 : state = config(modem);
1435 :
1436 : // if something failed, check the device errors
1437 0 : if(state != RADIOLIB_ERR_NONE) {
1438 : // unless mode is forced to standby, device errors will be 0
1439 0 : (void)standby();
1440 0 : uint16_t errors = getDeviceErrors();
1441 : RADIOLIB_DEBUG_BASIC_PRINTLN("Config failed, device errors: 0x%X", errors);
1442 :
1443 : // SPI command fail and oscillator start error flag indicate incorrectly set oscillator
1444 0 : if((state == RADIOLIB_ERR_SPI_CMD_FAILED) && (errors & RADIOLIB_SX126X_XOSC_START_ERR)) {
1445 : // typically users with XTAL devices will try to call the default begin method
1446 : // disable TCXO and try to run config again
1447 0 : this->tcxoVoltage = 0;
1448 : RADIOLIB_DEBUG_BASIC_PRINTLN("Bad oscillator selected, trying XTAL");
1449 :
1450 0 : state = setTCXO(0);
1451 0 : RADIOLIB_ASSERT(state);
1452 :
1453 0 : state = config(modem);
1454 : }
1455 : }
1456 0 : RADIOLIB_ASSERT(state);
1457 :
1458 0 : if(this->useRegulatorLDO) {
1459 0 : state = setRegulatorLDO();
1460 : } else {
1461 0 : state = setRegulatorDCDC();
1462 : }
1463 0 : return(state);
1464 : }
1465 :
1466 0 : int16_t SX126x::SPIparseStatus(uint8_t in) {
1467 0 : if((in & 0b00001110) == RADIOLIB_SX126X_STATUS_CMD_TIMEOUT) {
1468 0 : return(RADIOLIB_ERR_SPI_CMD_TIMEOUT);
1469 0 : } else if((in & 0b00001110) == RADIOLIB_SX126X_STATUS_CMD_INVALID) {
1470 0 : return(RADIOLIB_ERR_SPI_CMD_INVALID);
1471 0 : } else if((in & 0b00001110) == RADIOLIB_SX126X_STATUS_CMD_FAILED) {
1472 0 : return(RADIOLIB_ERR_SPI_CMD_FAILED);
1473 0 : } else if((in == 0x00) || (in == 0xFF)) {
1474 0 : return(RADIOLIB_ERR_CHIP_NOT_FOUND);
1475 : }
1476 0 : return(RADIOLIB_ERR_NONE);
1477 : }
1478 :
1479 0 : bool SX126x::findChip(const char* verStr) {
1480 0 : uint8_t i = 0;
1481 0 : bool flagFound = false;
1482 0 : while((i < 10) && !flagFound) {
1483 : // reset the module
1484 0 : if(this->resetOnStartup) {
1485 0 : reset(true);
1486 : }
1487 :
1488 : // read the version string
1489 0 : char version[16] = { 0 };
1490 0 : this->mod->SPIreadRegisterBurst(RADIOLIB_SX126X_REG_VERSION_STRING, 16, reinterpret_cast<uint8_t*>(version));
1491 :
1492 : // check version register
1493 0 : if(strncmp(verStr, version, 6) == 0) {
1494 : RADIOLIB_DEBUG_BASIC_PRINTLN("Found SX126x: RADIOLIB_SX126X_REG_VERSION_STRING:");
1495 : RADIOLIB_DEBUG_BASIC_HEXDUMP(reinterpret_cast<uint8_t*>(version), 16, RADIOLIB_SX126X_REG_VERSION_STRING);
1496 : RADIOLIB_DEBUG_BASIC_PRINTLN();
1497 0 : flagFound = true;
1498 : } else {
1499 : #if RADIOLIB_DEBUG_BASIC
1500 : RADIOLIB_DEBUG_BASIC_PRINTLN("SX126x not found! (%d of 10 tries) RADIOLIB_SX126X_REG_VERSION_STRING:", i + 1);
1501 : RADIOLIB_DEBUG_BASIC_HEXDUMP(reinterpret_cast<uint8_t*>(version), 16, RADIOLIB_SX126X_REG_VERSION_STRING);
1502 : RADIOLIB_DEBUG_BASIC_PRINTLN("Expected string: %s", verStr);
1503 : #endif
1504 0 : this->mod->hal->delay(10);
1505 0 : i++;
1506 : }
1507 : }
1508 :
1509 0 : return(flagFound);
1510 : }
1511 :
1512 : #endif
|