-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathLoRa.c
594 lines (466 loc) · 17.8 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
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
#include "LoRa.h"
/* ----------------------------------------------------------------------------- *\
name : newLoRa
description : it's a constructor for LoRa structure that assign default values
and pass created object (LoRa struct instanse)
arguments : Nothing
returns : A LoRa object whith these default values:
----------------------------------------
| carrier frequency = 433 MHz |
| spreading factor = 7 |
| bandwidth = 125 KHz |
| coding rate = 4/5 |
----------------------------------------
\* ----------------------------------------------------------------------------- */
LoRa newLoRa(){
LoRa new_LoRa;
new_LoRa.frequency = 433 ;
new_LoRa.spredingFactor = SF_7 ;
new_LoRa.bandWidth = BW_125KHz ;
new_LoRa.crcRate = CR_4_5 ;
new_LoRa.power = POWER_20db;
new_LoRa.overCurrentProtection = 100 ;
new_LoRa.preamble = 8 ;
return new_LoRa;
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_reset
description : reset module
arguments :
LoRa* LoRa --> LoRa object handler
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_reset(LoRa* _LoRa){
HAL_GPIO_WritePin(_LoRa->reset_port, _LoRa->reset_pin, GPIO_PIN_RESET);
HAL_Delay(1);
HAL_GPIO_WritePin(_LoRa->reset_port, _LoRa->reset_pin, GPIO_PIN_SET);
HAL_Delay(100);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_gotoMode
description : set LoRa Op mode
arguments :
LoRa* LoRa --> LoRa object handler
mode --> select from defined modes
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_gotoMode(LoRa* _LoRa, int mode){
uint8_t read;
uint8_t data;
read = LoRa_read(_LoRa, RegOpMode);
data = read;
if(mode == SLEEP_MODE){
data = (read & 0xF8) | 0x00;
_LoRa->current_mode = SLEEP_MODE;
}else if (mode == STNBY_MODE){
data = (read & 0xF8) | 0x01;
_LoRa->current_mode = STNBY_MODE;
}else if (mode == TRANSMIT_MODE){
data = (read & 0xF8) | 0x03;
_LoRa->current_mode = TRANSMIT_MODE;
}else if (mode == RXCONTIN_MODE){
data = (read & 0xF8) | 0x05;
_LoRa->current_mode = RXCONTIN_MODE;
}else if (mode == RXSINGLE_MODE){
data = (read & 0xF8) | 0x06;
_LoRa->current_mode = RXSINGLE_MODE;
}
LoRa_write(_LoRa, RegOpMode, data);
//HAL_Delay(10);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_readReg
description : read a register(s) by an address and a length,
then store value(s) at outpur array.
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t* address --> pointer to the beginning of address array
uint16_t r_length --> detemines number of addresse bytes that
you want to send
uint8_t* output --> pointer to the beginning of output array
uint16_t w_length --> detemines number of bytes that you want to read
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_readReg(LoRa* _LoRa, uint8_t* address, uint16_t r_length, uint8_t* output, uint16_t w_length){
HAL_GPIO_WritePin(_LoRa->CS_port, _LoRa->CS_pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(_LoRa->hSPIx, address, r_length, TRANSMIT_TIMEOUT);
while (HAL_SPI_GetState(_LoRa->hSPIx) != HAL_SPI_STATE_READY)
;
HAL_SPI_Receive(_LoRa->hSPIx, output, w_length, RECEIVE_TIMEOUT);
while (HAL_SPI_GetState(_LoRa->hSPIx) != HAL_SPI_STATE_READY)
;
HAL_GPIO_WritePin(_LoRa->CS_port, _LoRa->CS_pin, GPIO_PIN_SET);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_writeReg
description : write a value(s) in a register(s) by an address
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t* address --> pointer to the beginning of address array
uint16_t r_length --> detemines number of addresse bytes that
you want to send
uint8_t* output --> pointer to the beginning of values array
uint16_t w_length --> detemines number of bytes that you want to send
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_writeReg(LoRa* _LoRa, uint8_t* address, uint16_t r_length, uint8_t* values, uint16_t w_length){
HAL_GPIO_WritePin(_LoRa->CS_port, _LoRa->CS_pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(_LoRa->hSPIx, address, r_length, TRANSMIT_TIMEOUT);
while (HAL_SPI_GetState(_LoRa->hSPIx) != HAL_SPI_STATE_READY)
;
HAL_SPI_Transmit(_LoRa->hSPIx, values, w_length, TRANSMIT_TIMEOUT);
while (HAL_SPI_GetState(_LoRa->hSPIx) != HAL_SPI_STATE_READY)
;
HAL_GPIO_WritePin(_LoRa->CS_port, _LoRa->CS_pin, GPIO_PIN_SET);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setLowDaraRateOptimization
description : set the LowDataRateOptimization flag. Is is mandated for when the symbol length exceeds 16ms.
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t value --> 0 to disable, otherwise to enable
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setLowDaraRateOptimization(LoRa* _LoRa, uint8_t value){
uint8_t data;
uint8_t read;
read = LoRa_read(_LoRa, RegModemConfig3);
if(value)
data = read | 0x08;
else
data = read & 0xF7;
LoRa_write(_LoRa, RegModemConfig3, data);
HAL_Delay(10);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setAutoLDO
description : set the LowDataRateOptimization flag automatically based on the symbol length.
arguments :
LoRa* LoRa --> LoRa object handler
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setAutoLDO(LoRa* _LoRa){
double BW[] = {7.8, 10.4, 15.6, 20.8, 31.25, 41.7, 62.5, 125.0, 250.0, 500.0};
LoRa_setLowDaraRateOptimization(_LoRa, (long)((1 << _LoRa->spredingFactor) / ((double)BW[_LoRa->bandWidth])) > 16.0);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setFrequency
description : set carrier frequency e.g 433 MHz
arguments :
LoRa* LoRa --> LoRa object handler
int freq --> desired frequency in MHz unit, e.g 434
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setFrequency(LoRa* _LoRa, int freq){
uint8_t data;
uint32_t F;
F = (freq * 524288)>>5;
// write Msb:
data = F >> 16;
LoRa_write(_LoRa, RegFrMsb, data);
HAL_Delay(5);
// write Mid:
data = F >> 8;
LoRa_write(_LoRa, RegFrMid, data);
HAL_Delay(5);
// write Lsb:
data = F >> 0;
LoRa_write(_LoRa, RegFrLsb, data);
HAL_Delay(5);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setSpreadingFactor
description : set spreading factor, from 7 to 12.
arguments :
LoRa* LoRa --> LoRa object handler
int SP --> desired spreading factor e.g 7
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setSpreadingFactor(LoRa* _LoRa, int SF){
uint8_t data;
uint8_t read;
if(SF>12)
SF = 12;
if(SF<7)
SF = 7;
read = LoRa_read(_LoRa, RegModemConfig2);
HAL_Delay(10);
data = (SF << 4) + (read & 0x0F);
LoRa_write(_LoRa, RegModemConfig2, data);
HAL_Delay(10);
LoRa_setAutoLDO(_LoRa);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setPower
description : set power gain.
arguments :
LoRa* LoRa --> LoRa object handler
int power --> desired power like POWER_17db
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setPower(LoRa* _LoRa, uint8_t power){
LoRa_write(_LoRa, RegPaConfig, power);
HAL_Delay(10);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setOCP
description : set maximum allowed current.
arguments :
LoRa* LoRa --> LoRa object handler
int current --> desired max currnet in mA, e.g 120
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setOCP(LoRa* _LoRa, uint8_t current){
uint8_t OcpTrim = 0;
if(current<45)
current = 45;
if(current>240)
current = 240;
if(current <= 120)
OcpTrim = (current - 45)/5;
else if(current <= 240)
OcpTrim = (current + 30)/10;
OcpTrim = OcpTrim + (1 << 5);
LoRa_write(_LoRa, RegOcp, OcpTrim);
HAL_Delay(10);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setTOMsb_setCRCon
description : set timeout msb to 0xFF + set CRC enable.
arguments :
LoRa* LoRa --> LoRa object handler
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setTOMsb_setCRCon(LoRa* _LoRa){
uint8_t read, data;
read = LoRa_read(_LoRa, RegModemConfig2);
data = read | 0x07;
LoRa_write(_LoRa, RegModemConfig2, data);\
HAL_Delay(10);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_setTOMsb_setCRCon
description : set timeout msb to 0xFF + set CRC enable.
arguments :
LoRa* LoRa --> LoRa object handler
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_setSyncWord(LoRa* _LoRa, uint8_t syncword){
LoRa_write(_LoRa, RegSyncWord, syncword);
HAL_Delay(10);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_read
description : read a register by an address
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t address --> address of the register e.g 0x1D
returns : register value
\* ----------------------------------------------------------------------------- */
uint8_t LoRa_read(LoRa* _LoRa, uint8_t address){
uint8_t read_data;
uint8_t data_addr;
data_addr = address & 0x7F;
LoRa_readReg(_LoRa, &data_addr, 1, &read_data, 1);
//HAL_Delay(5);
return read_data;
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_write
description : write a value in a register by an address
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t address --> address of the register e.g 0x1D
uint8_t value --> value that you want to write
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_write(LoRa* _LoRa, uint8_t address, uint8_t value){
uint8_t data;
uint8_t addr;
addr = address | 0x80;
data = value;
LoRa_writeReg(_LoRa, &addr, 1, &data, 1);
//HAL_Delay(5);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_BurstWrite
description : write a set of values in a register by an address respectively
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t address --> address of the register e.g 0x1D
uint8_t *value --> address of values that you want to write
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_BurstWrite(LoRa* _LoRa, uint8_t address, uint8_t *value, uint8_t length){
uint8_t addr;
addr = address | 0x80;
//NSS = 1
HAL_GPIO_WritePin(_LoRa->CS_port, _LoRa->CS_pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(_LoRa->hSPIx, &addr, 1, TRANSMIT_TIMEOUT);
while (HAL_SPI_GetState(_LoRa->hSPIx) != HAL_SPI_STATE_READY)
;
//Write data in FiFo
HAL_SPI_Transmit(_LoRa->hSPIx, value, length, TRANSMIT_TIMEOUT);
while (HAL_SPI_GetState(_LoRa->hSPIx) != HAL_SPI_STATE_READY)
;
//NSS = 0
//HAL_Delay(5);
HAL_GPIO_WritePin(_LoRa->CS_port, _LoRa->CS_pin, GPIO_PIN_SET);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_isvalid
description : check the LoRa instruct values
arguments :
LoRa* LoRa --> LoRa object handler
returns : returns 1 if all of the values were given, otherwise returns 0
\* ----------------------------------------------------------------------------- */
uint8_t LoRa_isvalid(LoRa* _LoRa){
return 1;
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_transmit
description : Transmit data
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t data --> A pointer to the data you wanna send
uint8_t length --> Size of your data in Bytes
uint16_t timeOut --> Timeout in milliseconds
returns : 1 in case of success, 0 in case of timeout
\* ----------------------------------------------------------------------------- */
uint8_t LoRa_transmit(LoRa* _LoRa, uint8_t* data, uint8_t length, uint16_t timeout){
uint8_t read;
int mode = _LoRa->current_mode;
LoRa_gotoMode(_LoRa, STNBY_MODE);
read = LoRa_read(_LoRa, RegFiFoTxBaseAddr);
LoRa_write(_LoRa, RegFiFoAddPtr, read);
LoRa_write(_LoRa, RegPayloadLength, length);
LoRa_BurstWrite(_LoRa, RegFiFo, data, length);
LoRa_gotoMode(_LoRa, TRANSMIT_MODE);
while(1){
read = LoRa_read(_LoRa, RegIrqFlags);
if((read & 0x08)!=0){
LoRa_write(_LoRa, RegIrqFlags, 0xFF);
LoRa_gotoMode(_LoRa, mode);
return 1;
}
else{
if(--timeout==0){
LoRa_gotoMode(_LoRa, mode);
return 0;
}
}
HAL_Delay(1);
}
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_startReceiving
description : Start receiving continuously
arguments :
LoRa* LoRa --> LoRa object handler
returns : Nothing
\* ----------------------------------------------------------------------------- */
void LoRa_startReceiving(LoRa* _LoRa){
LoRa_gotoMode(_LoRa, RXCONTIN_MODE);
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_Receive
description : Read received data from module
arguments :
LoRa* LoRa --> LoRa object handler
uint8_t data --> A pointer to the array that you want to write bytes in it
uint8_t length --> Determines how many bytes you want to read
returns : The number of bytes received
\* ----------------------------------------------------------------------------- */
uint8_t LoRa_receive(LoRa* _LoRa, uint8_t* data, uint8_t length){
uint8_t read;
uint8_t number_of_bytes;
uint8_t min = 0;
for(int i=0; i<length; i++)
data[i]=0;
LoRa_gotoMode(_LoRa, STNBY_MODE);
read = LoRa_read(_LoRa, RegIrqFlags);
if((read & 0x40) != 0){
LoRa_write(_LoRa, RegIrqFlags, 0xFF);
number_of_bytes = LoRa_read(_LoRa, RegRxNbBytes);
read = LoRa_read(_LoRa, RegFiFoRxCurrentAddr);
LoRa_write(_LoRa, RegFiFoAddPtr, read);
min = length >= number_of_bytes ? number_of_bytes : length;
for(int i=0; i<min; i++)
data[i] = LoRa_read(_LoRa, RegFiFo);
}
LoRa_gotoMode(_LoRa, RXCONTIN_MODE);
return min;
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_getRSSI
description : initialize and set the right setting according to LoRa sruct vars
arguments :
LoRa* LoRa --> LoRa object handler
returns : Returns the RSSI value of last received packet.
\* ----------------------------------------------------------------------------- */
int LoRa_getRSSI(LoRa* _LoRa){
uint8_t read;
read = LoRa_read(_LoRa, RegPktRssiValue);
return -164 + read;
}
/* ----------------------------------------------------------------------------- *\
name : LoRa_init
description : initialize and set the right setting according to LoRa sruct vars
arguments :
LoRa* LoRa --> LoRa object handler
returns : Nothing
\* ----------------------------------------------------------------------------- */
uint16_t LoRa_init(LoRa* _LoRa){
uint8_t data;
uint8_t read;
if(LoRa_isvalid(_LoRa)){
// goto sleep mode:
LoRa_gotoMode(_LoRa, SLEEP_MODE);
HAL_Delay(10);
// turn on LoRa mode:
read = LoRa_read(_LoRa, RegOpMode);
HAL_Delay(10);
data = read | 0x80;
LoRa_write(_LoRa, RegOpMode, data);
HAL_Delay(100);
// set frequency:
LoRa_setFrequency(_LoRa, _LoRa->frequency);
// set output power gain:
LoRa_setPower(_LoRa, _LoRa->power);
// set over current protection:
LoRa_setOCP(_LoRa, _LoRa->overCurrentProtection);
// set LNA gain:
LoRa_write(_LoRa, RegLna, 0x23);
// set spreading factor, CRC on, and Timeout Msb:
LoRa_setTOMsb_setCRCon(_LoRa);
LoRa_setSpreadingFactor(_LoRa, _LoRa->spredingFactor);
// set Timeout Lsb:
LoRa_write(_LoRa, RegSymbTimeoutL, 0xFF);
// set bandwidth, coding rate and expilicit mode:
// 8 bit RegModemConfig --> | X | X | X | X | X | X | X | X |
// bits represent --> | bandwidth | CR |I/E|
data = 0;
data = (_LoRa->bandWidth << 4) + (_LoRa->crcRate << 1);
LoRa_write(_LoRa, RegModemConfig1, data);
LoRa_setAutoLDO(_LoRa);
// set preamble:
LoRa_write(_LoRa, RegPreambleMsb, _LoRa->preamble >> 8);
LoRa_write(_LoRa, RegPreambleLsb, _LoRa->preamble >> 0);
// DIO mapping: --> DIO: RxDone
read = LoRa_read(_LoRa, RegDioMapping1);
data = read | 0x3F;
LoRa_write(_LoRa, RegDioMapping1, data);
// goto standby mode:
LoRa_gotoMode(_LoRa, STNBY_MODE);
_LoRa->current_mode = STNBY_MODE;
HAL_Delay(10);
read = LoRa_read(_LoRa, RegVersion);
if(read == 0x12)
return LORA_OK;
else
return LORA_NOT_FOUND;
}
else {
return LORA_UNAVAILABLE;
}
}