-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio_driver_SX1276.c
1708 lines (1478 loc) · 53.9 KB
/
radio_driver_SX1276.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
/******************************************************************************
@Company:
Microchip Technology Inc.
@File Name:
radio_driver_SX1276.c
@Summary:
This is the Radio Driver SX1276 source file which contains LoRa-specific
Radio Driver functions declarations and defines for SX1276
@Description:
This source file provides LoRa-specific implementations for Radio Driver for SX1276.
Copyright (c) 2013 - 2016 released Microchip Technology Inc. All rights reserved.
Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).
You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.
SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*************************************************************************
* radio_driver_SX1276.c
*
* Radio Driver SX1276 source file
*
******************************************************************************/
#include "radio_registers_SX1276.h"
#include "radio_driver_SX1276.h"
#include "lorawan_defs.h"
#include "radio_driver_hal.h"
#include "lorax_radio.h"
#include "sw_timer.h"
#include "shell.h"
#include "measurements.h"
#define TIME_ON_AIR_LOAD_VALUE ((uint32_t)20000)
#define WATCHDOG_DEFAULT_TIME ((uint32_t)15000)
static RadioConfiguration_t RadioConfiguration;
static void RADIO_RxFSKTimeout(uint8_t param);
static void RADIO_WatchdogTimeout(uint8_t param);
static void RADIO_WriteMode(RadioMode_t newMode, RadioModulation_t newModulation, uint8_t blocking);
static void RADIO_Reset(void);
static void RADIO_WriteFrequency(uint32_t frequency);
static void RADIO_WriteFSKFrequencyDeviation(uint32_t frequencyDeviation);
static void RADIO_WriteFSKBitRate(uint32_t bitRate);
static void RADIO_WritePower(int8_t power);
static void RADIO_WriteConfiguration(uint16_t symbolTimeout);
static void RADIO_RxDone(void);
static void RADIO_FSKPayloadReady(void);
static void RADIO_RxTimeout(void);
static void RADIO_TxDone(void);
static void RADIO_FSKPacketSent(void);
static void RADIO_UnhandledInterrupt(RadioModulation_t modulation);
static void RADIO_FHSSChangeChannel(void);
uint8_t b[128];
uint8_t trace;
extern uint8_t rssi_off;
extern uint8_t mode;
extern uint32_t bandwidth;
extern uint8_t spread_factor;
uint8_t rectimer;
uint32_t rectimer_value, delta, ticks, ticks_old;
extern Data_t data;
void RADIO_RegisterWrite(uint8_t reg, uint8_t value)
{
HALSPICSAssert();
HALSPISend(REG_WRITE | reg);
HALSPISend(value);
if(trace)
{
send_chars("Write ");
send_chars(ui8tox(reg,b));
send_chars("=");
send_chars(ui8tox(value,b));
send_chars("\r\n");
}
HALSPICSDeassert();
}
uint8_t RADIO_RegisterRead(uint8_t reg)
{
uint8_t readValue;
reg &= 0x7F; // Make sure write bit is not set
HALSPICSAssert();
HALSPISend(reg);
readValue = HALSPISend(0xFF);
if(trace)
{
send_chars("Read ");
send_chars(ui8tox(reg,b));
send_chars("=");
send_chars(ui8tox(readValue,b));
send_chars("\r\n");
}
HALSPICSDeassert();
return readValue;
}
// This function repurposes DIO5 for ModeReady functionality
static void RADIO_WriteMode(RadioMode_t newMode, RadioModulation_t newModulation, uint8_t blocking)
{
uint8_t opMode;
uint8_t dioMapping;
RadioModulation_t currentModulation;
RadioMode_t currentMode;
if ((MODULATION_FSK == newModulation) &&
((MODE_RXSINGLE == newMode) || (MODE_CAD == newMode)))
{
// Unavaliable modes for FSK. Just return.
return;
}
// Sanity enforcement on parameters
newMode &= 0x07;
newModulation &= 0x01;
opMode = RADIO_RegisterRead(REG_OPMODE);
if ((opMode & 0x80) != 0)
{
currentModulation = MODULATION_LORA;
}
else
{
currentModulation = MODULATION_FSK;
}
currentMode = opMode & 0x07;
// If we need to change modulation, we need to do this in sleep mode.
// Otherwise, we can go straight to changing the current mode to newMode.
if (newModulation != currentModulation)
{
// Go to sleep
if (MODE_SLEEP != currentMode)
{
// Clear mode bits, effectively going to sleep
RADIO_RegisterWrite(REG_OPMODE, opMode & (~0x07));
currentMode = MODE_SLEEP;
}
// Change modulation
if (MODULATION_FSK == newModulation)
{
// Clear MSB and sleep bits to make it stay in sleep
opMode = opMode & (~0x87);
}
else
{
// LoRa mode. Set MSB and clear sleep bits to make it stay in sleep
opMode = 0x80 | (opMode & (~0x87));
}
RADIO_RegisterWrite(REG_OPMODE, opMode);
}
// From here on currentModulation is no longer current, we will use
// newModulation instead as it reflects the chip configuration.
// opMode reflects the actual configuration of the chip.
if (newMode != currentMode)
{
// If we need to block until the mode switch is ready, configure the
// DIO5 pin to relay this information.
if ((MODE_SLEEP != newMode) && (1 == blocking))
{
dioMapping = RADIO_RegisterRead(REG_DIOMAPPING2);
if (MODULATION_FSK == newModulation)
{
// FSK mode
dioMapping |= 0x30; // DIO5 = 11 means ModeReady in FSK mode
}
else
{
// LoRa mode
dioMapping &= ~0x30; // DIO5 = 00 means ModeReady in LoRa mode
}
RADIO_RegisterWrite(REG_DIOMAPPING2, dioMapping);
}
// Do the actual mode switch.
opMode &= ~0x07; // Clear old mode bits
opMode |= newMode; // Set new mode bits
RADIO_RegisterWrite(REG_OPMODE, opMode);
// If required and possible, wait for switch to complete
if (1 == blocking)
{
if (MODE_SLEEP != newMode)
{
while (HALDIO5PinValue() == 0)
;
}
else
{
SystemBlockingWaitMs(1);
}
}
}
}
static void RADIO_Reset(void)
{
HALResetPinMakeOutput();
HALResetPinOutputValue(0);
SystemBlockingWaitMs(1);
HALResetPinMakeInput();
SystemBlockingWaitMs(10);
//Added these two lines to make sure this pin is not left in floating state during sleep
HALResetPinOutputValue(1);
HALResetPinMakeOutput();
}
// The math in this function needs adjusting for FXOSC != 32MHz
static void RADIO_WriteFrequency(uint32_t frequency)
{
uint32_t num, num_mod;
// Frf = (Fxosc * num) / 2^19
// We take advantage of the fact that 32MHz = 15625Hz * 2^11
// This simplifies our formula to Frf = (15625Hz * num) / 2^8
// Thus, num = (Frf * 2^8) / 15625Hz
// First, do the division, since Frf * 2^8 does not fit in 32 bits
num = frequency / 15625;
num_mod = frequency % 15625;
// Now do multiplication as well, both for the quotient as well as for
// the remainder
num <<= SHIFT8;
num_mod <<= SHIFT8;
// Try to correct for the remainder. After the multiplication we can still
// recover some accuracy
num_mod = num_mod / 15625;
num += num_mod;
// Now variable num holds the representation of the frequency that needs to
// be loaded into the radio chip
RADIO_RegisterWrite(REG_FRFMSB, (num >> SHIFT16) & 0xFF);
RADIO_RegisterWrite(REG_FRFMID, (num >> SHIFT8) & 0xFF);
RADIO_RegisterWrite(REG_FRFLSB, num & 0xFF);
}
// The math in this function needs adjusting for FXOSC != 32MHz
// This function needs to be called with the radio configured in FSK mode
static void RADIO_WriteFSKFrequencyDeviation(uint32_t frequencyDeviation)
{
uint32_t num;
// Fdev = (Fxosc * num) / 2^19
// We take advantage of the fact that 32MHz = 15625Hz * 2^11
// This simplifies our formula to Fdev = (15625Hz * num) / 2^8
// Thus, num = (Fdev * 2^8) / 15625Hz
num = frequencyDeviation;
num <<= SHIFT8; // Multiply by 2^8
num /= 15625; // divide by 15625
// Now variable num holds the representation of the frequency deviation that
// needs to be loaded into the radio chip
RADIO_RegisterWrite(REG_FSK_FDEVMSB, (num >> SHIFT8) & 0xFF);
RADIO_RegisterWrite(REG_FSK_FDEVLSB, num & 0xFF);
}
// The math in this function needs adjusting for FXOSC != 32MHz
// This function needs to be called with the radio configured in FSK mode.
// BitrateFrac is always 0
static void RADIO_WriteFSKBitRate(uint32_t bitRate)
{
uint32_t num;
num = 32000000;
num /= bitRate;
// Now variable num holds the representation of the bitrate that
// needs to be loaded into the radio chip
RADIO_RegisterWrite(REG_FSK_BITRATEMSB, (num >> SHIFT8) & 0xFF);
RADIO_RegisterWrite(REG_FSK_BITRATELSB, num & 0xFF);
RADIO_RegisterWrite(REG_FSK_BITRATEFRAC, 0x00);
}
int8_t RADIO_GetMaxPower(void)
{
if (RadioConfiguration.paBoost == 0)
{
return 15;
}
else
{
return 20;
}
}
static void RADIO_WritePower(int8_t power)
{
uint8_t paDac;
uint8_t ocp;
if (RadioConfiguration.paBoost == 0)
{
// RFO pin used for RF output
if (power < -3)
{
power = -3;
}
if (power > 15)
{
power = 15;
}
paDac = RADIO_RegisterRead(REG_PADAC);
paDac &= ~(0x07);
paDac |= 0x04;
RADIO_RegisterWrite(REG_PADAC, paDac);
if (power < 0)
{
// MaxPower = 2
// Pout = 10.8 + MaxPower*0.6 - 15 + OutPower
// Pout = -3 + OutPower
power += 3;
RADIO_RegisterWrite(REG_PACONFIG, 0x20 | power);
}
else
{
// MaxPower = 7
// Pout = 10.8 + MaxPower*0.6 - 15 + OutPower
// Pout = OutPower
RADIO_RegisterWrite(REG_PACONFIG, 0x70 | power);
}
}
else
{
// PA_BOOST pin used for RF output
// Lower limit
if (power < 2)
{
power = 2;
}
// Upper limit
if (power >= 20)
{
power = 20;
}
else if (power > 17)
{
power = 17;
}
ocp = RADIO_RegisterRead(REG_OCP);
paDac = RADIO_RegisterRead(REG_PADAC);
paDac &= ~(0x07);
if (power == 20)
{
paDac |= 0x07;
power = 15;
ocp &= ~(0x20);
}
else
{
paDac |= 0x04;
power -= 2;
ocp |= 0x20;
}
RADIO_RegisterWrite(REG_PADAC, paDac);
RADIO_RegisterWrite(REG_PACONFIG, 0x80 | power);
RADIO_RegisterWrite(REG_OCP, ocp);
}
data.power=power;
}
void RADIO_Init(uint8_t *radioBuffer, uint32_t frequency)
{
uint8_t tmp8;
uint32_t tmp32;
int32_t p;
RadioConfiguration.frequency = frequency;
send_chars("Calibration F=");
send_chars(ui32toa(RadioConfiguration.frequency,b));
send_chars("\r\n");
set_s("DEVIATION",&RadioConfiguration.frequencyDeviation); // = 25000;
set_s("FSK_BITRATE",&RadioConfiguration.bitRate); // = 50000;
set_s("MODULATION",&tmp8);
if(tmp8) RadioConfiguration.modulation = MODULATION_FSK;
else RadioConfiguration.modulation = MODULATION_LORA;
switch(bandwidth)
{
case 125000:
RadioConfiguration.bandWidth = BW_125KHZ;
break;
case 250000:
RadioConfiguration.bandWidth = BW_250KHZ;
break;
case 500000:
RadioConfiguration.bandWidth = BW_500KHZ;
break;
default:
RadioConfiguration.bandWidth = BW_125KHZ;
break;
};
set_s("POWER",&p);
RadioConfiguration.outputPower=p; // = 1;
set_s("FEC",&RadioConfiguration.errorCodingRate); // = CR_4_5;
set_s("HEADER_MODE",&RadioConfiguration.implicitHeaderMode); // = 0;
set_s("PREAMBLE_LEN",&tmp32);
RadioConfiguration.preambleLen=(uint16_t)tmp32; // = 8;
set_s("SF",&spread_factor);
RadioConfiguration.dataRate=spread_factor; // = SF_12;
set_s("CRC",&RadioConfiguration.crcOn); // = 1;
set_s("BOOST",&RadioConfiguration.paBoost); // = 0;
set_s("IQ_INVERTED",&RadioConfiguration.iqInverted); // = 0;
set_s("SYNCWORDLEN",&RadioConfiguration.syncWordLen); // = 3;
set_s("SYNCWORD",&tmp32);
RadioConfiguration.syncWord[0] = (tmp32>>24)&0xFF; //0xC1;
RadioConfiguration.syncWord[1] = (tmp32>>16)&0xFF; //0x94;
RadioConfiguration.syncWord[2] = (tmp32>>8)&0xFF; //0xC1;
RadioConfiguration.syncWord[3] = tmp32&0xFF; //0;
set_s("LORA_SYNCWORD",&RadioConfiguration.syncWordLoRa);
RadioConfiguration.flags = 0;
RadioConfiguration.dataBufferLen = 0;
RadioConfiguration.dataBuffer = radioBuffer;
RadioConfiguration.frequencyHopPeriod = 0;
RadioConfiguration.watchdogTimerTimeout = WATCHDOG_DEFAULT_TIME;
set_s("MODULATION",&tmp8);
if(tmp8) RadioConfiguration.fskDataShaping = tmp8-1; //FSK_SHAPING_GAUSS_BT_0_5;
set_s("FSK_BW",&RadioConfiguration.rxBw); // = FSKBW_50_0KHZ;
set_s("FSK_AFC_BW",&RadioConfiguration.afcBw); // = FSKBW_83_3KHZ;
RadioConfiguration.fhssNextFrequency = NULL;
rectimer=SwTimerCreate();
rectimer_value=86400000;
SwTimerSetTimeout(rectimer,MS_TO_TICKS(rectimer_value));
SwTimerStart(rectimer);
delta=0;
ticks=0;
ticks_old=0;
// Make sure we do not allocate multiple software timers just because the
// radio's initialization function is called multiple times.
if (0 == RadioConfiguration.initialized)
{
// This behaviour depends on the compiler's behaviour regarding
// uninitialized variables. It should be configured to set them to 0.
RadioConfiguration.timeOnAirTimerId = SwTimerCreate();
RadioConfiguration.fskRxWindowTimerId = SwTimerCreate();
RadioConfiguration.watchdogTimerId = SwTimerCreate();
SwTimerSetCallback(RadioConfiguration.fskRxWindowTimerId, RADIO_RxFSKTimeout, 0);
SwTimerSetCallback(RadioConfiguration.watchdogTimerId, RADIO_WatchdogTimeout, 0);
RadioConfiguration.initialized = 1;
}
else
{
SwTimerStop(RadioConfiguration.timeOnAirTimerId);
SwTimerStop(RadioConfiguration.fskRxWindowTimerId);
SwTimerStop(RadioConfiguration.watchdogTimerId);
}
RADIO_Reset();
// Perform image and RSSI calibration. This also puts the radio in FSK mode.
// In order to perform image and RSSI calibration, we need the radio in
// FSK mode. To do this, we first put it in sleep mode.
RADIO_WriteMode(MODE_STANDBY, MODULATION_FSK, 1);
// Set frequency to do calibration at the configured frequency
RADIO_WriteFrequency(RadioConfiguration.frequency);
// Do not do autocalibration at runtime, start calibration now, Temp
// threshold for monitoring 10 deg. C, Temperature monitoring enabled
RADIO_RegisterWrite(REG_FSK_IMAGECAL, 0x42);
// Wait for calibration to complete
while ((RADIO_RegisterRead(REG_FSK_IMAGECAL) & 0x20) != 0)
;
// High frequency LNA current adjustment, 150% LNA current (Boost on)
RADIO_RegisterWrite(REG_LNA, 0x23);
// Triggering event: PreambleDetect does AfcAutoOn, AgcAutoOn
RADIO_RegisterWrite(REG_FSK_RXCONFIG, 0x1E);
// Preamble detector on, 2 bytes trigger an interrupt, Chip errors tolerated
// over the preamble size
RADIO_RegisterWrite(REG_FSK_PREAMBLEDETECT, 0xAA);
// Transmission starts as soon as there is a byte in the FIFO. FifoLevel
// interrupt is generated whenever there are at least 16 bytes in FIFO.
RADIO_RegisterWrite(REG_FSK_FIFOTHRESH, 0x8F);
// Set FSK max payload length to 255 bytes
RADIO_RegisterWrite(REG_FSK_PAYLOADLENGTH, 0xFF);
// Packet mode
RADIO_RegisterWrite(REG_FSK_PACKETCONFIG2, 1 << SHIFT6);
// Go to LoRa mode for this register to be set
RADIO_WriteMode(MODE_SLEEP, MODULATION_LORA, 1);
// Set LoRa max payload length
RADIO_RegisterWrite(REG_LORA_PAYLOADMAXLENGTH, 0xFF);
RadioConfiguration.regVersion = RADIO_RegisterRead(REG_VERSION);
}
void RADIO_SetLoRaSyncWord(uint8_t syncWord)
{
// Change LoRa syncword
RadioConfiguration.syncWordLoRa = syncWord;
}
uint8_t RADIO_GetLoRaSyncWord(void)
{
return RadioConfiguration.syncWordLoRa;
}
static void RADIO_WriteConfiguration(uint16_t symbolTimeout)
{
uint32_t tempValue;
uint8_t regValue;
uint8_t i;
// Load configuration from RadioConfiguration_t structure into radio
RADIO_WriteMode(MODE_SLEEP, RadioConfiguration.modulation, 0);
send_chars(MODULATION_LORA == RadioConfiguration.modulation ? "LORA " : "FSK ");
RADIO_WriteFrequency(RadioConfiguration.frequency);
printVar("F=",PAR_UI32,&RadioConfiguration.frequency,false,false);
RADIO_WritePower(RadioConfiguration.outputPower);
printVar(" P=",PAR_UI8,&RadioConfiguration.outputPower,false,false);
if (MODULATION_LORA == RadioConfiguration.modulation)
{
RADIO_RegisterWrite(REG_LORA_SYNCWORD, RadioConfiguration.syncWordLoRa);
RADIO_RegisterWrite(REG_LORA_MODEMCONFIG1,
(RadioConfiguration.bandWidth << SHIFT4) |
(RadioConfiguration.errorCodingRate << SHIFT1) |
(RadioConfiguration.implicitHeaderMode & 0x01));
RADIO_RegisterWrite(REG_LORA_MODEMCONFIG2,
(RadioConfiguration.dataRate << SHIFT4) |
((RadioConfiguration.crcOn & 0x01) << SHIFT2) |
((symbolTimeout & 0x0300) >> SHIFT8));
printVar(" SF=",PAR_UI8,&RadioConfiguration.dataRate,false,false);
printVar(" CRC=",PAR_UI8,&RadioConfiguration.crcOn,false,false);
// Handle frequency hopping, if necessary
if (0 != RadioConfiguration.frequencyHopPeriod)
{
tempValue = RadioConfiguration.frequencyHopPeriod;
// Multiply by BW/1000 (since period is in ms)
switch (RadioConfiguration.bandWidth)
{
case BW_125KHZ:
tempValue *= 125;
break;
case BW_250KHZ:
tempValue *= 250;
break;
case BW_500KHZ:
tempValue *= 500;
break;
default:
// Disable frequency hopping
tempValue = 0;
break;
}
// Divide by 2^SF
tempValue >>= RadioConfiguration.dataRate;
}
else
{
tempValue = 0;
}
RADIO_RegisterWrite(REG_LORA_HOPPERIOD, (uint8_t)tempValue);
RADIO_RegisterWrite(REG_LORA_SYMBTIMEOUTLSB, (symbolTimeout & 0xFF));
send_chars(" Symbol_Timeout=");
send_chars(ui32toa((uint32_t)symbolTimeout,b));
// If the symbol time is > 16ms, LowDataRateOptimize needs to be set
// This long symbol time only happens for SF12&BW125, SF12&BW250
// and SF11&BW125 and the following if statement checks for these
// conditions
regValue = RADIO_RegisterRead(REG_LORA_MODEMCONFIG3);
if (
(
(SF_12 == RadioConfiguration.dataRate) &&
((BW_125KHZ == RadioConfiguration.bandWidth) || (BW_250KHZ == RadioConfiguration.bandWidth))
) ||
(
(SF_11 == RadioConfiguration.dataRate) &&
(BW_125KHZ == RadioConfiguration.bandWidth)
)
)
{
regValue |= 1 << SHIFT3; // Set LowDataRateOptimize
}
else
{
regValue &= ~(1 << SHIFT3); // Clear LowDataRateOptimize
}
regValue |= 1 << SHIFT2; // LNA gain set by internal AGC loop
RADIO_RegisterWrite(REG_LORA_MODEMCONFIG3, regValue);
regValue = RADIO_RegisterRead(REG_LORA_DETECTOPTIMIZE);
regValue &= ~(0x07); // Clear DetectOptimize bits
regValue |= 0x03; // Set value for SF7 - SF12
RADIO_RegisterWrite(REG_LORA_DETECTOPTIMIZE, regValue);
// Also set DetectionThreshold value for SF7 - SF12
RADIO_RegisterWrite(REG_LORA_DETECTIONTHRESHOLD, 0x0A);
// Errata settings to mitigate spurious reception of a LoRa Signal
if (0x12 == RadioConfiguration.regVersion)
{
// Chip already is in sleep mode. For these BWs we don't need to
// offset Frf
if ( (BW_125KHZ == RadioConfiguration.bandWidth) ||
(BW_250KHZ == RadioConfiguration.bandWidth) )
{
regValue = RADIO_RegisterRead(0x31);
regValue &= ~0x80; // Clear bit 7
RADIO_RegisterWrite(0x31, regValue);
RADIO_RegisterWrite(0x2F, 0x40);
RADIO_RegisterWrite(0x30, 0x00);
}
if (BW_500KHZ == RadioConfiguration.bandWidth)
{
regValue = RADIO_RegisterRead(0x31);
regValue |= 0x80; // Set bit 7
RADIO_RegisterWrite(0x31, regValue);
}
}
regValue = RADIO_RegisterRead(REG_LORA_INVERTIQ);
if(RadioConfiguration.iqInverted & 0x01)
{
regValue &= ~(1 << SHIFT0);
regValue |= (1 << SHIFT6);
}
else
{
regValue |= (1 << SHIFT0);
regValue &= ~(1 << SHIFT6);
} // Set InvertIQ bit if needed
RADIO_RegisterWrite(REG_LORA_INVERTIQ, regValue);
printVar(" IQInv=",PAR_UI8,&RadioConfiguration.iqInverted,false,false);
regValue = REG_LORA_INVERTIQ2_VALUE_OFF & (~((RadioConfiguration.iqInverted & 0x01) << SHIFT2));
RADIO_RegisterWrite(REG_LORA_INVERTIQ2, regValue);
RADIO_RegisterWrite(REG_LORA_PREAMBLEMSB, RadioConfiguration.preambleLen >> SHIFT8);
RADIO_RegisterWrite(REG_LORA_PREAMBLELSB, RadioConfiguration.preambleLen & 0xFF);
RADIO_RegisterWrite(REG_LORA_FIFOADDRPTR, 0x00);
RADIO_RegisterWrite(REG_LORA_FIFOTXBASEADDR, 0x00);
RADIO_RegisterWrite(REG_LORA_FIFORXBASEADDR, 0x00);
// Errata sensitivity increase for 500kHz BW
if (0x12 == RadioConfiguration.regVersion)
{
if ( (BW_500KHZ == RadioConfiguration.bandWidth) &&
(RadioConfiguration.frequency >= FREQ_862000KHZ) &&
(RadioConfiguration.frequency <= FREQ_1020000KHZ)
)
{
RADIO_RegisterWrite(0x36, 0x02);
RADIO_RegisterWrite(0x3a, 0x64);
}
else if ( (BW_500KHZ == RadioConfiguration.bandWidth) &&
(RadioConfiguration.frequency >= FREQ_410000KHZ) &&
(RadioConfiguration.frequency <= FREQ_525000KHZ)
)
{
RADIO_RegisterWrite(0x36, 0x02);
RADIO_RegisterWrite(0x3a, 0x7F);
}
else
{
RADIO_RegisterWrite(0x36, 0x03);
}
// LoRa Inverted Polarity 500kHz fix (May 26, 2015 document)
if ((BW_500KHZ == RadioConfiguration.bandWidth) && (1 == RadioConfiguration.iqInverted))
{
RADIO_RegisterWrite(0x3A, 0x65); // Freq to time drift
RADIO_RegisterWrite(REG_LORA_INVERTIQ2, 25); // Freq to time invert = 0d25
}
else
{
RADIO_RegisterWrite(0x3A, 0x65); // Freq to time drift
RADIO_RegisterWrite(REG_LORA_INVERTIQ2, 29); // Freq to time invert = 0d29 (default)
}
}
// Clear all interrupts (just in case)
RADIO_RegisterWrite(REG_LORA_IRQFLAGS, 0xFF);
}
else
{
// FSK modulation
RADIO_WriteFSKFrequencyDeviation(RadioConfiguration.frequencyDeviation);
RADIO_WriteFSKBitRate(RadioConfiguration.bitRate);
RADIO_RegisterWrite(REG_FSK_PREAMBLEMSB, RadioConfiguration.preambleLen >> SHIFT8);
RADIO_RegisterWrite(REG_FSK_PREAMBLELSB, RadioConfiguration.preambleLen & 0xFF);
// Configure PaRamp
regValue = RADIO_RegisterRead(REG_PARAMP);
regValue &= ~0x60; // Clear shaping bits
regValue |= RadioConfiguration.fskDataShaping << SHIFT5;
RADIO_RegisterWrite(REG_PARAMP, regValue);
// Variable length packets, whitening, keep FIFO when CRC fails
// no address filtering, CCITT CRC and whitening
regValue = 0xC8;
if (RadioConfiguration.crcOn)
{
regValue |= 0x10; // Enable CRC
}
RADIO_RegisterWrite(REG_FSK_PACKETCONFIG1, regValue);
// Syncword value
for (i = 0; i < RadioConfiguration.syncWordLen; i++)
{
// Take advantage of the fact that the SYNCVALUE registers are
// placed at sequential addresses
RADIO_RegisterWrite(REG_FSK_SYNCVALUE1 + i, RadioConfiguration.syncWord[i]);
}
// Enable sync word generation/detection if needed, Syncword size = syncWordLen + 1 bytes
if (RadioConfiguration.syncWordLen != 0)
{
RADIO_RegisterWrite(REG_FSK_SYNCCONFIG, 0x10 | (RadioConfiguration.syncWordLen - 1));
}
else
{
RADIO_RegisterWrite(REG_FSK_SYNCCONFIG, 0x00);
}
// Clear all FSK interrupts (just in case)
RADIO_RegisterWrite(REG_FSK_IRQFLAGS1, 0xFF);
RADIO_RegisterWrite(REG_FSK_IRQFLAGS2, 0xFF);
}
send_chars("\r\n");
}
RadioError_t RADIO_TransmitCW(void)
{
if ((RadioConfiguration.flags & (RADIO_FLAG_TRANSMITTING | RADIO_FLAG_RECEIVING)) != 0)
{
return ERR_RADIO_BUSY;
}
RadioConfiguration.modulation = MODULATION_LORA;
// Since we're interested in a transmission, rxWindowSize is irrelevant.
// Setting it to 4 is a valid option.
RADIO_WriteConfiguration(4);
RadioConfiguration.flags |= RADIO_FLAG_TRANSMITTING;
RadioConfiguration.flags &= ~RADIO_FLAG_TIMEOUT;
RADIO_RegisterWrite(0x3D, 0xA1);
RADIO_RegisterWrite(0x36, 0x01);
RADIO_RegisterWrite(0x1E, 0x08);
RADIO_RegisterWrite(0x01, 0x8B);
return ERR_NONE;
}
RadioError_t RADIO_StopCW(void)
{
RADIO_WriteMode(MODE_STANDBY, RadioConfiguration.modulation, 0);
SystemBlockingWaitMs(100);
RADIO_WriteMode(MODE_SLEEP, RadioConfiguration.modulation, 0);
SystemBlockingWaitMs(100);
return ERR_NONE;
}
RadioError_t RADIO_Transmit(uint8_t *buffer, uint8_t bufferLen)
{
uint8_t regValue;
uint8_t i;
if ((RadioConfiguration.flags & RADIO_FLAG_RXDATA) != 0)
{
return ERR_BUFFER_LOCKED;
}
if ((RadioConfiguration.flags & (RADIO_FLAG_TRANSMITTING | RADIO_FLAG_RECEIVING)) != 0)
{
return ERR_RADIO_BUSY;
}
if ((MODULATION_FSK == RadioConfiguration.modulation) && (bufferLen > 64))
{
return ERR_DATA_SIZE;
}
if(RADIO_GetPABoost()) V1_SetLow();
else V1_SetHigh();
SwTimerStop(RadioConfiguration.timeOnAirTimerId);
// Since we're interested in a transmission, rxWindowSize is irrelevant.
// Setting it to 4 is a valid option.
RADIO_WriteConfiguration(4);
if (MODULATION_LORA == RadioConfiguration.modulation)
{
RADIO_RegisterWrite(REG_LORA_PAYLOADLENGTH, bufferLen);
// Configure PaRamp
regValue = RADIO_RegisterRead(REG_PARAMP);
regValue &= ~0x0F; // Clear lower 4 bits
regValue |= 0x08; // 50us PA Ramp-up time
RADIO_RegisterWrite(REG_PARAMP, regValue);
// DIO0 = 01 means TxDone in LoRa mode.
// DIO2 = 00 means FHSSChangeChannel
RADIO_RegisterWrite(REG_DIOMAPPING1, 0x40);
RADIO_RegisterWrite(REG_DIOMAPPING2, 0x00);
RADIO_WriteMode(MODE_STANDBY, RadioConfiguration.modulation, 1);
}
else
{
// DIO0 = 00 means PacketSent in FSK Tx mode
RADIO_RegisterWrite(REG_DIOMAPPING1, 0x00);
RADIO_RegisterWrite(REG_DIOMAPPING2, 0x00);
}
if (MODULATION_FSK == RadioConfiguration.modulation)
{
// FSK requires the length to be sent to the FIFO
RADIO_RegisterWrite(REG_FIFO, bufferLen);
}
HALSPICSAssert();
HALSPISend(REG_WRITE | REG_FIFO);
for (i = 0; i < bufferLen; i++)
{
HALSPISend(buffer[i]);
}
HALSPICSDeassert();
RadioConfiguration.flags |= RADIO_FLAG_TRANSMITTING;
RadioConfiguration.flags &= ~(RADIO_FLAG_TIMEOUT | RADIO_FLAG_RXERROR);
// Non blocking switch. We don't really care when it starts transmitting.
// If accurate timing of the time on air is required, the simplest way to
// achieve it is to change this to a blocking mode switch.
RADIO_WriteMode(MODE_TX, RadioConfiguration.modulation, 0);
send_chars("Transmit start ");
// Set timeout to some very large value since the timer counts down.
// Leaving the callback uninitialized it will assume the default value of
// NULL which in turns means no callback.
SwTimerSetTimeout(RadioConfiguration.timeOnAirTimerId, MS_TO_TICKS(TIME_ON_AIR_LOAD_VALUE));
SwTimerStart(RadioConfiguration.timeOnAirTimerId);
if (0 != RadioConfiguration.watchdogTimerTimeout)
{
SwTimerSetTimeout(RadioConfiguration.watchdogTimerId, MS_TO_TICKS(RadioConfiguration.watchdogTimerTimeout));
SwTimerStart(RadioConfiguration.watchdogTimerId);
}
data.batLevel=getBatteryLevel();
return ERR_NONE;
}
// rxWindowSize parameter is in symbols for LoRa and ms for FSK
RadioError_t RADIO_ReceiveStart(uint16_t rxWindowSize)
{
if ((RadioConfiguration.flags & RADIO_FLAG_RXDATA) != 0)
{
return ERR_BUFFER_LOCKED;
}
if ((RadioConfiguration.flags & (RADIO_FLAG_TRANSMITTING | RADIO_FLAG_RECEIVING)) != 0)
{
return ERR_RADIO_BUSY;
}
if (0 == rxWindowSize)
{
RADIO_WriteConfiguration(4);
}
else
{
RADIO_WriteConfiguration(rxWindowSize);
}
V1_SetHigh();
if (MODULATION_LORA == RadioConfiguration.modulation)
{
// All LoRa packets are received with explicit header, so this register
// is not used. However, a value of 0 is not allowed.
RADIO_RegisterWrite(REG_LORA_PAYLOADLENGTH, 0x01);
// DIO0 = 00 means RxDone in LoRa mode
// DIO1 = 00 means RxTimeout in LoRa mode
// DIO2 = 00 means FHSSChangeChannel
// Other DIOs are unused.
RADIO_RegisterWrite(REG_DIOMAPPING1, 0x00);
RADIO_RegisterWrite(REG_DIOMAPPING2, 0x00);
}
else
{
RADIO_RegisterWrite(REG_FSK_RXBW, RadioConfiguration.rxBw);
RADIO_RegisterWrite(REG_FSK_AFCBW, RadioConfiguration.afcBw);
// DIO0 = 00 means PayloadReady in FSK Rx mode
RADIO_RegisterWrite(REG_DIOMAPPING1, 0x00);
RADIO_RegisterWrite(REG_DIOMAPPING2, 0x00);
}
RadioConfiguration.flags |= RADIO_FLAG_RECEIVING;
RadioConfiguration.flags &= ~(RADIO_FLAG_TIMEOUT | RADIO_FLAG_RXERROR);
// Will use non blocking switches to RadioSetMode. We don't really care
// when it starts receiving.
if (0 == rxWindowSize)
{
RADIO_WriteMode(MODE_RXCONT, RadioConfiguration.modulation, 0);
}
else
{
if (MODULATION_LORA == RadioConfiguration.modulation)
{
RADIO_WriteMode(MODE_RXSINGLE, MODULATION_LORA, 0);
}
else
{
RADIO_WriteMode(MODE_RXCONT, MODULATION_FSK, 0);
SwTimerSetTimeout(RadioConfiguration.fskRxWindowTimerId, MS_TO_TICKS_SHORT(rxWindowSize));
SwTimerStart(RadioConfiguration.fskRxWindowTimerId);
}
}
send_chars("Receiving start... ");
if (0 != RadioConfiguration.watchdogTimerTimeout)
{
SwTimerSetTimeout(RadioConfiguration.watchdogTimerId, MS_TO_TICKS(RadioConfiguration.watchdogTimerTimeout));
SwTimerStart(RadioConfiguration.watchdogTimerId);
}
return ERR_NONE;
}
void RADIO_ReceiveStop(void)
{
if (RADIO_FLAG_RECEIVING == RadioConfiguration.flags)
{
RADIO_WriteMode(MODE_SLEEP, RadioConfiguration.modulation, 0);
RadioConfiguration.flags &= ~RADIO_FLAG_RECEIVING;
}
}
static void RADIO_RxDone(void)
{
uint8_t i, irqFlags;
irqFlags = RADIO_RegisterRead(REG_LORA_IRQFLAGS);
// Clear RxDone interrupt (also CRC error and ValidHeader interrupts, if
// they exist)
RADIO_RegisterWrite(REG_LORA_IRQFLAGS, (1<<SHIFT6) | (1<<SHIFT5) | (1<<SHIFT4));