-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSX1272.cpp
executable file
·7311 lines (6512 loc) · 207 KB
/
SX1272.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
/*
* Library for LoRa 868 / 915MHz SX1272 LoRa module
*
* Copyright (C) Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*
* Version: 1.1
* Design: David Gascón
* Implementation: Covadonga Albiñana & Victor Boria
*/
//**********************************************************************
// Includes
//**********************************************************************
#include "SX1272.h"
#include <math.h>
/* CHANGE LOGS by C. Pham
* August 28th, 2018
* - add a small delay in the availableData() loop that decreases the CPU load of the lora_gateway process to 4~5% instead of nearly 100%
* - suggested by rertini (https://github.com/CongducPham/LowCostLoRaGw/issues/211)
* March 28th, 2018
* - check at packet reception that the packet type is correct, otherwise discard the packet and returned error code is 5
* - add max number of retries for CarrierSense
* Feb 28th, 2018
* - there is no longer is_binary flag, replaced by is_downlink flag
* - the flags are then from left to right: ack_requested|encrypted|with_appkey|is_downlink
* Feb 25th, 2018
* - use shared payload buffer for packet_sent and packet_received
* - use dedicated smaller buffer for ACK
* Feb 13th, 2018
* - fix bug in availableData() to set back the LoRa module into standby mode. This affected only some radio modules
* November 10th, 2017
* - change the way packet's RSSI is computed
* November 7th, 2017
* - CRC (RxPayloadCrcOn) is set back to OFF for the gateway
* - bug fix in how the CRC is checked at receiver in getPacket() function
* June, 22th, 2017
* - setPowerDBM(uint8_t dbm) calls setPower('X') when dbm is set to 20
* Apr, 21th, 2017
* - change the way timeout are detected: exitTime=millis()+(unsigned long)wait; then millis() < exitTime;
* Mar, 26th, 2017
* - insert delay(100) before setting radio module to sleep mode. Remove unstability issue
* - (proposed by escyes - https://github.com/CongducPham/LowCostLoRaGw/issues/53#issuecomment-289237532)
* Jan, 11th, 2017
* - fix bug in getRSSIpacket() when SNR < 0 thanks to John Rohde from Aarhus University
* Dec, 17th, 2016
* - fix bug making -DPABOOST in radio.makefile inoperant
* Now, 26th, 2016
* - add preliminary support for ToA limitation
* - when in "production" mode, uncomment #define LIMIT_TOA
* Now, 16th, 2016
* - provide better power management mechanisms
* - manage PA_BOOST and dBm setting
* Jan, 23rd, 2016
* - the packet format at transmission does not use the original Libelium format anymore
* * the retry field is removed therefore all operations using retry will probably not work well, not tested though
* - therefore DO NOT use sendPacketTimeoutACKRetries()
* - the reason is that we do not want to have a reserved byte after the payload
* * the length field is removed because it is much better to get the packet length at the reception side
* * after the dst field, we inserted a packet type field to better identify the packet type: DATA, ACK, encryption, app key,...
* - the format is now dst(1B) ptype(1B) src(1B) seq(1B) payload(xB)
* - ptype is decomposed in 2 parts type(4bits) flags(4bits)
* - type can take current value of DATA=0001 and ACK=0010
* - the flags are from left to right: ack_requested|encrypted|with_appkey|is_binary
* - ptype can be set with setPacketType(), see constant defined in SX1272.h
* - the header length is then 4 instead of 5
* Jan, 16th, 2016
* - add support for SX1276, automatic detect
* - add LF/HF calibaration copied from LoRaMAC-Node. Don't know if it is really necessary though
* - change various radio settings
* Dec, 10th, 2015
* - add SyncWord for test with simple LoRaWAN
* - add mode 11 that have BW=125, CR=4/5, SF=7 on channel 868.1MHz
* - use following in your code if (loraMode==11) { e = sx1272.setChannel(CH_18_868); }
* Nov, 13th, 2015
* - add CarrierSense() to perform some Listen Before Talk procedure
* - add dynamic ACK suport
* - compile with W_REQUESTED_ACK, retry field is used to indicate at the receiver that an ACK should be sent
* - receiveWithTimeout() has been modified to send an ACK if retry is 1
* - at sender side, sendPacketTimeoutACK() has been modified to indicate whether retry should be set to 1 or not in setPacket()
* - receiver should always use receiveWithTimeout() while sender decides to use sendPacketTimeout() or sendPacketTimeoutACK()
* Jun, 2015
* - Add time on air computation and CAD features
*/
// Added by C. Pham
// based on SIFS=3CAD
uint8_t sx1272_SIFS_value[11]={0, 183, 94, 44, 47, 23, 24, 12, 12, 7, 4};
uint8_t sx1272_CAD_value[11]={0, 62, 31, 16, 16, 8, 9, 5, 3, 1, 1};
//#define LIMIT_TOA
// 0.1% for testing
//#define MAX_DUTY_CYCLE_PER_HOUR 3600L
// 1%, regular mode
#define MAX_DUTY_CYCLE_PER_HOUR 36000L
// normally 1 hour, set to smaller value for testing
#define DUTYCYCLE_DURATION 3600000L
// 4 min for testing
//#define DUTYCYCLE_DURATION 240000L
// end
//**********************************************************************/
// Public functions.
//**********************************************************************/
SX1272::SX1272()
{
// Initialize class variables
_bandwidth = BW_125;
_codingRate = CR_5;
_spreadingFactor = SF_7;
_channel = CH_12_900;
_header = HEADER_ON;
_CRC = CRC_OFF;
_modem = FSK;
_power = 15;
_packetNumber = 0;
_reception = CORRECT_PACKET;
_retries = 0;
// added by C. Pham
_defaultSyncWord=0x12;
_rawFormat=false;
_extendedIFS=false;
_RSSIonSend=true;
// disabled by default
_enableCarrierSense=false;
// DIFS by default
_send_cad_number=9;
#ifdef PABOOST
_needPABOOST=true;
#else
_needPABOOST=false;
#endif
_limitToA=false;
_startToAcycle=millis();
_remainingToA=MAX_DUTY_CYCLE_PER_HOUR;
#ifdef W_REQUESTED_ACK
_requestACK = 0;
#endif
#ifdef W_NET_KEY
_my_netkey[0] = net_key_0;
_my_netkey[1] = net_key_1;
#endif
// we use the same memory area to reduce memory footprint
packet_sent.data=packet_data;
packet_received.data=packet_data;
// ACK packet has a very small separate memory area
ACK.data=ack_data;
// end
// modified by C. Pham
_maxRetries = 0;
packet_sent.retry = _retries;
};
// added by C. Pham
// copied from LoRaMAC-Node
/*!
* Performs the Rx chain calibration for LF and HF bands
* \remark Must be called just after the reset so all registers are at their
* default values
*/
void SX1272::RxChainCalibration()
{
if (_board==SX1276Chip) {
printf("SX1276 LF/HF calibration\n");
// Cut the PA just in case, RFO output, power = -1 dBm
writeRegister( REG_PA_CONFIG, 0x00 );
// Launch Rx chain calibration for LF band
writeRegister( REG_IMAGE_CAL, ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_MASK ) | RF_IMAGECAL_IMAGECAL_START );
while( ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_RUNNING ) == RF_IMAGECAL_IMAGECAL_RUNNING )
{
}
// Sets a Frequency in HF band
setChannel(CH_17_868);
// Launch Rx chain calibration for HF band
writeRegister( REG_IMAGE_CAL, ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_MASK ) | RF_IMAGECAL_IMAGECAL_START );
while( ( readRegister( REG_IMAGE_CAL ) & RF_IMAGECAL_IMAGECAL_RUNNING ) == RF_IMAGECAL_IMAGECAL_RUNNING )
{
}
}
}
/*
Function: Sets the module ON.
Returns: uint8_t setLORA state
*/
uint8_t SX1272::ON()
{
uint8_t state = 2;
#if (SX1272_debug_mode > 1)
printf("\n");
printf("Starting 'ON'\n");
#endif
// Powering the module
pinMode(SX1272_SS,OUTPUT);
digitalWrite(SX1272_SS,HIGH);
delay(100);
//Configure the MISO, MOSI, CS, SPCR.
SPI.begin();
//Set Most significant bit first
SPI.setBitOrder(MSBFIRST);
//Divide the clock frequency
SPI.setClockDivider(SPI_CLOCK_DIV64);
//Set data mode
SPI.setDataMode(SPI_MODE0);
delay(100);
// added by C. Pham
pinMode(SX1272_RST,OUTPUT);
digitalWrite(SX1272_RST,HIGH);
delay(100);
digitalWrite(SX1272_RST,LOW);
delay(100);
// from single_chan_pkt_fwd by Thomas Telkamp
uint8_t version = readRegister(REG_VERSION);
if (version == 0x22) {
// sx1272
printf("SX1272 detected, starting.\n");
_board = SX1272Chip;
} else {
// sx1276?
digitalWrite(SX1272_RST, LOW);
delay(100);
digitalWrite(SX1272_RST, HIGH);
delay(100);
version = readRegister(REG_VERSION);
if (version == 0x12) {
// sx1276
printf("SX1276 detected, starting.\n");
_board = SX1276Chip;
} else {
printf("Unrecognized transceiver.\n");
}
}
// end from single_chan_pkt_fwd by Thomas Telkamp
// added by C. Pham
RxChainCalibration();
setMaxCurrent(0x1B);
#if (SX1272_debug_mode > 1)
printf("## Setting ON with maximum current supply ##\n");
printf("\n");
#endif
// set LoRa mode
state = setLORA();
// Added by C. Pham for ToA computation
getPreambleLength();
#ifdef W_NET_KEY
//#if (SX1272_debug_mode > 1)
printf("## SX1272 layer has net key##\n");
//#endif
#endif
#ifdef W_INITIALIZATION
// CAUTION
// doing initialization as proposed by Libelium seems not to work for the SX1276
// so we decided to leave the default value of the SX127x, then configure the radio when
// setting to LoRa mode
//Set initialization values
writeRegister(0x0,0x0);
// comment by C. Pham
// still valid for SX1276
writeRegister(0x1,0x81);
// end
writeRegister(0x2,0x1A);
writeRegister(0x3,0xB);
writeRegister(0x4,0x0);
writeRegister(0x5,0x52);
writeRegister(0x6,0xD8);
writeRegister(0x7,0x99);
writeRegister(0x8,0x99);
// modified by C. Pham
// added by C. Pham
if (_board==SX1272Chip)
// RFIO_pin RFU OutputPower
// 0 000 0000
writeRegister(0x9,0x0);
else
// RFO_pin MaxP OutputPower
// 0 100 1111
// set MaxPower to 0x4 and OutputPower to 0
writeRegister(0x9,0x40);
writeRegister(0xA,0x9);
writeRegister(0xB,0x3B);
// comment by C. Pham
// still valid for SX1276
writeRegister(0xC,0x23);
// REG_RX_CONFIG
writeRegister(0xD,0x1);
writeRegister(0xE,0x80);
writeRegister(0xF,0x0);
writeRegister(0x10,0x0);
writeRegister(0x11,0x0);
writeRegister(0x12,0x0);
writeRegister(0x13,0x0);
writeRegister(0x14,0x0);
writeRegister(0x15,0x0);
writeRegister(0x16,0x0);
writeRegister(0x17,0x0);
writeRegister(0x18,0x10);
writeRegister(0x19,0x0);
writeRegister(0x1A,0x0);
writeRegister(0x1B,0x0);
writeRegister(0x1C,0x0);
// added by C. Pham
if (_board==SX1272Chip) {
// comment by C. Pham
// 0x4A = 01 001 0 1 0
// BW=250 CR=4/5 ImplicitH_off RxPayloadCrcOn_on LowDataRateOptimize_off
writeRegister(0x1D,0x4A);
// 1001 0 1 11
// SF=9 TxContinuous_off AgcAutoOn SymbTimeOut
writeRegister(0x1E,0x97);
}
else {
// 1000 001 0
// BW=250 CR=4/5 ImplicitH_off
writeRegister(0x1D,0x82);
// 1000 0 1 11
// SF=9 TxContinuous_off RxPayloadCrcOn_on SymbTimeOut
writeRegister(0x1E,0x97);
}
// end
writeRegister(0x1F,0xFF);
writeRegister(0x20,0x0);
writeRegister(0x21,0x8);
writeRegister(0x22,0xFF);
writeRegister(0x23,0xFF);
writeRegister(0x24,0x0);
writeRegister(0x25,0x0);
// added by C. Pham
if (_board==SX1272Chip)
writeRegister(0x26,0x0);
else
// 0000 0 1 00
// reserved LowDataRateOptimize_off AgcAutoOn reserved
writeRegister(0x26,0x04);
// REG_SYNC_CONFIG
writeRegister(0x27,0x0);
writeRegister(0x28,0x0);
writeRegister(0x29,0x0);
writeRegister(0x2A,0x0);
writeRegister(0x2B,0x0);
writeRegister(0x2C,0x0);
writeRegister(0x2D,0x50);
writeRegister(0x2E,0x14);
writeRegister(0x2F,0x40);
writeRegister(0x30,0x0);
writeRegister(0x31,0x3);
writeRegister(0x32,0x5);
writeRegister(0x33,0x27);
writeRegister(0x34,0x1C);
writeRegister(0x35,0xA);
writeRegister(0x36,0x0);
writeRegister(0x37,0xA);
writeRegister(0x38,0x42);
writeRegister(0x39,0x12);
writeRegister(0x3A,0x65);
writeRegister(0x3B,0x1D);
writeRegister(0x3C,0x1);
writeRegister(0x3D,0xA1);
writeRegister(0x3E,0x0);
writeRegister(0x3F,0x0);
writeRegister(0x40,0x0);
writeRegister(0x41,0x0);
// commented by C. Pham
// since now we handle also the SX1276
//writeRegister(0x42,0x22);
#endif
// added by C. Pham
// default sync word for non-LoRaWAN
setSyncWord(_defaultSyncWord);
getSyncWord();
_defaultSyncWord=_syncWord;
//end
return state;
}
/*
Function: Sets the module OFF.
Returns: Nothing
*/
void SX1272::OFF()
{
#if (SX1272_debug_mode > 1)
printf("\n");
printf("Starting 'OFF'\n");
#endif
SPI.end();
// Powering the module
pinMode(SX1272_SS,OUTPUT);
digitalWrite(SX1272_SS,LOW);
#if (SX1272_debug_mode > 1)
printf("## Setting OFF ##\n");
printf("\n");
#endif
}
/*
Function: Reads the indicated register.
Returns: The content of the register
Parameters:
address: address register to read from
*/
byte SX1272::readRegister(byte address)
{
digitalWrite(SX1272_SS,LOW);
bitClear(address, 7); // Bit 7 cleared to write in registers
//SPI.transfer(address);
//value = SPI.transfer(0x00);
txbuf[0] = address;
txbuf[1] = 0x00;
maxWrite16();
digitalWrite(SX1272_SS,HIGH);
#if (SX1272_debug_mode > 1)
printf("## Reading: ##\tRegister ");
printf("%X", address);
printf(": ");
printf("%X", rxbuf[1]);
printf("\n");
#endif
return rxbuf[1];
}
/*
Function: Writes on the indicated register.
Returns: Nothing
Parameters:
address: address register to write in
data : value to write in the register
*/
void SX1272::writeRegister(byte address, byte data)
{
digitalWrite(SX1272_SS,LOW);
delay(1);
bitSet(address, 7); // Bit 7 set to read from registers
//SPI.transfer(address);
//SPI.transfer(data);
txbuf[0] = address;
txbuf[1] = data;
maxWrite16();
//digitalWrite(SX1272_SS,HIGH);
#if (SX1272_debug_mode > 1)
printf("## Writing: ##\tRegister ");
bitClear(address, 7);
printf("%X", address);
printf(": ");
printf("%X", data);
printf("\n");
#endif
}
/*
Function: It gets the temperature from the measurement block module.
Returns: Integer that determines if there has been any error
state = 2 --> The command has not been executed
state = 1 --> There has been an error while executing the command
state = 0 --> The command has been executed with no errors
*/
void SX1272::maxWrite16()
{
digitalWrite(SX1272_SS,LOW);
SPI.transfernb(txbuf, rxbuf, 2);
digitalWrite(SX1272_SS,HIGH);
}
/*
Function: Clears the interruption flags
Returns: Nothing
*/
void SX1272::clearFlags()
{
byte st0;
st0 = readRegister(REG_OP_MODE); // Save the previous status
if( _modem == LORA )
{ // LoRa mode
writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // Stdby mode to write in registers
writeRegister(REG_IRQ_FLAGS, 0xFF); // LoRa mode flags register
writeRegister(REG_OP_MODE, st0); // Getting back to previous status
#if (SX1272_debug_mode > 1)
printf("## LoRa flags cleared ##\n");
#endif
}
else
{ // FSK mode
writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // Stdby mode to write in registers
writeRegister(REG_IRQ_FLAGS1, 0xFF); // FSK mode flags1 register
writeRegister(REG_IRQ_FLAGS2, 0xFF); // FSK mode flags2 register
writeRegister(REG_OP_MODE, st0); // Getting back to previous status
#if (SX1272_debug_mode > 1)
printf("## FSK flags cleared ##\n");
#endif
}
}
/*
Function: Sets the module in LoRa mode.
Returns: Integer that determines if there has been any error
state = 2 --> The command has not been executed
state = 1 --> There has been an error while executing the command
state = 0 --> The command has been executed with no errors
*/
uint8_t SX1272::setLORA()
{
uint8_t state = 2;
byte st0;
#if (SX1272_debug_mode > 1)
printf("\n");
printf("Starting 'setLORA'\n");
#endif
// modified by C. Pham
uint8_t retry=0;
do {
delay(200);
writeRegister(REG_OP_MODE, FSK_SLEEP_MODE); // Sleep mode (mandatory to set LoRa mode)
writeRegister(REG_OP_MODE, LORA_SLEEP_MODE); // LoRa sleep mode
writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
delay(50+retry*10);
st0 = readRegister(REG_OP_MODE);
printf("...\n");
if ((retry % 2)==0)
if (retry==20)
retry=0;
else
retry++;
/*
if (st0!=LORA_STANDBY_MODE) {
pinMode(SX1272_RST,OUTPUT);
digitalWrite(SX1272_RST,HIGH);
delay(100);
digitalWrite(SX1272_RST,LOW);
}
*/
} while (st0!=LORA_STANDBY_MODE); // LoRa standby mode
if( st0 == LORA_STANDBY_MODE )
{ // LoRa mode
_modem = LORA;
state = 0;
#if (SX1272_debug_mode > 1)
printf("## LoRa set with success ##\n");
printf("\n");
#endif
}
else
{ // FSK mode
_modem = FSK;
state = 1;
#if (SX1272_debug_mode > 1)
printf("** There has been an error while setting LoRa **\n");
printf("\n");
#endif
}
return state;
}
/*
Function: Sets the module in FSK mode.
Returns: Integer that determines if there has been any error
state = 2 --> The command has not been executed
state = 1 --> There has been an error while executing the command
state = 0 --> The command has been executed with no errors
*/
uint8_t SX1272::setFSK()
{
uint8_t state = 2;
byte st0;
byte config1;
if (_board==SX1276Chip)
printf("Warning: FSK has not been tested on SX1276!\n");
#if (SX1272_debug_mode > 1)
printf("\n");
printf("Starting 'setFSK'\n");
#endif
if( _modem = LORA )
{
writeRegister(REG_OP_MODE, LORA_STANDBY_MODE);
writeRegister(REG_OP_MODE, LORA_SLEEP_MODE);
}
writeRegister(REG_OP_MODE, FSK_SLEEP_MODE); // Sleep mode (mandatory to change mode)
writeRegister(REG_OP_MODE, FSK_STANDBY_MODE); // FSK standby mode
config1 = readRegister(REG_PACKET_CONFIG1);
config1 = config1 & 0B01111101; // clears bits 8 and 1 from REG_PACKET_CONFIG1
config1 = config1 | 0B00000100; // sets bit 2 from REG_PACKET_CONFIG1
writeRegister(REG_PACKET_CONFIG1,config1); // AddressFiltering = NodeAddress + BroadcastAddress
writeRegister(REG_FIFO_THRESH, 0x80); // condition to start packet tx
config1 = readRegister(REG_SYNC_CONFIG);
config1 = config1 & 0B00111111;
writeRegister(REG_SYNC_CONFIG,config1);
delay(100);
st0 = readRegister(REG_OP_MODE); // Reading config mode
if( st0 == FSK_STANDBY_MODE )
{ // FSK mode
_modem = FSK;
state = 0;
#if (SX1272_debug_mode > 1)
printf("## FSK set with success ##\n");
printf("\n");
#endif
}
else
{ // LoRa mode
_modem = LORA;
state = 1;
#if (SX1272_debug_mode > 1)
printf("** There has been an error while setting FSK **\n");
printf("\n");
#endif
}
return state;
}
/*
Function: Gets the bandwidth, coding rate and spreading factor of the LoRa modulation.
Returns: Integer that determines if there has been any error
state = 2 --> The command has not been executed
state = 1 --> There has been an error while executing the command
state = 0 --> The command has been executed with no errors
*/
uint8_t SX1272::getMode()
{
byte st0;
int8_t state = 2;
byte value = 0x00;
#if (SX1272_debug_mode > 1)
printf("\n");
printf("Starting 'getMode'\n");
#endif
st0 = readRegister(REG_OP_MODE); // Save the previous status
if( _modem == FSK )
{
setLORA(); // Setting LoRa mode
}
value = readRegister(REG_MODEM_CONFIG1);
// added by C. Pham
if (_board==SX1272Chip) {
_bandwidth = (value >> 6); // Storing 2 MSB from REG_MODEM_CONFIG1 (=_bandwidth)
// added by C. Pham
// convert to common bandwidth values used by both SX1272 and SX1276
_bandwidth += 7;
}
else
_bandwidth = (value >> 4); // Storing 4 MSB from REG_MODEM_CONFIG1 (=_bandwidth)
if (_board==SX1272Chip)
_codingRate = (value >> 3) & 0x07; // Storing third, forth and fifth bits from
else
_codingRate = (value >> 1) & 0x07; // Storing 3-1 bits REG_MODEM_CONFIG1 (=_codingRate)
value = readRegister(REG_MODEM_CONFIG2); // REG_MODEM_CONFIG1 (=_codingRate)
_spreadingFactor = (value >> 4) & 0x0F; // Storing 4 MSB from REG_MODEM_CONFIG2 (=_spreadingFactor)
state = 1;
if( isBW(_bandwidth) ) // Checking available values for:
{ // _bandwidth
if( isCR(_codingRate) ) // _codingRate
{ // _spreadingFactor
if( isSF(_spreadingFactor) )
{
state = 0;
}
}
}
#if (SX1272_debug_mode > 1)
printf("## Parameters from configuration mode are:\n");
printf("\t Bandwidth: ");
printf("%X", _bandwidth);
printf("\n");
printf("\t Coding Rate: ");
printf("%X", _codingRate);
printf("\n");
printf("\t Spreading Factor: ");
printf("%X", _spreadingFactor);
printf(" ##\n");
printf("\n");
#endif
writeRegister(REG_OP_MODE, st0); // Getting back to previous status
delay(100);
return state;
}
/*
Function: Sets the bandwidth, coding rate and spreading factor of the LoRa modulation.
Returns: Integer that determines if there has been any error
state = 2 --> The command has not been executed
state = 1 --> There has been an error while executing the command
state = 0 --> The command has been executed with no errors
state = -1 --> Forbidden command for this protocol
Parameters:
mode: mode number to set the required BW, SF and CR of LoRa modem.
*/
int8_t SX1272::setMode(uint8_t mode)
{
int8_t state = 2;
byte st0;
byte config1 = 0x00;
byte config2 = 0x00;
#if (SX1272_debug_mode > 1)
printf("\n");
printf("Starting 'setMode'\n");
#endif
st0 = readRegister(REG_OP_MODE); // Save the previous status
if( _modem == FSK )
{
setLORA();
}
writeRegister(REG_OP_MODE, LORA_STANDBY_MODE); // LoRa standby mode
switch (mode)
{
// mode 1 (better reach, medium time on air)
case 1:
setCR(CR_5); // CR = 4/5
setSF(SF_12); // SF = 12
setBW(BW_125); // BW = 125 KHz
break;
// mode 2 (medium reach, less time on air)
case 2:
setCR(CR_5); // CR = 4/5
setSF(SF_12); // SF = 12
setBW(BW_250); // BW = 250 KHz
break;
// mode 3 (worst reach, less time on air)
case 3:
setCR(CR_5); // CR = 4/5
setSF(SF_10); // SF = 10
setBW(BW_125); // BW = 125 KHz
break;
// mode 4 (better reach, low time on air)
case 4:
setCR(CR_5); // CR = 4/5
setSF(SF_12); // SF = 12
setBW(BW_500); // BW = 500 KHz
break;
// mode 5 (better reach, medium time on air)
case 5:
setCR(CR_5); // CR = 4/5
setSF(SF_10); // SF = 10
setBW(BW_250); // BW = 250 KHz
break;
// mode 6 (better reach, worst time-on-air)
case 6:
setCR(CR_5); // CR = 4/5
setSF(SF_11); // SF = 11
setBW(BW_500); // BW = 500 KHz
break;
// mode 7 (medium-high reach, medium-low time-on-air)
case 7:
setCR(CR_5); // CR = 4/5
setSF(SF_9); // SF = 9
setBW(BW_250); // BW = 250 KHz
break;
// mode 8 (medium reach, medium time-on-air)
case 8:
setCR(CR_5); // CR = 4/5
setSF(SF_9); // SF = 9
setBW(BW_500); // BW = 500 KHz
break;
// mode 9 (medium-low reach, medium-high time-on-air)
case 9:
setCR(CR_5); // CR = 4/5
setSF(SF_8); // SF = 8
setBW(BW_500); // BW = 500 KHz
break;
// mode 10 (worst reach, less time_on_air)
case 10:
setCR(CR_5); // CR = 4/5
setSF(SF_7); // SF = 7
setBW(BW_500); // BW = 500 KHz
break;
// added by C. Pham
// test for LoRaWAN channel
case 11:
setCR(CR_5); // CR = 4/5
setSF(SF_12); // SF = 12
setBW(BW_125); // BW = 125 KHz
// set the sync word to the LoRaWAN sync word which is 0x34
setSyncWord(0x34);
printf("** Using sync word of 0x");
printf("%X\n", _syncWord);
break;
default: state = -1; // The indicated mode doesn't exist
};
if( state == -1 ) // if state = -1, don't change its value
{
#if (SX1272_debug_mode > 1)
printf("** The indicated mode doesn't exist, ");
printf("please select from 1 to 10 **\n");
#endif
}
else
{
state = 1;
config1 = readRegister(REG_MODEM_CONFIG1);
switch (mode)
{ // Different way to check for each mode:
// (config1 >> 3) ---> take out bits 7-3 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
// (config2 >> 4) ---> take out bits 7-4 from REG_MODEM_CONFIG2 (=_spreadingFactor)
// mode 1: BW = 125 KHz, CR = 4/5, SF = 12.
case 1:
//modified by C. Pham
if (_board==SX1272Chip) {
if( (config1 >> 3) == 0x01 )
state=0;
}
else {
// (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
if( (config1 >> 1) == 0x39 )
state=0;
}
if( state==0) {
state = 1;
config2 = readRegister(REG_MODEM_CONFIG2);
if( (config2 >> 4) == SF_12 )
{
state = 0;
}
}
break;
// mode 2: BW = 250 KHz, CR = 4/5, SF = 12.
case 2:
//modified by C. Pham
if (_board==SX1272Chip) {
if( (config1 >> 3) == 0x09 )
state=0;
}
else {
// (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
if( (config1 >> 1) == 0x41 )
state=0;
}
if( state==0) {
state = 1;
config2 = readRegister(REG_MODEM_CONFIG2);
if( (config2 >> 4) == SF_12 )
{
state = 0;
}
}
break;
// mode 3: BW = 125 KHz, CR = 4/5, SF = 10.
case 3:
//modified by C. Pham
if (_board==SX1272Chip) {
if( (config1 >> 3) == 0x01 )
state=0;
}
else {
// (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
if( (config1 >> 1) == 0x39 )
state=0;
}
if( state==0) {
state = 1;
config2 = readRegister(REG_MODEM_CONFIG2);
if( (config2 >> 4) == SF_10 )
{
state = 0;
}
}
break;
// mode 4: BW = 500 KHz, CR = 4/5, SF = 12.
case 4:
//modified by C. Pham
if (_board==SX1272Chip) {
if( (config1 >> 3) == 0x11 )
state=0;
}
else {
// (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
if( (config1 >> 1) == 0x49 )
state=0;
}
if( state==0) {
state = 1;
config2 = readRegister(REG_MODEM_CONFIG2);
if( (config2 >> 4) == SF_12 )
{
state = 0;
}
}
break;
// mode 5: BW = 250 KHz, CR = 4/5, SF = 10.
case 5:
//modified by C. Pham
if (_board==SX1272Chip) {
if( (config1 >> 3) == 0x09 )
state=0;
}
else {
// (config1 >> 1) ---> take out bits 7-1 from REG_MODEM_CONFIG1 (=_bandwidth & _codingRate together)
if( (config1 >> 1) == 0x41 )
state=0;
}
if( state==0) {
state = 1;
config2 = readRegister(REG_MODEM_CONFIG2);
if( (config2 >> 4) == SF_10 )
{
state = 0;
}
}