forked from openLRSng/openLRSng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RX.h
1073 lines (955 loc) · 26.4 KB
/
RX.h
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
/****************************************************
* OpenLRSng receiver code
****************************************************/
#include <avr/eeprom.h>
uint8_t RF_channel = 0;
uint32_t lastPacketTimeUs = 0;
uint32_t lastRSSITimeUs = 0;
uint32_t linkLossTimeMs;
uint32_t lastBeaconTimeMs;
uint8_t RSSI_count = 0;
uint16_t RSSI_sum = 0;
uint8_t lastRSSIvalue = 0;
uint8_t smoothRSSI = 0;
uint8_t compositeRSSI = 0;
uint16_t lastAFCCvalue = 0;
uint16_t linkQuality = 0;
uint8_t linkQ;
uint8_t ppmCountter = 0;
uint16_t ppmSync = 40000;
uint8_t ppmChannels = 8;
volatile uint8_t disablePWM = 0;
volatile uint8_t disablePPM = 0;
uint8_t failsafeActive = 0;
uint16_t failsafePPM[PPM_CHANNELS];
uint8_t linkAcquired = 0;
uint8_t numberOfLostPackets = 0;
volatile uint8_t slaveState = 0; // 0 - no slave, 1 - slave initializing, 2 - slave running, 3- errored
uint32_t slaveFailedMs = 0;
bool willhop = 0, fs_saved = 0;
pinMask_t chToMask[PPM_CHANNELS];
pinMask_t clearMask;
void outputUp(uint8_t no)
{
PORTB |= chToMask[no].B;
PORTC |= chToMask[no].C;
PORTD |= chToMask[no].D;
}
void outputDownAll()
{
PORTB &= clearMask.B;
PORTC &= clearMask.C;
PORTD &= clearMask.D;
}
#if (F_CPU == 16000000)
#define PWM_MULTIPLIER 2
#define PPM_PULSELEN 600
#define PWM_DEJITTER 32
#define PPM_FRAMELEN 40000
#elif (F_CPU == 8000000)
#define PWM_MULTIPLIER 1
#define PPM_PULSELEN 300
#define PWM_DEJITTER 16
#define PPM_FRAMELEN 20000
#else
#error F_CPU not supported
#endif
volatile uint16_t nextICR1;
ISR(TIMER1_OVF_vect)
{
if (ppmCountter < ppmChannels) {
ICR1 = nextICR1;
nextICR1 = servoBits2Us(PPM[ppmCountter]) * PWM_MULTIPLIER;
ppmSync -= nextICR1;
if (ppmSync < (rx_config.minsync * PWM_MULTIPLIER)) {
ppmSync = rx_config.minsync * PWM_MULTIPLIER;
}
if ((disablePPM) || ((rx_config.flags & PPM_MAX_8CH) && (ppmCountter >= 8))) {
#ifdef USE_OCR1B
OCR1B = 65535; //do not generate a pulse
#else
OCR1A = 65535; //do not generate a pulse
#endif
} else {
#ifdef USE_OCR1B
OCR1B = nextICR1 - PPM_PULSELEN;
#else
OCR1A = nextICR1 - PPM_PULSELEN;
#endif
}
while (TCNT1 < PWM_DEJITTER);
outputDownAll();
if ((!disablePWM) && (ppmCountter > 0)) {
outputUp(ppmCountter - 1);
}
ppmCountter++;
} else {
ICR1 = nextICR1;
nextICR1 = ppmSync;
if (disablePPM) {
#ifdef USE_OCR1B
OCR1B = 65535; //do not generate a pulse
#else
OCR1A = 65535; //do not generate a pulse
#endif
} else {
#ifdef USE_OCR1B
OCR1B = nextICR1 - PPM_PULSELEN;
#else
OCR1A = nextICR1 - PPM_PULSELEN;
#endif
}
ppmSync = PPM_FRAMELEN;
while (TCNT1 < PWM_DEJITTER);
outputDownAll();
if (!disablePWM) {
outputUp(ppmChannels - 1);
}
ppmCountter = 0 ;
}
}
uint16_t RSSI2Bits(uint8_t rssi)
{
uint16_t ret = (uint16_t)rssi << 2;
if (ret < 12) {
ret = 12;
} else if (ret > 1012) {
ret = 1012;
}
return ret;
}
void set_PPM_rssi()
{
if (rx_config.RSSIpwm < 48) {
uint8_t out;
switch (rx_config.RSSIpwm & 0x30) {
case 0x00:
out = compositeRSSI;
break;
case 0x10:
out = (linkQ << 4);
break;
default:
out = smoothRSSI;
break;
}
PPM[rx_config.RSSIpwm & 0x0f] = RSSI2Bits(out);
} else if (rx_config.RSSIpwm < 63) {
PPM[(rx_config.RSSIpwm & 0x0f)] = RSSI2Bits(linkQ << 4);
PPM[(rx_config.RSSIpwm & 0x0f)+1] = RSSI2Bits(smoothRSSI);
}
}
void set_RSSI_output()
{
linkQ = countSetBits(linkQuality & 0x7fff);
if (linkQ == 15) {
// RSSI 0 - 255 mapped to 192 - ((255>>2)+192) == 192-255
compositeRSSI = (smoothRSSI >> 1) + 128;
} else {
// linkquality gives 0 to 14*9 == 126
compositeRSSI = linkQ * 9;
}
cli();
set_PPM_rssi();
sei();
if (rx_config.pinMapping[RSSI_OUTPUT] == PINMAP_RSSI) {
if ((compositeRSSI == 0) || (compositeRSSI == 255)) {
TCCR2A &= ~(1 << COM2B1); // disable RSSI PWM output
digitalWrite(OUTPUT_PIN[RSSI_OUTPUT], (compositeRSSI == 0) ? LOW : HIGH);
} else {
OCR2B = compositeRSSI;
TCCR2A |= (1 << COM2B1); // enable RSSI PWM output
}
}
}
static const uint16_t switchThresholds[3] = { 178, 500, 844 };
void updateSwitches()
{
uint8_t i;
for (i = 0; i < OUTPUTS; i++) {
uint8_t map = rx_config.pinMapping[i];
if ((map & 0xf0) == 0x10) { // 16-31
digitalWrite(OUTPUT_PIN[i], (PPM[map & 0x0f] > switchThresholds[i%3]) ? HIGH : LOW);
}
}
}
void failsafeApply()
{
if (failsafePPM[0] != 0xffff) {
for (int16_t i = 0; i < PPM_CHANNELS; i++) {
if (i == (rx_config.RSSIpwm & 0x0f)) {
continue;
}
if ((i == (rx_config.RSSIpwm & 0x0f) + 1) && (rx_config.RSSIpwm > 47)) {
continue;
}
cli();
PPM[i] = failsafePPM[i];
sei();
}
updateSwitches();
}
}
void setupOutputs()
{
uint8_t i;
ppmChannels = getChannelCount(&bind_data);
if ((rx_config.RSSIpwm & 0x0f) == ppmChannels) {
ppmChannels += 1;
}
if ((rx_config.RSSIpwm > 47) &&
(rx_config.RSSIpwm < 63) &&
((rx_config.RSSIpwm & 0x0f) == ppmChannels-1)) {
ppmChannels += 1;
}
if (ppmChannels > 16) {
ppmChannels=16;
}
for (i = 0; i < OUTPUTS; i++) {
chToMask[i].B = 0;
chToMask[i].C = 0;
chToMask[i].D = 0;
}
clearMask.B = 0xff;
clearMask.C = 0xff;
clearMask.D = 0xff;
for (i = 0; i < OUTPUTS; i++) {
if (rx_config.pinMapping[i] < PPM_CHANNELS) {
chToMask[rx_config.pinMapping[i]].B |= OUTPUT_MASKS[i].B;
chToMask[rx_config.pinMapping[i]].C |= OUTPUT_MASKS[i].C;
chToMask[rx_config.pinMapping[i]].D |= OUTPUT_MASKS[i].D;
clearMask.B &= ~OUTPUT_MASKS[i].B;
clearMask.C &= ~OUTPUT_MASKS[i].C;
clearMask.D &= ~OUTPUT_MASKS[i].D;
}
}
for (i = 0; i < OUTPUTS; i++) {
switch (rx_config.pinMapping[i]) {
case PINMAP_ANALOG:
pinMode(OUTPUT_PIN[i], INPUT);
break;
case PINMAP_TXD:
case PINMAP_RXD:
case PINMAP_SDA:
case PINMAP_SCL:
break; //ignore serial/I2C for now
default:
if (i == RXD_OUTPUT) {
UCSR0B &= 0xEF; //disable serial RXD
}
if (i == TXD_OUTPUT) {
UCSR0B &= 0xF7; //disable serial TXD
}
pinMode(OUTPUT_PIN[i], OUTPUT); //PPM,PWM,RSSI,LBEEP
break;
}
}
if (rx_config.pinMapping[PPM_OUTPUT] == PINMAP_PPM) {
digitalWrite(OUTPUT_PIN[PPM_OUTPUT], HIGH);
#ifdef USE_OCR1B
TCCR1A = (1 << WGM11) | (1 << COM1B1);
#else
TCCR1A = (1 << WGM11) | (1 << COM1A1);
#endif
} else {
TCCR1A = (1 << WGM11);
}
disablePWM = 1;
disablePPM = 1;
if ((rx_config.pinMapping[RSSI_OUTPUT] == PINMAP_RSSI) ||
(rx_config.pinMapping[RSSI_OUTPUT] == PINMAP_LBEEP)) {
pinMode(OUTPUT_PIN[RSSI_OUTPUT], OUTPUT);
digitalWrite(OUTPUT_PIN[RSSI_OUTPUT], LOW);
if (rx_config.pinMapping[RSSI_OUTPUT] == PINMAP_RSSI) {
TCCR2A = (1 << WGM20);
TCCR2B = (1 << CS20);
} else { // LBEEP
TCCR2A = (1 << WGM21); // mode=CTC
#if (F_CPU == 16000000)
TCCR2B = (1 << CS22) | (1 << CS20); // prescaler = 128
#elif (F_CPU == 8000000)
TCCR2B = (1 << CS22); // prescaler = 64
#else
#error F_CPU not supported
#endif
OCR2A = 62; // 1KHz
}
}
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
#ifdef USE_OCR1B
OCR1B = 65535; // no pulse =)
#else
OCR1A = 65535; // no pulse =)
#endif
ICR1 = 2000; // just initial value, will be constantly updated
ppmSync = PPM_FRAMELEN;
nextICR1 = PPM_FRAMELEN;
ppmCountter = 0;
TIMSK1 |= (1 << TOIE1);
if ((rx_config.flags & IMMEDIATE_OUTPUT) && failsafePPM[0]!=0xffff) {
failsafeApply();
disablePPM=0;
disablePWM=0;
}
}
void updateLBeep(bool packetLost)
{
#if defined(LLIND_OUTPUT)
if (rx_config.pinMapping[LLIND_OUTPUT] == PINMAP_LLIND) {
digitalWrite(OUTPUT_PIN[LLIND_OUTPUT],packetLost);
}
#endif
if (rx_config.pinMapping[RSSI_OUTPUT] == PINMAP_LBEEP) {
if (packetLost) {
TCCR2A |= (1 << COM2B0); // enable tone
} else {
TCCR2A &= ~(1 << COM2B0); // disable tone
}
}
}
uint8_t bindReceive(uint32_t timeout)
{
uint32_t start = millis();
uint8_t rxb;
init_rfm(1);
RF_Mode = Receive;
to_rx_mode();
Serial.println("Waiting bind\n");
while ((!timeout) || ((millis() - start) < timeout)) {
if (RF_Mode == Received) {
Serial.println("Got pkt\n");
spiSendAddress(0x7f); // Send the package read command
rxb = spiReadData();
if (rxb == 'b') {
for (uint8_t i = 0; i < sizeof(bind_data); i++) {
*(((uint8_t*) &bind_data) + i) = spiReadData();
}
if (bind_data.version == BINDING_VERSION) {
Serial.println("data good\n");
rxb = 'B';
tx_packet(&rxb, 1); // ACK that we got bound
Green_LED_ON; //signal we got bound on LED:s
return 1;
}
} else if ((rxb == 'p') || (rxb == 'i')) {
uint8_t rxc_buf[sizeof(rx_config) + 1];
if (rxb == 'p') {
rxc_buf[0] = 'P';
timeout = 0;
} else {
rxInitDefaults(1);
rxc_buf[0] = 'I';
}
if (watchdogUsed) {
rx_config.flags|=WATCHDOG_USED;
} else {
rx_config.flags&=~WATCHDOG_USED;
}
memcpy(rxc_buf + 1, &rx_config, sizeof(rx_config));
tx_packet(rxc_buf, sizeof(rx_config) + 1);
} else if (rxb == 't') {
uint8_t rxc_buf[sizeof(rxSpecialPins) + 5];
timeout = 0;
rxc_buf[0] = 'T';
rxc_buf[1] = (version >> 8);
rxc_buf[2] = (version & 0xff);
rxc_buf[3] = OUTPUTS;
rxc_buf[4] = sizeof(rxSpecialPins) / sizeof(rxSpecialPins[0]);
memcpy(rxc_buf + 5, &rxSpecialPins, sizeof(rxSpecialPins));
tx_packet(rxc_buf, sizeof(rxSpecialPins) + 5);
} else if (rxb == 'u') {
for (uint8_t i = 0; i < sizeof(rx_config); i++) {
*(((uint8_t*) &rx_config) + i) = spiReadData();
}
accessEEPROM(0, true);
rxb = 'U';
tx_packet(&rxb, 1); // ACK that we updated settings
} else if (rxb == 'f') {
uint8_t rxc_buf[33];
if (failsafePPM[0]!=0xffff) {
rxc_buf[0]='F';
for (uint8_t i = 0; i < 16; i++) {
uint16_t us = servoBits2Us(failsafePPM[i]);
rxc_buf[i * 2 + 1] = (us >> 8);
rxc_buf[i * 2 + 2] = (us & 0xff);
}
} else {
rxc_buf[0]='f';
}
tx_packet(rxc_buf, 33);
} else if (rxb == 'g') {
for (uint8_t i = 0; i < 16 ; i++) {
uint16_t val;
val = (uint16_t)spiReadData() << 8;
val += spiReadData();
failsafePPM[i] = servoUs2Bits(val);
}
rxb = 'G';
failsafeSave();
tx_packet(&rxb, 1);
} else if (rxb == 'G') {
failsafePPM[0] = 0xffff;
failsafeSave();
rxb = 'G';
tx_packet(&rxb, 1);
}
RF_Mode = Receive;
rx_reset();
}
}
return 0;
}
int8_t checkIfConnected(uint8_t pin1, uint8_t pin2)
{
int8_t ret = 0;
pinMode(pin1, OUTPUT);
digitalWrite(pin1, 1);
digitalWrite(pin2, 1);
delayMicroseconds(10);
if (digitalRead(pin2)) {
digitalWrite(pin1, 0);
delayMicroseconds(10);
if (!digitalRead(pin2)) {
ret = 1;
}
}
pinMode(pin1, INPUT);
digitalWrite(pin1, 0);
digitalWrite(pin2, 0);
return ret;
}
uint8_t rx_buf[21]; // RX buffer (uplink)
// First byte of RX buf is
// MSB..LSB [1bit uplink seqno.] [1bit downlink seqno] [6bits type)
// type 0x00 normal servo, 0x01 failsafe set
// type 0x38..0x3f uplinkked serial data
uint8_t tx_buf[9]; // TX buffer (downlink)(type plus 8 x data)
// First byte is meta
// MSB..LSB [1 bit uplink seq] [1bit downlink seqno] [6b telemtype]
// 0x00 link info [RSSI] [AFCC]*2 etc...
// type 0x38-0x3f downlink serial data 1-8 bytes
#define SERIAL_BUFSIZE 32
uint8_t serial_buffer[SERIAL_BUFSIZE];
uint8_t serial_head;
uint8_t serial_tail;
uint8_t hopcount;
uint8_t slaveAct = 0;
uint8_t slaveCnt = 0;
uint8_t slaveHandler(uint8_t *data, uint8_t flags)
{
if (flags & MYI2C_SLAVE_ISTX) {
if (flags & MYI2C_SLAVE_ISFIRST) {
*data = slaveState;
slaveCnt=0;
} else {
if (slaveCnt < getPacketSize(&bind_data)) {
*data = rx_buf[slaveCnt++];
} else {
return 0;
}
}
} else {
if (flags & MYI2C_SLAVE_ISFIRST) {
slaveAct = *data;
slaveCnt = 0;
if ((slaveAct & 0xe0) == 0x60) {
if (slaveState >= 2) {
RF_channel = (*data & 0x1f);
slaveState=3; // to RX mode
}
return 0;
} else if (slaveAct==0xfe) {
// deinitialize
slaveState=0;
return 0;
}
} else {
if (slaveAct==0xff) {
// load bind_data
if (slaveCnt<sizeof(bind_data)) {
((uint8_t *)(&bind_data))[slaveCnt++] = *data;
if (slaveCnt == sizeof(bind_data)) {
slaveState=1;
return 0;
}
} else {
return 0;
}
}
}
}
return 1;
}
void slaveLoop()
{
myI2C_slaveSetup(32, 0, 0, slaveHandler);
slaveState=0;
while(1) {
if (slaveState == 1) {
init_rfm(0); // Configure the RFM22B's registers for normal operation
slaveState = 2; // BIND applied
Red_LED_OFF;
} else if (slaveState == 3) {
Green_LED_OFF;
rfmSetChannel(RF_channel);
RF_Mode = Receive;
rx_reset();
slaveState = 4; // in RX mode
} else if (slaveState == 4) {
if (RF_Mode == Received) {
spiSendAddress(0x7f); // Send the package read command
for (int16_t i = 0; i < getPacketSize(&bind_data); i++) {
rx_buf[i] = spiReadData();
}
slaveState = 5;
Green_LED_ON;
}
}
}
}
void reinitSlave()
{
uint8_t ret, buf[sizeof(bind_data)+1];
buf[0] = 0xff;
memcpy(buf+1,&bind_data,sizeof(bind_data));
ret = myI2C_writeTo(32, buf, sizeof(bind_data)+1, MYI2C_WAIT);
if (ret==0) {
ret = myI2C_readFrom(32, buf, 1, MYI2C_WAIT);
if ((ret==0)) {
slaveState = 2;
} else {
slaveState = 255;
}
} else {
slaveState = 255;
}
if (slaveState==2) {
} else {
slaveFailedMs = millis();
}
}
void setup()
{
watchdogConfig(WATCHDOG_OFF);
//LEDs
pinMode(Green_LED, OUTPUT);
pinMode(Red_LED, OUTPUT);
setupSPI();
#ifdef SDN_pin
pinMode(SDN_pin, OUTPUT); //SDN
digitalWrite(SDN_pin, 0);
#endif
pinMode(0, INPUT); // Serial Rx
pinMode(1, OUTPUT); // Serial Tx
Serial.begin(115200);
rxReadEeprom();
failsafeLoad();
Serial.print("OpenLRSng RX starting ");
printVersion(version);
Serial.print(" on HW ");
Serial.println(BOARD_TYPE);
setupRfmInterrupt();
sei();
Red_LED_ON;
if (checkIfConnected(OUTPUT_PIN[2], OUTPUT_PIN[3])) { // ch1 - ch2 --> force scannerMode
while (1) {
Red_LED_OFF;
Green_LED_OFF;
scannerMode();
}
}
if (checkIfConnected(OUTPUT_PIN[0], OUTPUT_PIN[1]) || (!bindReadEeprom())) {
Serial.print("EEPROM data not valid or bind jumpper set, forcing bind\n");
if (bindReceive(0)) {
bindWriteEeprom();
Serial.println("Saved bind data to EEPROM\n");
Green_LED_ON;
}
setupOutputs();
} else {
setupOutputs();
if ((rx_config.pinMapping[SDA_OUTPUT] != PINMAP_SDA) ||
(rx_config.pinMapping[SCL_OUTPUT] != PINMAP_SCL)) {
rx_config.flags &= ~SLAVE_MODE;
}
if ((rx_config.flags & ALWAYS_BIND) && (!(rx_config.flags & SLAVE_MODE))) {
if (bindReceive(500)) {
bindWriteEeprom();
Serial.println("Saved bind data to EEPROM\n");
setupOutputs(); // parameters may have changed
Green_LED_ON;
}
}
}
if ((rx_config.pinMapping[SDA_OUTPUT] == PINMAP_SDA) &&
(rx_config.pinMapping[SCL_OUTPUT] == PINMAP_SCL)) {
myI2C_init(1);
if (rx_config.flags & SLAVE_MODE) {
Serial.println("I am slave");
slaveLoop();
} else {
uint8_t ret,buf;
delay(20);
ret = myI2C_readFrom(32, &buf, 1, MYI2C_WAIT);
if (ret==0) {
slaveState = 1;
}
}
}
Serial.print("Entering normal mode");
watchdogConfig(WATCHDOG_2S);
init_rfm(0); // Configure the RFM22B's registers for normal operation
RF_channel = 0;
rfmSetChannel(RF_channel);
// Count hopchannels as we need it later
hopcount=0;
while ((hopcount < MAXHOPS) && (bind_data.hopchannel[hopcount] != 0)) {
hopcount++;
}
//################### RX SYNC AT STARTUP #################
RF_Mode = Receive;
to_rx_mode();
if (slaveState) {
reinitSlave();
}
if ((rx_config.pinMapping[TXD_OUTPUT] == PINMAP_SPKTRM) ||
(rx_config.pinMapping[TXD_OUTPUT] == PINMAP_SUMD)) {
Serial.begin(115200);
} else if (rx_config.pinMapping[TXD_OUTPUT] == PINMAP_SBUS) {
Serial.begin(100000);
UCSR0C |= 1<<UPM01; // set even parity
} else if ((bind_data.flags & TELEMETRY_MASK) == TELEMETRY_FRSKY) {
Serial.begin(9600);
} else {
if (bind_data.serial_baudrate < 10) {
Serial.begin(9600);
} else {
Serial.begin(bind_data.serial_baudrate);
}
}
while (Serial.available()) {
Serial.read();
}
if (rx_config.pinMapping[RXD_OUTPUT]!=PINMAP_RXD) {
UCSR0B &= 0xEF; //disable serial RXD
}
if ((rx_config.pinMapping[TXD_OUTPUT]!=PINMAP_TXD) &&
(rx_config.pinMapping[TXD_OUTPUT]!=PINMAP_SUMD) &&
(rx_config.pinMapping[TXD_OUTPUT]!=PINMAP_SBUS) &&
(rx_config.pinMapping[TXD_OUTPUT]!=PINMAP_SPKTRM)) {
UCSR0B &= 0xF7; //disable serial TXD
}
serial_head = 0;
serial_tail = 0;
linkAcquired = 0;
lastPacketTimeUs = micros();
}
void checkSerial()
{
while (Serial.available() && (((serial_tail + 1) % SERIAL_BUFSIZE) != serial_head)) {
serial_buffer[serial_tail] = Serial.read();
serial_tail = (serial_tail + 1) % SERIAL_BUFSIZE;
}
}
void slaveHop()
{
if (slaveState == 2) {
uint8_t buf;
buf = 0x60 + RF_channel;
if (myI2C_writeTo(32, &buf, 1, MYI2C_WAIT)) {
slaveState = 255;
slaveFailedMs = millis();
}
}
}
// Return slave state or 255 in case of error
uint8_t readSlaveState()
{
uint8_t ret = 255, buf;
if (slaveState == 2) {
ret = myI2C_readFrom(32, &buf, 1, MYI2C_WAIT);
if (ret) {
slaveState = 255;
slaveFailedMs = millis();
ret=255;
} else {
ret=buf;
}
}
return ret;
}
//#define SLAVE_STATISTICS
#ifdef SLAVE_STATISTICS
uint16_t rxBoth = 0;
uint16_t rxSlave = 0;
uint16_t rxMaster = 0;
uint32_t rxStatsMs = 0;
#endif
//############ MAIN LOOP ##############
void loop()
{
uint32_t timeUs, timeMs;
watchdogReset();
if (spiReadRegister(0x0C) == 0) { // detect the locked module and reboot
Serial.println("RX hang");
init_rfm(0);
to_rx_mode();
}
checkSerial();
timeUs = micros();
uint8_t slaveReceived = 0;
if (5 == readSlaveState()) {
slaveReceived = 1;
}
retry:
if ((RF_Mode == Received) || (slaveReceived)) {
uint32_t timeTemp = micros();
if (RF_Mode == Received) {
spiSendAddress(0x7f); // Send the package read command
for (int16_t i = 0; i < getPacketSize(&bind_data); i++) {
rx_buf[i] = spiReadData();
}
lastAFCCvalue = rfmGetAFCC();
Green_LED_ON;
} else {
uint8_t ret, slave_buf[22];
ret = myI2C_readFrom(32, slave_buf, getPacketSize(&bind_data) + 1, MYI2C_WAIT);
if (ret) {
slaveState = 255;
slaveFailedMs = millis();
slaveReceived = 0;
goto retry; //slave failed when reading packet...
} else {
memcpy(rx_buf, slave_buf + 1, getPacketSize(&bind_data));
}
}
lastPacketTimeUs = timeTemp; // used saved timestamp to avoid skew by I2C
numberOfLostPackets = 0;
linkQuality <<= 1;
linkQuality |= 1;
Red_LED_OFF;
updateLBeep(false);
#ifdef SLAVE_STATISTICS
if (5 == readSlaveState()) {
if (RF_Mode == Received) {
rxBoth++;
} else {
rxSlave++;
}
} else {
rxMaster++;
}
#endif
if ((rx_buf[0] & 0x3e) == 0x00) {
cli();
unpackChannels(bind_data.flags & 7, PPM, rx_buf + 1);
#ifdef DEBUG_DUMP_PPM
for (uint8_t i = 0; i < 8; i++) {
Serial.print(PPM[i]);
Serial.print(',');
}
Serial.println();
#endif
set_PPM_rssi();
sei();
if (rx_buf[0] & 0x01) {
if (!fs_saved) {
for (int16_t i = 0; i < PPM_CHANNELS; i++) {
failsafePPM[i] = PPM[i];
}
failsafeSave();
fs_saved = 1;
}
} else if (fs_saved) {
fs_saved = 0;
}
} else {
// something else than servo data...
if ((rx_buf[0] & 0x38) == 0x38) {
if ((rx_buf[0] ^ tx_buf[0]) & 0x80) {
// We got new data... (not retransmission)
uint8_t i;
tx_buf[0] ^= 0x80; // signal that we got it
if (rx_config.pinMapping[TXD_OUTPUT] == PINMAP_TXD) {
for (i = 0; i <= (rx_buf[0] & 7);) {
i++;
Serial.write(rx_buf[i]);
}
}
}
}
}
if (linkAcquired == 0) {
linkAcquired = 1;
}
failsafeActive = 0;
disablePWM = 0;
disablePPM = 0;
if (bind_data.flags & TELEMETRY_MASK) {
if ((tx_buf[0] ^ rx_buf[0]) & 0x40) {
// resend last message
} else {
tx_buf[0] &= 0xc0;
tx_buf[0] ^= 0x40; // swap sequence as we have new data
if (serial_head != serial_tail) {
uint8_t bytes = 0;
while ((bytes < 8) && (serial_head != serial_tail)) {
bytes++;
tx_buf[bytes] = serial_buffer[serial_head];
serial_head = (serial_head + 1) % SERIAL_BUFSIZE;
}
tx_buf[0] |= (0x37 + bytes);
} else {
// tx_buf[0] lowest 6 bits left at 0
tx_buf[1] = lastRSSIvalue;
if (rx_config.pinMapping[ANALOG0_OUTPUT] == PINMAP_ANALOG) {
tx_buf[2] = analogRead(OUTPUT_PIN[ANALOG0_OUTPUT]) >> 2;
#ifdef ANALOG0_OUTPUT_ALT
} else if (rx_config.pinMapping[ANALOG0_OUTPUT_ALT] == PINMAP_ANALOG) {
tx_buf[2] = analogRead(OUTPUT_PIN[ANALOG0_OUTPUT_ALT]) >> 2;
#endif
} else {
tx_buf[2] = 0;
}
if (rx_config.pinMapping[ANALOG1_OUTPUT] == PINMAP_ANALOG) {
tx_buf[3] = analogRead(OUTPUT_PIN[ANALOG1_OUTPUT]) >> 2;
#ifdef ANALOG1_OUTPUT_ALT
} else if (rx_config.pinMapping[ANALOG1_OUTPUT_ALT] == PINMAP_ANALOG) {
tx_buf[3] = analogRead(OUTPUT_PIN[ANALOG1_OUTPUT_ALT]) >> 2;
#endif
} else {
tx_buf[3] = 0;
}
tx_buf[4] = (lastAFCCvalue >> 8);
tx_buf[5] = lastAFCCvalue & 0xff;
tx_buf[6] = countSetBits(linkQuality & 0x7fff);
}
}
#ifdef TEST_NO_ACK_BY_CH1
if (PPM[0]<900) {
tx_packet_async(tx_buf, 9);
while(!tx_done()) {
checkSerial();
}
}
#else
tx_packet_async(tx_buf, 9);
while(!tx_done()) {
checkSerial();
}
#endif
#ifdef TEST_HALT_RX_BY_CH2
if (PPM[1]>1013) {
fatalBlink(3);
}
#endif
}
updateSwitches();
RF_Mode = Receive;
rx_reset();
willhop = 1;
Green_LED_OFF;
}
timeUs = micros();
timeMs = millis();
// sample RSSI when packet is in the 'air'
if ((numberOfLostPackets < 2) && (lastRSSITimeUs != lastPacketTimeUs) &&
(timeUs - lastPacketTimeUs) > (getInterval(&bind_data) - 1500)) {
lastRSSITimeUs = lastPacketTimeUs;
lastRSSIvalue = rfmGetRSSI(); // Read the RSSI value
RSSI_sum += lastRSSIvalue; // tally up for average
RSSI_count++;
if (RSSI_count > 8) {
RSSI_sum /= RSSI_count;
smoothRSSI = (((uint16_t)smoothRSSI * 3 + (uint16_t)RSSI_sum * 1) / 4);
set_RSSI_output();
RSSI_sum = 0;
RSSI_count = 0;
}
}
if (linkAcquired) {
if ((numberOfLostPackets < hopcount) && ((timeUs - lastPacketTimeUs) > (getInterval(&bind_data) + 1000))) {
// we lost packet, hop to next channel
linkQuality <<= 1;
willhop = 1;
if (numberOfLostPackets == 0) {
linkLossTimeMs = timeMs;
lastBeaconTimeMs = 0;
}
numberOfLostPackets++;
lastPacketTimeUs += getInterval(&bind_data);
willhop = 1;
Red_LED_ON;
updateLBeep(true);
set_RSSI_output();
} else if ((numberOfLostPackets == hopcount) && ((timeUs - lastPacketTimeUs) > (getInterval(&bind_data) * hopcount))) {
// hop slowly to allow resync with TX
linkQuality = 0;
willhop = 1;
smoothRSSI = 0;