forked from mnsnoop/esp32Micronet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio.cpp
836 lines (712 loc) · 26.2 KB
/
radio.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
#include "radio.h"
Radio *rRadio = NULL;
void IRAM_ATTR OutgoingTimerCallback()
{//hardware timer callback. it sends a null packet to wake up the sender thread. Probably should use another form or signaling but this was easy.
_sPacketOutgoing pog;
pog.p.bSize = 0;
pog.p.tTime = 0;
pog.p.bMessage = NULL;
pog.iPreambleBytes = 0;
pog.cbCallback = NULL;
pog.vArg = NULL;
xQueueSendToBackFromISR(rRadio->qhOutgoingPacket, (const void *)&pog, NULL);
//fixme: potential issue. if this message fails to send with a full q then the timer will never get reset and send q never processed.
}
void IRAM_ATTR ISRCalibration()
{//This creates an even data signal (0x55 0b01010101) for calibrating.
if (rRadio->bCalFlipFlop)
{
digitalWrite(PIN_IO, 1);
rRadio->bCalFlipFlop = false;
}
else
{
digitalWrite(PIN_IO, 0);
rRadio->bCalFlipFlop = true;
}
}
void IRAM_ATTR ISRDataOut()
{//ISR triggered by the radio's clock to set the data pin
byte b;
if (rRadio->iBitPos < 0)
{//get next byte.
switch (rRadio->eOutputState)
{
case OS_START_PADDING_BYTE:
rRadio->eOutputState = OS_PREAMBLE;
break;
case OS_PREAMBLE:
rRadio->iPreambleTimeout--;
if (rRadio->iPreambleTimeout == 1)
rRadio->eOutputState = OS_SYNCWORD;
break;
case OS_SYNCWORD:
rRadio->tSent = esp_timer_get_time(); //log start of packet for timing purposes.
rRadio->eOutputState = OS_DATA;
break;
case OS_DATA:
rRadio->iBytePos++;
if (rRadio->iBytePos >= rRadio->iPacketSize)
rRadio->eOutputState = OS_END_PADDING_BYTE;
break;
case OS_END_PADDING_BYTE:
rRadio->Stop();
return;
break;
}
rRadio->iBitPos = 7;
}
switch (rRadio->eOutputState)
{
case OS_START_PADDING_BYTE:
b = 0;
break;
case OS_PREAMBLE:
b = MN_PREAMBLE;
break;
case OS_SYNCWORD:
b = MN_SYNCWORD;
break;
case OS_DATA:
b = rRadio->bWorkingBytes[rRadio->iBytePos];
break;
case OS_END_PADDING_BYTE:
b = 0;
break;
}
digitalWrite(PIN_IO, GetBit(b, rRadio->iBitPos--));
}
void IRAM_ATTR ISRDataIn()
{//ISR triggered by the radio's clock to read the data pin
bool bIn = digitalRead(PIN_IO);
#ifdef CC1000
bIn = !bIn; //*Note: When using high-side LO injection the data at DIO will be inverted.
#endif
if (rRadio->eInputState != IS_DATA)
{//if we're waiting for preable only save the last 4 bytes. shift the bits to the left. put the newest bit at the back.
uint32_t *iP = (uint32_t *) rRadio->bWorkingBytes;
*iP <<= 1;
SetBit((byte *)&rRadio->bWorkingBytes[0], 0, bIn);
}
if (rRadio->eInputState == IS_WAIT &&
(rRadio->bWorkingBytes[0] == MN_PREAMBLE &&
rRadio->bWorkingBytes[1] == MN_PREAMBLE &&
rRadio->bWorkingBytes[2] == MN_PREAMBLE &&
rRadio->bWorkingBytes[3] == MN_PREAMBLE))
{ //we're getting a preamble. lock the average filter and change status.
#ifdef CC1000
rRadio->ProgramWrite(0b00100001, 0b00011011, false);
#endif
rRadio->eInputState = IS_PREAMBLE;
rRadio->iPreambleTimeout = 16;
//print("preamble(): %.2X %.2X %.2X %.2X\n", Radio->bWorkingBytes[0], Radio->bWorkingBytes[1], Radio->bWorkingBytes[2], Radio->bWorkingBytes[3]);
}
else if (rRadio->eInputState == IS_PREAMBLE)
{
if (rRadio->bWorkingBytes[0] == MN_PREAMBLE)
rRadio->iPreambleTimeout = 16;
else if (rRadio->bWorkingBytes[0] == MN_SYNCWORD &&
rRadio->bWorkingBytes[1] == MN_PREAMBLE &&
rRadio->bWorkingBytes[2] == MN_PREAMBLE &&
rRadio->bWorkingBytes[3] == MN_PREAMBLE)
{//end of the preamble. setup for data.
rRadio->eInputState = IS_DATA;
rRadio->iBitPos = 7;
rRadio->iBytePos = 0;
rRadio->iPacketSize = MN_MAXPACKETSIZE;
rRadio->tPacketTime = esp_timer_get_time();
}
rRadio->iPreambleTimeout--;
if (rRadio->iPreambleTimeout == 0)
{ //This isn't the preamble we're looking for. unlock avg filter. reset.
#ifdef CC1000
rRadio->ProgramWrite(0b00100001, 0b00001011, false); //unlock average filter
#endif
//print("ISRDataIn(): Error, Preamble error tolerance exceeded. %.2X %.2X %.2X %.2X\n", rRadio->bWorkingBytes[0], rRadio->bWorkingBytes[1], rRadio->bWorkingBytes[2], rRadio->bWorkingBytes[3]);
rRadio->eInputState = IS_WAIT;
rRadio->bWorkingBytes[0] = 0;
rRadio->bWorkingBytes[1] = 0;
rRadio->bWorkingBytes[2] = 0;
rRadio->bWorkingBytes[3] = 0;
}
}
else if (rRadio->eInputState == IS_DATA)
{//We're recieving a packet.
SetBit((byte *)&rRadio->bWorkingBytes[rRadio->iBytePos], rRadio->iBitPos, bIn);
rRadio->iBitPos--;
if (rRadio->iBitPos < 0)
{//current byte is filled up.
rRadio->iBitPos = 7; //reset bitpos
rRadio->iBytePos++; //increment bytepos
if (rRadio->iBytePos == 14)
{//check length checksum and see if this is a legit packet
if (rRadio->bWorkingBytes[12] == rRadio->bWorkingBytes[13])
{//match, set the size.
rRadio->iPacketSize = rRadio->bWorkingBytes[13] - 11;
}
else
{//they don't match. bad packet or error. unlock avg filter. reset.
#ifdef CC1000
rRadio->ProgramWrite(0b00100001, 0b00001011, false); //unlock average filter
#endif
//print("ISRDataIn(): Error, length bytes don't match (%.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X %.2X)\n", rRadio->bWorkingBytes[0], rRadio->bWorkingBytes[1], rRadio->bWorkingBytes[2], rRadio->bWorkingBytes[3], rRadio->bWorkingBytes[4], rRadio->bWorkingBytes[5], rRadio->bWorkingBytes[6], rRadio->bWorkingBytes[7], rRadio->bWorkingBytes[8], rRadio->bWorkingBytes[9], rRadio->bWorkingBytes[10], rRadio->bWorkingBytes[11], rRadio->bWorkingBytes[12], rRadio->bWorkingBytes[13], rRadio->bWorkingBytes[14]);
rRadio->eInputState = IS_WAIT;
}
}
rRadio->iPacketSize--;
if (rRadio->iPacketSize <= 0)
{//received whole packet. ship it out.
_sPacket p;
p.bSize = rRadio->iBytePos;
p.tTime = rRadio->tPacketTime;
p.bMessage = (byte *)malloc(sizeof(byte) * rRadio->iBytePos);
memcpy(p.bMessage, rRadio->bWorkingBytes, p.bSize);
if (xQueueSendToBackFromISR(rRadio->qhIncomingPacket, (const void *)&p, NULL) != pdPASS)
{
free(p.bMessage);
print(LL_ERROR, "ISRDataIn(): Error adding byte to queue.\n");
}
//rRadio->Stop(false);
rRadio->iBitPos = 0;
rRadio->eInputState = IS_WAIT;
#ifdef CC1000
rRadio->ProgramWrite(0b00100001, 0b00001011, false); //unlock average filter
#endif
}
}
}
}
Radio::Radio()
{
eRadioState = RS_UNKNOWN;
rRadio = this;
thSenderBlocking = NULL;
qhProgramming = xQueueCreate(QUEUEMAXPROGRAMMING, sizeof(_sProgramming));
qhIncomingPacket = xQueueCreate(QUEUEMAXINCOMINGPACKETS, sizeof(_sPacket));
qhOutgoingPacket = xQueueCreate(QUEUEMAXOUTGOINGPACKETS, sizeof(_sPacketOutgoing));
//fixme: optimize stack sizes
xTaskCreate(ProgramHandler, "Programming handler", configMINIMAL_STACK_SIZE + 1000, (void *)this, tskIDLE_PRIORITY + 10, &thProgramHandler);
xTaskCreatePinnedToCore(OutgoingPacketHandler, "Outgoing packet handler", configMINIMAL_STACK_SIZE + 3000, (void *)this, tskIDLE_PRIORITY + 9, &thOutgoingPacketHandler, 1);
sphProgrammingInProgress = xSemaphoreCreateBinary();
xSemaphoreGive(sphProgrammingInProgress);
hwtOutgoing = timerBegin(3, 80, true); //nmea2k uses hw timer 0.
timerAttachInterrupt(hwtOutgoing, &OutgoingTimerCallback, true);
Initialize();
}
Radio::~Radio()
{
timerEnd(hwtOutgoing);
vSemaphoreDelete(sphProgrammingInProgress);
vTaskDelete(thProgramHandler);
vTaskDelete(thOutgoingPacketHandler);
vQueueDelete(qhOutgoingPacket);
vQueueDelete(qhIncomingPacket);
vQueueDelete(qhProgramming);
#ifdef CC1101
SPI.endTransaction();
SPI.end();
#endif
rRadio = NULL;
}
void Radio::ProgramHandler(void *c)
{
Radio *ccR = (Radio *)c;
for (;;)
{
_sProgramming sProgramming;
if (xQueueReceive(ccR->qhProgramming, (void *)&sProgramming, portMAX_DELAY) == pdFALSE)
{
print(LL_ERROR, "Radio::ProgramHandler(): pdFalse returned from xQueueReceive while waiting for sProgramming.\n");
continue;
}
xSemaphoreTake(ccR->sphProgrammingInProgress, portMAX_DELAY);
#ifdef CC1000
pinMode(PIN_PDATA, OUTPUT);
digitalWrite(PIN_PDATA, 1);
digitalWrite(PIN_PCLOCK, 1);
digitalWrite(PIN_PALE, 1);
ets_delay_us(3);
digitalWrite(PIN_PALE, 0);
ets_delay_us(2);
for (int i = 7; i >= 0; i--)
{ //write address
digitalWrite(PIN_PCLOCK, 1);
digitalWrite(PIN_PDATA, GetBit(sProgramming.bAddress, i));
ndelay();
digitalWrite(PIN_PCLOCK, 0);
ndelay();
ndelay();
}
digitalWrite(PIN_PCLOCK, 1);
ets_delay_us(5);
digitalWrite(PIN_PALE, 1);
ets_delay_us(2);
for (int i = 7; i >= 0; i--)
{ //write data
digitalWrite(PIN_PCLOCK, 1);
digitalWrite(PIN_PDATA, GetBit(sProgramming.bData, i));
ndelay();
digitalWrite(PIN_PCLOCK, 0);
ndelay();
ndelay();
}
digitalWrite(PIN_PCLOCK, 1);
ets_delay_us(5);
digitalWrite(PIN_PDATA, 0);
digitalWrite(PIN_PCLOCK, 0);
digitalWrite(PIN_PALE, 1);
ets_delay_us(5);
#endif
#ifdef CC1101
ccR->SPI.beginTransaction(ccR->spiSettings);
digitalWrite(PIN_CS, LOW);
if (sProgramming.bStrobe)
{
ccR->SPI.transfer(sProgramming.bAddress | CC1101_WRITE_SINGLE);
}
else
{
ccR->SPI.transfer(sProgramming.bAddress | CC1101_WRITE_SINGLE);
ccR->SPI.transfer(sProgramming.bData);
}
digitalWrite(PIN_CS, HIGH);
ccR->SPI.endTransaction();
#endif
xSemaphoreGive(ccR->sphProgrammingInProgress);
if (sProgramming.tNotify)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
vTaskNotifyGiveFromISR(sProgramming.tNotify, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken == pdTRUE)
portYIELD_FROM_ISR();
}
}
}
void Radio::ProgramWrite(byte bAddress, byte bData, bool bBlock, bool bStrobe)
{
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
_sProgramming sProgramming;
sProgramming.bStrobe = bStrobe;
sProgramming.bAddress = bAddress;
sProgramming.bData = bData;
if (bBlock)
sProgramming.tNotify = xTaskGetCurrentTaskHandle();
else
sProgramming.tNotify = NULL;
if (xQueueSendToBackFromISR(qhProgramming, (const void *)&sProgramming, &xHigherPriorityTaskWoken) != pdPASS)
print(LL_ERROR, "Radio::ProgramWrite(): Error adding address byte to queue.\n");
if (xHigherPriorityTaskWoken == pdTRUE)
portYIELD_FROM_ISR();
if (bBlock)
{
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
}
}
byte Radio::ProgramRead(byte bAddress)
{
byte ret = 0;
xSemaphoreTake(sphProgrammingInProgress, portMAX_DELAY);
#ifdef CC1000
pinMode(PIN_PDATA, OUTPUT);
digitalWrite(PIN_PDATA, 1);
digitalWrite(PIN_PCLOCK, 1);
digitalWrite(PIN_PALE, 1);
ets_delay_us(3);
digitalWrite(PIN_PALE, 0);
ets_delay_us(2);
for (int i = 7; i >= 0; i--)
{
digitalWrite(PIN_PCLOCK, 1);
digitalWrite(PIN_PDATA, GetBit(bAddress, i));
ndelay();
digitalWrite(PIN_PCLOCK, 0);
ndelay();
ndelay();
}
digitalWrite(PIN_PCLOCK, 1);
ets_delay_us(5);
digitalWrite(PIN_PDATA, 1);
pinMode(PIN_PDATA, INPUT);
digitalWrite(PIN_PALE, 1);
ets_delay_us(2);
for (int i = 7; i >= 0; i--)
{
if (digitalRead(PIN_PDATA))
ret |= 1UL << i;
digitalWrite(PIN_PCLOCK, 0);
ndelay();
ndelay();
digitalWrite(PIN_PCLOCK, 1);
ndelay();
}
digitalWrite(PIN_PCLOCK, 1);
ets_delay_us(5);
pinMode(PIN_PDATA, OUTPUT);
digitalWrite(PIN_PDATA, 0);
digitalWrite(PIN_PCLOCK, 0);
digitalWrite(PIN_PALE, 1);
ets_delay_us(5);
#endif
#ifdef CC1101
SPI.beginTransaction(spiSettings);
digitalWrite(PIN_CS, LOW);
while(digitalRead(PIN_MISO))
delay(1);
SPI.transfer(bAddress | CC1101_READ_SINGLE);
ret = SPI.transfer(0);
digitalWrite(PIN_CS, HIGH);
SPI.endTransaction();
#endif
xSemaphoreGive(sphProgrammingInProgress);
return ret;
}
void Radio::Initialize()
{
pinMode(PIN_OP, OUTPUT);
#ifdef CC1000
pinMode(PIN_PALE, OUTPUT);
pinMode(PIN_PDATA, OUTPUT);
pinMode(PIN_PCLOCK, OUTPUT);
pinMode(PIN_DCLOCK, INPUT);
pinMode(PIN_DDATA, INPUT);
//Master reset
digitalWrite(PIN_PALE, 1);
digitalWrite(PIN_PDATA, 0);
digitalWrite(PIN_PCLOCK, 0);
ProgramWrite(0b00000001, 0b00111111, true);
delay(2);
ProgramWrite(0b00000001, 0b00111110, true);
delay(2);
ProgramWrite(0b00000001, 0b00111111, true);
delay(2);
//end master reset
ProgramWrite(0b00000011, 0b01111100, false); //FreqWordA2:8151040
ProgramWrite(0b00000101, 0b00100000, false); //FreqWordA1:8134656
ProgramWrite(0b00000111, 0b00000000, false); //FreqWordA0:8134656
ProgramWrite(0b00001001, 0b01011101, false); //FreqWordB2:6103040
ProgramWrite(0b00001011, 0b00001011, false); //FreqWordB1:6097664
ProgramWrite(0b00001101, 0b01000011, false); //FreqWordB0:6097731
ProgramWrite(0b00001111, 0b00000001, false); //FSep1 Word: 427 Value: 64050/48037.5 Hz (refdiv 6/8)
ProgramWrite(0b00010001, 0b10101011, false); //FSep1 Word: 427 Value: 64050/48037.5 Hz (refdiv 6/8)
ProgramWrite(0b00010011, 0b10001100, false); //Current Register VCOCurrent: 1450µA LODrive:2mA PADrive:1mA
ProgramWrite(0b00010101, 0b00110010, false); //Front_end Register
ProgramWrite(0b00010111, 0b11111111, false); //PAPow 11111111
ProgramWrite(0b00011001, 0b00110000, false); //Referance Divider: 6
ProgramWrite(0b00011011, 0b00010000, false); //Lock Register
ProgramWrite(0b00011101, 0b00100110, false); //Calibration Register Cal_Start: 0
ProgramWrite(0b00011111, 0b11000011, false); //Modem2 Register PEAK_LEVEL_OFFSET:67(1000011)
ProgramWrite(0b00100001, 0b00001011, false); //Modem1 Register LOCK_AVG_IN:0 LOCK_AVG_MODE:1 SETTLING 01 MODEM_RESET_N 1
ProgramWrite(0b00100011, 0b01010000, false); //Modem0 Register BAUDRATE:101 DATA_FORMAT:00 XOSC_FREQ 00
ProgramWrite(0b00100101, 0b00010000, false); //Match Register
ProgramWrite(0b00100111, 0b00000001, false); //FSCTRL Register
ProgramWrite(0b00111001, 0b00000000, false); //Prescaler Register
ProgramWrite(0b10000101, 0b00111111, false); //TEST4 Register
Calibrate();
#endif
#ifdef CC1101
pinMode(PIN_SCLK, OUTPUT);
pinMode(PIN_MOSI, OUTPUT);
pinMode(PIN_MISO, INPUT);
pinMode(PIN_CS, OUTPUT);
pinMode(PIN_G0, INPUT); //tx/rx data
//master reset
digitalWrite(PIN_SCLK, HIGH);
digitalWrite(PIN_MOSI, LOW);
digitalWrite(PIN_CS, LOW);
delay(1);
digitalWrite(PIN_CS, HIGH);
delay(1);
digitalWrite(PIN_CS, LOW);
while(digitalRead(PIN_MISO))
delay(1);
digitalWrite(PIN_CS, HIGH);
SPI.begin(PIN_SCLK, PIN_MISO, PIN_MOSI, -1);
SPI.beginTransaction(spiSettings);
digitalWrite(PIN_CS, LOW);
SPI.transfer(CC1101_SRES);
while(digitalRead(PIN_MISO))
delay(1);
digitalWrite(PIN_CS, HIGH);
SPI.endTransaction();
//end master reset
// ProgramWrite(CC1101_IOCFG2, 0x0B);
ProgramWrite(CC1101_IOCFG1, 0x0B);
ProgramWrite(CC1101_IOCFG0, 0x0C);
// ProgramWrite(CC1101_FIFOTHR, 0x00);
// ProgramWrite(CC1101_SYNC1, 0x00);
// ProgramWrite(CC1101_SYNC0, 0x00);
// ProgramWrite(CC1101_PKTLEN, 0x00);
// ProgramWrite(CC1101_PKTCTRL1, 0x00);
ProgramWrite(CC1101_PKTCTRL0, 0x12); //Serial data in/out. Inf packet mode.
// ProgramWrite(CC1101_ADDR, 0x00);
// ProgramWrite(CC1101_CHANNR, 0x00);
ProgramWrite(CC1101_FSCTRL1, 0x0B);
ProgramWrite(CC1101_FSCTRL0, 0x00);
ProgramWrite(CC1101_FREQ2, CC1101_FREQWORD2);
ProgramWrite(CC1101_FREQ1, CC1101_FREQWORD1);
ProgramWrite(CC1101_FREQ0, CC1101_FREQWORD0);
ProgramWrite(CC1101_MDMCFG4, 0x7B);
ProgramWrite(CC1101_MDMCFG3, 0x83);
ProgramWrite(CC1101_MDMCFG2, 0x00);
ProgramWrite(CC1101_MDMCFG1, 0x22);
ProgramWrite(CC1101_MDMCFG0, 0xF8);
ProgramWrite(CC1101_DEVIATN, 0x42);
// ProgramWrite(CC1101_MCSM2, 0x00);
// ProgramWrite(CC1101_MCSM1, 0x00);
ProgramWrite(CC1101_MCSM0, 0x18); //0x18 = automatic calibration?!
ProgramWrite(CC1101_FOCCFG, 0x1D);
ProgramWrite(CC1101_BSCFG, 0x1C);
ProgramWrite(CC1101_AGCCTRL2, 0xC7);
ProgramWrite(CC1101_AGCCTRL1, 0x00);
ProgramWrite(CC1101_AGCCTRL0, 0xB2);
// ProgramWrite(CC1101_WOREVT1, 0x00);
// ProgramWrite(CC1101_WOREVT0, 0x00);
// ProgramWrite(CC1101_WORCTRL, 0x00);
// ProgramWrite(CC1101_FREND1, 0x00);
ProgramWrite(CC1101_FREND0, 0x10);
ProgramWrite(CC1101_FSCAL3, 0xEA);
ProgramWrite(CC1101_FSCAL2, 0x2A);
ProgramWrite(CC1101_FSCAL1, 0x00);
ProgramWrite(CC1101_FSCAL0, 0x1F);
// ProgramWrite(CC1101_RCCTRL1, 0x00);
// ProgramWrite(CC1101_RCCTRL0, 0x00);
ProgramWrite(CC1101_FSTEST, 0x59);
// ProgramWrite(CC1101_PTEST, 0x00);
// ProgramWrite(CC1101_AGCTEST, 0x00);
ProgramWrite(CC1101_TEST2, 0x81);
ProgramWrite(CC1101_TEST1, 0x35);
ProgramWrite(CC1101_TEST0, 0x09);
ProgramWrite(CC1101_PATABLE, CC1101_POWER);
#endif
// pinMode(PIN_G0, OUTPUT); //Set data pin mode to output
// attachInterrupt(digitalPinToInterrupt(PIN_MISO), ISRCalibration, RISING); //Set special calibration interript handler
// rRadio->ProgramStrobe(CC1101_STX, false);
eRadioState = RS_IDLE;
}
void Radio::Calibrate()
{//fixme: this should calibrate the cc1101 too so it doesn't have such a large delay switching from rx/tx
if (eRadioState != RS_IDLE &&
eRadioState != RS_UNKNOWN)
{
print(LL_ERROR, "Radio::Calibrate(): Tried to calibrate radio when not idle (%d).\n", eRadioState);
return;
}
eRadioState = RS_CALIBRATE;
//RX calibration / Freq A
ProgramWrite(0b00010111, 0b00000000, false); //PAPow 00000000
ProgramWrite(0b00000001, 0b00111011, false); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:1 Core_PD:0 Bias_PD:1)
ProgramWrite(0b00000001, 0b00111001, false); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:1 Core_PD:0 Bias_PD:0)
ProgramWrite(0b00100001, 0b00001011, false); //Modem1 Register LOCK_AVG_IN:0 LOCK_AVG_MODE:1 SETTLING 01 MODEM_RESET_N 1
ProgramWrite(0b00000001, 0b00110001, false); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:0 Core_PD:0 Bias_PD:0)
ProgramWrite(0b00011001, 0b01000000, false); //Referance Divider: 8
ProgramWrite(0b00010011, 0b10001100, false); //Current Register VCOCurrent: 1450µA LODrive:2mA PADrive:1mA
ProgramWrite(0b00000001, 0b00010001, true); //Freq:A Power State:(RX_PD:0 TX_PD:1 FS_PD:0 Core_PD:0 Bias_PD:0) !!START RX!!
ProgramWrite(0b00011101, 0b10100110, true); //Calibration Register Cal_Start: 1
while (GetBit(ProgramRead(0b00011100), 3) == 0) //Wait for calibration to finish
delay(1);
ProgramWrite(0b00011101, 0b00100110, false); //Calibration Register Cal_Start: 0
ProgramWrite(0b00000001, 0b00111001, true); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:1 Core_PD:0 Bias_PD:0) !!STOP RX!!
//TX calibration / Freq B
pinMode(PIN_DDATA, OUTPUT); //Set data pin mode to output
attachInterrupt(digitalPinToInterrupt(PIN_DCLOCK), ISRCalibration, RISING); //Set special calibration interript handler
ProgramWrite(0b00000001, 0b11110001, false); //Freq:B Power State:(RX_PD:1 TX_PD:1 FS_PD:0 Core_PD:0 Bias_PD:0)
ProgramWrite(0b00011001, 0b00110000, false); //Referance Divider: 6
ProgramWrite(0b00010011, 0b11110011, false); //Current Register VCOCurrent: 1450µA LODrive:0.5mA PADrive:4mA
ProgramWrite(0b00000001, 0b11100001, true); //Freq:B Power State:(RX_PD:1 TX_PD:0 FS_PD:0 Core_PD:0 Bias_PD:0) !!START TX!!
ProgramWrite(0b00011101, 0b10100110, true); //Calibration Register Cal_Start: 1
while (GetBit(ProgramRead(0b00011100), 3) == 0) //Wait for calibration to finish
delay(1);
ProgramWrite(0b00011101, 0b00100110, false); //Calibration Register Cal_Start: 0
ProgramWrite(0b00000001, 0b00111111, true); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:1 Core_PD:1 Bias_PD:1) !!STOP TX!!
detachInterrupt(digitalPinToInterrupt(PIN_DCLOCK)); //Remove calibration interrupt handler
eRadioState = RS_IDLE;
}
void Radio::OutgoingPacketHandler(void *r)
{
Radio *rRadio = (Radio *)r;
_sPacketOutgoing pog[QUEUEMAXOUTGOINGPACKETS + 1];
bool bSkipWait = false;
for (int i = 0; i < QUEUEMAXOUTGOINGPACKETS + 1; i++)
pog[i].p.tTime = 0;
for (;;)
{
if (bSkipWait)
bSkipWait = false;
else
{
_sPacketOutgoing pTmp;
int ret = xQueuePeek(rRadio->qhOutgoingPacket, (void *)&pTmp, portMAX_DELAY); //If the queue is full it'll cause a race condition until not full
}
//disable timer incase this was manually triggered
timerAlarmDisable(rRadio->hwtOutgoing);
//check queue to see if there's something to send coming up
uint64_t tCurrentTick = esp_timer_get_time();
int tTicksTillNextSend = (int)(pog[0].p.tTime - tCurrentTick);
bool bShift = false;
bool bSent = false;
if (pog[0].p.tTime != 0)
{//next packet has a valid send time
if (tTicksTillNextSend < -500) //missed the window. should not happen.
{//missed the window
print(LL_ERROR, "Radio::OutgoingPacketHandler: Missed send window by %d μs.\n", (uint32_t)tCurrentTick - pog[0].p.tTime);
bShift = true;
}
else if (tTicksTillNextSend < 500)
{//window imminent.
if (rRadio->eRadioState != RS_IDLE)
rRadio->Stop();
while (esp_timer_get_time() < pog[0].p.tTime - 1)
ets_delay_us(0);
rRadio->eRadioState = RS_TX;
rRadio->iBitPos = 7;
rRadio->iBytePos = 0;
rRadio->iPreambleTimeout = pog[0].iPreambleBytes;
rRadio->iPacketSize = pog[0].p.bSize;
rRadio->eOutputState = OS_START_PADDING_BYTE;
rRadio->thSenderBlocking = xTaskGetCurrentTaskHandle();
memcpy(rRadio->bWorkingBytes, pog[0].p.bMessage, pog[0].p.bSize);
#ifdef CC1000
pinMode(PIN_DDATA, OUTPUT);
attachInterrupt(digitalPinToInterrupt(PIN_DCLOCK), ISRDataOut, RISING); //Data is clocked into CC1000 at the rising edge of DCLK so we set the bit on the falling edge.
//rRadio->ProgramWrite(0b00010111, 0b00000000, false); //PAPow 00000000
rRadio->ProgramWrite(0b00000001, 0b11110001, false); //Freq:B Power State:(RX_PD:1 TX_PD:1 FS_PD:0 Core_PD:0 Bias_PD:0)
rRadio->ProgramWrite(0b00011001, 0b00110000, false); //Referance Divider: 6
rRadio->ProgramWrite(0b00010011, 0b11110011, false); //Current Register VCOCurrent: 1450µA LODrive:0.5mA PADrive:4mA
rRadio->ProgramWrite(0b00010111, 0b11111111, false); //PAPow 11111111
rRadio->ProgramWrite(0b00000001, 0b11100001, false); //Freq:B Power State:(RX_PD:1 TX_PD:0 FS_PD:0 Core_PD:0 Bias_PD:0) !!START TX!!
#endif
#ifdef CC1101
pinMode(PIN_G0, OUTPUT);
attachInterrupt(digitalPinToInterrupt(PIN_MISO), ISRDataOut, RISING); //Data is clocked into CC1000 at the rising edge of DCLK so we set the bit on the falling edge.
rRadio->ProgramStrobe(CC1101_STX, false);
#endif
ulTaskNotifyTake(pdTRUE, portMAX_DELAY); //wait until send is finished.
pog[0].p.tTime = rRadio->tSent; //update with actual SoP send time.
bSent = true;
bShift = true;
}
//shift the queue left
if (bShift)
{
if (pog[0].cbCallback)
pog[0].cbCallback(pog[0].vArg, &pog[0].p, bSent);
if (pog[0].p.bMessage)
free(pog[0].p.bMessage);
for (int i = 0; i < QUEUEMAXOUTGOINGPACKETS; i++)
pog[i] = pog[i + 1];
}
}
//Find first free slot in q
int i = 0;
while (i < QUEUEMAXOUTGOINGPACKETS && pog[i].p.tTime != 0)
i++;
//fill the q
while (i < QUEUEMAXOUTGOINGPACKETS && xQueueReceive(rRadio->qhOutgoingPacket, (void *)&pog[i], 0) == pdTRUE)
i++;
//sort the q
bool bSwapped = false;
i = 0;
while (i < QUEUEMAXOUTGOINGPACKETS && pog[i + 1].p.tTime != 0)
{
if (pog[i + 1].p.tTime < pog[i].p.tTime)
{
_sPacketOutgoing ptmp = pog[i];
pog[i] = pog[i + 1];
pog[i + 1] = ptmp;
bSwapped = true;
}
i++;
if (pog[i + 1].p.tTime == 0 && bSwapped == true)
{
i = 0;
bSwapped = false;
}
}
//setup timer
if (pog[0].p.tTime > 0)
{
int64_t tDelay = pog[0].p.tTime - esp_timer_get_time() - 500;
if (tDelay < 0)
{
bSkipWait = true;
// print(LL_DEBUG, "Skipping delay. pog[0].p.tTime = %d gettime() = %d\n", (uint32_t)pog[0].p.tTime, (uint32_t)esp_timer_get_time());
}
else
{
// print(LL_DEBUG, "Setting delay to %dμs. pog[0].p.tTime = %d gettime() = %d\n", (uint32_t)tDelay, (uint32_t)esp_timer_get_time(), (uint32_t)esp_timer_get_time());
timerWrite(rRadio->hwtOutgoing, 0);
timerAlarmWrite(rRadio->hwtOutgoing, tDelay, false);
timerAlarmEnable(rRadio->hwtOutgoing);
}
}
}
}
void Radio::Stop()
{
if (eRadioState == RS_UNKNOWN)
{
print(LL_ERROR, "CCRadio::Stop(): Tried to stop radio in unknown state.\n");
return;
}
#ifdef CC1000
detachInterrupt(digitalPinToInterrupt(PIN_DCLOCK));
ProgramWrite(0b00000001, 0b00111111, bBlock); //idle radio
ProgramWrite(0b00010111, 0b00000000, bBlock); //PAPow 00000000 Not sure why but the Micronet MCU's do this after idling the radio.
#endif
#ifdef CC1101
detachInterrupt(digitalPinToInterrupt(PIN_MISO));
ProgramStrobe(CC1101_SIDLE);
#endif
if (thSenderBlocking)
{
xTaskNotifyGive(thSenderBlocking);
thSenderBlocking = NULL;
}
eRadioState = RS_IDLE;
}
void Radio::Listen()
{
if (eRadioState != RS_IDLE)
{
print(LL_ERROR, "Radio::Listen(): Tried to RX when not idle (%d).\n", eRadioState);
return;
}
//setup for recieving data
memset(bWorkingBytes, 0, MN_MAXPACKETSIZE);
iBitPos = 0;
eInputState = IS_WAIT;
#ifdef CC1000
ProgramWrite(0b00010111, 0b00000000, false); //PAPow 00000000
ProgramWrite(0b00000001, 0b00111011, false); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:1 Core_PD:0 Bias_PD:1)
ProgramWrite(0b00000001, 0b00111001, false); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:1 Core_PD:0 Bias_PD:0)
ProgramWrite(0b00100001, 0b00001011, false); //Modem1 Register LOCK_AVG_IN:0 LOCK_AVG_MODE:1 SETTLING 01 MODEM_RESET_N 1
ProgramWrite(0b00000001, 0b00110001, false); //Freq:A Power State:(RX_PD:1 TX_PD:1 FS_PD:0 Core_PD:0 Bias_PD:0)
ProgramWrite(0b00011001, 0b01000000, false); //Referance Divider: 8
ProgramWrite(0b00010011, 0b10001100, false); //Current Register VCOCurrent: 1450µA LODrive:2mA PADrive:1mA
ProgramWrite(0b00000001, 0b00010001, false); //Freq:A Power State:(RX_PD:0 TX_PD:1 FS_PD:0 Core_PD:0 Bias_PD:0) !!START RX!!
pinMode(PIN_DDATA, INPUT);
attachInterrupt(digitalPinToInterrupt(PIN_DCLOCK), ISRDataIn, FALLING); //datasheet says data should be clocked in on the rising edge. I've found falling far more stable.
#endif
#ifdef CC1101
ProgramStrobe(CC1101_SRX, false);
pinMode(PIN_G0, INPUT);
attachInterrupt(digitalPinToInterrupt(PIN_MISO), ISRDataIn, FALLING); //datasheet says data should be clocked in on the rising edge. Given the ~7us ISR delay I've found falling far more stable.
#endif
}
void Radio::Send(byte *bData, int iLength, uint64_t tSendAt, int iPreambleBytes, void (*cbCallback)(void *v, _sPacket *p, bool bSent), void *vArg)
{
if (uxQueueMessagesWaiting(qhOutgoingPacket) < QUEUEMAXOUTGOINGPACKETS - 2)//the -2 leaves room for the hardware timer to send a null packet to wake the sender task.
{
_sPacketOutgoing pog;
if (iLength > MN_MAXPACKETSIZE)
iLength = MN_MAXPACKETSIZE;
pog.p.bSize = iLength;
pog.p.tTime = tSendAt;
pog.p.bMessage = (byte *)malloc(iLength * sizeof(byte));
memcpy(pog.p.bMessage, bData, iLength);
pog.iPreambleBytes = iPreambleBytes + 1; //fixme why the +1? needed???
pog.cbCallback = cbCallback;
pog.vArg = vArg;
xQueueSendToBack(qhOutgoingPacket, (const void *)&pog, portMAX_DELAY);
}
}