forked from novag/SlimLoRa
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSlimLoRa.cpp
More file actions
2567 lines (2161 loc) · 79.4 KB
/
SlimLoRa.cpp
File metadata and controls
2567 lines (2161 loc) · 79.4 KB
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
/*
* Copyright (c) 2021-2026 Michales Michaloudes
* Copyright (c) 2018-2021 Hendrik Hagendorn
* Copyright (c) 2015-2016 Ideetron B.V. - AES routines
*
* 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 <http:// www.gnu.org/licenses/>.
*/
#include <Arduino.h>
#include <SPI.h>
#include "SlimLoRa_atomic.h"
#include "SlimLoRa.h"
#include "SlimLoRaTimers.h"
#if ARDUINO_EEPROM == 2
ExternalEEPROM EEPROM;
#endif
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
#include <avr/pgmspace.h>
#endif
#if LORAWAN_OTAA_ENABLED
extern const uint8_t DevEUI[8];
extern const uint8_t JoinEUI[8];
#if LORAWAN_V1_1_ENABLED
extern const uint8_t NwkKey[16];
#endif // LORAWAN_V1_1_ENABLED
extern const uint8_t AppKey[16];
#else
extern const uint8_t NwkSKey[16];
extern const uint8_t AppSKey[16];
extern const uint8_t DevAddr[4];
#endif // LORAWAN_OTAA_ENABLED
#if defined CATCH_DIVIDER && defined (__AVR__)
volatile uint8_t clockShift; // used to drift timings according to clock frequency if changed via software.
#endif
#if NON_BLOCKING
volatile bool rxTimerTriggered = false;
#endif
#if DEBUG_RXSYMBOLS >= 1
int32_t microsStart;
#endif
static SPISettings RFM_spisettings = SPISettings(4000000, MSBFIRST, SPI_MODE0);
#if ARDUINO_EEPROM == 0
/**
* AVR style EEPROM variables
* https://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html
*/
uint16_t eeprom_lw_tx_frame_counter EEMEM = 1;
uint16_t eeprom_lw_rx_frame_counter EEMEM = 0;
uint8_t eeprom_lw_rx1_data_rate_offset EEMEM = 0;
uint8_t eeprom_lw_rx2_data_rate EEMEM = 0;
uint8_t eeprom_lw_rx1_delay EEMEM = 0;
uint8_t eeprom_lw_NbTrans EEMEM = 0;
uint16_t eeprom_lw_ChMask EEMEM = 0;
uint8_t eeprom_lw_down_packet[SLIMLORA_DOWNLINK_PAYLOAD_SIZE];
uint8_t eeprom_lw_down_port;
#endif
#ifdef EU863
// Frequency band for europe
// TODO remove PROGMEM code to enable CFlist and New Channel Req
// First 3 channels are enabled by default
// LoRaWAN spec. p. 24 of regional parameters 1.0.3
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint8_t PROGMEM SlimLoRa::kFrequencyTable[9][3] = {
#else
const uint8_t SlimLoRa::kFrequencyTable[9][3] = {
#endif
{ 0xD9, 0x06, 0x8B }, // Channel 0 868.100 MHz / 61.035 Hz = 14222987 = 0xD9068B
{ 0xD9, 0x13, 0x58 }, // Channel 1 868.300 MHz / 61.035 Hz = 14226264 = 0xD91358
{ 0xD9, 0x20, 0x24 }, // Channel 2 868.500 MHz / 61.035 Hz = 14229540 = 0xD92024
{ 0xD8, 0xC6, 0x8B }, // Channel 3 867.100 MHz / 61.035 Hz = 14206603 = 0xD8C68B
{ 0xD8, 0xD3, 0x58 }, // Channel 4 867.300 MHz / 61.035 Hz = 14209880 = 0xD8D358
{ 0xD8, 0xE0, 0x24 }, // Channel 5 867.500 MHz / 61.035 Hz = 14213156 = 0xD8E024
{ 0xD8, 0xEC, 0xF1 }, // Channel 6 867.700 MHz / 61.035 Hz = 14216433 = 0xD8ECF1
{ 0xD8, 0xF9, 0xBE }, // Channel 7 867.900 MHz / 61.035 Hz = 14219710 = 0xD8F9BE
{ 0xD9, 0x61, 0xBE } // Downlink 869.525 MHz / 61.035 Hz = 14246334 = 0xD961BE
};
#endif
#ifdef AU915 // According to Regional Parameters of LoRaWAN 1.0.3 spec page: 37 line 850 there is 64 channels starting from 915.200 MHz to 927.800 MHz with 200MHz steps.
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint8_t PROGMEM SlimLoRa::kFrequencyTable[9][3] = {
#else
const uint8_t SlimLoRa::kFrequencyTable[9][3] = {
#endif
{0xE5, 0x33, 0x5A}, // Channel 0 916.800 MHz / 61.035 Hz = 15020890 = 0xE5335A
{0xE5, 0x40, 0x26}, // Channel 1 917.000 MHz / 61.035 Hz = 15024166 = 0xE54026
{0xE5, 0x4C, 0xF3}, // Channel 2 917.200 MHz / 61.035 Hz = 15027443 = 0xE54CF3
{0xE5, 0x59, 0xC0}, // Channel 3 917.400 MHz / 61.035 Hz = 15030720 = 0xE559C0
{0xE5, 0x66, 0x8D}, // Channel 4 917.600 MHz / 61.035 Hz = 15033997 = 0xE5668D
{0xE5, 0x73, 0x5A}, // Channel 5 917.800 MHz / 61.035 Hz = 15037274 = 0xE5735A
{0xE5, 0x80, 0x27}, // Channel 6 918.000 MHz / 61.035 Hz = 15040551 = 0xE58027
{0xE5, 0x8C, 0xF3}, // Channel 7 918.200 MHz / 61.035 Hz = 15043827 = 0xE58CF3
{0xE5, 0x8C, 0xF3} // Downlink ??? MHz / 61.035 Hz = 15043827 = 0xE58CF3 // TODO 8 channels LoRa BW 500kHz, DR8 to DR13 starting at 923.300 MHz to 927.500 MHz, steps: 600KHz.
};
#endif
// BEELAN https://github.com/ElectronicCats/Beelan-LoRaWAN/blob/82da458bf8f98e8bf0e4c4eb5b7dd1b66787d2ba/src/arduino-rfm/RFM95.cpp#L38
#ifdef US902 // page 21 line 435: 64 chanels starting 902.300 MHz to 914.900 MHz steps: 200KHz. DR0 (SF10) to DR3 (SF7) only!
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint8_t PROGMEM SlimLoRa::kFrequencyTable[9][3] = {
#else
const uint8_t SlimLoRa::kFrequencyTable[9][3] = {
#endif
{0xE1, 0xF9, 0xC0}, // Channel 0 903.900 MHz / 61.035 Hz = 14809536 = 0xE1F9C0
{0xE2, 0x06, 0x8C}, // Channel 1 904.100 MHz / 61.035 Hz = 14812812 = 0xE2068C
{0xE2, 0x13, 0x59}, // Channel 2 904.300 MHz / 61.035 Hz = 14816089 = 0xE21359
{0xE2, 0x20, 0x26}, // Channel 3 904.500 MHz / 61.035 Hz = 14819366 = 0xE22026
{0xE2, 0x2C, 0xF3}, // Channel 4 904.700 MHz / 61.035 Hz = 14822643 = 0xE22CF3
{0xE2, 0x39, 0xC0}, // Channel 5 904.900 MHz / 61.035 Hz = 14825920 = 0xE239C0
{0xE2, 0x46, 0x8C}, // Channel 6 905.100 MHz / 61.035 Hz = 14829196 = 0xE2468C
{0xE2, 0x53, 0x59}, // Channel 7 905.300 MHz / 61.035 Hz = 14832473 = 0xE25359
{0xE5, 0x8C, 0xF3} // Downlink RX2 923.300 MHz / 61.035 Hz = 14832473 = 0xE25359 page 25 line 556
};
// TODO if more than 8 channels: RX1 page 25 line 554 'Downlink channel is modulo 8 upstream'. In other words: up 0 /down 0, up 7 /down 7, up 8 /down 0, up 15 /down 7.
// in bash words: for i in `seq 0 63`; do echo -n "up: $i RX1: "; expr $i % 8; done
#endif
#ifdef AS920
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint8_t PROGMEM SlimLoRa::kFrequencyTable[9][3] = {
#else
const uint8_t SlimLoRa::kFrequencyTable[9][3] = {
#endif
{0xE6, 0xCC, 0xF4}, // Channel 0 868.100 MHz / 61.035 Hz = 15125748 = 0xE6CCF4
{0xE6, 0xD9, 0xC0}, // Channel 1 868.300 MHz / 61.035 Hz = 15129024 = 0xE6D9C0
{0xE6, 0x8C, 0xF3}, // Channel 2 868.500 MHz / 61.035 Hz = 15109363 = 0xE68CF3
{0xE6, 0x99, 0xC0}, // Channel 3 867.100 MHz / 61.035 Hz = 15112640 = 0xE699C0
{0xE6, 0xA6, 0x8D}, // Channel 4 867.300 MHz / 61.035 Hz = 15115917 = 0xE6A68D
{0xE6, 0xB3, 0x5A}, // Channel 5 867.500 MHz / 61.035 Hz = 15119194 = 0xE6B35A
{0xE6, 0xC0, 0x27}, // Channel 6 867.700 MHz / 61.035 Hz = 15122471 = 0xE6C027
{0xE6, 0x80, 0x27}, // Channel 7 867.900 MHz / 61.035 Hz = 15106087 = 0xE68027
{0xE5, 0x8C, 0xF3} // Downlink ??? MHz / 61.035 Hz = 15043827 = 0xE58CF3 // TODO
};
#endif
// TODO for other regions.
// EVAL
//#ifdef EU863
// Data rate
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint8_t PROGMEM SlimLoRa::kDataRateTable[7][3] = {
#else
const uint8_t SlimLoRa::kDataRateTable[7][3] = {
#endif
// bw sf agc
{ 0x72, 0xC4, 0x0C }, // SF12BW125
{ 0x72, 0xB4, 0x0C }, // SF11BW125
{ 0x72, 0xA4, 0x04 }, // SF10BW125
{ 0x72, 0x94, 0x04 }, // SF9BW125
{ 0x72, 0x84, 0x04 }, // SF8BW125
{ 0x72, 0x74, 0x04 }, // SF7BW125
{ 0x82, 0x74, 0x04 } // SF7BW250
};
//#endif // EU863
// Half symbol times
// TODO for other regions
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint32_t PROGMEM SlimLoRa::kDRMicrosPerHalfSymbol[7] = {
#else
const uint32_t SlimLoRa::kDRMicrosPerHalfSymbol[7] = {
#endif
(((uint32_t)128 << 7) * MICROS_PER_SECOND + 500000) / 1000000, // SF12BW125 (uint32_t) to protect from buffer overflow
(((uint32_t)128 << 6) * MICROS_PER_SECOND + 500000) / 1000000, // SF11BW125
(((uint32_t)128 << 5) * MICROS_PER_SECOND + 500000) / 1000000, // SF10BW125
((128 << 4) * MICROS_PER_SECOND + 500000) / 1000000, // SF9BW125
((128 << 3) * MICROS_PER_SECOND + 500000) / 1000000, // SF8BW125
((128 << 2) * MICROS_PER_SECOND + 500000) / 1000000, // SF7BW125
((128 << 1) * MICROS_PER_SECOND + 500000) / 1000000 // SF7BW250
};
// S table for AES encryption
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
const uint8_t PROGMEM SlimLoRa::kSTable[16][16] = {
#else
const uint8_t SlimLoRa::kSTable[16][16] = {
#endif
{0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76},
{0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0},
{0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8, 0x31, 0x15},
{0x04, 0xC7, 0x23, 0xC3, 0x18, 0x96, 0x05, 0x9A, 0x07, 0x12, 0x80, 0xE2, 0xEB, 0x27, 0xB2, 0x75},
{0x09, 0x83, 0x2C, 0x1A, 0x1B, 0x6E, 0x5A, 0xA0, 0x52, 0x3B, 0xD6, 0xB3, 0x29, 0xE3, 0x2F, 0x84},
{0x53, 0xD1, 0x00, 0xED, 0x20, 0xFC, 0xB1, 0x5B, 0x6A, 0xCB, 0xBE, 0x39, 0x4A, 0x4C, 0x58, 0xCF},
{0xD0, 0xEF, 0xAA, 0xFB, 0x43, 0x4D, 0x33, 0x85, 0x45, 0xF9, 0x02, 0x7F, 0x50, 0x3C, 0x9F, 0xA8},
{0x51, 0xA3, 0x40, 0x8F, 0x92, 0x9D, 0x38, 0xF5, 0xBC, 0xB6, 0xDA, 0x21, 0x10, 0xFF, 0xF3, 0xD2},
{0xCD, 0x0C, 0x13, 0xEC, 0x5F, 0x97, 0x44, 0x17, 0xC4, 0xA7, 0x7E, 0x3D, 0x64, 0x5D, 0x19, 0x73},
{0x60, 0x81, 0x4F, 0xDC, 0x22, 0x2A, 0x90, 0x88, 0x46, 0xEE, 0xB8, 0x14, 0xDE, 0x5E, 0x0B, 0xDB},
{0xE0, 0x32, 0x3A, 0x0A, 0x49, 0x06, 0x24, 0x5C, 0xC2, 0xD3, 0xAC, 0x62, 0x91, 0x95, 0xE4, 0x79},
{0xE7, 0xC8, 0x37, 0x6D, 0x8D, 0xD5, 0x4E, 0xA9, 0x6C, 0x56, 0xF4, 0xEA, 0x65, 0x7A, 0xAE, 0x08},
{0xBA, 0x78, 0x25, 0x2E, 0x1C, 0xA6, 0xB4, 0xC6, 0xE8, 0xDD, 0x74, 0x1F, 0x4B, 0xBD, 0x8B, 0x8A},
{0x70, 0x3E, 0xB5, 0x66, 0x48, 0x03, 0xF6, 0x0E, 0x61, 0x35, 0x57, 0xB9, 0x86, 0xC1, 0x1D, 0x9E},
{0xE1, 0xF8, 0x98, 0x11, 0x69, 0xD9, 0x8E, 0x94, 0x9B, 0x1E, 0x87, 0xE9, 0xCE, 0x55, 0x28, 0xDF},
{0x8C, 0xA1, 0x89, 0x0D, 0xBF, 0xE6, 0x42, 0x68, 0x41, 0x99, 0x2D, 0x0F, 0xB0, 0x54, 0xBB, 0x16}
};
// TODO add pins for IRQ.
// TODO add EEPROM_OFFSET
SlimLoRa::SlimLoRa(uint8_t pin_nss) {
pin_nss_ = pin_nss;
}
#if COUNT_TX_DURATION == 1
// return the Duration of transmission in ms.
uint16_t SlimLoRa::GetTXms(){
return slimTotalTXms;
}
// After RX done, calculate the TX ms duration here to not break RX timing windows.
// Also store the lastTXms duration (current transmission)
void SlimLoRa::CalculateTXms(){
slimLastTXms = slimEndTXtimestamp - slimStartTXtimestamp;
slimTotalTXms += slimLastTXms;
}
// erase the duration of TXms
void SlimLoRa::ZeroTXms(){
slimTotalTXms = 0;
slimLastTXms = 0;
}
#endif // COUNT_TX_DURATION == 1
void SlimLoRa::printHex(uint8_t *value, uint8_t len){
Serial.print(F("MSB: 0x"));
for (int8_t i = 0; i < len; i++ ) {
if (value[i] == 0x0 ) { Serial.print(F("00")); continue; }
if (value[i] <= 0xF ) { Serial.print(F("0")); Serial.print(value[i], HEX);continue; }
Serial.print(value[i], HEX);
}
Serial.print(F(", LSB: 0x"));
for (int8_t i = len - 1; i >= 0; i-- ) {
if (value[i] == 0x0 ) { Serial.print(F("00")); continue; }
if (value[i] <= 0xF ) { Serial.print(F("0")); Serial.print(value[i], HEX); continue; }
Serial.print(value[i], HEX);
}
}
#if DEBUG_SLIM >= 1
void SlimLoRa::printDownlink(){
Serial.print(F("\nPort Down\t: ")); Serial.print(downPort);
Serial.print(F("\nPacket Length\t: ")); Serial.print(packet_length);
Serial.print(F("\nPayload Length\t: "));Serial.print(payload_length);
Serial.print(F("\nSNR 8bit\t: ")); Serial.print(last_packet_snrB);
Serial.print(F("\nSNR 8bit / 4\t: ")); Serial.print(last_packet_snr_);
Serial.flush();
}
#if LORAWAN_OTAA_ENABLED == 0
void printDevAddr(){
Serial.print(F("\nMSB: 0x"));
for (int8_t i = 0; i < 4; i++ ) {
if (DevAddr[i] == 0x0 ) { Serial.print(F("00")); continue; }
if (DevAddr[i] <= 0xF ) { Serial.print(F("0")); Serial.print(DevAddr[i], HEX);continue; }
Serial.print(DevAddr[i], HEX);
}
Serial.print(F("\nLSB: 0x"));
for (int8_t i = 4 - 1; i >= 0; i-- ) {
if (DevAddr[i] == 0x0 ) { Serial.print(F("00")); continue; }
if (DevAddr[i] <= 0xF ) { Serial.print(F("0")); Serial.print(DevAddr[i], HEX); continue; }
Serial.print(DevAddr[i], HEX);
}
Serial.println();
}
#endif // LORAWA_OTAA_ENABLED == 0
// Mark data in Serial log that must be kept secret.
void printNOWEB(){
Serial.print(F("\nNOWEB "));
}
#endif
#if ARDUINO_EEPROM >= 1
/**
* Function to write array to eeprom
*
* @param eepromAddr eeprom address.
* @param arrayData Array to store
* @param size size of array
*
*/
void SlimLoRa::setArrayEEPROM(uint16_t eepromAddr, uint8_t *arrayData, uint8_t size) {
for ( uint8_t i = 0; i < size; i++ ) {
#if ARDUINO_EEPROM == 2
EEPROM.putChanged(eepromAddr + i, arrayData[i]);
#else
EEPROM.update(eepromAddr + i, arrayData[i]);
#endif
#if (DEBUG_SLIM & 0x08) == 0x08
if ( i == 0 ) {
Serial.print(F("WRITE EEPROM Address: 0x"));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
} else {
Serial.print(F(", "));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
}
#endif
}
#if (DEBUG_SLIM & 0x08) == 0x08
Serial.print(F("WRITE: "));printHex(arrayData, size);
#endif
}
/**
* Function to read array to eeprom
*
* @param eepromAddr eeprom address.
* @param arrayData Array to read
* @param size size of array
*
*/
void SlimLoRa::getArrayEEPROM(uint16_t eepromAddr, uint8_t *arrayData, uint8_t size) {
for ( uint8_t i = 0; i < size; i++ ) {
arrayData[i] = EEPROM.read(eepromAddr + i);
#if DEBUG_SLIM >= 3
if ( i == 0 ) {
Serial.print(F("\nREAD EEPROM Address->Value: 0x"));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
} else {
Serial.print(F(", "));Serial.print(eepromAddr + i, HEX);Serial.print(F("->0x"));Serial.print(arrayData[i], HEX);
}
#endif
}
#if DEBUG_SLIM >= 3
Serial.print(F("\nread- "));printHex(arrayData, size);
#endif
}
#endif // ARDUINO_EEPROM >= 1
#if DEBUG_SLIM >= 1 && LORAWAN_KEEP_SESSION == 1
void SlimLoRa::printMAC(){
#if LORAWAN_OTAA_ENABLED
uint8_t dev_addr[4], app_s_key[16], nwk_s_key[16];
Serial.print(F("\n\nEEPROM Addr: "));Serial.print(EEPROM_OFFSET);
Serial.print(F("\tNet ID: "));
Serial.print(EEPROM.read(EEPROM_NETID), HEX);
Serial.print(F("-"));
Serial.print(EEPROM.read(EEPROM_NETID + 1), HEX);
Serial.print(F("-"));
Serial.print(EEPROM.read(EEPROM_NETID + 2), HEX);
Serial.print(F("\nMAC Join: "));Serial.print(GetHasJoined());
Serial.print(F("\ndevNonce DEC\t\t: "));;Serial.print(GetDevNonce() >> 8);Serial.print(GetDevNonce());
Serial.print(F("\njoinDevNonce DEC\t: "));Serial.print(GetJoinNonce() >> 24);Serial.print(GetJoinNonce() >> 16);Serial.print(GetJoinNonce() >> 8);Serial.println(GetJoinNonce());
GetDevAddr(dev_addr);Serial.print(F("\nDevAddr\t: "));printHex(dev_addr, 4);
GetNwkSEncKey(nwk_s_key);
//GetFNwkSIntKey(); // not used
GetAppSKey(app_s_key);
#if DEBUG_SLIM >= 2
Serial.print(F("\nJoinEUI"));
printHex(JoinEUI,8);
Serial.print(F("\nDevEUI"));
printHex(DevEUI,8);
Serial.print(F("\nAppKey"));
printHex(AppKey,16);
#endif
#else // ABP
Serial.print(F("\nABP DevAddr: "));printDevAddr();
#endif // LORAWAN_OTAA_ENABLED
Serial.print(F("\nTx#\t\t: "));Serial.print(GetTxFrameCounter());Serial.print(F("\tRAM: "));Serial.println(tx_frame_counter_);
Serial.print(F("Rx#\t\t: "));Serial.print(GetRxFrameCounter());Serial.print(F("\tRAM: "));Serial.println(rx_frame_counter_);
Serial.print(F("ACK\t: "));Serial.print(ack_);
Serial.print(F("\nNbTrans_counter\t: "));Serial.print(NbTrans_counter);
Serial.print(F("\nNbTrans\t\t: "));Serial.print(NbTrans);
Serial.print(F("\nChMask\t\t: "));Serial.print(ChMask);
GetChMask();
Serial.print(F("\nRX1 delay\t: "));Serial.print(GetRx1Delay());Serial.print(F(", System Setting: "));Serial.print(LORAWAN_JOIN_ACCEPT_DELAY1_MICROS / 1000000);Serial.print(F("s, RX2: "));Serial.print(LORAWAN_JOIN_ACCEPT_DELAY2_MICROS / 1000000);Serial.println("s, ");
Serial.print(F("Rx1 DR\t\t: "));Serial.println(data_rate_);
Serial.print(F("Rx1 DR offset\t: "));Serial.println(GetRx1DataRateOffset());
Serial.print(F("Rx2 DR RAM\t: "));Serial.println(rx2_data_rate_);
Serial.print(F("Rx2 DR EEPROM\t: "));Serial.println(GetRx2DataRate());
Serial.print(F("ADR_ACK_cnt\t: "));Serial.print(adr_ack_limit_counter_);
Serial.print(F("\nSlimLoRa drift\t: "));Serial.print(SLIMLORA_DRIFT);
Serial.print(F("\ndelay RX1\t: "));Serial.print(CalculateRxDelay(data_rate_, GetRx1Delay() * MICROS_PER_SECOND));
Serial.print(F("\trx_symbols_: "));Serial.print(rx_symbols_);
Serial.print(F("\trx_symbols_ MSB: "));Serial.print(RfmRead(RFM_REG_MODEM_CONFIG_2) & 0b11);
Serial.print(F("\ndelay RX2\t: "));Serial.print(CalculateRxDelay(rx2_data_rate_, GetRx1Delay() * MICROS_PER_SECOND + MICROS_PER_SECOND));
Serial.print(F("\trx_symbols_: "));Serial.print(rx_symbols_);
Serial.print(F("\trx_symbols_ MSB: "));Serial.print(RfmRead(RFM_REG_MODEM_CONFIG_2) & 0b11);
Serial.print(F("\ndelay Join RX1\t: "));Serial.print(CalculateRxDelay(data_rate_, LORAWAN_JOIN_ACCEPT_DELAY1_MICROS));
Serial.print(F("\trx_symbols_: "));Serial.print(rx_symbols_);
Serial.print(F("\trx_symbols_ MSB: "));Serial.print(RfmRead(RFM_REG_MODEM_CONFIG_2) & 0b11);
Serial.print(F("\ndelay Join RX2\t: "));Serial.print(CalculateRxDelay(rx2_data_rate_, LORAWAN_JOIN_ACCEPT_DELAY2_MICROS));
Serial.print(F("\trx_symbols_: "));Serial.print(rx_symbols_);
Serial.print(F("\trx_symbols_ MSB: "));Serial.println(RfmRead(RFM_REG_MODEM_CONFIG_2) & 0b11);
//Serial.print(F("\nLNA REG : "));Serial.println(RfmRead(RFM_REG_LNA));
//Serial.print(F("\nCONFIG_3: "));Serial.println(RfmRead(RFM_REG_MODEM_CONFIG_3));
}
#endif // DEBUG_SLIM
#if DEBUG_SLIM >= 1 && LORAWAN_KEEP_SESSION == 0
void SlimLoRa::printMAC(){
uint8_t dev_addr[4], app_s_key[16], nwk_s_key[16];
Serial.print(F("\nMAC Join: "));Serial.print(has_joined_);
Serial.print(F("\nTx#\t\t: "));Serial.println(tx_frame_counter_);
Serial.print(F("Rx#\t\t: "));Serial.println(rx_frame_counter_);
}
#endif
void SlimLoRa::Begin() {
uint8_t detect_optimize;
SPI.begin();
#if defined CATCH_DIVIDER && defined (__AVR__)
clockShift = clock_prescale_get();
#endif
pinMode(pin_nss_, OUTPUT);
// Sleep
RfmWrite(RFM_REG_OP_MODE, 0x00);
// LoRa mode
RfmWrite(RFM_REG_OP_MODE, 0x80);
// +16 dBm output power. Upper limit for EU868
SetPower(16);
// Preamble length: 8 symbols
// 0x0008 + 4 = 12
RfmWrite(RFM_REG_PREAMBLE_MSB, 0x00);
RfmWrite(RFM_REG_PREAMBLE_LSB, 0x08);
// LoRa sync word
RfmWrite(RFM_REG_SYNC_WORD, 0x34);
// Default 60. Does not harm
// SetCurrentLimit(60);
// Errata Note - 2.3 Receiver Spurious Reception
detect_optimize = RfmRead(RFM_REG_DETECT_OPTIMIZE);
RfmWrite(RFM_REG_DETECT_OPTIMIZE, (detect_optimize & 0x78) | 0x03);
RfmWrite(RFM_REG_IF_FREQ_1, 0x00);
RfmWrite(RFM_REG_IF_FREQ_2, 0x40);
// FIFO pointers
RfmWrite(RFM_REG_FIFO_TX_BASE_ADDR, 0x80);
RfmWrite(RFM_REG_FIFO_RX_BASE_ADDR, 0x00);
// Init MAC state
#if LORAWAN_KEEP_SESSION && LORAWAN_OTAA_ENABLED
has_joined_ = GetHasJoined();
GetChMask();
GetNbTrans();
#endif
NbTrans_counter = NbTrans;
#if LORAWAN_KEEP_SESSION
tx_frame_counter_ = GetTxFrameCounter();
rx_frame_counter_ = GetRxFrameCounter();
rx2_data_rate_ = GetRx2DataRate();
rx1_delay_micros_ = GetRx1Delay() * MICROS_PER_SECOND;
rx1_data_rate_offset_ = GetRx1DataRateOffset();
#endif
#if DEBUG_SLIM >= 1
Serial.println(F("\nInit of RFM done."));
printMAC();
#endif
}
// This function is only usefull to gain some mA during setup on application.
// It offers a useless jump in library. Don't used if you need some bytes of flash memory
void SlimLoRa::sleep() {
RfmWrite(RFM_REG_OP_MODE, 0x00);
}
/* Copied from RadioLib project
\brief Sets current limit for over current protection at transmitter amplifier. Allowed values range from 45 to 120 mA in 5 mA steps and 120 to 240 mA in 10 mA steps.
\param currentLimit Current limit to be set (in mA).
*/
void SlimLoRa::SetCurrentLimit(uint8_t currentLimit) {
// check allowed range
if(!(((currentLimit >= 45) && (currentLimit <= 240)) || (currentLimit == 0))) {
currentLimit = 60; // default value. Taken from RadioLib
}
// Switch RFM to standby
RfmWrite(RFM_REG_OP_MODE, 0x81);
// set OCP limit
uint8_t raw;
if(currentLimit == 0) {
// limit set to 0, disable OCP
RfmWrite(RFM_REG_OCP_TRIM, RFM_OCP_TRIM_OFF);
} else if(currentLimit <= 120) {
raw = (currentLimit - 45) / 5;
RfmWrite(RFM_REG_OCP_TRIM, RFM_OCP_TRIM_ON | raw);
} else if(currentLimit <= 240) {
raw = (currentLimit + 30) / 10;
RfmWrite(RFM_REG_OCP_TRIM, RFM_OCP_TRIM_ON | raw);
}
}
void SlimLoRa::wait_until(unsigned long microsstamp) {
long delta;
// this probably breaks timing for RX2 window
#if DEBUG_SLIM >= 2
Serial.print(F("\nwait_until: "));Serial.print(microsstamp);
Serial.print(F("\nmicros now: "));Serial.println(micros());
Serial.print(F("\nrx_symbols_\t: "));Serial.print(rx_symbols_);
Serial.print(F("\n... MSB\t\t: "));Serial.println(RfmRead(RFM_REG_MODEM_CONFIG_2) & 0b11);
#endif
#if DEBUG_RXSYMBOLS >= 1
microsStart = micros();
#endif
while (1) {
#ifdef ATOMIC_ENABLE
// overflow fail safe logic used. Described here: https://arduino.stackexchange.com/a/12588/59046
// WAS: FORCEON
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
#endif
delta = microsstamp - micros(); // overflow after 70 minutes
#ifdef ATOMIC_ENABLE
}
#endif
if (delta <= 0) {
break;
}
}
}
void SlimLoRa::SetDataRate(uint8_t dr) {
data_rate_ = dr;
}
uint8_t SlimLoRa::GetDataRate() {
return data_rate_;
}
/**************************************************************************/
/*!
@brief Sets the TX power
@param power How much TX power in dBm
*/
/**************************************************************************/
// Valid values in dBm are: 80, +1 to +17 and +20.
//
// 18-19dBm are undefined in doc but maybe possible. Here are ignored.
// Chip works with three modes. This function offer granularity of 1dBm
// but the chip is capable of more.
//
// -4.2 to 0 is in reality -84 to -80dBm
void SlimLoRa::SetPower(uint8_t power) {
// values to be packed in one byte
bool PaBoost; // TODO: merge those to one byte
int8_t OutputPower; // 0-15
int8_t MaxPower; // 0-7
// this value goes to the register (packed bytes)
uint8_t DataPower;
// 1st possibility -80
if ( power == 80 ) { // force -80dBm (lower power)
RfmWrite(RFM_REG_PA_CONFIG, 0);
#ifdef EU863
tx_power = 7; // fake -14dBm report.
#endif
return;
// 2nd possibility: range 1 to 17dBm
} else if ( power >= 0 && power < 2 ) { // assume 1 db is given.
PaBoost = 1;
MaxPower = 7;
OutputPower = 1;
// Store LoRaWAN style tx_power
tx_power = 7;
} else if ( power >= 2 && power <=17 ) {
PaBoost = 1;
MaxPower = 7;
// formula to find the OutputPower.
OutputPower = power - 2;
// Store LoRaWAN style tx_power to be on-par with the protocol.
if ( power > 14 ) {
tx_power = 0;
} else {
// EU868, AS923, KR920, RU864 14dB attenuation.
// TODO: #ifdef (EU863 | AS923 ...)
#ifdef EU863
tx_power = ((LORAWAN_EU868_TX_POWER_MAX * 2) - power) / 2;
#endif
// IN865 20dB attenuation
#ifdef IN865
tx_power = ((LORAWAN_IN865_TX_POWER_MAX * 2) - power) / 2;
#endif
#ifdef US920
// US902 28dB attenuation
tx_power = ((LORAWAN_US902_TX_POWER_MAX * 2) - power) / 2;
#endif
}
}
// 3rd possibility. 20dBm. Special case
// Max Antenna VSWR 3:1, Duty Cycle <1% or destroyed(?) chip
if ( power == 20 ) {
PaBoost = 1;
OutputPower = 15;
MaxPower = 7;
RfmWrite(RFM_REG_PA_DAC, 0x87); // only for +20dBm probably with 0x86,0x85 = 19,18dBm
} else {
// Setting for non +20dBm power
RfmWrite(RFM_REG_PA_DAC, 0x84);
}
// Pack the above data to one byte and send it to HOPE RFM9x
DataPower = (PaBoost << 7) + (MaxPower << 4) + OutputPower;
//PA pin. Default value is 0x4F (DEC 79, 3dBm) from HOPE, 0xFF (DEC 255 / 17dBm) from adafruit.
RfmWrite(RFM_REG_PA_CONFIG,DataPower);
#if DEBUG_SLIM >= 1
Serial.print(F("\nPower (dBm): "));Serial.println(power);
#endif // DEBUG_SLIM
}
/**
* Function for receiving a packet using the RFM
*
* @param packet Pointer to RX packet array.
* @param packet_max_length Maximum number of bytes to read from RX packet. project
* @param channel The frequency table channel index.
* @param dri The data rate table index.
* @param rx_microsstamp Listen until rx_microsstamp elapsed.
* @return The packet length or an error code.
*/
int8_t SlimLoRa::RfmReceivePacket(uint8_t *packet, uint8_t packet_max_length, uint8_t channel, uint8_t dri, uint32_t rx_microsstamp) {
uint8_t modem_config_3, irq_flags, packet_length, read_length;
#if DEBUG_SLIM >= 2
if ( channel == 8 ) Serial.println(F("\n\nRX2\n"));
#endif
// Wait for start time
// TODO add here clockShift in case of CATCH_DIVIDER?
// TODO #2 verify LORAWAN_RX_MARGIN_MICROS value with 8 Mhz and 4 Mhz.
wait_until(rx_microsstamp - LORAWAN_RX_SETUP_MICROS);
// Switch RFM to standby
RfmWrite(RFM_REG_OP_MODE, 0x81);
// Invert IQ
RfmWrite(RFM_REG_INVERT_IQ, 0x66);
RfmWrite(RFM_REG_INVERT_IQ_2, 0x19);
// Set SPI pointer to start of Rx part in FiFo
RfmWrite(RFM_REG_FIFO_ADDR_PTR, 0x00);
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
// Channel
RfmWrite(RFM_REG_FR_MSB, pgm_read_byte(&(kFrequencyTable[channel][0])));
RfmWrite(RFM_REG_FR_MID, pgm_read_byte(&(kFrequencyTable[channel][1])));
RfmWrite(RFM_REG_FR_LSB, pgm_read_byte(&(kFrequencyTable[channel][2])));
// Bandwidth / Coding Rate / Implicit Header Mode
RfmWrite(RFM_REG_MODEM_CONFIG_1, pgm_read_byte(&(kDataRateTable[dri][0])));
// Spreading Factor / Tx Continuous Mode / Crc
RfmWrite(RFM_REG_MODEM_CONFIG_2, pgm_read_byte(&(kDataRateTable[dri][1])));
// Automatic Gain Control / Low Data Rate Optimize
modem_config_3 = pgm_read_byte(&(kDataRateTable[dri][2]));
#else
RfmWrite(RFM_REG_FR_MSB, kFrequencyTable[channel][0]);
RfmWrite(RFM_REG_FR_MID, kFrequencyTable[channel][1]);
RfmWrite(RFM_REG_FR_LSB, kFrequencyTable[channel][2]);
// Bandwidth / Coding Rate / Implicit Header Mode
RfmWrite(RFM_REG_MODEM_CONFIG_1, kDataRateTable[dri][0]);
// Spreading Factor / Tx Continuous Mode / Crc
RfmWrite(RFM_REG_MODEM_CONFIG_2, kDataRateTable[dri][1]);
// Automatic Gain Control / Low Data Rate Optimize
modem_config_3 = kDataRateTable[dri][2];
#endif
if (dri == SF12BW125 || dri == SF11BW125) {
modem_config_3 |= 0x08;
}
#ifdef REDUCE_LNA
// disable AgcAutoOn
modem_config_3 &= 0b11111011; // clear [2] bit to disable AgcAutoOn p. 109
RfmWrite(RFM_REG_MODEM_CONFIG_3, modem_config_3);
// 0b1 is maximum, b110 (6) is minimum. So 6 = SF7, 5 = SF8, 4 = SF9, 3 = SF10, 2 = SF11, 1 = SF12
modem_config_3 = data_rate_ + 1;
if ( modem_config_3 > 6 ) modem_config_3 = 6; // handle overflow
modem_config_3 <<= 5; // Shift to 7-5 bits
RfmWrite(RFM_REG_LNA, modem_config_3);
#endif
// Automatic Gain Control / Low Data Rate Optimize
RfmWrite(RFM_REG_MODEM_CONFIG_3, modem_config_3);
// Rx timeout
modem_config_3 = RfmRead(RFM_REG_MODEM_CONFIG_2) & 0b11111100; // EVAL: added 10+ instructions (delay)
RfmWrite(RFM_REG_MODEM_CONFIG_2, modem_config_3 | rx_symbols_ >> 8); // MSB [0-1]
RfmWrite(RFM_REG_SYMB_TIMEOUT_LSB, rx_symbols_); // LSB 8-bits
// Clear interrupts
RfmWrite(RFM_REG_IRQ_FLAGS, 0xFF);
// Wait for rx time
wait_until(rx_microsstamp);
// Switch RFM to Rx
RfmWrite(RFM_REG_OP_MODE, 0x86);
// Wait for RxDone or RxTimeout
// TODO: switch to IRQ code to save some MCU cycles if we are in RX2
// check with channel if we are in RX2.
// Probably it breaks timing.
#if DEBUG_SLIM >= 2
Serial.print(F("\npre IRQ\t\t: "));Serial.print(micros());
#endif
do {
irq_flags = RfmRead(RFM_REG_IRQ_FLAGS);
} while (!(irq_flags & 0xC0));
// Probably it breaks timing.
#if DEBUG_SLIM >= 2
Serial.print(F("\nafter timeout\t: "));Serial.println(micros());
#endif
packet_length = RfmRead(RFM_REG_RX_NB_BYTES);
RfmWrite(RFM_REG_FIFO_ADDR_PTR, RfmRead(RFM_REG_FIFO_RX_CURRENT_ADDR));
if (packet_max_length < packet_length) {
read_length = packet_max_length;
} else {
read_length = packet_length;
}
for (uint8_t i = 0; i < read_length; i++) {
packet[i] = RfmRead(RFM_REG_FIFO);
}
// SNR
#if DEBUG_SLIM >= 1 // temp DEBUG to check RadioLib vs novag method
last_packet_snr_ = (int8_t) RfmRead(RFM_REG_PKT_SNR_VALUE);
last_packet_snrB = last_packet_snr_;
last_packet_snr_ /= 4;
#else
last_packet_snr_ = (int8_t) RfmRead(RFM_REG_PKT_SNR_VALUE) / 4;
#endif
#if DEBUG_RXSYMBOLS >= 1 && DEBUG_SLIM > 0
Serial.print(F("\n\n >>>RX duration (ms): "));Serial.println( (micros() - microsStart) / 1000 );
#endif
// Clear interrupts
RfmWrite(RFM_REG_IRQ_FLAGS, 0xFF);
// Switch RFM to sleep
RfmWrite(RFM_REG_OP_MODE, 0x00);
switch (irq_flags & 0xC0) {
case RFM_STATUS_RX_TIMEOUT:
return RFM_ERROR_RX_TIMEOUT;
case RFM_STATUS_RX_DONE_CRC_ERROR:
return RFM_ERROR_CRC;
case RFM_STATUS_RX_DONE:
return packet_length;
}
return RFM_ERROR_UNKNOWN;
}
/**
* Senda a packet using the RFM.
*
* @param packet Pointer to TX packet array.
* @param packet_length Length of the TX packet.
* @param channel The frequency table channel index.
* @param dri The data rate table index.
*/
void SlimLoRa::RfmSendPacket(uint8_t *packet, uint8_t packet_length, uint8_t channel, uint8_t dri) {
// TODO if dri is FSK re-init modem.
#if defined CATCH_DIVIDER && defined (__AVR__)
clockShift = clock_prescale_get();
#if DEBUG_SLIM >= 1
Serial.print(F("\nclockShift: "));Serial.println(clockShift);
#endif
#endif
uint8_t modem_config_3;
// Switch RFM to standby
RfmWrite(RFM_REG_OP_MODE, 0x81);
// Don't invert IQ
RfmWrite(RFM_REG_INVERT_IQ, 0x27);
RfmWrite(RFM_REG_INVERT_IQ_2, 0x1D);
#if defined (__AVR__) && defined SLIMLORA_USE_PROGMEM
// Channel
#if defined(EU_DR6)
// SF7BW250 is only for channel 868.3 - second channel.
if ( dri == SF7BW250 ) {
channel_ = 1; // this is needed for downlink channel.
// Only with that works. Compiler BUG?
RfmWrite(RFM_REG_FR_MSB, pgm_read_byte(&(kFrequencyTable[1][0])));
RfmWrite(RFM_REG_FR_MID, pgm_read_byte(&(kFrequencyTable[1][1])));
RfmWrite(RFM_REG_FR_LSB, pgm_read_byte(&(kFrequencyTable[1][2])));
} else {
#endif
RfmWrite(RFM_REG_FR_MSB, pgm_read_byte(&(kFrequencyTable[channel][0])));
RfmWrite(RFM_REG_FR_MID, pgm_read_byte(&(kFrequencyTable[channel][1])));
RfmWrite(RFM_REG_FR_LSB, pgm_read_byte(&(kFrequencyTable[channel][2])));
#if defined(EU_DR6)
}
#endif
// Bandwidth / Coding Rate / Implicit Header Mode
RfmWrite(RFM_REG_MODEM_CONFIG_1, pgm_read_byte(&(kDataRateTable[dri][0])));
// Spreading Factor / Tx Continuous Mode / Crc
RfmWrite(RFM_REG_MODEM_CONFIG_2, pgm_read_byte(&(kDataRateTable[dri][1])));
// Automatic Gain Control / Low Data Rate Optimize
modem_config_3 = pgm_read_byte(&(kDataRateTable[dri][2]));
#else
// Channel
#if defined(EU_DR6)
// SF7BW250 is only for channel 868.3 - second channel.
if ( dri == SF7BW250 ) {
channel_ = 1; // this is needed for downlink channel.
RfmWrite(RFM_REG_FR_MSB, kFrequencyTable[1][0]);
RfmWrite(RFM_REG_FR_MID, kFrequencyTable[1][1]);
RfmWrite(RFM_REG_FR_LSB, kFrequencyTable[1][2]);
} else {
#endif
RfmWrite(RFM_REG_FR_MSB, kFrequencyTable[channel][0]);
RfmWrite(RFM_REG_FR_MID, kFrequencyTable[channel][1]);
RfmWrite(RFM_REG_FR_LSB, kFrequencyTable[channel][2]);
#if defined(EU_DR6)
}
#endif
// Bandwidth / Coding Rate / Implicit Header Mode
RfmWrite(RFM_REG_MODEM_CONFIG_1, kDataRateTable[dri][0]);
// Spreading Factor / Tx Continuous Mode / Crc
RfmWrite(RFM_REG_MODEM_CONFIG_2, kDataRateTable[dri][1]);
// Automatic Gain Control / Low Data Rate Optimize
modem_config_3 = kDataRateTable[dri][2];
#endif
if (dri == SF12BW125 || dri == SF11BW125) {
modem_config_3 |= 0x08;
}
RfmWrite(RFM_REG_MODEM_CONFIG_3, modem_config_3);
// Set payload length to the right length
RfmWrite(RFM_REG_PAYLOAD_LENGTH, packet_length);
// Set SPI pointer to start of Tx part in FiFo
RfmWrite(RFM_REG_FIFO_ADDR_PTR, 0x80);
// Write Payload to FiFo
for (uint8_t i = 0; i < packet_length; i++) {
RfmWrite(RFM_REG_FIFO, *packet);
packet++;
}
#if COUNT_TX_DURATION == 1
// Timestamp before TX. Don't to anything else to protect timing.
slimStartTXtimestamp = millis();
#endif
// Switch RFM to Tx
RfmWrite(RFM_REG_OP_MODE, 0x83);
// Wait for TxDone in the RegIrqFlags register.
//
// START OF TinyLoRa
// EVAL it's better with _irq to TxDone?
//
// Switch _irq to TxDone
// RfmWrite(0x40, 0x40);
//
// white _irq to pull high
// while (digitalRead(_irq) == LOW) { }
// END OF TinyLoRa
while ((RfmRead(RFM_REG_IRQ_FLAGS) & RFM_STATUS_TX_DONE) != RFM_STATUS_TX_DONE);
#ifdef ATOMIC_ENABLE
// https://arduino.stackexchange.com/questions/77494/which-arduinos-support-atomic-block
// disable interrupts.
// WAS: FORCEON
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
tx_done_micros_ = micros();
#endif
#ifdef ATOMIC_ENABLE
}
#endif
#if COUNT_TX_DURATION == 1
// Works with SF7 JOIN TTN GW in same room delay of 5s
// Works with SF8 JOIN Helium GW in outdoors delay of 5s
// Works with SF7 downlinks Helium GW in same room delay 2s - RX2 window
slimEndTXtimestamp = millis();
#endif
// Clear interrupt
RfmWrite(RFM_REG_IRQ_FLAGS, 0xFF);
// Switch RFM to sleep
RfmWrite(RFM_REG_OP_MODE, 0x00);
// BUG: this is not needed when Join. But Join calls this RfmSendPacket function
// NbTrans controls the fCnt
if ( NbTrans_counter > 0 ) {
NbTrans_counter--;
}
// Increase tx_frame_counter if NbTrans_counter is 0 and reset NbTrans_counter
// p. 19 l. 520
// TODO fCnt don't increase if we have CONFIRMED_DATA_UP without ACK
// semi TODO fCnt don't increase if we have UNCONFIRMED_DATA_UP and NbTrans to do.
if ( NbTrans_counter == 0 ) {
tx_frame_counter_++;
adr_ack_limit_counter_++;
#ifdef DYNAMIC_ADR_ACK_LIMIT
if ( adr_ack_limit_counter_ >= adr_ack_limit ) {
#else
if ( adr_ack_limit_counter_ >= LORAWAN_ADR_ACK_LIMIT ) {
#endif
adr_ack_delay_counter_++;
}
NbTrans_counter = NbTrans;
}
#if LORAWAN_KEEP_SESSION
// Saves memory cycles. We jump at worst case scenario EEPROM_WRITE_TX_COUNT fCnt.
if (tx_frame_counter_ % EEPROM_WRITE_TX_COUNT == 0) {
SetTxFrameCounter();
}
#endif
}
/**
* Writes a value to a register of the RFM.
*
* @param address Address of the register to be written.
* @param data Data to be written.
*/
void SlimLoRa::RfmWrite(uint8_t address, uint8_t data) {
SPI.beginTransaction(RFM_spisettings);
// Set NSS pin Low to start communication
digitalWrite(pin_nss_, LOW);
// Send addres with MSB 1 to write
SPI.transfer(address | 0x80);
// Send Data
SPI.transfer(data);
// Set NSS pin High to end communication
digitalWrite(pin_nss_, HIGH);
SPI.endTransaction();
}
/**
* Reads a value from a register of the RFM.
*