-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSX128x.cpp
1628 lines (1386 loc) · 42.2 KB
/
SX128x.cpp
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
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of SX1280 Portable driver.
Copyright (C) 2020 ReimuNotMoe
This program is based on sx1280-driver from Semtech S.A.,
see LICENSE-SEMTECH.txt for details.
Original maintainer of sx1280-driver: Miguel Luis, Gregory Cristian
and Matthieu Verdy
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "SX128x.hpp"
void SX128x::Init() {
Reset();
Wakeup();
SetRegistersDefault();
}
void SX128x::SetRegistersDefault(void )
{
for( int16_t i = 0; i < sizeof( RadioRegsInit ) / sizeof( RadioRegisters_t ); i++ ) {
WriteRegister( RadioRegsInit[i].Addr, RadioRegsInit[i].Value );
}
}
uint16_t SX128x::GetFirmwareVersion(void )
{
return( ( ( ReadRegister( REG_LR_FIRMWARE_VERSION_MSB ) ) << 8 ) | ( ReadRegister( REG_LR_FIRMWARE_VERSION_MSB + 1 ) ) );
}
SX128x::RadioStatus_t SX128x::GetStatus(void )
{
uint8_t stat = 0;
RadioStatus_t status;
ReadCommand( RADIO_GET_STATUS, ( uint8_t * )&stat, 1 );
status.Value = stat;
return( status );
}
SX128x::RadioOperatingModes_t SX128x::GetOpMode(void )
{
return( OperatingMode );
}
void SX128x::SetSleep(SleepParams_t sleepConfig )
{
uint8_t sleep = ( sleepConfig.WakeUpRTC << 3 ) |
( sleepConfig.InstructionRamRetention << 2 ) |
( sleepConfig.DataBufferRetention << 1 ) |
( sleepConfig.DataRamRetention );
OperatingMode = MODE_SLEEP;
WriteCommand( RADIO_SET_SLEEP, &sleep, 1 );
}
void SX128x::SetStandby(RadioStandbyModes_t standbyConfig )
{
std::lock_guard<std::mutex> lg(IOLock2);
WriteCommand( RADIO_SET_STANDBY, ( uint8_t* )&standbyConfig, 1 );
if (standbyConfig == STDBY_RC )
{
OperatingMode = MODE_STDBY_RC;
}
else
{
OperatingMode = MODE_STDBY_XOSC;
}
}
void SX128x::SetFs(void )
{
WriteCommand( RADIO_SET_FS, 0, 0 );
OperatingMode = MODE_FS;
}
void SX128x::SetTx(TickTime_t timeout )
{
std::lock_guard<std::mutex> lg(IOLock2);
uint8_t buf[3];
buf[0] = timeout.PeriodBase;
buf[1] = ( uint8_t )( ( timeout.PeriodBaseCount >> 8 ) & 0x00FF );
buf[2] = ( uint8_t )( timeout.PeriodBaseCount & 0x00FF );
ClearIrqStatus( IRQ_RADIO_ALL );
// If the radio is doing ranging operations, then apply the specific calls
// prior to SetTx
if (GetPacketType( true ) == PACKET_TYPE_RANGING )
{
SetRangingRole( RADIO_RANGING_ROLE_MASTER );
}
HalPostRx();
HalPreTx();
WriteCommand( RADIO_SET_TX, buf, 3 );
OperatingMode = MODE_TX;
}
void SX128x::SetRx(TickTime_t timeout )
{
std::lock_guard<std::mutex> lg(IOLock2);
uint8_t buf[3];
buf[0] = timeout.PeriodBase;
buf[1] = ( uint8_t )( ( timeout.PeriodBaseCount >> 8 ) & 0x00FF );
buf[2] = ( uint8_t )( timeout.PeriodBaseCount & 0x00FF );
ClearIrqStatus( IRQ_RADIO_ALL );
// If the radio is doing ranging operations, then apply the specific calls
// prior to SetRx
if (GetPacketType( true ) == PACKET_TYPE_RANGING )
{
SetRangingRole( RADIO_RANGING_ROLE_SLAVE );
}
HalPostTx();
HalPreRx();
WriteCommand( RADIO_SET_RX, buf, 3 );
OperatingMode = MODE_RX;
}
void SX128x::SetRxDutyCycle(RadioTickSizes_t periodBase, uint16_t periodBaseCountRx, uint16_t periodBaseCountSleep )
{
uint8_t buf[5];
buf[0] = periodBase;
buf[1] = ( uint8_t )( ( periodBaseCountRx >> 8 ) & 0x00FF );
buf[2] = ( uint8_t )( periodBaseCountRx & 0x00FF );
buf[3] = ( uint8_t )( ( periodBaseCountSleep >> 8 ) & 0x00FF );
buf[4] = ( uint8_t )( periodBaseCountSleep & 0x00FF );
HalPostTx();
HalPreRx();
WriteCommand( RADIO_SET_RXDUTYCYCLE, buf, 5 );
OperatingMode = MODE_RX;
}
void SX128x::SetCad(void )
{
std::lock_guard<std::mutex> lg(IOLock2);
HalPostTx();
HalPreRx();
WriteCommand( RADIO_SET_CAD, 0, 0 );
OperatingMode = MODE_CAD;
}
void SX128x::SetTxContinuousWave(void )
{
std::lock_guard<std::mutex> lg(IOLock2);
HalPostRx();
HalPreTx();
WriteCommand( RADIO_SET_TXCONTINUOUSWAVE, 0, 0 );
}
void SX128x::SetTxContinuousPreamble(void )
{
std::lock_guard<std::mutex> lg(IOLock2);
HalPostRx();
HalPreTx();
WriteCommand( RADIO_SET_TXCONTINUOUSPREAMBLE, 0, 0 );
}
void SX128x::SetPacketType(RadioPacketTypes_t packetType )
{
// Save packet type internally to avoid questioning the radio
this->PacketType = packetType;
WriteCommand( RADIO_SET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
}
SX128x::RadioPacketTypes_t SX128x::GetPacketType(bool returnLocalCopy )
{
RadioPacketTypes_t packetType = PACKET_TYPE_NONE;
if (returnLocalCopy == false )
{
ReadCommand( RADIO_GET_PACKETTYPE, ( uint8_t* )&packetType, 1 );
if (this->PacketType != packetType )
{
this->PacketType = packetType;
}
}
else
{
packetType = this->PacketType;
}
return packetType;
}
void SX128x::SetRfFrequency(uint32_t rfFrequency )
{
uint8_t buf[3];
uint32_t freq = 0;
freq = ( uint32_t )( ( double )rfFrequency / ( double )FREQ_STEP );
buf[0] = ( uint8_t )( ( freq >> 16 ) & 0xFF );
buf[1] = ( uint8_t )( ( freq >> 8 ) & 0xFF );
buf[2] = ( uint8_t )( freq & 0xFF );
WriteCommand( RADIO_SET_RFFREQUENCY, buf, 3 );
}
void SX128x::SetTxParams(int8_t power, RadioRampTimes_t rampTime )
{
uint8_t buf[2];
// The power value to send on SPI/UART is in the range [0..31] and the
// physical output power is in the range [-18..13]dBm
buf[0] = power + 18;
buf[1] = ( uint8_t )rampTime;
WriteCommand( RADIO_SET_TXPARAMS, buf, 2 );
}
void SX128x::SetCadParams(RadioLoRaCadSymbols_t cadSymbolNum )
{
WriteCommand( RADIO_SET_CADPARAMS, ( uint8_t* )&cadSymbolNum, 1 );
OperatingMode = MODE_CAD;
}
void SX128x::SetBufferBaseAddresses(uint8_t txBaseAddress, uint8_t rxBaseAddress )
{
uint8_t buf[2];
buf[0] = txBaseAddress;
buf[1] = rxBaseAddress;
WriteCommand( RADIO_SET_BUFFERBASEADDRESS, buf, 2 );
}
void SX128x::SetModulationParams(const ModulationParams_t& modParams )
{
uint8_t buf[3];
// Check if required configuration corresponds to the stored packet type
// If not, silently update radio packet type
if (this->PacketType != modParams.PacketType )
{
this->SetPacketType( modParams.PacketType );
}
switch( modParams.PacketType )
{
case PACKET_TYPE_GFSK:
buf[0] = modParams.Params.Gfsk.BitrateBandwidth;
buf[1] = modParams.Params.Gfsk.ModulationIndex;
buf[2] = modParams.Params.Gfsk.ModulationShaping;
break;
case PACKET_TYPE_LORA:
case PACKET_TYPE_RANGING:
buf[0] = modParams.Params.LoRa.SpreadingFactor;
buf[1] = modParams.Params.LoRa.Bandwidth;
buf[2] = modParams.Params.LoRa.CodingRate;
this->LoRaBandwidth = modParams.Params.LoRa.Bandwidth;
break;
case PACKET_TYPE_FLRC:
buf[0] = modParams.Params.Flrc.BitrateBandwidth;
buf[1] = modParams.Params.Flrc.CodingRate;
buf[2] = modParams.Params.Flrc.ModulationShaping;
break;
case PACKET_TYPE_BLE:
buf[0] = modParams.Params.Ble.BitrateBandwidth;
buf[1] = modParams.Params.Ble.ModulationIndex;
buf[2] = modParams.Params.Ble.ModulationShaping;
break;
case PACKET_TYPE_NONE:
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
break;
}
WriteCommand( RADIO_SET_MODULATIONPARAMS, buf, 3 );
CurrentModParams = modParams;
}
void SX128x::SetPacketParams(const PacketParams_t& packetParams)
{
uint8_t buf[7];
// Check if required configuration corresponds to the stored packet type
// If not, silently update radio packet type
if (this->PacketType != packetParams.PacketType )
{
this->SetPacketType( packetParams.PacketType );
}
switch( packetParams.PacketType )
{
case PACKET_TYPE_GFSK:
buf[0] = packetParams.Params.Gfsk.PreambleLength;
buf[1] = packetParams.Params.Gfsk.SyncWordLength;
buf[2] = packetParams.Params.Gfsk.SyncWordMatch;
buf[3] = packetParams.Params.Gfsk.HeaderType;
buf[4] = packetParams.Params.Gfsk.PayloadLength;
buf[5] = packetParams.Params.Gfsk.CrcLength;
buf[6] = packetParams.Params.Gfsk.Whitening;
break;
case PACKET_TYPE_LORA:
case PACKET_TYPE_RANGING:
buf[0] = packetParams.Params.LoRa.PreambleLength;
buf[1] = packetParams.Params.LoRa.HeaderType;
buf[2] = packetParams.Params.LoRa.PayloadLength;
buf[3] = packetParams.Params.LoRa.Crc;
buf[4] = packetParams.Params.LoRa.InvertIQ;
buf[5] = 0;
buf[6] = 0;
break;
case PACKET_TYPE_FLRC:
buf[0] = packetParams.Params.Flrc.PreambleLength;
buf[1] = packetParams.Params.Flrc.SyncWordLength;
buf[2] = packetParams.Params.Flrc.SyncWordMatch;
buf[3] = packetParams.Params.Flrc.HeaderType;
buf[4] = packetParams.Params.Flrc.PayloadLength;
buf[5] = packetParams.Params.Flrc.CrcLength;
buf[6] = packetParams.Params.Flrc.Whitening;
break;
case PACKET_TYPE_BLE:
buf[0] = packetParams.Params.Ble.ConnectionState;
buf[1] = packetParams.Params.Ble.CrcLength;
buf[2] = packetParams.Params.Ble.BleTestPayload;
buf[3] = packetParams.Params.Ble.Whitening;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
break;
case PACKET_TYPE_NONE:
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
break;
}
WriteCommand( RADIO_SET_PACKETPARAMS, buf, 7 );
CurrentPacketParams = packetParams;
}
void SX128x::ForcePreambleLength(RadioPreambleLengths_t preambleLength )
{
this->WriteRegister( REG_LR_PREAMBLELENGTH, ( this->ReadRegister( REG_LR_PREAMBLELENGTH ) & MASK_FORCE_PREAMBLELENGTH ) | preambleLength );
}
void SX128x::GetRxBufferStatus(uint8_t *rxPayloadLength, uint8_t *rxStartBufferPointer )
{
uint8_t status[2];
ReadCommand( RADIO_GET_RXBUFFERSTATUS, status, 2 );
// In case of LORA fixed header, the rxPayloadLength is obtained by reading
// the register REG_LR_PAYLOADLENGTH
if (( this -> GetPacketType( true ) == PACKET_TYPE_LORA ) && ( ReadRegister( REG_LR_PACKETPARAMS ) >> 7 == 1 ) )
{
*rxPayloadLength = ReadRegister( REG_LR_PAYLOADLENGTH );
}
else if (this -> GetPacketType( true ) == PACKET_TYPE_BLE )
{
// In the case of BLE, the size returned in status[0] do not include the 2-byte length PDU header
// so it is added there
*rxPayloadLength = status[0] + 2;
}
else
{
*rxPayloadLength = status[0];
}
*rxStartBufferPointer = status[1];
}
void SX128x::GetPacketStatus(PacketStatus_t *packetStatus )
{
uint8_t status[5];
ReadCommand( RADIO_GET_PACKETSTATUS, status, 5 );
packetStatus->packetType = this -> GetPacketType( true );
switch( packetStatus->packetType )
{
case PACKET_TYPE_GFSK:
packetStatus->Gfsk.RssiSync = -( status[1] / 2 );
packetStatus->Gfsk.ErrorStatus.SyncError = ( status[2] >> 6 ) & 0x01;
packetStatus->Gfsk.ErrorStatus.LengthError = ( status[2] >> 5 ) & 0x01;
packetStatus->Gfsk.ErrorStatus.CrcError = ( status[2] >> 4 ) & 0x01;
packetStatus->Gfsk.ErrorStatus.AbortError = ( status[2] >> 3 ) & 0x01;
packetStatus->Gfsk.ErrorStatus.HeaderReceived = ( status[2] >> 2 ) & 0x01;
packetStatus->Gfsk.ErrorStatus.PacketReceived = ( status[2] >> 1 ) & 0x01;
packetStatus->Gfsk.ErrorStatus.PacketControlerBusy = status[2] & 0x01;
packetStatus->Gfsk.TxRxStatus.RxNoAck = ( status[3] >> 5 ) & 0x01;
packetStatus->Gfsk.TxRxStatus.PacketSent = status[3] & 0x01;
packetStatus->Gfsk.SyncAddrStatus = status[4] & 0x07;
break;
case PACKET_TYPE_LORA:
case PACKET_TYPE_RANGING:
packetStatus->LoRa.RssiPkt = -( status[0] / 2 );
( status[1] < 128 ) ? ( packetStatus->LoRa.SnrPkt = status[1] / 4 ) : ( packetStatus->LoRa.SnrPkt = ( ( status[1] - 256 ) /4 ) );
break;
case PACKET_TYPE_FLRC:
packetStatus->Flrc.RssiSync = -( status[1] / 2 );
packetStatus->Flrc.ErrorStatus.SyncError = ( status[2] >> 6 ) & 0x01;
packetStatus->Flrc.ErrorStatus.LengthError = ( status[2] >> 5 ) & 0x01;
packetStatus->Flrc.ErrorStatus.CrcError = ( status[2] >> 4 ) & 0x01;
packetStatus->Flrc.ErrorStatus.AbortError = ( status[2] >> 3 ) & 0x01;
packetStatus->Flrc.ErrorStatus.HeaderReceived = ( status[2] >> 2 ) & 0x01;
packetStatus->Flrc.ErrorStatus.PacketReceived = ( status[2] >> 1 ) & 0x01;
packetStatus->Flrc.ErrorStatus.PacketControlerBusy = status[2] & 0x01;
packetStatus->Flrc.TxRxStatus.RxPid = ( status[3] >> 6 ) & 0x03;
packetStatus->Flrc.TxRxStatus.RxNoAck = ( status[3] >> 5 ) & 0x01;
packetStatus->Flrc.TxRxStatus.RxPidErr = ( status[3] >> 4 ) & 0x01;
packetStatus->Flrc.TxRxStatus.PacketSent = status[3] & 0x01;
packetStatus->Flrc.SyncAddrStatus = status[4] & 0x07;
break;
case PACKET_TYPE_BLE:
packetStatus->Ble.RssiSync = -( status[1] / 2 );
packetStatus->Ble.ErrorStatus.SyncError = ( status[2] >> 6 ) & 0x01;
packetStatus->Ble.ErrorStatus.LengthError = ( status[2] >> 5 ) & 0x01;
packetStatus->Ble.ErrorStatus.CrcError = ( status[2] >> 4 ) & 0x01;
packetStatus->Ble.ErrorStatus.AbortError = ( status[2] >> 3 ) & 0x01;
packetStatus->Ble.ErrorStatus.HeaderReceived = ( status[2] >> 2 ) & 0x01;
packetStatus->Ble.ErrorStatus.PacketReceived = ( status[2] >> 1 ) & 0x01;
packetStatus->Ble.ErrorStatus.PacketControlerBusy = status[2] & 0x01;
packetStatus->Ble.TxRxStatus.PacketSent = status[3] & 0x01;
packetStatus->Ble.SyncAddrStatus = status[4] & 0x07;
break;
case PACKET_TYPE_NONE:
// In that specific case, we set everything in the packetStatus to zeros
// and reset the packet type accordingly
memset( packetStatus, 0, sizeof( PacketStatus_t ) );
packetStatus->packetType = PACKET_TYPE_NONE;
break;
}
}
int8_t SX128x::GetRssiInst(void )
{
uint8_t raw = 0;
ReadCommand( RADIO_GET_RSSIINST, &raw, 1 );
return ( int8_t ) ( -raw / 2 );
}
void SX128x::SetDioIrqParams(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 );
WriteCommand( RADIO_SET_DIOIRQPARAMS, buf, 8 );
}
uint16_t SX128x::GetIrqStatus(void )
{
uint8_t irqStatus[2];
ReadCommand( RADIO_GET_IRQSTATUS, irqStatus, 2 );
return ( irqStatus[0] << 8 ) | irqStatus[1];
}
void SX128x::ClearIrqStatus(uint16_t irqMask )
{
uint8_t buf[2];
buf[0] = ( uint8_t )( ( ( uint16_t )irqMask >> 8 ) & 0x00FF );
buf[1] = ( uint8_t )( ( uint16_t )irqMask & 0x00FF );
WriteCommand( RADIO_CLR_IRQSTATUS, buf, 2 );
}
void SX128x::Calibrate(CalibrationParams_t calibParam )
{
uint8_t cal = ( calibParam.ADCBulkPEnable << 5 ) |
( calibParam.ADCBulkNEnable << 4 ) |
( calibParam.ADCPulseEnable << 3 ) |
( calibParam.PLLEnable << 2 ) |
( calibParam.RC13MEnable << 1 ) |
( calibParam.RC64KEnable );
WriteCommand( RADIO_CALIBRATE, &cal, 1 );
}
void SX128x::SetRegulatorMode(RadioRegulatorModes_t mode )
{
WriteCommand( RADIO_SET_REGULATORMODE, ( uint8_t* )&mode, 1 );
}
void SX128x::SetSaveContext(void )
{
WriteCommand( RADIO_SET_SAVECONTEXT, 0, 0 );
}
void SX128x::SetAutoTx(uint16_t time )
{
uint16_t compensatedTime = time - ( uint16_t )AUTO_TX_OFFSET;
uint8_t buf[2];
buf[0] = ( uint8_t )( ( compensatedTime >> 8 ) & 0x00FF );
buf[1] = ( uint8_t )( compensatedTime & 0x00FF );
WriteCommand( RADIO_SET_AUTOTX, buf, 2 );
}
void SX128x::StopAutoTx(void )
{
uint8_t buf[2] = {0x00, 0x00};
WriteCommand( RADIO_SET_AUTOTX, buf, 2 );
}
void SX128x::SetAutoFs(bool enableAutoFs )
{
WriteCommand( RADIO_SET_AUTOFS, ( uint8_t * )&enableAutoFs, 1 );
}
void SX128x::SetLongPreamble(bool enable )
{
WriteCommand( RADIO_SET_LONGPREAMBLE, ( uint8_t * )&enable, 1 );
}
void SX128x::SetPayload(uint8_t *buffer, uint8_t size, uint8_t offset )
{
WriteBuffer( offset, buffer, size );
}
uint8_t SX128x::GetPayload(uint8_t *buffer, uint8_t *size , uint8_t maxSize )
{
uint8_t offset;
GetRxBufferStatus( size, &offset );
if (*size > maxSize )
{
return 1;
}
ReadBuffer( offset, buffer, *size );
return 0;
}
void SX128x::SendPayload(uint8_t *payload, uint8_t size, TickTime_t timeout, uint8_t offset )
{
SetPayload( payload, size, offset );
SetTx( timeout );
}
uint8_t SX128x::SetSyncWord(uint8_t syncWordIdx, uint8_t *syncWord )
{
uint16_t addr;
uint8_t syncwordSize = 0;
switch( GetPacketType( true ) )
{
case PACKET_TYPE_GFSK:
syncwordSize = 5;
switch( syncWordIdx )
{
case 1:
addr = REG_LR_SYNCWORDBASEADDRESS1;
break;
case 2:
addr = REG_LR_SYNCWORDBASEADDRESS2;
break;
case 3:
addr = REG_LR_SYNCWORDBASEADDRESS3;
break;
default:
return 1;
}
break;
case PACKET_TYPE_FLRC:
// For FLRC packet type, the SyncWord is one byte shorter and
// the base address is shifted by one byte
syncwordSize = 4;
switch( syncWordIdx )
{
case 1:
addr = REG_LR_SYNCWORDBASEADDRESS1 + 1;
break;
case 2:
addr = REG_LR_SYNCWORDBASEADDRESS2 + 1;
break;
case 3:
addr = REG_LR_SYNCWORDBASEADDRESS3 + 1;
break;
default:
return 1;
}
break;
case PACKET_TYPE_BLE:
// For Ble packet type, only the first SyncWord is used and its
// address is shifted by one byte
syncwordSize = 4;
switch( syncWordIdx )
{
case 1:
addr = REG_LR_SYNCWORDBASEADDRESS1 + 1;
break;
default:
return 1;
}
break;
default:
return 1;
}
WriteRegister( addr, syncWord, syncwordSize );
return 0;
}
void SX128x::SetSyncWordErrorTolerance(uint8_t ErrorBits )
{
ErrorBits = ( ReadRegister( REG_LR_SYNCWORDTOLERANCE ) & 0xF0 ) | ( ErrorBits & 0x0F );
WriteRegister( REG_LR_SYNCWORDTOLERANCE, ErrorBits );
}
uint8_t SX128x::SetCrcSeed(uint8_t *seed )
{
uint8_t updated = 0;
switch( GetPacketType( true ) )
{
case PACKET_TYPE_GFSK:
case PACKET_TYPE_FLRC:
WriteRegister( REG_LR_CRCSEEDBASEADDR, seed, 2 );
updated = 1;
break;
case PACKET_TYPE_BLE:
this->WriteRegister(0x9c7, seed[2] );
this->WriteRegister(0x9c8, seed[1] );
this->WriteRegister(0x9c9, seed[0] );
updated = 1;
break;
default:
break;
}
return updated;
}
void SX128x::SetBleAccessAddress(uint32_t accessAddress )
{
this->WriteRegister( REG_LR_BLE_ACCESS_ADDRESS, ( accessAddress >> 24 ) & 0x000000FF );
this->WriteRegister( REG_LR_BLE_ACCESS_ADDRESS + 1, ( accessAddress >> 16 ) & 0x000000FF );
this->WriteRegister( REG_LR_BLE_ACCESS_ADDRESS + 2, ( accessAddress >> 8 ) & 0x000000FF );
this->WriteRegister( REG_LR_BLE_ACCESS_ADDRESS + 3, accessAddress & 0x000000FF );
}
void SX128x::SetBleAdvertizerAccessAddress(void )
{
this->SetBleAccessAddress( BLE_ADVERTIZER_ACCESS_ADDRESS );
}
void SX128x::SetCrcPolynomial(uint16_t polynomial )
{
uint8_t val[2];
val[0] = ( uint8_t )( polynomial >> 8 ) & 0xFF;
val[1] = ( uint8_t )( polynomial & 0xFF );
switch( GetPacketType( true ) )
{
case PACKET_TYPE_GFSK:
case PACKET_TYPE_FLRC:
WriteRegister( REG_LR_CRCPOLYBASEADDR, val, 2 );
break;
default:
break;
}
}
void SX128x::SetWhiteningSeed(uint8_t seed )
{
switch( GetPacketType( true ) )
{
case PACKET_TYPE_GFSK:
case PACKET_TYPE_FLRC:
case PACKET_TYPE_BLE:
WriteRegister( REG_LR_WHITSEEDBASEADDR, seed );
break;
default:
break;
}
}
void SX128x::EnableManualGain(void )
{
this->WriteRegister( REG_ENABLE_MANUAL_GAIN_CONTROL, this->ReadRegister( REG_ENABLE_MANUAL_GAIN_CONTROL ) | MASK_MANUAL_GAIN_CONTROL );
this->WriteRegister( REG_DEMOD_DETECTION, this->ReadRegister( REG_DEMOD_DETECTION ) & MASK_DEMOD_DETECTION );
}
void SX128x::DisableManualGain(void )
{
this->WriteRegister( REG_ENABLE_MANUAL_GAIN_CONTROL, this->ReadRegister( REG_ENABLE_MANUAL_GAIN_CONTROL ) & (~MASK_MANUAL_GAIN_CONTROL) );
this->WriteRegister( REG_DEMOD_DETECTION, this->ReadRegister( REG_DEMOD_DETECTION ) | (~MASK_DEMOD_DETECTION) );
}
void SX128x::SetManualGainValue(uint8_t gain )
{
this->WriteRegister( REG_MANUAL_GAIN_VALUE, ( this->ReadRegister( REG_MANUAL_GAIN_VALUE ) & MASK_MANUAL_GAIN_VALUE ) | gain );
}
void SX128x::SetLNAGainSetting(const RadioLnaSettings_t lnaSetting )
{
switch(lnaSetting)
{
case LNA_HIGH_SENSITIVITY_MODE:
{
this->WriteRegister( REG_LNA_REGIME, this->ReadRegister( REG_LNA_REGIME ) | MASK_LNA_REGIME );
break;
}
case LNA_LOW_POWER_MODE:
{
this->WriteRegister( REG_LNA_REGIME, this->ReadRegister( REG_LNA_REGIME ) & ~MASK_LNA_REGIME );
break;
}
}
}
void SX128x::SetRangingIdLength(RadioRangingIdCheckLengths_t length )
{
switch( GetPacketType( true ) )
{
case PACKET_TYPE_RANGING:
WriteRegister( REG_LR_RANGINGIDCHECKLENGTH, ( ( ( ( uint8_t )length ) & 0x03 ) << 6 ) | ( ReadRegister( REG_LR_RANGINGIDCHECKLENGTH ) & 0x3F ) );
break;
default:
break;
}
}
void SX128x::SetDeviceRangingAddress(uint32_t address )
{
uint8_t addrArray[] = { static_cast<uint8_t>(address >> 24), static_cast<uint8_t>(address >> 16),
static_cast<uint8_t>(address >> 8), static_cast<uint8_t>(address) };
switch( GetPacketType( true ) )
{
case PACKET_TYPE_RANGING:
WriteRegister( REG_LR_DEVICERANGINGADDR, addrArray, 4 );
break;
default:
break;
}
}
void SX128x::SetRangingRequestAddress(uint32_t address )
{
uint8_t addrArray[] = { static_cast<uint8_t>(address >> 24), static_cast<uint8_t>(address >> 16),
static_cast<uint8_t>(address >> 8), static_cast<uint8_t>(address) };
switch( GetPacketType( true ) )
{
case PACKET_TYPE_RANGING:
WriteRegister( REG_LR_REQUESTRANGINGADDR, addrArray, 4 );
break;
default:
break;
}
}
double SX128x::GetRangingResult(RadioRangingResultTypes_t resultType )
{
uint32_t valLsb = 0;
double val = 0.0;
switch( GetPacketType( true ) )
{
case PACKET_TYPE_RANGING:
this->SetStandby( STDBY_XOSC );
this->WriteRegister( 0x97F, this->ReadRegister( 0x97F ) | ( 1 << 1 ) ); // enable LORA modem clock
WriteRegister( REG_LR_RANGINGRESULTCONFIG, ( ReadRegister( REG_LR_RANGINGRESULTCONFIG ) & MASK_RANGINGMUXSEL ) | ( ( ( ( uint8_t )resultType ) & 0x03 ) << 4 ) );
valLsb = ( ( ReadRegister( REG_LR_RANGINGRESULTBASEADDR ) << 16 ) | ( ReadRegister( REG_LR_RANGINGRESULTBASEADDR + 1 ) << 8 ) | ( ReadRegister( REG_LR_RANGINGRESULTBASEADDR + 2 ) ) );
this->SetStandby( STDBY_RC );
// Convertion from LSB to distance. For explanation on the formula, refer to Datasheet of SX1280
switch( resultType )
{
case RANGING_RESULT_RAW:
// Convert the ranging LSB to distance in meter
// The theoretical conversion from register value to distance [m] is given by:
// distance [m] = ( complement2( register ) * 150 ) / ( 2^12 * bandwidth[MHz] ) )
// The API provide BW in [Hz] so the implemented formula is complement2( register ) / bandwidth[Hz] * A,
// where A = 150 / (2^12 / 1e6) = 36621.09
val = ( double )complement2( valLsb, 24 ) / ( double )this->GetLoRaBandwidth( ) * 36621.09375;
break;
case RANGING_RESULT_AVERAGED:
case RANGING_RESULT_DEBIASED:
case RANGING_RESULT_FILTERED:
val = ( double )valLsb * 20.0 / 100.0;
break;
default:
val = 0.0;
}
break;
default:
break;
}
return val;
}
uint8_t SX128x::GetRangingPowerDeltaThresholdIndicator(void )
{
SetStandby( STDBY_XOSC );
WriteRegister( 0x97F, ReadRegister( 0x97F ) | ( 1 << 1 ) ); // enable LoRa modem clock
WriteRegister( REG_LR_RANGINGRESULTCONFIG, ( ReadRegister( REG_LR_RANGINGRESULTCONFIG ) & MASK_RANGINGMUXSEL ) | ( ( ( ( uint8_t )RANGING_RESULT_RAW ) & 0x03 ) << 4 ) ); // Select raw results
return ReadRegister( REG_RANGING_RSSI );
}
void SX128x::SetRangingCalibration(uint16_t cal )
{
switch( GetPacketType( true ) )
{
case PACKET_TYPE_RANGING:
WriteRegister( REG_LR_RANGINGRERXTXDELAYCAL, ( uint8_t )( ( cal >> 8 ) & 0xFF ) );
WriteRegister( REG_LR_RANGINGRERXTXDELAYCAL + 1, ( uint8_t )( ( cal ) & 0xFF ) );
break;
default:
break;
}
}
void SX128x::RangingClearFilterResult(void )
{
uint8_t regVal = ReadRegister( REG_LR_RANGINGRESULTCLEARREG );
// To clear result, set bit 5 to 1 then to 0
WriteRegister( REG_LR_RANGINGRESULTCLEARREG, regVal | ( 1 << 5 ) );
WriteRegister( REG_LR_RANGINGRESULTCLEARREG, regVal & ( ~( 1 << 5 ) ) );
}
void SX128x::RangingSetFilterNumSamples(uint8_t num )
{
// Silently set 8 as minimum value
WriteRegister( REG_LR_RANGINGFILTERWINDOWSIZE, ( num < DEFAULT_RANGING_FILTER_SIZE ) ? DEFAULT_RANGING_FILTER_SIZE : num );
}
void SX128x::SetRangingRole(RadioRangingRoles_t role )
{
uint8_t buf[1];
buf[0] = role;
WriteCommand( RADIO_SET_RANGING_ROLE, &buf[0], 1 );
}
double SX128x::GetFrequencyError( )
{
uint8_t efeRaw[3] = {0};
uint32_t efe = 0;
double efeHz = 0.0;
switch( this->GetPacketType( true ) )
{
case PACKET_TYPE_LORA:
case PACKET_TYPE_RANGING:
efeRaw[0] = this->ReadRegister( REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB );
efeRaw[1] = this->ReadRegister( REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB + 1 );
efeRaw[2] = this->ReadRegister( REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB + 2 );
efe = ( efeRaw[0]<<16 ) | ( efeRaw[1]<<8 ) | efeRaw[2];
efe &= REG_LR_ESTIMATED_FREQUENCY_ERROR_MASK;
efeHz = 1.55 * ( double )complement2( efe, 20 ) / ( 1600.0 / ( double )this->GetLoRaBandwidth( ) * 1000.0 );
break;
case PACKET_TYPE_NONE:
case PACKET_TYPE_BLE:
case PACKET_TYPE_FLRC:
case PACKET_TYPE_GFSK:
break;
}
return efeHz;
}
//void SX1280::SetPollingMode( void )
//{
// this->PollingMode = true;
//}
int32_t SX128x::complement2(const uint32_t num, const uint8_t bitCnt )
{
int32_t retVal = ( int32_t )num;
if (num >= 2<<( bitCnt - 2 ) )
{
retVal -= 2<<( bitCnt - 1 );
}
return retVal;
}
int32_t SX128x::GetLoRaBandwidth( )
{
int32_t bwValue = 0;
switch( this->LoRaBandwidth )
{
case LORA_BW_0200:
bwValue = 203125;
break;
case LORA_BW_0400:
bwValue = 406250;
break;
case LORA_BW_0800:
bwValue = 812500;
break;
case LORA_BW_1600:
bwValue = 1625000;
break;
default:
bwValue = 0;
}
return bwValue;
}
//void SX1280::SetInterruptMode( void )
//{
// this->PollingMode = false;
//}
//void SX1280::OnDioIrq( void )
//{
// /*
// * When polling mode is activated, it is up to the application to call
// * ProcessIrqs( ). Otherwise, the driver automatically calls ProcessIrqs( )
// * on radio interrupt.
// */
// if (this->PollingMode == true )
// {
// this->IrqState = true;
// }
// else
// {
// this->ProcessIrqs();
// }
//}
void SX128x::ProcessIrqs() {
std::unique_lock<std::mutex> lg(IOLock2);
RadioPacketTypes_t packetType = PACKET_TYPE_NONE;
// if (this->PollingMode == true )
// {
// if (this->IrqState == true )
// {
//// __disable_irq();
// this->IrqState = false;
//// __enable_irq();
// }
// else
// {
// return;
// }
// }
packetType = GetPacketType( true );
uint16_t irqRegs = GetIrqStatus();
ClearIrqStatus( IRQ_RADIO_ALL );
lg.unlock();
auto& txDone = callbacks.txDone;
auto& rxDone = callbacks.rxDone;
auto& rxSyncWordDone = callbacks.rxSyncWordDone;
auto& rxHeaderDone = callbacks.rxHeaderDone;
auto& txTimeout = callbacks.txTimeout;
auto& rxTimeout = callbacks.rxTimeout;
auto& rxError = callbacks.rxError;
auto& rangingDone = callbacks.rangingDone;
auto& cadDone = callbacks.cadDone;
//#if (SX1280_DEBUG == 1 )
// DigitalOut TEST_PIN_1( D14 );
// DigitalOut TEST_PIN_2( D15 );
// for( int i = 0x8000; i != 0; i >>= 1 )
// {