-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJora.cpp
606 lines (462 loc) · 12.1 KB
/
Jora.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
#include "Jora.h"
/*
#ifdef ARDUINO
#define printf Definitions::prf
#endif
*/
JoraClass::JoraClass():
#ifdef ARDUINO
_spiSettings(LORA_DEFAULT_SPI_FREQUENCY, MSBFIRST, SPI_MODE0),
_spi(&LORA_DEFAULT_SPI),
#endif
_cs(CS_PIN),
_reset(RESET_PIN),
_irq(IRQ_PIN),
_frequency(0),
_packetIndex(0),
_implicitHeaderMode(0),
_onReceive(NULL)
{
}
//Sets up the onReceive ISR
void JoraClass::onReceive(void(*callback)(int))
{
_onReceive = callback;
if (callback) {
pinMode(_irq, INPUT);
writeRegister(REG_DIO_MAPPING_1, 0x00);
#ifdef ARDUINO
#ifdef SPI_HAS_NOTUSINGINTERRUPT
SPI.usingInterrupt(digitalPinToInterrupt(_irq));
#endif
attachInterrupt(digitalPinToInterrupt(_irq), JoraClass::onDio0Rise, RISING);
}
else {
detachInterrupt(digitalPinToInterrupt(_irq));
#ifdef SPI_HAS_NOTUSINGINTERRUPT
SPI.notUsingInterrupt(digitalPinToInterrupt(_irq));
#endif
}
#else
//for pi
if(wiringPiISR(_irq, INT_EDGE_RISING, &JoraClass::onDio0Rise) < 0){ //was &JoraClass
_logger.log("failed to setup ISR\n");
while(true){ //stay here
_logger.log(".");
delay(3000);
}
}else{
_logger.log("ISR setup Success!\n");
}
}
#endif
}
int JoraClass::parsePacket(int size)
{
int packetLength = 0;
int irqFlags = readRegister(REG_IRQ_FLAGS);
//_logger.log("irgFlags: %d\n", irqFlags);
//_logger.log("Mode: %d\n", readRegister(REG_OP_MODE));
if (size > 0) {
implicitHeaderMode();
writeRegister(REG_PAYLOAD_LENGTH, size & 0xff);
} else {
explicitHeaderMode();
}
// clear IRQ's
writeRegister(REG_IRQ_FLAGS, irqFlags);
if ((irqFlags & IRQ_RX_DONE_MASK) && (irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
// received a packet
_packetIndex = 0;
// read packet length
if (_implicitHeaderMode) {
packetLength = readRegister(REG_PAYLOAD_LENGTH);
} else {
packetLength = readRegister(REG_RX_NB_BYTES);
}
// set FIFO address to current RX address
writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR));
// put in standby mode
idle();
} else if (readRegister(REG_OP_MODE) != (MODE_LONG_RANGE_MODE | MODE_RX_SINGLE)) {
// not currently in RX mode
// reset FIFO address
writeRegister(REG_FIFO_ADDR_PTR, 0);
// put in single RX mode
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_SINGLE);
}
return packetLength;
}
int JoraClass::packetRssi()
{
return (readRegister(REG_PKT_RSSI_VALUE) - (_frequency < 868E6 ? 164 : 157));
}
int JoraClass::beginPacket(int implicitHeader)
{
// put in standby mode
idle();
if (implicitHeader) {
implicitHeaderMode();
} else {
explicitHeaderMode();
}
// reset FIFO address and payload length
writeRegister(REG_FIFO_ADDR_PTR, 0);
writeRegister(REG_PAYLOAD_LENGTH, 0);
return 1;
}
void JoraClass::flush()
{
}
int JoraClass::available()
{
return (readRegister(REG_RX_NB_BYTES) - _packetIndex);
}
int JoraClass::read()
{
if (!available()) {
return -1;
}
_packetIndex++;
uint8_t r = readRegister(REG_FIFO);
//_logger.log("%d", r);
return r;//readRegister(REG_FIFO);
}
int JoraClass::peek()
{
if (!available()) {
return -1;
}
// store current FIFO address
int currentAddress = readRegister(REG_FIFO_ADDR_PTR);
// read
uint8_t b = readRegister(REG_FIFO);
// restore FIFO address
writeRegister(REG_FIFO_ADDR_PTR, currentAddress);
return b;
}
size_t JoraClass::write(uint8_t byte)
{
return write(&byte, sizeof(byte));
}
size_t JoraClass::write(const uint8_t *buffer, size_t size)
{
int currentLength = readRegister(REG_PAYLOAD_LENGTH);
// check size
if ((currentLength + size) > MAX_PKT_LENGTH) {
size = MAX_PKT_LENGTH - currentLength;
}
// write data
for (size_t i = 0; i < size; i++) {
writeRegister(REG_FIFO, buffer[i]);
//_logger.log("%d", buffer[i]);
}
// update length
writeRegister(REG_PAYLOAD_LENGTH, currentLength + size);
return size;
}
void setPayload(uint8_t byte){
//set standby mode to write
//writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_STDBY);
//setting addr pointer in FIFO buf
//writeRegister(REG_FIFO_ADDR_PTR, 0x80);
//write REG_FIFO with data
// writeRegister(REG_FIFO, byte);
}
int JoraClass::endPacket()
{
// put in TX mode
//Serial.println("Placing in tx mode: ");Serial.println(MODE_LONG_RANGE_MODE | MODE_TX)
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
// wait for TX done
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
//yield();
delay(1); //ont have yield
}
// clear IRQ's
writeRegister(REG_IRQ_FLAGS, IRQ_TX_DONE_MASK);
return 1;
}
void JoraClass::sendPayload(){
//set TX mode
//writeRegister(REG_OP_MODE, MODE_TX);
//endPacket();
//set back to receive mode??
//idle();
}
void JoraClass::receive(int size)
{
if (size > 0) {
implicitHeaderMode();
writeRegister(REG_PAYLOAD_LENGTH, size & 0xff);
} else {
explicitHeaderMode();
}
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS);
}
void JoraClass::setPins(int cs, int reset, int irq)
{
_cs = cs;
_reset = reset;
_irq= irq;
}
void JoraClass::explicitHeaderMode()
{
_implicitHeaderMode = 0;
writeRegister(REG_MODEM_CONFIG_1, readRegister(REG_MODEM_CONFIG_1) & 0xfe);
}
void JoraClass::implicitHeaderMode()
{
_implicitHeaderMode = 1;
writeRegister(REG_MODEM_CONFIG_1, readRegister(REG_MODEM_CONFIG_1) | 0x01);
}
//for pi because ISR just goes off flat chat when a new message comes in
void JoraClass::handleMessage() {
int irqFlags = readRegister(REG_IRQ_FLAGS);
//_logger.log("irqFlags: %d\n", irqFlags);
writeRegister(REG_IRQ_FLAGS, irqFlags);
if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
// received a packet
_packetIndex = 0;
// read packet length
int packetLength = _implicitHeaderMode ? readRegister(REG_PAYLOAD_LENGTH) : readRegister(REG_RX_NB_BYTES);
// set FIFO address to current RX address
writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR));
//_logger.log("_onReceive: %d\n", getOR());
//_logger.log("onReceive: %d\n", _onReceive);
if (_onReceive) {
//_logger.log("onReceive > 0\n");
_onReceive(packetLength);
}
// reset FIFO address
writeRegister(REG_FIFO_ADDR_PTR, 0);
newMessage = false;
}
}
void JoraClass::handleDio0Rise()
{
newMessage = true;
#ifdef ARDUINO
int irqFlags = readRegister(REG_IRQ_FLAGS);
//_logger.log("irqFlags: %d\n", irqFlags);
writeRegister(REG_IRQ_FLAGS, irqFlags);
if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
// received a packet
_packetIndex = 0;
// read packet length
int packetLength = _implicitHeaderMode ? readRegister(REG_PAYLOAD_LENGTH) : readRegister(REG_RX_NB_BYTES);
// set FIFO address to current RX address
writeRegister(REG_FIFO_ADDR_PTR, readRegister(REG_FIFO_RX_CURRENT_ADDR));
//_logger.log("_onReceive: %d\n", getOR());
//_logger.log("onReceive: %d\n", _onReceive);
if (_onReceive) {
//_logger.log("onReceive > 0\n");
_onReceive(packetLength);
}
// reset FIFO address
writeRegister(REG_FIFO_ADDR_PTR, 0);
newMessage = false;
}
#endif
}
void JoraClass::onDio0Rise()
{
Jora.handleDio0Rise();
}
#ifndef ARDUINO
uint8_t JoraClass::setSPI(){
_logger.log(Definitions::debug, "Starting SPI...\n");
if(!bcm2835_init()){
_logger.log(Definitions::debug, "bcm2835_init failed. Are you running as root??\n");
return -1;
}else if(!bcm2835_spi_begin()){
_logger.log(Definitions::debug, "bcm2835_spi_begin failed\n");
return -1;
}else{
_logger.log(Definitions::debug, "bcm2835 initialized!\n");
//all defaults
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); //65536
return 0;
}
}
#endif
int JoraClass::begin(long freq){
pinMode(_cs, OUTPUT);
digitalWrite(_cs, HIGH);
#ifndef ARDUINO
if(_reset != -1){
_logger.log(Definitions::debug, "Resetting Jora...\n");
pinMode(_reset, OUTPUT);
digitalWrite(_reset, LOW);
delay(10);
digitalWrite(_reset, HIGH);
delay(10);
}
if(setSPI() < 0){
_logger.log(Definitions::debug, "setSPI failed!");
return -1;
}
#else
if (_reset != -1) {
pinMode(_reset, OUTPUT);
// perform reset
digitalWrite(_reset, LOW);
delay(10);
digitalWrite(_reset, HIGH);
delay(10);
}
// start SPI
_spi->begin();
#endif
uint8_t version = readRegister(REG_VERSION);
if(version != 0x12){
return -1;
}
#ifdef DEBUG
_logger.log(Definitions::debug, "Semtech version: %#02x\n", version);
#endif
sleep();
setFrequency(freq); //set lora radio freq
//set base addresses
writeRegister(REG_FIFO_TX_BASE_ADDR, 0);
writeRegister(REG_FIFO_RX_BASE_ADDR, 0);
//set LNA boost
writeRegister(REG_LNA, readRegister(REG_LNA) | 0x03);
//set auto AGC
writeRegister(REG_MODEM_CONFIG_3, 0x04);
//set output power to 14 dBm
setTxPower(17); //default - can be overwritten later
idle();
return 0;
}
void JoraClass::setFrequency(uint64_t frequency)
{
_logger.log(Definitions::debug, "Setting radio freq...\n");
_frequency = frequency;
uint64_t frf = ((uint64_t)frequency << 19) / 32000000;
writeRegister(REG_FRF_MSB, (uint8_t)(frf >> 16));
writeRegister(REG_FRF_MID, (uint8_t)(frf >> 8));
writeRegister(REG_FRF_LSB, (uint8_t)(frf >> 0));
}
long JoraClass::getFrequency(){
uint8_t msb = readRegister(REG_FRF_MSB);
uint8_t mid = readRegister(REG_FRF_MID);
uint8_t lsb = readRegister(REG_FRF_LSB);
uint32_t ch = ((uint32_t)msb << 16) + ((uint32_t)mid << 8) + lsb;
return ch;
}
void JoraClass::sleep()
{
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_SLEEP);
#ifdef DEBUG
_logger.log(Definitions::debug, "Mode: Sleep (%#02x)\n", readRegister(REG_OP_MODE));
#endif
}
void JoraClass::idle()
{
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_STDBY);
#ifdef DEBUG
_logger.log("standby\n");
//_logger.log(Definitions::debug, "Mode: Standby (%#02x)\n", readRegister(REG_OP_MODE));
#endif
}
void JoraClass::end(){
sleep();
#ifdef ARDUINO
// stop SPI
_spi->end();
#else
bcm2835_spi_end();
bcm2835_close();
#endif
}
void JoraClass::setTxPower(int level, int outputPin)
{
if (PA_OUTPUT_RFO_PIN == outputPin) {
// RFO
if (level < 0) {
level = 0;
} else if (level > 14) {
level = 14;
}
writeRegister(REG_PA_CONFIG, 0x70 | level);
} else {
// PA BOOST
if (level < 2) {
level = 2;
} else if (level > 17) {
level = 17;
}
writeRegister(REG_PA_CONFIG, PA_BOOST | (level - 2));
}
}
/*
uint8_t JoraClass::singleTransfer(uint8_t addr, uint8_t val){
dWrite(_cs, 0);
delay(1);
bitClear(addr, 7); //set to read from reg
txbuf[0] = addr;
txbuf[1] = 0x00; //val;
dWrite(_cs, 0);
bcm2835_spi_transfernb(txbuf, rxbuf, sizeof(txbuf));
_logger.log("size_t: %d\n", sizeof(txbuf));
dWrite(_cs, 1);
return 0; //data in rxbuf
}
*/
void JoraClass::writeRegister(uint8_t addr, uint8_t data){
#ifdef ARDUINO
singleTransfer(addr | 0x80, data);
#else
//singleTransfer(addr | 0x08, val);
digitalWrite(_cs, LOW);
delay(1);
bitSet(addr, 7); //bit 7 set to read from reg
txbuf[0] = addr;
txbuf[1] = data;
digitalWrite(_cs, LOW);
bcm2835_spi_transfernb(txbuf, rxbuf, sizeof(txbuf));
digitalWrite(_cs, HIGH);
#endif
}
#ifdef ARDUINO
uint8_t JoraClass::singleTransfer(uint8_t address, uint8_t value)
{
digitalWrite(_cs, LOW);
_spi->beginTransaction(_spiSettings);
//Serial.print(address, HEX); Serial.print(": "); Serial.println(value, HEX);
_spi->transfer(address);
uint8_t response = _spi->transfer(value);
_spi->endTransaction();
digitalWrite(_cs, HIGH);
return response;
}
#endif
uint8_t JoraClass::readRegister(uint8_t addr){
#ifdef ARDUINO
return singleTransfer(addr & 0x7f, 0x00);
#else
digitalWrite(_cs, LOW);
bitClear(addr, 7);
txbuf[0] = addr;
txbuf[1] = 0x00;
digitalWrite(_cs, LOW);
bcm2835_spi_transfernb(txbuf, rxbuf, sizeof(txbuf));
digitalWrite(_cs, HIGH);
//printf("%d", rxbuf[1]);
return rxbuf[1];
/*txbuf[0] = addr & 0x7F;
txbuf[1] = 0x00;
bcm2835_gpio_write(CS_PIN,0);
bcm2835_spi_transfernb( txbuf, rxbuf, sizeof(txbuf) );
bcm2835_gpio_write(CS_PIN,1);
return rxbuf[1]; //data is in rxbuf
*/
#endif
}
int JoraClass::getRegVal(uint8_t addr){
return readRegister(addr);
}
JoraClass Jora;