-
Notifications
You must be signed in to change notification settings - Fork 0
/
sx126x.c
1543 lines (1292 loc) · 53.1 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
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
/**
* @file sx126x.c
*
* @brief SX126x radio driver implementation
*
* The Clear BSD License
* Copyright Semtech Corporation 2021. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Semtech corporation nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
* THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SEMTECH CORPORATION BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* -----------------------------------------------------------------------------
* --- DEPENDENCIES ------------------------------------------------------------
*/
#include <stddef.h>
#include "sx126x.h"
#include "sx126x_hal.h"
#include "sx126x_regs.h"
/*
* -----------------------------------------------------------------------------
* --- PRIVATE MACROS-----------------------------------------------------------
*/
/*
* -----------------------------------------------------------------------------
* --- PRIVATE CONSTANTS -------------------------------------------------------
*/
/**
* @brief Internal frequency of the radio
*/
#define SX126X_XTAL_FREQ 32000000UL
/**
* @brief Internal frequency of the radio
*/
#define SX126X_RTC_FREQ_IN_HZ 64000UL
/**
* @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 ) )
/*
* -----------------------------------------------------------------------------
* --- PRIVATE TYPES -----------------------------------------------------------
*/
/**
* Commands Interface
*/
typedef enum sx126x_commands_e
{
// Operational Modes Functions
SX126X_SET_SLEEP = 0x84,
SX126X_SET_STANDBY = 0x80,
SX126X_SET_FS = 0xC1,
SX126X_SET_TX = 0x83,
SX126X_SET_RX = 0x82,
SX126X_SET_STOP_TIMER_ON_PREAMBLE = 0x9F,
SX126X_SET_RX_DUTY_CYCLE = 0x94,
SX126X_SET_CAD = 0xC5,
SX126X_SET_TX_CONTINUOUS_WAVE = 0xD1,
SX126X_SET_TX_INFINITE_PREAMBLE = 0xD2,
SX126X_SET_REGULATOR_MODE = 0x96,
SX126X_CALIBRATE = 0x89,
SX126X_CALIBRATE_IMAGE = 0x98,
SX126X_SET_PA_CFG = 0x95,
SX126X_SET_RX_TX_FALLBACK_MODE = 0x93,
// Registers and buffer Access
SX126X_WRITE_REGISTER = 0x0D,
SX126X_READ_REGISTER = 0x1D,
SX126X_WRITE_BUFFER = 0x0E,
SX126X_READ_BUFFER = 0x1E,
// DIO and IRQ Control Functions
SX126X_SET_DIO_IRQ_PARAMS = 0x08,
SX126X_GET_IRQ_STATUS = 0x12,
SX126X_CLR_IRQ_STATUS = 0x02,
SX126X_SET_DIO2_AS_RF_SWITCH_CTRL = 0x9D,
SX126X_SET_DIO3_AS_TCXO_CTRL = 0x97,
// RF Modulation and Packet-Related Functions
SX126X_SET_RF_FREQUENCY = 0x86,
SX126X_SET_PKT_TYPE = 0x8A,
SX126X_GET_PKT_TYPE = 0x11,
SX126X_SET_TX_PARAMS = 0x8E,
SX126X_SET_MODULATION_PARAMS = 0x8B,
SX126X_SET_PKT_PARAMS = 0x8C,
SX126X_SET_CAD_PARAMS = 0x88,
SX126X_SET_BUFFER_BASE_ADDRESS = 0x8F,
SX126X_SET_LORA_SYMB_NUM_TIMEOUT = 0xA0,
// Communication Status Information
SX126X_GET_STATUS = 0xC0,
SX126X_GET_RX_BUFFER_STATUS = 0x13,
SX126X_GET_PKT_STATUS = 0x14,
SX126X_GET_RSSI_INST = 0x15,
SX126X_GET_STATS = 0x10,
SX126X_RESET_STATS = 0x00,
// Miscellaneous
SX126X_GET_DEVICE_ERRORS = 0x17,
SX126X_CLR_DEVICE_ERRORS = 0x07,
} sx126x_commands_t;
/**
* Commands Interface buffer sizes
*/
typedef enum sx126x_commands_size_e
{
// Operational Modes Functions
SX126X_SIZE_SET_SLEEP = 2,
SX126X_SIZE_SET_STANDBY = 2,
SX126X_SIZE_SET_FS = 1,
SX126X_SIZE_SET_TX = 4,
SX126X_SIZE_SET_RX = 4,
SX126X_SIZE_SET_STOP_TIMER_ON_PREAMBLE = 2,
SX126X_SIZE_SET_RX_DUTY_CYCLE = 7,
SX126X_SIZE_SET_CAD = 1,
SX126X_SIZE_SET_TX_CONTINUOUS_WAVE = 1,
SX126X_SIZE_SET_TX_INFINITE_PREAMBLE = 1,
SX126X_SIZE_SET_REGULATOR_MODE = 2,
SX126X_SIZE_CALIBRATE = 2,
SX126X_SIZE_CALIBRATE_IMAGE = 3,
SX126X_SIZE_SET_PA_CFG = 5,
SX126X_SIZE_SET_RX_TX_FALLBACK_MODE = 2,
// Registers and buffer Access
// Full size: this value plus buffer size
SX126X_SIZE_WRITE_REGISTER = 3,
// Full size: this value plus buffer size
SX126X_SIZE_READ_REGISTER = 4,
// Full size: this value plus buffer size
SX126X_SIZE_WRITE_BUFFER = 2,
// Full size: this value plus buffer size
SX126X_SIZE_READ_BUFFER = 3,
// DIO and IRQ Control Functions
SX126X_SIZE_SET_DIO_IRQ_PARAMS = 9,
SX126X_SIZE_GET_IRQ_STATUS = 2,
SX126X_SIZE_CLR_IRQ_STATUS = 3,
SX126X_SIZE_SET_DIO2_AS_RF_SWITCH_CTRL = 2,
SX126X_SIZE_SET_DIO3_AS_TCXO_CTRL = 5,
// RF Modulation and Packet-Related Functions
SX126X_SIZE_SET_RF_FREQUENCY = 5,
SX126X_SIZE_SET_PKT_TYPE = 2,
SX126X_SIZE_GET_PKT_TYPE = 2,
SX126X_SIZE_SET_TX_PARAMS = 3,
SX126X_SIZE_SET_MODULATION_PARAMS_GFSK = 9,
SX126X_SIZE_SET_MODULATION_PARAMS_BPSK = 5,
SX126X_SIZE_SET_MODULATION_PARAMS_LORA = 5,
SX126X_SIZE_SET_PKT_PARAMS_GFSK = 10,
SX126X_SIZE_SET_PKT_PARAMS_BPSK = 2,
SX126X_SIZE_SET_PKT_PARAMS_LORA = 7,
SX126X_SIZE_SET_CAD_PARAMS = 8,
SX126X_SIZE_SET_BUFFER_BASE_ADDRESS = 3,
SX126X_SIZE_SET_LORA_SYMB_NUM_TIMEOUT = 2,
// Communication Status Information
SX126X_SIZE_GET_STATUS = 1,
SX126X_SIZE_GET_RX_BUFFER_STATUS = 2,
SX126X_SIZE_GET_PKT_STATUS = 2,
SX126X_SIZE_GET_RSSI_INST = 2,
SX126X_SIZE_GET_STATS = 2,
SX126X_SIZE_RESET_STATS = 7,
// Miscellaneous
SX126X_SIZE_GET_DEVICE_ERRORS = 2,
SX126X_SIZE_CLR_DEVICE_ERRORS = 3,
SX126X_SIZE_MAX_BUFFER = 255,
SX126X_SIZE_DUMMY_BYTE = 1,
} sx126x_commands_size_t;
typedef struct
{
uint32_t bw;
uint8_t param;
} gfsk_bw_t;
gfsk_bw_t gfsk_bw[] = {
{ 4800, SX126X_GFSK_BW_4800 }, { 5800, SX126X_GFSK_BW_5800 }, { 7300, SX126X_GFSK_BW_7300 },
{ 9700, SX126X_GFSK_BW_9700 }, { 11700, SX126X_GFSK_BW_11700 }, { 14600, SX126X_GFSK_BW_14600 },
{ 19500, SX126X_GFSK_BW_19500 }, { 23400, SX126X_GFSK_BW_23400 }, { 29300, SX126X_GFSK_BW_29300 },
{ 39000, SX126X_GFSK_BW_39000 }, { 46900, SX126X_GFSK_BW_46900 }, { 58600, SX126X_GFSK_BW_58600 },
{ 78200, SX126X_GFSK_BW_78200 }, { 93800, SX126X_GFSK_BW_93800 }, { 117300, SX126X_GFSK_BW_117300 },
{ 156200, SX126X_GFSK_BW_156200 }, { 187200, SX126X_GFSK_BW_187200 }, { 234300, SX126X_GFSK_BW_234300 },
{ 312000, SX126X_GFSK_BW_312000 }, { 373600, SX126X_GFSK_BW_373600 }, { 467000, SX126X_GFSK_BW_467000 },
};
/*
* -----------------------------------------------------------------------------
* --- PRIVATE VARIABLES -------------------------------------------------------
*/
/*
* -----------------------------------------------------------------------------
* --- PRIVATE FUNCTIONS DECLARATION -------------------------------------------
*/
/**
* @brief 15.1.2 Workaround
*
* @remark Before any packet transmission, bit #2 of SX126X_REG_TX_MODULATION shall be set to:
* 0 if the LoRa BW = 500 kHz
* 1 for any other LoRa BW
* 1 for any (G)FSK configuration
*
* @param [in] context Chip implementation context.
* @param [in] pkt_type The modulation type (G)FSK/LoRa
* @param [in] bw In case of LoRa modulation the bandwith must be specified
*
* @returns Operation status
*/
static sx126x_status_t sx126x_tx_modulation_workaround( const void* context, sx126x_pkt_type_t pkt_type,
sx126x_lora_bw_t bw );
static inline uint32_t sx126x_get_gfsk_crc_len_in_bytes( sx126x_gfsk_crc_types_t crc_type );
/*
* -----------------------------------------------------------------------------
* --- PUBLIC FUNCTIONS DEFINITION ---------------------------------------------
*/
sx126x_status_t sx126x_set_sleep( const void* context, const sx126x_sleep_cfgs_t cfg )
{
const uint8_t buf[SX126X_SIZE_SET_SLEEP] = {
SX126X_SET_SLEEP,
( uint8_t ) cfg,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_SLEEP, 0, 0 );
}
sx126x_status_t sx126x_set_standby( const void* context, const sx126x_standby_cfg_t cfg )
{
const uint8_t buf[SX126X_SIZE_SET_STANDBY] = {
SX126X_SET_STANDBY,
( uint8_t ) cfg,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_STANDBY, 0, 0 );
}
sx126x_status_t sx126x_set_fs( const void* context )
{
const uint8_t buf[SX126X_SIZE_SET_FS] = {
SX126X_SET_FS,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_FS, 0, 0 );
}
sx126x_status_t sx126x_set_tx( const void* context, const uint32_t timeout_in_ms )
{
if( timeout_in_ms > SX126X_MAX_TIMEOUT_IN_MS )
{
return SX126X_STATUS_UNKNOWN_VALUE;
}
const uint32_t timeout_in_rtc_step = sx126x_convert_timeout_in_ms_to_rtc_step( timeout_in_ms );
return sx126x_set_tx_with_timeout_in_rtc_step( context, timeout_in_rtc_step );
}
sx126x_status_t sx126x_set_tx_with_timeout_in_rtc_step( const void* context, const uint32_t timeout_in_rtc_step )
{
const uint8_t buf[SX126X_SIZE_SET_TX] = {
SX126X_SET_TX,
( uint8_t )( timeout_in_rtc_step >> 16 ),
( uint8_t )( timeout_in_rtc_step >> 8 ),
( uint8_t )( timeout_in_rtc_step >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_TX, 0, 0 );
}
sx126x_status_t sx126x_set_rx( const void* context, const uint32_t timeout_in_ms )
{
if( timeout_in_ms > SX126X_MAX_TIMEOUT_IN_MS )
{
return SX126X_STATUS_UNKNOWN_VALUE;
}
const uint32_t timeout_in_rtc_step = sx126x_convert_timeout_in_ms_to_rtc_step( timeout_in_ms );
return sx126x_set_rx_with_timeout_in_rtc_step( context, timeout_in_rtc_step );
}
sx126x_status_t sx126x_set_rx_with_timeout_in_rtc_step( const void* context, const uint32_t timeout_in_rtc_step )
{
const uint8_t buf[SX126X_SIZE_SET_RX] = {
SX126X_SET_RX,
( uint8_t )( timeout_in_rtc_step >> 16 ),
( uint8_t )( timeout_in_rtc_step >> 8 ),
( uint8_t )( timeout_in_rtc_step >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_RX, 0, 0 );
}
sx126x_status_t sx126x_stop_timer_on_preamble( const void* context, const bool enable )
{
const uint8_t buf[SX126X_SIZE_SET_STOP_TIMER_ON_PREAMBLE] = {
SX126X_SET_STOP_TIMER_ON_PREAMBLE,
( enable == true ) ? 1 : 0,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_STOP_TIMER_ON_PREAMBLE, 0, 0 );
}
sx126x_status_t sx126x_set_rx_duty_cycle( const void* context, const uint32_t rx_time_in_ms,
const uint32_t sleep_time_in_ms )
{
const uint32_t rx_time_in_rtc_step = sx126x_convert_timeout_in_ms_to_rtc_step( rx_time_in_ms );
const uint32_t sleep_time_in_rtc_step = sx126x_convert_timeout_in_ms_to_rtc_step( sleep_time_in_ms );
return sx126x_set_rx_duty_cycle_with_timings_in_rtc_step( context, rx_time_in_rtc_step, sleep_time_in_rtc_step );
}
sx126x_status_t sx126x_set_rx_duty_cycle_with_timings_in_rtc_step( const void* context,
const uint32_t rx_time_in_rtc_step,
const uint32_t sleep_time_in_rtc_step )
{
const uint8_t buf[SX126X_SIZE_SET_RX_DUTY_CYCLE] = {
SX126X_SET_RX_DUTY_CYCLE,
( uint8_t )( rx_time_in_rtc_step >> 16 ),
( uint8_t )( rx_time_in_rtc_step >> 8 ),
( uint8_t )( rx_time_in_rtc_step >> 0 ),
( uint8_t )( sleep_time_in_rtc_step >> 16 ),
( uint8_t )( sleep_time_in_rtc_step >> 8 ),
( uint8_t )( sleep_time_in_rtc_step >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_RX_DUTY_CYCLE, 0, 0 );
}
sx126x_status_t sx126x_set_cad( const void* context )
{
const uint8_t buf[SX126X_SIZE_SET_CAD] = {
SX126X_SET_CAD,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_CAD, 0, 0 );
}
sx126x_status_t sx126x_set_tx_cw( const void* context )
{
const uint8_t buf[SX126X_SIZE_SET_TX_CONTINUOUS_WAVE] = {
SX126X_SET_TX_CONTINUOUS_WAVE,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_TX_CONTINUOUS_WAVE, 0, 0 );
}
sx126x_status_t sx126x_set_tx_infinite_preamble( const void* context )
{
const uint8_t buf[SX126X_SIZE_SET_TX_INFINITE_PREAMBLE] = {
SX126X_SET_TX_INFINITE_PREAMBLE,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_TX_INFINITE_PREAMBLE, 0, 0 );
}
sx126x_status_t sx126x_set_reg_mode( const void* context, const sx126x_reg_mod_t mode )
{
const uint8_t buf[SX126X_SIZE_SET_REGULATOR_MODE] = {
SX126X_SET_REGULATOR_MODE,
( uint8_t ) mode,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_REGULATOR_MODE, 0, 0 );
}
sx126x_status_t sx126x_cal( const void* context, const sx126x_cal_mask_t param )
{
const uint8_t buf[SX126X_SIZE_CALIBRATE] = {
SX126X_CALIBRATE,
( uint8_t ) param,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_CALIBRATE, 0, 0 );
}
sx126x_status_t sx126x_cal_img( const void* context, const uint8_t freq1, const uint8_t freq2 )
{
const uint8_t buf[SX126X_SIZE_CALIBRATE_IMAGE] = {
SX126X_CALIBRATE_IMAGE,
freq1,
freq2,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_CALIBRATE_IMAGE, 0, 0 );
}
sx126x_status_t sx126x_cal_img_in_mhz( const void* context, const uint16_t freq1_in_mhz, const uint16_t freq2_in_mhz )
{
// Perform a floor() to get a value for freq1 corresponding to a frequency lower than or equal to freq1_in_mhz
const uint8_t freq1 = freq1_in_mhz / SX126X_IMAGE_CALIBRATION_STEP_IN_MHZ;
// Perform a ceil() to get a value for freq2 corresponding to a frequency higher than or equal to freq2_in_mhz
const uint8_t freq2 =
( freq2_in_mhz + SX126X_IMAGE_CALIBRATION_STEP_IN_MHZ - 1 ) / SX126X_IMAGE_CALIBRATION_STEP_IN_MHZ;
return sx126x_cal_img( context, freq1, freq2 );
}
sx126x_status_t sx126x_set_pa_cfg( const void* context, const sx126x_pa_cfg_params_t* params )
{
const uint8_t buf[SX126X_SIZE_SET_PA_CFG] = {
SX126X_SET_PA_CFG, params->pa_duty_cycle, params->hp_max, params->device_sel, params->pa_lut,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_PA_CFG, 0, 0 );
}
sx126x_status_t sx126x_set_rx_tx_fallback_mode( const void* context, const sx126x_fallback_modes_t fallback_mode )
{
const uint8_t buf[SX126X_SIZE_SET_RX_TX_FALLBACK_MODE] = {
SX126X_SET_RX_TX_FALLBACK_MODE,
( uint8_t ) fallback_mode,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_RX_TX_FALLBACK_MODE, 0, 0 );
}
//
// Registers and buffer Access
//
sx126x_status_t sx126x_write_register( const void* context, const uint16_t address, const uint8_t* buffer,
const uint8_t size )
{
const uint8_t buf[SX126X_SIZE_WRITE_REGISTER] = {
SX126X_WRITE_REGISTER,
( uint8_t )( address >> 8 ),
( uint8_t )( address >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_WRITE_REGISTER, buffer, size );
}
sx126x_status_t sx126x_read_register( const void* context, const uint16_t address, uint8_t* buffer, const uint8_t size )
{
const uint8_t buf[SX126X_SIZE_READ_REGISTER] = {
SX126X_READ_REGISTER,
( uint8_t )( address >> 8 ),
( uint8_t )( address >> 0 ),
SX126X_NOP,
};
return ( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_READ_REGISTER, buffer, size );
}
sx126x_status_t sx126x_write_buffer( const void* context, const uint8_t offset, const uint8_t* buffer,
const uint8_t size )
{
const uint8_t buf[SX126X_SIZE_WRITE_BUFFER] = {
SX126X_WRITE_BUFFER,
offset,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_WRITE_BUFFER, buffer, size );
}
sx126x_status_t sx126x_read_buffer( const void* context, const uint8_t offset, uint8_t* buffer, const uint8_t size )
{
const uint8_t buf[SX126X_SIZE_READ_BUFFER] = {
SX126X_READ_BUFFER,
offset,
SX126X_NOP,
};
return ( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_READ_BUFFER, buffer, size );
}
//
// DIO and IRQ Control Functions
//
sx126x_status_t sx126x_set_dio_irq_params( const void* context, const uint16_t irq_mask, const uint16_t dio1_mask,
const uint16_t dio2_mask, const uint16_t dio3_mask )
{
const uint8_t buf[SX126X_SIZE_SET_DIO_IRQ_PARAMS] = {
SX126X_SET_DIO_IRQ_PARAMS, ( uint8_t )( irq_mask >> 8 ), ( uint8_t )( irq_mask >> 0 ),
( uint8_t )( dio1_mask >> 8 ), ( uint8_t )( dio1_mask >> 0 ), ( uint8_t )( dio2_mask >> 8 ),
( uint8_t )( dio2_mask >> 0 ), ( uint8_t )( dio3_mask >> 8 ), ( uint8_t )( dio3_mask >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_DIO_IRQ_PARAMS, 0, 0 );
}
sx126x_status_t sx126x_get_irq_status( const void* context, sx126x_irq_mask_t* irq )
{
const uint8_t buf[SX126X_SIZE_GET_IRQ_STATUS] = {
SX126X_GET_IRQ_STATUS,
SX126X_NOP,
};
uint8_t irq_local[sizeof( sx126x_irq_mask_t )] = { 0x00 };
const sx126x_status_t status = ( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_IRQ_STATUS,
irq_local, sizeof( sx126x_irq_mask_t ) );
if( status == SX126X_STATUS_OK )
{
*irq = ( ( sx126x_irq_mask_t ) irq_local[0] << 8 ) + ( ( sx126x_irq_mask_t ) irq_local[1] << 0 );
}
return status;
}
sx126x_status_t sx126x_clear_irq_status( const void* context, const sx126x_irq_mask_t irq_mask )
{
const uint8_t buf[SX126X_SIZE_CLR_IRQ_STATUS] = {
SX126X_CLR_IRQ_STATUS,
( uint8_t )( irq_mask >> 8 ),
( uint8_t )( irq_mask >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_CLR_IRQ_STATUS, 0, 0 );
}
sx126x_status_t sx126x_get_and_clear_irq_status( const void* context, sx126x_irq_mask_t* irq )
{
sx126x_irq_mask_t sx126x_irq_mask = SX126X_IRQ_NONE;
sx126x_status_t status = sx126x_get_irq_status( context, &sx126x_irq_mask );
if( ( status == SX126X_STATUS_OK ) && ( sx126x_irq_mask != 0 ) )
{
status = sx126x_clear_irq_status( context, sx126x_irq_mask );
}
if( ( status == SX126X_STATUS_OK ) && ( irq != NULL ) )
{
*irq = sx126x_irq_mask;
}
return status;
}
sx126x_status_t sx126x_set_dio2_as_rf_sw_ctrl( const void* context, const bool enable )
{
const uint8_t buf[SX126X_SIZE_SET_DIO2_AS_RF_SWITCH_CTRL] = {
SX126X_SET_DIO2_AS_RF_SWITCH_CTRL,
( enable == true ) ? 1 : 0,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_DIO2_AS_RF_SWITCH_CTRL, 0, 0 );
}
sx126x_status_t sx126x_set_dio3_as_tcxo_ctrl( const void* context, const sx126x_tcxo_ctrl_voltages_t tcxo_voltage,
const uint32_t timeout )
{
const uint8_t buf[SX126X_SIZE_SET_DIO3_AS_TCXO_CTRL] = {
SX126X_SET_DIO3_AS_TCXO_CTRL, ( uint8_t ) tcxo_voltage, ( uint8_t )( timeout >> 16 ),
( uint8_t )( timeout >> 8 ), ( uint8_t )( timeout >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_DIO3_AS_TCXO_CTRL, 0, 0 );
}
//
// RF Modulation and Packet-Related Functions
//
sx126x_status_t sx126x_set_rf_freq( const void* context, const uint32_t freq_in_hz )
{
const uint32_t freq = sx126x_convert_freq_in_hz_to_pll_step( freq_in_hz );
return sx126x_set_rf_freq_in_pll_steps( context, freq );
}
sx126x_status_t sx126x_set_rf_freq_in_pll_steps( const void* context, const uint32_t freq )
{
const uint8_t buf[SX126X_SIZE_SET_RF_FREQUENCY] = {
SX126X_SET_RF_FREQUENCY, ( uint8_t )( freq >> 24 ), ( uint8_t )( freq >> 16 ),
( uint8_t )( freq >> 8 ), ( uint8_t )( freq >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_RF_FREQUENCY, 0, 0 );
}
sx126x_status_t sx126x_set_pkt_type( const void* context, const sx126x_pkt_type_t pkt_type )
{
const uint8_t buf[SX126X_SIZE_SET_PKT_TYPE] = {
SX126X_SET_PKT_TYPE,
( uint8_t ) pkt_type,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_PKT_TYPE, 0, 0 );
}
sx126x_status_t sx126x_get_pkt_type( const void* context, sx126x_pkt_type_t* pkt_type )
{
const uint8_t buf[SX126X_SIZE_GET_PKT_TYPE] = {
SX126X_GET_PKT_TYPE,
SX126X_NOP,
};
return ( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_PKT_TYPE, ( uint8_t* ) pkt_type, 1 );
}
sx126x_status_t sx126x_set_tx_params( const void* context, const int8_t pwr_in_dbm, const sx126x_ramp_time_t ramp_time )
{
const uint8_t buf[SX126X_SIZE_SET_TX_PARAMS] = {
SX126X_SET_TX_PARAMS,
( uint8_t ) pwr_in_dbm,
( uint8_t ) ramp_time,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_TX_PARAMS, 0, 0 );
}
sx126x_status_t sx126x_set_gfsk_mod_params( const void* context, const sx126x_mod_params_gfsk_t* params )
{
const uint32_t bitrate = ( uint32_t )( 32 * SX126X_XTAL_FREQ / params->br_in_bps );
const uint32_t fdev = sx126x_convert_freq_in_hz_to_pll_step( params->fdev_in_hz );
const uint8_t buf[SX126X_SIZE_SET_MODULATION_PARAMS_GFSK] = {
SX126X_SET_MODULATION_PARAMS, ( uint8_t )( bitrate >> 16 ), ( uint8_t )( bitrate >> 8 ),
( uint8_t )( bitrate >> 0 ), ( uint8_t )( params->pulse_shape ), params->bw_dsb_param,
( uint8_t )( fdev >> 16 ), ( uint8_t )( fdev >> 8 ), ( uint8_t )( fdev >> 0 ),
};
sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_MODULATION_PARAMS_GFSK, 0, 0 );
if( status == SX126X_STATUS_OK )
{
// WORKAROUND - Modulation Quality with 500 kHz LoRa Bandwidth, see DS_SX1261-2_V1.2 datasheet chapter 15.1
status = sx126x_tx_modulation_workaround( context, SX126X_PKT_TYPE_GFSK, ( sx126x_lora_bw_t ) 0 );
// WORKAROUND END
}
return status;
}
sx126x_status_t sx126x_set_bpsk_mod_params( const void* context, const sx126x_mod_params_bpsk_t* params )
{
const uint32_t bitrate = ( uint32_t )( 32 * SX126X_XTAL_FREQ / params->br_in_bps );
const uint8_t buf[SX126X_SIZE_SET_MODULATION_PARAMS_BPSK] = {
SX126X_SET_MODULATION_PARAMS, ( uint8_t )( bitrate >> 16 ), ( uint8_t )( bitrate >> 8 ),
( uint8_t )( bitrate >> 0 ), ( uint8_t )( params->pulse_shape ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_MODULATION_PARAMS_BPSK, 0, 0 );
}
sx126x_status_t sx126x_set_lora_mod_params( const void* context, const sx126x_mod_params_lora_t* params )
{
const uint8_t buf[SX126X_SIZE_SET_MODULATION_PARAMS_LORA] = {
SX126X_SET_MODULATION_PARAMS, ( uint8_t )( params->sf ), ( uint8_t )( params->bw ),
( uint8_t )( params->cr ), params->ldro & 0x01,
};
sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_MODULATION_PARAMS_LORA, 0, 0 );
if( status == SX126X_STATUS_OK )
{
// WORKAROUND - Modulation Quality with 500 kHz LoRa Bandwidth, see datasheet DS_SX1261-2_V1.2 §15.1
status = sx126x_tx_modulation_workaround( context, SX126X_PKT_TYPE_LORA, params->bw );
// WORKAROUND END
}
return status;
}
sx126x_status_t sx126x_set_gfsk_pkt_params( const void* context, const sx126x_pkt_params_gfsk_t* params )
{
const uint8_t buf[SX126X_SIZE_SET_PKT_PARAMS_GFSK] = {
SX126X_SET_PKT_PARAMS,
( uint8_t )( params->preamble_len_in_bits >> 8 ),
( uint8_t )( params->preamble_len_in_bits >> 0 ),
( uint8_t )( params->preamble_detector ),
params->sync_word_len_in_bits,
( uint8_t )( params->address_filtering ),
( uint8_t )( params->header_type ),
params->pld_len_in_bytes,
( uint8_t )( params->crc_type ),
( uint8_t )( params->dc_free ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_PKT_PARAMS_GFSK, 0, 0 );
}
sx126x_status_t sx126x_set_bpsk_pkt_params( const void* context, const sx126x_pkt_params_bpsk_t* params )
{
const uint8_t buf[SX126X_SIZE_SET_PKT_PARAMS_BPSK] = {
SX126X_SET_PKT_PARAMS,
params->pld_len_in_bytes,
};
sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_PKT_PARAMS_BPSK, 0, 0 );
if( status != SX126X_STATUS_OK )
{
return status;
}
const uint8_t buf2[] = {
( uint8_t )( params->ramp_up_delay >> 8 ), ( uint8_t )( params->ramp_up_delay >> 0 ),
( uint8_t )( params->ramp_down_delay >> 8 ), ( uint8_t )( params->ramp_down_delay >> 0 ),
( uint8_t )( params->pld_len_in_bits >> 8 ), ( uint8_t )( params->pld_len_in_bits >> 0 ),
};
return sx126x_write_register( context, 0x00F0, buf2, sizeof( buf2 ) );
}
sx126x_status_t sx126x_set_lora_pkt_params( const void* context, const sx126x_pkt_params_lora_t* params )
{
const uint8_t buf[SX126X_SIZE_SET_PKT_PARAMS_LORA] = {
SX126X_SET_PKT_PARAMS,
( uint8_t )( params->preamble_len_in_symb >> 8 ),
( uint8_t )( params->preamble_len_in_symb >> 0 ),
( uint8_t )( params->header_type ),
params->pld_len_in_bytes,
( uint8_t )( params->crc_is_on ? 1 : 0 ),
( uint8_t )( params->invert_iq_is_on ? 1 : 0 ),
};
sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_PKT_PARAMS_LORA, 0, 0 );
// WORKAROUND - Optimizing the Inverted IQ Operation, see datasheet DS_SX1261-2_V1.2 §15.4
if( status == SX126X_STATUS_OK )
{
uint8_t reg_value = 0;
status = sx126x_read_register( context, SX126X_REG_IQ_POLARITY, ®_value, 1 );
if( status == SX126X_STATUS_OK )
{
if( params->invert_iq_is_on == true )
{
reg_value &= ~( 1 << 2 ); // Bit 2 set to 0 when using inverted IQ polarity
}
else
{
reg_value |= ( 1 << 2 ); // Bit 2 set to 1 when using standard IQ polarity
}
status = sx126x_write_register( context, SX126X_REG_IQ_POLARITY, ®_value, 1 );
}
}
// WORKAROUND END
return status;
}
sx126x_status_t sx126x_set_gfsk_pkt_address( const void* context, const uint8_t node_address,
const uint8_t broadcast_address )
{
const uint8_t addresses[2] = { node_address, broadcast_address };
return sx126x_write_register( context, SX126X_REG_GFSK_NODE_ADDRESS, addresses, 2 );
}
sx126x_status_t sx126x_set_cad_params( const void* context, const sx126x_cad_params_t* params )
{
const uint8_t buf[SX126X_SIZE_SET_CAD_PARAMS] = {
SX126X_SET_CAD_PARAMS,
( uint8_t ) params->cad_symb_nb,
params->cad_detect_peak,
params->cad_detect_min,
( uint8_t ) params->cad_exit_mode,
( uint8_t )( params->cad_timeout >> 16 ),
( uint8_t )( params->cad_timeout >> 8 ),
( uint8_t )( params->cad_timeout >> 0 ),
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_CAD_PARAMS, 0, 0 );
}
sx126x_status_t sx126x_set_buffer_base_address( const void* context, const uint8_t tx_base_address,
const uint8_t rx_base_address )
{
const uint8_t buf[SX126X_SIZE_SET_BUFFER_BASE_ADDRESS] = {
SX126X_SET_BUFFER_BASE_ADDRESS,
tx_base_address,
rx_base_address,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_BUFFER_BASE_ADDRESS, 0, 0 );
}
sx126x_status_t sx126x_set_lora_symb_nb_timeout( const void* context, const uint8_t nb_of_symbs )
{
uint8_t exp = 0;
uint8_t mant =
( ( ( nb_of_symbs > SX126X_MAX_LORA_SYMB_NUM_TIMEOUT ) ? SX126X_MAX_LORA_SYMB_NUM_TIMEOUT : nb_of_symbs ) +
1 ) >>
1;
while( mant > 31 )
{
mant = ( mant + 3 ) >> 2;
exp++;
}
const uint8_t buf[SX126X_SIZE_SET_LORA_SYMB_NUM_TIMEOUT] = {
SX126X_SET_LORA_SYMB_NUM_TIMEOUT,
mant << ( 2 * exp + 1 ),
};
sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_SET_LORA_SYMB_NUM_TIMEOUT, 0, 0 );
if( ( status == SX126X_STATUS_OK ) && ( nb_of_symbs > 0 ) )
{
uint8_t reg = exp + ( mant << 3 );
status = sx126x_write_register( context, SX126X_REG_LR_SYNCH_TIMEOUT, ®, 1 );
}
return status;
}
//
// Communication Status Information
//
sx126x_status_t sx126x_get_status( const void* context, sx126x_chip_status_t* radio_status )
{
const uint8_t buf[SX126X_SIZE_GET_STATUS] = {
SX126X_GET_STATUS,
};
uint8_t status_local = 0;
const sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_STATUS, &status_local, 1 );
if( status == SX126X_STATUS_OK )
{
radio_status->cmd_status =
( sx126x_cmd_status_t )( ( status_local & SX126X_CMD_STATUS_MASK ) >> SX126X_CMD_STATUS_POS );
radio_status->chip_mode =
( sx126x_chip_modes_t )( ( status_local & SX126X_CHIP_MODES_MASK ) >> SX126X_CHIP_MODES_POS );
}
return status;
}
sx126x_status_t sx126x_get_rx_buffer_status( const void* context, sx126x_rx_buffer_status_t* rx_buffer_status )
{
const uint8_t buf[SX126X_SIZE_GET_RX_BUFFER_STATUS] = {
SX126X_GET_RX_BUFFER_STATUS,
SX126X_NOP,
};
uint8_t status_local[sizeof( sx126x_rx_buffer_status_t )] = { 0x00 };
const sx126x_status_t status = ( sx126x_status_t ) sx126x_hal_read(
context, buf, SX126X_SIZE_GET_RX_BUFFER_STATUS, status_local, sizeof( sx126x_rx_buffer_status_t ) );
if( status == SX126X_STATUS_OK )
{
rx_buffer_status->pld_len_in_bytes = status_local[0];
rx_buffer_status->buffer_start_pointer = status_local[1];
}
return status;
}
sx126x_status_t sx126x_get_gfsk_pkt_status( const void* context, sx126x_pkt_status_gfsk_t* pkt_status )
{
const uint8_t buf[SX126X_SIZE_GET_PKT_STATUS] = {
SX126X_GET_PKT_STATUS,
SX126X_NOP,
};
uint8_t pkt_status_local[3] = { 0x00 };
const sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_PKT_STATUS, pkt_status_local, 3 );
if( status == SX126X_STATUS_OK )
{
pkt_status->rx_status.pkt_sent =
( ( pkt_status_local[0] & SX126X_GFSK_RX_STATUS_PKT_SENT_MASK ) != 0 ) ? true : false;
pkt_status->rx_status.pkt_received =
( ( pkt_status_local[0] & SX126X_GFSK_RX_STATUS_PKT_RECEIVED_MASK ) != 0 ) ? true : false;
pkt_status->rx_status.abort_error =
( ( pkt_status_local[0] & SX126X_GFSK_RX_STATUS_ABORT_ERROR_MASK ) != 0 ) ? true : false;
pkt_status->rx_status.length_error =
( ( pkt_status_local[0] & SX126X_GFSK_RX_STATUS_LENGTH_ERROR_MASK ) != 0 ) ? true : false;
pkt_status->rx_status.crc_error =
( ( pkt_status_local[0] & SX126X_GFSK_RX_STATUS_CRC_ERROR_MASK ) != 0 ) ? true : false;
pkt_status->rx_status.adrs_error =
( ( pkt_status_local[0] & SX126X_GFSK_RX_STATUS_ADRS_ERROR_MASK ) != 0 ) ? true : false;
pkt_status->rssi_sync = ( int8_t )( -pkt_status_local[1] >> 1 );
pkt_status->rssi_avg = ( int8_t )( -pkt_status_local[2] >> 1 );
}
return status;
}
sx126x_status_t sx126x_get_lora_pkt_status( const void* context, sx126x_pkt_status_lora_t* pkt_status )
{
const uint8_t buf[SX126X_SIZE_GET_PKT_STATUS] = {
SX126X_GET_PKT_STATUS,
SX126X_NOP,
};
uint8_t pkt_status_local[sizeof( sx126x_pkt_status_lora_t )] = { 0x00 };
const sx126x_status_t status = ( sx126x_status_t ) sx126x_hal_read(
context, buf, SX126X_SIZE_GET_PKT_STATUS, pkt_status_local, sizeof( sx126x_pkt_status_lora_t ) );
if( status == SX126X_STATUS_OK )
{
pkt_status->rssi_pkt_in_dbm = ( int8_t )( -pkt_status_local[0] >> 1 );
pkt_status->snr_pkt_in_db = ( ( ( int8_t ) pkt_status_local[1] ) + 2 ) >> 2;
pkt_status->signal_rssi_pkt_in_dbm = ( int8_t )( -pkt_status_local[2] >> 1 );
}
return status;
}
sx126x_status_t sx126x_get_rssi_inst( const void* context, int16_t* rssi_in_dbm )
{
const uint8_t buf[SX126X_SIZE_GET_RSSI_INST] = {
SX126X_GET_RSSI_INST,
SX126X_NOP,
};
uint8_t rssi_local = 0x00;
const sx126x_status_t status =
( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_RSSI_INST, &rssi_local, 1 );
if( status == SX126X_STATUS_OK )
{
*rssi_in_dbm = ( int8_t )( -rssi_local >> 1 );
}
return status;
}
sx126x_status_t sx126x_get_gfsk_stats( const void* context, sx126x_stats_gfsk_t* stats )
{
const uint8_t buf[SX126X_SIZE_GET_STATS] = {
SX126X_GET_STATS,
SX126X_NOP,
};
uint8_t stats_local[sizeof( sx126x_stats_gfsk_t )] = { 0 };
const sx126x_status_t status = ( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_STATS,
stats_local, sizeof( sx126x_stats_gfsk_t ) );
if( status == SX126X_STATUS_OK )
{
stats->nb_pkt_received = ( ( uint16_t ) stats_local[0] << 8 ) + ( uint16_t ) stats_local[1];
stats->nb_pkt_crc_error = ( ( uint16_t ) stats_local[2] << 8 ) + ( uint16_t ) stats_local[3];
stats->nb_pkt_len_error = ( ( uint16_t ) stats_local[4] << 8 ) + ( uint16_t ) stats_local[5];
}
return status;
}
sx126x_status_t sx126x_get_lora_stats( const void* context, sx126x_stats_lora_t* stats )
{
const uint8_t buf[SX126X_SIZE_GET_STATS] = {
SX126X_GET_STATS,
SX126X_NOP,
};
uint8_t stats_local[sizeof( sx126x_stats_lora_t )] = { 0 };
const sx126x_status_t status = ( sx126x_status_t ) sx126x_hal_read( context, buf, SX126X_SIZE_GET_STATS,
stats_local, sizeof( sx126x_stats_lora_t ) );
if( status == SX126X_STATUS_OK )
{
stats->nb_pkt_received = ( ( uint16_t ) stats_local[0] << 8 ) + ( uint16_t ) stats_local[1];
stats->nb_pkt_crc_error = ( ( uint16_t ) stats_local[2] << 8 ) + ( uint16_t ) stats_local[3];
stats->nb_pkt_header_error = ( ( uint16_t ) stats_local[4] << 8 ) + ( uint16_t ) stats_local[5];
}
return status;
}
sx126x_status_t sx126x_reset_stats( const void* context )
{
const uint8_t buf[SX126X_SIZE_RESET_STATS] = {
SX126X_RESET_STATS, SX126X_NOP, SX126X_NOP, SX126X_NOP, SX126X_NOP, SX126X_NOP, SX126X_NOP,
};
return ( sx126x_status_t ) sx126x_hal_write( context, buf, SX126X_SIZE_RESET_STATS, 0, 0 );