-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
sx126x.c
831 lines (699 loc) · 23.6 KB
/
sx126x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*!
* \file sx126x.c
*
* \brief SX126x driver implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
* ______ _
* / _____) _ | |
* ( (____ _____ ____ _| |_ _____ ____| |__
* \____ \| ___ | (_ _) ___ |/ ___) _ \
* _____) ) ____| | | || |_| ____( (___| | | |
* (______/|_____)_|_|_| \__)_____)\____)_| |_|
* (C)2013-2017 Semtech
*
* \endcode
*
* \author Miguel Luis ( Semtech )
*
* \author Gregory Cristian ( Semtech )
*/
#if defined(__NuttX__) && defined(__clang__) // Workaround for NuttX with zig cc
#include <arch/types.h>
#include "../../nuttx/include/limits.h"
#endif // defined(__NuttX__) && defined(__clang__)
#include <stdio.h>
#include <string.h>
#include "../include/sx126x-utilities.h"
#include "../include/radio.h"
#include "../include/sx126x.h"
#include "../include/sx126x-board.h"
/*!
* \brief Internal frequency of the radio
*/
#define SX126X_XTAL_FREQ 32000000UL
/*!
* \brief Scaling factor used to perform fixed-point operations
*/
#define SX126X_PLL_STEP_SHIFT_AMOUNT ( 14 )
/*!
* \brief PLL step - scaled with SX126X_PLL_STEP_SHIFT_AMOUNT
*/
#define SX126X_PLL_STEP_SCALED ( SX126X_XTAL_FREQ >> ( 25 - SX126X_PLL_STEP_SHIFT_AMOUNT ) )
/*!
* \brief Maximum value for parameter symbNum in \ref SX126xSetLoRaSymbNumTimeout
*/
#define SX126X_MAX_LORA_SYMB_NUM_TIMEOUT 248
/*!
* \brief Radio registers definition
*/
typedef struct
{
uint16_t Addr; //!< The address of the register
uint8_t Value; //!< The value of the register
}RadioRegisters_t;
/*!
* \brief Stores the current packet type set in the radio
*/
static RadioPacketTypes_t PacketType;
/*!
* \brief Stores the current packet header type set in the radio
*/
static volatile RadioLoRaPacketLengthsMode_t LoRaHeaderType;
/*!
* \brief Stores the last frequency error measured on LoRa received packet
*/
volatile uint32_t FrequencyError = 0;
/*!
* \brief Hold the status of the Image calibration
*/
static bool ImageCalibrated = false;
/*!
* \brief Get the number of PLL steps for a given frequency in Hertz
*
* \param [in] freqInHz Frequency in Hertz
*
* \returns Number of PLL steps
*/
static uint32_t SX126xConvertFreqInHzToPllStep( uint32_t freqInHz );
/*
* SX126x DIO IRQ callback functions prototype
*/
/*!
* \brief DIO 0 IRQ callback
*/
void SX126xOnDioIrq( void );
/*!
* \brief DIO 0 IRQ callback
*/
void SX126xSetPollingMode( void );
/*!
* \brief DIO 0 IRQ callback
*/
void SX126xSetInterruptMode( void );
/*
* \brief Process the IRQ if handled by the driver
*/
void SX126xProcessIrqs( void );
void SX126xInit( DioIrqHandler dioIrq )
{
SX126xReset( );
SX126xIoIrqInit( dioIrq );
SX126xWakeup( );
SX126xSetStandby( STDBY_RC );
// Initialize TCXO control
SX126xIoTcxoInit( );
// Initialize RF switch control
SX126xIoRfSwitchInit( );
// Force image calibration
ImageCalibrated = false;
SX126xSetOperatingMode( MODE_STDBY_RC );
}
void SX126xCheckDeviceReady( void )
{
if( ( SX126xGetOperatingMode( ) == MODE_SLEEP ) || ( SX126xGetOperatingMode( ) == MODE_RX_DC ) )
{
SX126xWakeup( );
// Switch is turned off when device is in sleep mode and turned on is all other modes
SX126xAntSwOn( );
}
SX126xWaitOnBusy( );
}
void SX126xSetPayload( uint8_t *payload, uint8_t size )
{
SX126xWriteBuffer( 0x00, payload, size );
}
uint8_t SX126xGetPayload( uint8_t *buffer, uint8_t *size, uint8_t maxSize )
{
uint8_t offset = 0;
SX126xGetRxBufferStatus( size, &offset );
if( *size > maxSize )
{
return 1;
}
SX126xReadBuffer( offset, buffer, *size );
return 0;
}
void SX126xSendPayload( uint8_t *payload, uint8_t size, uint32_t timeout )
{
SX126xSetPayload( payload, size );
SX126xSetTx( timeout );
}
uint8_t SX126xSetSyncWord( uint8_t *syncWord )
{
SX126xWriteRegisters( REG_LR_SYNCWORDBASEADDRESS, syncWord, 8 );
return 0;
}
void SX126xSetCrcSeed( uint16_t seed )
{
uint8_t buf[2];
buf[0] = ( uint8_t )( ( seed >> 8 ) & 0xFF );
buf[1] = ( uint8_t )( seed & 0xFF );
switch( SX126xGetPacketType( ) )
{
case PACKET_TYPE_GFSK:
SX126xWriteRegisters( REG_LR_CRCSEEDBASEADDR, buf, 2 );
break;
default:
break;
}
}
void SX126xSetCrcPolynomial( uint16_t polynomial )
{
uint8_t buf[2];
buf[0] = ( uint8_t )( ( polynomial >> 8 ) & 0xFF );
buf[1] = ( uint8_t )( polynomial & 0xFF );
switch( SX126xGetPacketType( ) )
{
case PACKET_TYPE_GFSK:
SX126xWriteRegisters( REG_LR_CRCPOLYBASEADDR, buf, 2 );
break;
default:
break;
}
}
void SX126xSetWhiteningSeed( uint16_t seed )
{
uint8_t regValue = 0;
switch( SX126xGetPacketType( ) )
{
case PACKET_TYPE_GFSK:
regValue = SX126xReadRegister( REG_LR_WHITSEEDBASEADDR_MSB ) & 0xFE;
regValue = ( ( seed >> 8 ) & 0x01 ) | regValue;
SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_MSB, regValue ); // only 1 bit.
SX126xWriteRegister( REG_LR_WHITSEEDBASEADDR_LSB, ( uint8_t )seed );
break;
default:
break;
}
}
uint32_t SX126xGetRandom( void )
{
uint32_t number = 0;
uint8_t regAnaLna = 0;
uint8_t regAnaMixer = 0;
regAnaLna = SX126xReadRegister( REG_ANA_LNA );
SX126xWriteRegister( REG_ANA_LNA, regAnaLna & ~( 1 << 0 ) );
regAnaMixer = SX126xReadRegister( REG_ANA_MIXER );
SX126xWriteRegister( REG_ANA_MIXER, regAnaMixer & ~( 1 << 7 ) );
// Set radio in continuous reception
SX126xSetRx( 0xFFFFFF ); // Rx Continuous
SX126xReadRegisters( RANDOM_NUMBER_GENERATORBASEADDR, ( uint8_t* )&number, 4 );
SX126xSetStandby( STDBY_RC );
SX126xWriteRegister( REG_ANA_LNA, regAnaLna );
SX126xWriteRegister( REG_ANA_MIXER, regAnaMixer );
return number;
}
void SX126xSetSleep( SleepParams_t sleepConfig )
{
SX126xAntSwOff( );
uint8_t value = ( ( ( uint8_t )sleepConfig.Fields.WarmStart << 2 ) |
( ( uint8_t )sleepConfig.Fields.Reset << 1 ) |
( ( uint8_t )sleepConfig.Fields.WakeUpRTC ) );
if( sleepConfig.Fields.WarmStart == 0 )
{
// Force image calibration
ImageCalibrated = false;
}
SX126xWriteCommand( RADIO_SET_SLEEP, &value, 1 );
SX126xSetOperatingMode( MODE_SLEEP );
}
void SX126xSetStandby( RadioStandbyModes_t standbyConfig )
{
SX126xWriteCommand( RADIO_SET_STANDBY, ( uint8_t* )&standbyConfig, 1 );
if( standbyConfig == STDBY_RC )
{
SX126xSetOperatingMode( MODE_STDBY_RC );
}
else
{
SX126xSetOperatingMode( MODE_STDBY_XOSC );
}
}
void SX126xSetFs( void )
{
SX126xWriteCommand( RADIO_SET_FS, 0, 0 );
SX126xSetOperatingMode( MODE_FS );
}
void SX126xSetTx( uint32_t timeout )
{
uint8_t buf[3];
SX126xSetOperatingMode( MODE_TX );
buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
buf[2] = ( uint8_t )( timeout & 0xFF );
SX126xWriteCommand( RADIO_SET_TX, buf, 3 );
}
void SX126xSetRx( uint32_t timeout )
{
uint8_t buf[3];
SX126xSetOperatingMode( MODE_RX );
SX126xWriteRegister( REG_RX_GAIN, 0x94 ); // default gain
buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
buf[2] = ( uint8_t )( timeout & 0xFF );
SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
}
void SX126xSetRxBoosted( uint32_t timeout )
{
uint8_t buf[3];
SX126xSetOperatingMode( MODE_RX );
SX126xWriteRegister( REG_RX_GAIN, 0x96 ); // max LNA gain, increase current by ~2mA for around ~3dB in sensitivity
buf[0] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
buf[1] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
buf[2] = ( uint8_t )( timeout & 0xFF );
SX126xWriteCommand( RADIO_SET_RX, buf, 3 );
}
void SX126xSetRxDutyCycle( uint32_t rxTime, uint32_t sleepTime )
{
uint8_t buf[6];
buf[0] = ( uint8_t )( ( rxTime >> 16 ) & 0xFF );
buf[1] = ( uint8_t )( ( rxTime >> 8 ) & 0xFF );
buf[2] = ( uint8_t )( rxTime & 0xFF );
buf[3] = ( uint8_t )( ( sleepTime >> 16 ) & 0xFF );
buf[4] = ( uint8_t )( ( sleepTime >> 8 ) & 0xFF );
buf[5] = ( uint8_t )( sleepTime & 0xFF );
SX126xWriteCommand( RADIO_SET_RXDUTYCYCLE, buf, 6 );
SX126xSetOperatingMode( MODE_RX_DC );
}
void SX126xSetCad( void )
{
SX126xWriteCommand( RADIO_SET_CAD, 0, 0 );
SX126xSetOperatingMode( MODE_CAD );
}
void SX126xSetTxContinuousWave( void )
{
SX126xWriteCommand( RADIO_SET_TXCONTINUOUSWAVE, 0, 0 );
SX126xSetOperatingMode( MODE_TX );
}
void SX126xSetTxInfinitePreamble( void )
{
SX126xWriteCommand( RADIO_SET_TXCONTINUOUSPREAMBLE, 0, 0 );
SX126xSetOperatingMode( MODE_TX );
}
void SX126xSetStopRxTimerOnPreambleDetect( bool enable )
{
SX126xWriteCommand( RADIO_SET_STOPRXTIMERONPREAMBLE, ( uint8_t* )&enable, 1 );
}
void SX126xSetLoRaSymbNumTimeout( uint8_t symbNum )
{
uint8_t mant = ( ( ( symbNum > SX126X_MAX_LORA_SYMB_NUM_TIMEOUT ) ?
SX126X_MAX_LORA_SYMB_NUM_TIMEOUT :
symbNum ) + 1 ) >> 1;
uint8_t exp = 0;
uint8_t reg = 0;
while( mant > 31 )
{
mant = ( mant + 3 ) >> 2;
exp++;
}
reg = mant << ( 2 * exp + 1 );
SX126xWriteCommand( RADIO_SET_LORASYMBTIMEOUT, ®, 1 );
if( symbNum != 0 )
{
reg = exp + ( mant << 3 );
SX126xWriteRegister( REG_LR_SYNCH_TIMEOUT, reg );
}
}
void SX126xSetRegulatorMode( RadioRegulatorMode_t mode )
{
SX126xWriteCommand( RADIO_SET_REGULATORMODE, ( uint8_t* )&mode, 1 );
}
void SX126xCalibrate( CalibrationParams_t calibParam )
{
uint8_t value = ( ( ( uint8_t )calibParam.Fields.ImgEnable << 6 ) |
( ( uint8_t )calibParam.Fields.ADCBulkPEnable << 5 ) |
( ( uint8_t )calibParam.Fields.ADCBulkNEnable << 4 ) |
( ( uint8_t )calibParam.Fields.ADCPulseEnable << 3 ) |
( ( uint8_t )calibParam.Fields.PLLEnable << 2 ) |
( ( uint8_t )calibParam.Fields.RC13MEnable << 1 ) |
( ( uint8_t )calibParam.Fields.RC64KEnable ) );
SX126xWriteCommand( RADIO_CALIBRATE, &value, 1 );
}
void SX126xCalibrateImage( uint32_t freq )
{
uint8_t calFreq[2];
if( freq > 900000000 )
{
calFreq[0] = 0xE1;
calFreq[1] = 0xE9;
}
else if( freq > 850000000 )
{
calFreq[0] = 0xD7;
calFreq[1] = 0xDB;
}
else if( freq > 770000000 )
{
calFreq[0] = 0xC1;
calFreq[1] = 0xC5;
}
else if( freq > 460000000 )
{
calFreq[0] = 0x75;
calFreq[1] = 0x81;
}
else if( freq > 425000000 )
{
calFreq[0] = 0x6B;
calFreq[1] = 0x6F;
}
SX126xWriteCommand( RADIO_CALIBRATEIMAGE, calFreq, 2 );
}
void SX126xSetPaConfig( uint8_t paDutyCycle, uint8_t hpMax, uint8_t deviceSel, uint8_t paLut )
{
printf("SX126xSetPaConfig: paDutyCycle=%d, hpMax=%d, deviceSel=%d, paLut=%d \r\n", (int) paDutyCycle, (int) hpMax, (int) deviceSel, (int) paLut);
uint8_t buf[4];
buf[0] = paDutyCycle;
buf[1] = hpMax;
buf[2] = deviceSel;
buf[3] = paLut;
SX126xWriteCommand( RADIO_SET_PACONFIG, buf, 4 );
}
void SX126xSetRxTxFallbackMode( uint8_t fallbackMode )
{
SX126xWriteCommand( RADIO_SET_TXFALLBACKMODE, &fallbackMode, 1 );
}
void SX126xSetDioIrqParams( uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask )
{
uint8_t buf[8];
buf[0] = ( uint8_t )( ( irqMask >> 8 ) & 0x00FF );
buf[1] = ( uint8_t )( irqMask & 0x00FF );
buf[2] = ( uint8_t )( ( dio1Mask >> 8 ) & 0x00FF );
buf[3] = ( uint8_t )( dio1Mask & 0x00FF );
buf[4] = ( uint8_t )( ( dio2Mask >> 8 ) & 0x00FF );
buf[5] = ( uint8_t )( dio2Mask & 0x00FF );
buf[6] = ( uint8_t )( ( dio3Mask >> 8 ) & 0x00FF );
buf[7] = ( uint8_t )( dio3Mask & 0x00FF );
SX126xWriteCommand( RADIO_CFG_DIOIRQ, buf, 8 );
}
uint16_t SX126xGetIrqStatus( void )
{
uint8_t irqStatus[2];
SX126xReadCommand( RADIO_GET_IRQSTATUS, irqStatus, 2 );
return ( irqStatus[0] << 8 ) | irqStatus[1];
}
void SX126xSetDio2AsRfSwitchCtrl( uint8_t enable )
{
SX126xWriteCommand( RADIO_SET_RFSWITCHMODE, &enable, 1 );
}
void SX126xSetDio3AsTcxoCtrl( RadioTcxoCtrlVoltage_t tcxoVoltage, uint32_t timeout )
{
uint8_t buf[4];
buf[0] = tcxoVoltage & 0x07;
buf[1] = ( uint8_t )( ( timeout >> 16 ) & 0xFF );
buf[2] = ( uint8_t )( ( timeout >> 8 ) & 0xFF );
buf[3] = ( uint8_t )( timeout & 0xFF );
SX126xWriteCommand( RADIO_SET_TCXOMODE, buf, 4 );
}
void SX126xSetRfFrequency( uint32_t frequency )
{
uint8_t buf[4];
if( ImageCalibrated == false )
{
SX126xCalibrateImage( frequency );
ImageCalibrated = true;
}
uint32_t freqInPllSteps = SX126xConvertFreqInHzToPllStep( frequency );
buf[0] = ( uint8_t )( ( freqInPllSteps >> 24 ) & 0xFF );
buf[1] = ( uint8_t )( ( freqInPllSteps >> 16 ) & 0xFF );
buf[2] = ( uint8_t )( ( freqInPllSteps >> 8 ) & 0xFF );
buf[3] = ( uint8_t )( freqInPllSteps & 0xFF );
SX126xWriteCommand( RADIO_SET_RFFREQUENCY, buf, 4 );
}
void SX126xSetPacketType( RadioPacketTypes_t packetType )
{
// Save packet type internally to avoid questioning the radio
PacketType = packetType;
SX126xWriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
}
RadioPacketTypes_t SX126xGetPacketType( void )
{
return PacketType;
}
void SX126xSetTxParams( int8_t power, RadioRampTimes_t rampTime )
{
printf("SX126xSetTxParams: power=%d, rampTime=%d\r\n", (int) power, (int) rampTime);
uint8_t buf[2];
if( SX126xGetDeviceId( ) == SX1261 )
{
if( power == 15 )
{
SX126xSetPaConfig( 0x06, 0x00, 0x01, 0x01 );
}
else
{
SX126xSetPaConfig( 0x04, 0x00, 0x01, 0x01 );
}
if( power >= 14 )
{
power = 14;
}
else if( power < -17 )
{
power = -17;
}
//// TODO: Set the current max value in the over current protection. From SX126x-Arduino/src/radio/sx126x/sx126x.cpp
SX126xWriteRegister(REG_OCP, 0x18); // current max is 80 mA for the whole device
}
else // sx1262
{
// WORKAROUND - Better Resistance of the SX1262 Tx to Antenna Mismatch, see DS_SX1261-2_V1.2 datasheet chapter 15.2
SX126xWriteRegister( REG_TX_CLAMP_CFG, SX126xReadRegister( REG_TX_CLAMP_CFG ) | ( 0x0F << 1 ) );
// WORKAROUND END
SX126xSetPaConfig( 0x04, 0x07, 0x00, 0x01 );
if( power > 22 )
{
power = 22;
}
else if( power < -9 )
{
power = -9;
}
//// TODO: Set the current max value in the over current protection. From SX126x-Arduino/src/radio/sx126x/sx126x.cpp
SX126xWriteRegister(REG_OCP, 0x38); // current max 160mA for the whole device
}
buf[0] = power;
buf[1] = ( uint8_t )rampTime;
SX126xWriteCommand( RADIO_SET_TXPARAMS, buf, 2 );
}
void SX126xSetModulationParams( ModulationParams_t *modulationParams )
{
uint8_t n;
uint32_t tempVal = 0;
uint8_t buf[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// Check if required configuration corresponds to the stored packet type
// If not, silently update radio packet type
if( PacketType != modulationParams->PacketType )
{
SX126xSetPacketType( modulationParams->PacketType );
}
switch( modulationParams->PacketType )
{
case PACKET_TYPE_GFSK:
n = 8;
tempVal = ( uint32_t )( 32 * SX126X_XTAL_FREQ / modulationParams->Params.Gfsk.BitRate );
buf[0] = ( tempVal >> 16 ) & 0xFF;
buf[1] = ( tempVal >> 8 ) & 0xFF;
buf[2] = tempVal & 0xFF;
buf[3] = modulationParams->Params.Gfsk.ModulationShaping;
buf[4] = modulationParams->Params.Gfsk.Bandwidth;
tempVal = SX126xConvertFreqInHzToPllStep( modulationParams->Params.Gfsk.Fdev );
buf[5] = ( tempVal >> 16 ) & 0xFF;
buf[6] = ( tempVal >> 8 ) & 0xFF;
buf[7] = ( tempVal& 0xFF );
SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
break;
case PACKET_TYPE_LORA:
n = 4;
buf[0] = modulationParams->Params.LoRa.SpreadingFactor;
buf[1] = modulationParams->Params.LoRa.Bandwidth;
buf[2] = modulationParams->Params.LoRa.CodingRate;
buf[3] = modulationParams->Params.LoRa.LowDatarateOptimize;
SX126xWriteCommand( RADIO_SET_MODULATIONPARAMS, buf, n );
break;
default:
case PACKET_TYPE_NONE:
return;
}
}
void SX126xSetPacketParams( PacketParams_t *packetParams )
{
uint8_t n;
uint8_t crcVal = 0;
uint8_t buf[9] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// Check if required configuration corresponds to the stored packet type
// If not, silently update radio packet type
if( PacketType != packetParams->PacketType )
{
SX126xSetPacketType( packetParams->PacketType );
}
switch( packetParams->PacketType )
{
case PACKET_TYPE_GFSK:
if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_IBM )
{
SX126xSetCrcSeed( CRC_IBM_SEED );
SX126xSetCrcPolynomial( CRC_POLYNOMIAL_IBM );
crcVal = RADIO_CRC_2_BYTES;
}
else if( packetParams->Params.Gfsk.CrcLength == RADIO_CRC_2_BYTES_CCIT )
{
SX126xSetCrcSeed( CRC_CCITT_SEED );
SX126xSetCrcPolynomial( CRC_POLYNOMIAL_CCITT );
crcVal = RADIO_CRC_2_BYTES_INV;
}
else
{
crcVal = packetParams->Params.Gfsk.CrcLength;
}
n = 9;
buf[0] = ( packetParams->Params.Gfsk.PreambleLength >> 8 ) & 0xFF;
buf[1] = packetParams->Params.Gfsk.PreambleLength;
buf[2] = packetParams->Params.Gfsk.PreambleMinDetect;
buf[3] = ( packetParams->Params.Gfsk.SyncWordLength /*<< 3*/ ); // convert from byte to bit
buf[4] = packetParams->Params.Gfsk.AddrComp;
buf[5] = packetParams->Params.Gfsk.HeaderType;
buf[6] = packetParams->Params.Gfsk.PayloadLength;
buf[7] = crcVal;
buf[8] = packetParams->Params.Gfsk.DcFree;
break;
case PACKET_TYPE_LORA:
n = 6;
buf[0] = ( packetParams->Params.LoRa.PreambleLength >> 8 ) & 0xFF;
buf[1] = packetParams->Params.LoRa.PreambleLength;
buf[2] = LoRaHeaderType = packetParams->Params.LoRa.HeaderType;
buf[3] = packetParams->Params.LoRa.PayloadLength;
buf[4] = packetParams->Params.LoRa.CrcMode;
buf[5] = packetParams->Params.LoRa.InvertIQ;
break;
default:
case PACKET_TYPE_NONE:
return;
}
SX126xWriteCommand( RADIO_SET_PACKETPARAMS, buf, n );
}
void SX126xSetCadParams( RadioLoRaCadSymbols_t cadSymbolNum, uint8_t cadDetPeak, uint8_t cadDetMin, RadioCadExitModes_t cadExitMode, uint32_t cadTimeout )
{
uint8_t buf[7];
buf[0] = ( uint8_t )cadSymbolNum;
buf[1] = cadDetPeak;
buf[2] = cadDetMin;
buf[3] = ( uint8_t )cadExitMode;
buf[4] = ( uint8_t )( ( cadTimeout >> 16 ) & 0xFF );
buf[5] = ( uint8_t )( ( cadTimeout >> 8 ) & 0xFF );
buf[6] = ( uint8_t )( cadTimeout & 0xFF );
SX126xWriteCommand( RADIO_SET_CADPARAMS, buf, 7 );
SX126xSetOperatingMode( MODE_CAD );
}
void SX126xSetBufferBaseAddress( uint8_t txBaseAddress, uint8_t rxBaseAddress )
{
uint8_t buf[2];
buf[0] = txBaseAddress;
buf[1] = rxBaseAddress;
SX126xWriteCommand( RADIO_SET_BUFFERBASEADDRESS, buf, 2 );
}
RadioStatus_t SX126xGetStatus( void )
{
uint8_t stat = 0;
RadioStatus_t status = { .Value = 0 };
stat = SX126xReadCommand( RADIO_GET_STATUS, NULL, 0 );
status.Fields.CmdStatus = ( stat & ( 0x07 << 1 ) ) >> 1;
status.Fields.ChipMode = ( stat & ( 0x07 << 4 ) ) >> 4;
return status;
}
int8_t SX126xGetRssiInst( void )
{
uint8_t buf[1];
int8_t rssi = 0;
SX126xReadCommand( RADIO_GET_RSSIINST, buf, 1 );
rssi = -buf[0] >> 1;
return rssi;
}
void SX126xGetRxBufferStatus( uint8_t *payloadLength, uint8_t *rxStartBufferPointer )
{
uint8_t status[2];
SX126xReadCommand( RADIO_GET_RXBUFFERSTATUS, status, 2 );
// In case of LORA fixed header, the payloadLength is obtained by reading
// the register REG_LR_PAYLOADLENGTH
if( ( SX126xGetPacketType( ) == PACKET_TYPE_LORA ) && ( LoRaHeaderType == LORA_PACKET_FIXED_LENGTH ) )
{
*payloadLength = SX126xReadRegister( REG_LR_PAYLOADLENGTH );
}
else
{
*payloadLength = status[0];
}
*rxStartBufferPointer = status[1];
}
void SX126xGetPacketStatus( PacketStatus_t *pktStatus )
{
uint8_t status[3];
SX126xReadCommand( RADIO_GET_PACKETSTATUS, status, 3 );
pktStatus->packetType = SX126xGetPacketType( );
switch( pktStatus->packetType )
{
case PACKET_TYPE_GFSK:
pktStatus->Params.Gfsk.RxStatus = status[0];
pktStatus->Params.Gfsk.RssiSync = -status[1] >> 1;
pktStatus->Params.Gfsk.RssiAvg = -status[2] >> 1;
pktStatus->Params.Gfsk.FreqError = 0;
break;
case PACKET_TYPE_LORA:
pktStatus->Params.LoRa.RssiPkt = -status[0] >> 1;
// Returns SNR value [dB] rounded to the nearest integer value
pktStatus->Params.LoRa.SnrPkt = ( ( ( int8_t )status[1] ) + 2 ) >> 2;
pktStatus->Params.LoRa.SignalRssiPkt = -status[2] >> 1;
pktStatus->Params.LoRa.FreqError = FrequencyError;
break;
default:
case PACKET_TYPE_NONE:
// In that specific case, we set everything in the pktStatus to zeros
// and reset the packet type accordingly
memset( pktStatus, 0, sizeof( PacketStatus_t ) );
pktStatus->packetType = PACKET_TYPE_NONE;
break;
}
}
RadioError_t SX126xGetDeviceErrors( void )
{
uint8_t err[] = { 0, 0 };
RadioError_t error = { .Value = 0 };
SX126xReadCommand( RADIO_GET_ERROR, ( uint8_t* )err, 2 );
error.Fields.PaRamp = ( err[0] & ( 1 << 0 ) ) >> 0;
error.Fields.PllLock = ( err[1] & ( 1 << 6 ) ) >> 6;
error.Fields.XoscStart = ( err[1] & ( 1 << 5 ) ) >> 5;
error.Fields.ImgCalib = ( err[1] & ( 1 << 4 ) ) >> 4;
error.Fields.AdcCalib = ( err[1] & ( 1 << 3 ) ) >> 3;
error.Fields.PllCalib = ( err[1] & ( 1 << 2 ) ) >> 2;
error.Fields.Rc13mCalib = ( err[1] & ( 1 << 1 ) ) >> 1;
error.Fields.Rc64kCalib = ( err[1] & ( 1 << 0 ) ) >> 0;
return error;
}
void SX126xClearDeviceErrors( void )
{
uint8_t buf[2] = { 0x00, 0x00 };
SX126xWriteCommand( RADIO_CLR_ERROR, buf, 2 );
}
void SX126xClearIrqStatus( uint16_t irq )
{
uint8_t buf[2];
buf[0] = ( uint8_t )( ( ( uint16_t )irq >> 8 ) & 0x00FF );
buf[1] = ( uint8_t )( ( uint16_t )irq & 0x00FF );
SX126xWriteCommand( RADIO_CLR_IRQSTATUS, buf, 2 );
}
static uint32_t SX126xConvertFreqInHzToPllStep( uint32_t freqInHz )
{
uint32_t stepsInt;
uint32_t stepsFrac;
// pllSteps = freqInHz / (SX126X_XTAL_FREQ / 2^19 )
// Get integer and fractional parts of the frequency computed with a PLL step scaled value
stepsInt = freqInHz / SX126X_PLL_STEP_SCALED;
stepsFrac = freqInHz - ( stepsInt * SX126X_PLL_STEP_SCALED );
// Apply the scaling factor to retrieve a frequency in Hz (+ ceiling)
return ( stepsInt << SX126X_PLL_STEP_SHIFT_AMOUNT ) +
( ( ( stepsFrac << SX126X_PLL_STEP_SHIFT_AMOUNT ) + ( SX126X_PLL_STEP_SCALED >> 1 ) ) /
SX126X_PLL_STEP_SCALED );
}