-
Notifications
You must be signed in to change notification settings - Fork 9
/
lora.c
executable file
·557 lines (451 loc) · 12.9 KB
/
lora.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
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/spi.h"
#include "types.h"
#include "misc.h"
#include "lora.h"
#define PIN_MISO 16
#define PIN_CS 17
#define PIN_SCK 18
#define PIN_MOSI 19
#define PIN_DIO0 22
#define SPI_PORT spi0
#define READ_BIT 0x80
#define LORA_RTTY_COUNT 0
#define LORA_RTTY_EVERY 0
#define LORA_CALL_COUNT 0
#define LORA_CALL_MODE 5
#define LORA_CALL_FREQ 433.650
#define LORA_BINARY 0
#define LORA_REPEAT_SLOT_1 0
#define LORA_REPEAT_SLOT_2 0
#define LORA_SLOT 0
#define LORA_CYCLETIME 0
#define LORA_ID 0
typedef enum {lmIdle, lmListening, lmSending} tLoRaMode;
struct TBinaryPacket
{
uint8_t PayloadIDs;
uint16_t Counter;
uint16_t BiSeconds;
float Latitude;
float Longitude;
int32_t Altitude;
}; // __attribute__ ((packed));
static tLoRaMode LoRaMode;
static char PayloadID[32];
static int CallingCount=0;
static int RTTYCount=0;
static int InRTTYMode=0;
static int SendingRTTY=0;
static unsigned long TimeToSendIfNoGPS=0;
static int RTTYIndex, RTTYMask, RTTYLength;
static int FSKBitRate, FSKOverSample, RTTYBitLength;
static struct TBinaryPacket PacketToRepeat;
static uint8_t SendRepeatedPacket, RepeatedPacketType=0;
static unsigned char Sentence[256];
static int ImplicitOrExplicit;
static inline void cs_select()
{
asm volatile("nop \n nop \n nop");
gpio_put(PIN_CS, 0); // Active low
asm volatile("nop \n nop \n nop");
}
static inline void cs_deselect()
{
asm volatile("nop \n nop \n nop");
gpio_put(PIN_CS, 1);
asm volatile("nop \n nop \n nop");
}
static void writeRegister(uint8_t reg, uint8_t data)
{
uint8_t buf[2];
buf[0] = reg | 0x80;
buf[1] = data;
cs_select();
spi_write_blocking(SPI_PORT, buf, 2);
cs_deselect();
sleep_ms(1);
}
static uint8_t readRegister(uint8_t addr)
{
uint8_t buf[1];
addr &= 0x7F;
cs_select();
spi_write_blocking(SPI_PORT, &addr, 1);
sleep_ms(1);
spi_read_blocking(SPI_PORT, 0, buf, 1);
cs_deselect();
sleep_ms(1);
// printf("READ %02X\n", buf[0]);
return buf[0];
}
void SetDeviceMode(uint8_t newMode)
{
static uint8_t currentMode = 0xFF;
if (newMode == currentMode)
return;
switch (newMode)
{
case RF98_MODE_TX:
writeRegister(REG_LNA, LNA_OFF_GAIN); // TURN LNA OFF FOR TRANSMITT
writeRegister(REG_PA_CONFIG, PA_MAX_UK);
writeRegister(REG_OPMODE, newMode);
currentMode = newMode;
break;
case RF98_MODE_RX_CONTINUOUS:
writeRegister(REG_PA_CONFIG, PA_OFF_BOOST); // TURN PA OFF FOR RECIEVE??
writeRegister(REG_LNA, LNA_MAX_GAIN); // MAX GAIN FOR RECIEVE
writeRegister(REG_OPMODE, newMode);
currentMode = newMode;
break;
case RF98_MODE_SLEEP:
writeRegister(REG_OPMODE, newMode);
currentMode = newMode;
break;
case RF98_MODE_STANDBY:
writeRegister(REG_OPMODE, newMode);
currentMode = newMode;
break;
default: return;
}
if (newMode != RF98_MODE_SLEEP)
{
sleep_ms(1);
}
return;
}
void SetModemToLoRaMode()
{
// printf("Setting LoRa Mode\n");
SetDeviceMode(RF98_MODE_SLEEP);
writeRegister(REG_OPMODE,0x80);
// printf("LoRa Mode Set\n");
}
void SetFrequency(double Frequency)
{
unsigned long FrequencyValue;
// printf("Frequency is %.3f\n", Frequency);
Frequency = Frequency * 7110656 / 434;
FrequencyValue = (unsigned long)(Frequency);
// printf("FrequencyValue is %lu\n", FrequencyValue);
writeRegister(0x06, (FrequencyValue >> 16) & 0xFF); // Set frequency
writeRegister(0x07, (FrequencyValue >> 8) & 0xFF);
writeRegister(0x08, FrequencyValue & 0xFF);
}
void SetupRFM98(float Frequency, int Mode)
{
int ErrorCoding;
int Bandwidth;
int SpreadingFactor;
int LowDataRateOptimize;
int PayloadLength;
SetModemToLoRaMode();
// Frequency
SetFrequency(Frequency); // + LORA_OFFSET / 1000.0);
// LoRa settings for various modes. We support modes 2 (repeater mode), 1 (normally used for SSDV) and 0 (normal slow telemetry mode).
if (Mode == 5)
{
ImplicitOrExplicit = EXPLICIT_MODE;
ErrorCoding = ERROR_CODING_4_8;
Bandwidth = BANDWIDTH_41K7;
SpreadingFactor = SPREADING_11;
LowDataRateOptimize = 0;
}
else if (Mode == 2)
{
ImplicitOrExplicit = EXPLICIT_MODE;
ErrorCoding = ERROR_CODING_4_8;
Bandwidth = BANDWIDTH_62K5;
SpreadingFactor = SPREADING_8;
LowDataRateOptimize = 0;
}
else if (Mode == 1)
{
ImplicitOrExplicit = IMPLICIT_MODE;
ErrorCoding = ERROR_CODING_4_5;
Bandwidth = BANDWIDTH_20K8;
SpreadingFactor = SPREADING_6;
LowDataRateOptimize = 0;
}
else // if (Mode == 0)
{
ImplicitOrExplicit = EXPLICIT_MODE;
ErrorCoding = ERROR_CODING_4_8;
Bandwidth = BANDWIDTH_20K8;
SpreadingFactor = SPREADING_11;
LowDataRateOptimize = 0x08;
}
PayloadLength = ImplicitOrExplicit == IMPLICIT_MODE ? 255 : 0;
writeRegister(REG_MODEM_CONFIG, ImplicitOrExplicit | ErrorCoding | Bandwidth);
writeRegister(REG_MODEM_CONFIG2, SpreadingFactor | CRC_ON);
writeRegister(REG_MODEM_CONFIG3, 0x04 | LowDataRateOptimize); // 0x04: AGC sets LNA gain
// writeRegister(REG_DETECT_OPT, (SpreadingFactor == SPREADING_6) ? 0x05 : 0x03); // 0x05 For SF6; 0x03 otherwise
writeRegister(REG_DETECT_OPT, (readRegister(REG_DETECT_OPT) & 0xF8) | ((SpreadingFactor == SPREADING_6) ? 0x05 : 0x03)); // 0x05 For SF6; 0x03 otherwise
writeRegister(REG_DETECTION_THRESHOLD, (SpreadingFactor == SPREADING_6) ? 0x0C : 0x0A); // 0x0C for SF6, 0x0A otherwise
writeRegister(REG_PAYLOAD_LENGTH, PayloadLength);
writeRegister(REG_RX_NB_BYTES, PayloadLength);
// Change the DIO mapping to 01 so we can listen for TxDone on the interrupt
writeRegister(REG_DIO_MAPPING_1,0x40);
writeRegister(REG_DIO_MAPPING_2,0x00);
// Go to standby mode
SetDeviceMode(RF98_MODE_STANDBY);
// printf("Setup Complete\n");
}
int BuildLoRaCall(unsigned char *TxLine)
{
/*
char Frequency[8];
dtostrf(LORA_FREQUENCY, 7, 3, Frequency);
sprintf((char *)TxLine, "^^%s,%s,%d,%d,%d,%d,%d",
LORA_PAYLOAD_ID, Frequency,
LORA_MODE == 1 ? 1 : 0,
LORA_MODE == 1 ? ERROR_CODING_4_5 : ERROR_CODING_4_8,
LORA_MODE == 2 ? BANDWIDTH_62K5 : BANDWIDTH_20K8,
LORA_MODE == 2 ? SPREADING_8 : (LORA_MODE == 1 ? SPREADING_6 : SPREADING_11),
LORA_MODE == 0 ? 0x08 : 0);
return strlen((char *)TxLine) + 1;
*/
}
int BuildLoRaPositionPacket(struct TGPS *GPS, unsigned char *TxLine)
{
/*
struct TBinaryPacket BinaryPacket;
SentenceCounter++;
BinaryPacket.PayloadIDs = 0xC0 | (LORA_ID << 3) | LORA_ID;
BinaryPacket.Counter = SentenceCounter;
BinaryPacket.BiSeconds = GPS->SecondsInDay / 2L;
BinaryPacket.Latitude = GPS->Latitude;
BinaryPacket.Longitude = GPS->Longitude;
BinaryPacket.Altitude = GPS->Altitude;
memcpy(TxLine, &BinaryPacket, sizeof(BinaryPacket));
return sizeof(struct TBinaryPacket);
*/
}
int TimeToSend(struct TGPS *GPS)
{
int CycleSeconds;
SendRepeatedPacket = 0;
if (LORA_CYCLETIME == 0)
{
// Not using time to decide when we can send
return 1;
}
/*
if ((millis() > (LastLoRaTX + LORA_CYCLETIME*1000+2000)) && (TimeToSendIfNoGPS == 0))
{
// Timed out
printf("Using Timeout\n");
return 1;
}
*/
if (GPS->Satellites > 0)
{
/*
static int LastCycleSeconds=-1;
// Can't Tx twice at the same time
CycleSeconds = (GPS->SecondsInDay+LORA_CYCLETIME-17) % LORA_CYCLETIME; // Could just use GPS time, but it's nice to see the slot agree with UTC
if (CycleSeconds != LastCycleSeconds)
{
LastCycleSeconds = CycleSeconds;
if (CycleSeconds == LORA_SLOT)
{
printf("Using GPS Timing\n");
SendRepeatedPacket = 0;
return 1;
}
if (RepeatedPacketType && ((CycleSeconds == LORA_REPEAT_SLOT_1) || (CycleSeconds == LORA_REPEAT_SLOT_2)))
{
printf("Time to repeat\n");
SendRepeatedPacket = RepeatedPacketType;
RepeatedPacketType = 0;
return 1;
}
}
*/
}
/*
else if ((TimeToSendIfNoGPS > 0) && (millis() >= TimeToSendIfNoGPS))
{
printf("Using LoRa Timing\n");
SendRepeatedPacket = 0;
return 1;
}
*/
return 0;
}
int LoRaIsFree(struct TGPS *GPS)
{
if ((LoRaMode != lmSending) || gpio_get(PIN_DIO0))
{
// Either not sending, or was but now it's sent. Clear the flag if we need to
if (LoRaMode == lmSending)
{
// Clear that IRQ flag
writeRegister( REG_IRQ_FLAGS, 0x08);
LoRaMode = lmIdle;
}
// Now we test to see if we're doing TDM or not
// For TDM, if it's not a slot that we send in, then we should be in listening mode
// Otherwise, we just send
if (TimeToSend(GPS))
{
// Either sending continuously, or it's our slot to send in
// printf("Channel %d is free\n", Channel);
return 1;
}
/*
if (LORA_CYCLETIME > 0)
{
// TDM system and not time to send, so we can listen
if (LoRaMode == lmIdle)
{
startReceiving();
}
}
*/
}
return 0;
}
void setup_lora(float Frequency, int Mode, char *Callsign)
{
// Set up SPI LoRa Module
printf(" - Init LORA - ");
// SPI0 at 0.5MHz.
spi_init(SPI_PORT, 500*1000);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_init(PIN_CS);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
// Chip select is active-low, so we'll initialise it to a driven-high state
gpio_set_dir(PIN_CS, GPIO_OUT);
gpio_put(PIN_CS, 1);
// DIO0 is input
gpio_set_dir(PIN_DIO0, GPIO_IN);
SetupRFM98(Frequency, Mode);
strcpy(PayloadID, Callsign);
printf("OK\n");
}
void SendLoRaPacket(unsigned char *buffer, int Length, int CallingPacket)
{
uint8_t buf[1];
// LastLoRaTX = millis();
TimeToSendIfNoGPS = 0;
// if (InRTTYMode != 0)
// {
// setupRFM98(LORA_FREQUENCY, LORA_MODE);
// InRTTYMode = 0;
// }
InRTTYMode = 0;
/*
if (CallingPacket)
{
SsetupRFM98(LORA_CALL_FREQ, LORA_CALL_MODE);
}
else
{
setupRFM98(LORA_FREQUENCY, LORA_MODE);
}
*/
// printf("Sending %d bytes\n", Length);
SetDeviceMode(RF98_MODE_STANDBY);
writeRegister(REG_DIO_MAPPING_1, 0x40); // 01 00 00 00 maps DIO0 to TxDone
writeRegister(REG_FIFO_TX_BASE_AD, 0x00); // Update the address ptr to the current tx base address
writeRegister(REG_FIFO_ADDR_PTR, 0x00);
if (ImplicitOrExplicit == EXPLICIT_MODE)
{
writeRegister(REG_PAYLOAD_LENGTH, Length);
}
cs_select();
buf[0] = REG_FIFO | 0x80;
cs_select();
spi_write_blocking(SPI_PORT, buf, 1);
spi_write_blocking(SPI_PORT, buffer, Length);
cs_deselect();
// go into transmit mode
SetDeviceMode(RF98_MODE_TX);
LoRaMode = lmSending;
SendingRTTY = 0;
}
void check_lora(struct TGPS *GPS)
{
// CheckFSKBuffer();
// CheckLoRaRx();
if (LoRaIsFree(GPS))
{
// printf("LoRa is free\n");
if (SendRepeatedPacket == 3)
{
// Repeat ASCII sentence
Sentence[0] = '%';
SendLoRaPacket(Sentence, strlen((char *)Sentence)+1, 0);
RepeatedPacketType = 0;
SendRepeatedPacket = 0;
}
else if (SendRepeatedPacket == 2)
{
printf("Repeating uplink packet\n");
// 0x80 | (LORA_ID << 3) | TargetID
SendLoRaPacket((unsigned char *)&PacketToRepeat, sizeof(PacketToRepeat), 0);
RepeatedPacketType = 0;
SendRepeatedPacket = 0;
}
else if (SendRepeatedPacket == 1)
{
printf("Repeating balloon packet\n");
// 0x80 | (LORA_ID << 3) | TargetID
RepeatedPacketType = 0;
SendRepeatedPacket = 0;
}
else
{
int PacketLength;
if (++RTTYCount >= (LORA_RTTY_COUNT + LORA_RTTY_EVERY))
{
RTTYCount = 0;
}
if (RTTYCount < LORA_RTTY_COUNT)
{
/*
// Send RTTY packet
PacketLength = BuildSentence(GPS, (char *)Sentence, PayloadID);
printf("LoRa: Tx RTTY packet\n");
SendLoRaRTTY(PacketLength);
*/
}
else
{
if ((LORA_CALL_COUNT > 0) && (++CallingCount > LORA_CALL_COUNT))
{
CallingCount = 0;
SetupRFM98(LORA_CALL_FREQ, LORA_CALL_MODE);
PacketLength = BuildLoRaCall(Sentence);
printf("LoRa: Calling Mode");
SendLoRaPacket(Sentence, PacketLength, 1);
}
else
{
// if ((LORA_CALL_COUNT > 0) && (CallingCount == 1))
// {
// setupRFM98(LORA_FREQUENCY, LORA_MODE);
// }
if (LORA_BINARY)
{
// 0x80 | (LORA_ID << 3) | TargetID
PacketLength = BuildLoRaPositionPacket(GPS, Sentence);
printf("LoRa: Tx Binary packet");
}
else
{
PacketLength = BuildSentence(GPS, (char *)Sentence, PayloadID);
// printf("LoRa: Tx ASCII Sentence\n");
printf("\n%s\r\n", (char *)Sentence);
}
SendLoRaPacket(Sentence, PacketLength, 0);
}
}
}
}
}