-
Notifications
You must be signed in to change notification settings - Fork 1
/
frsky.cpp
1459 lines (1275 loc) · 34.9 KB
/
frsky.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
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
/*
* Authors - Mike Blandford
* Bertrand Songis <bsongis@gmail.com>, Bryan J.Rentoul (Gruvin) <gruvin@gmail.com> and Philip Moss
*
* Adapted from jeti.cpp code by Karl Szmutny <shadow@privy.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
*/
#include "er9x.h"
#include "frsky.h"
// Enumerate FrSky packet codes
#define LINKPKT 0xfe
#define USRPKT 0xfd
#define A11PKT 0xfc
#define A12PKT 0xfb
#define A21PKT 0xfa
#define A22PKT 0xf9
#define ALRM_REQUEST 0xf8
#define RSSIRXPKT 0xf7
#define RSSITXPKT 0xf6
#define RSSI_REQUEST 0xf1
#define START_STOP 0x7e
#define BYTESTUFF 0x7d
#define STUFF_MASK 0x20
#define PRIVATE 0x1B
// SPORT defines
#define DATA_FRAME 0x10
#ifdef N2F
#define HUB_START_STOP 0x5e
#define FRSKYHUB 0x5e //???
#define BYTECANCEL 0x18
#endif
// Translate hub data positions
// Add a top bit, first word of two word value
// When top bit found, just save index(with top bit) and value
// When next value received, if saved value has top bit set, stoore the value and clear top bit
// then process latest values, saving them if necessary.
const prog_uint8_t APM Fr_indices[] =
{
HUBDATALENGTH-1,
FR_GPS_ALT | 0x80,
FR_TEMP1,
FR_RPM,
FR_FUEL,
FR_TEMP2,
FR_CELL_V,
HUBDATALENGTH-1,HUBDATALENGTH-1,
FR_GPS_ALTd,
HUBDATALENGTH-1,HUBDATALENGTH-1,
HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,
FR_ALT_BARO | 0x80,
FR_GPS_SPEED | 0x80,
FR_GPS_LONG | 0x80,
FR_GPS_LAT | 0x80,
FR_COURSE,
FR_GPS_DATMON,
FR_GPS_YEAR,
FR_GPS_HRMIN,
FR_GPS_SEC,
FR_GPS_SPEEDd,
FR_GPS_LONGd,
FR_GPS_LATd,
FR_COURSEd,
HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,
FR_ALT_BAROd,
FR_LONG_E_W,
FR_LAT_N_S,
FR_ACCX,
FR_ACCY,
FR_ACCZ,
FR_VSPD,
FR_CURRENT,
FR_V_AMP | 0x80,
FR_V_AMPd,
HUBDATALENGTH-1,
HUBDATALENGTH-1
} ;
uint8_t AltitudeDecimals ;
int16_t WholeAltitude ;
#define FRSKY_SPORT_PACKET_SIZE 9
uint8_t frskyRxBuffer[19]; // Receive buffer. 9 bytes (full packet), worst case 18 bytes with byte-stuffing (+1)
struct t_frskyTx
{
uint8_t frskyTxBuffer[19]; // Ditto for transmit buffer
uint8_t frskyTxBufferCount ;
uint8_t frskyTxISRIndex ;
} FrskyTx ;
//uint8_t FrskyRxBufferReady = 0;
uint8_t frskyStreaming = 0;
uint8_t frskyUsrStreaming = 0;
FrskyData frskyTelemetry[4];
//FrskyData frskyRSSI[2];
Frsky_current_info Frsky_current[2] ;
uint8_t frskyRSSIlevel[2] ;
uint8_t frskyRSSItype[2] ;
struct FrskyAlarm {
uint8_t level; // The alarm's 'urgency' level. 0=disabled, 1=yellow, 2=orange, 3=red
uint8_t greater; // 1 = 'if greater than'. 0 = 'if less than'
uint8_t value; // The threshold above or below which the alarm will sound
};
struct FrskyAlarm frskyAlarms[4];
struct t_frksy_user
{
uint8_t state ;
uint8_t stuff ;
uint8_t id ;
uint8_t lobyte ;
uint8_t hibyte ;
uint8_t ready ;
} Frsky_user;
int16_t FrskyHubData[HUBDATALENGTH] ; // All 38 words
struct t_hub_max_min FrskyHubMaxMin ;
uint8_t FrskyVolts[12];
uint8_t FrskyBattCells=0;
int16_t Frsky_Amp_hour_prescale ;
uint8_t FrskyTelemetryType ;
#if defined(VARIO)
struct t_vario VarioData ;
#endif
void evalVario(int16_t altitude_bp, uint16_t altitude_ap)
{
#if defined(VARIO)
struct t_vario *vptr ;
vptr = &VarioData ;
int32_t varioAltitude_cm = (int32_t)altitude_bp * 100 + (altitude_bp > 0 ? altitude_ap : -altitude_ap) ;
uint8_t varioAltitudeQueuePointer = vptr->VarioAltitudeQueuePointer + 1 ;
if (varioAltitudeQueuePointer >= VARIO_QUEUE_LENGTH)
{
varioAltitudeQueuePointer = 0 ;
}
vptr->VarioAltitudeQueuePointer = varioAltitudeQueuePointer ;
vptr->VarioSpeed -= vptr->VarioAltitudeQueue[varioAltitudeQueuePointer] ;
vptr->VarioAltitudeQueue[varioAltitudeQueuePointer] = varioAltitude_cm - vptr->VarioAltitude_cm;
vptr->VarioAltitude_cm = varioAltitude_cm;
vptr->VarioSpeed += vptr->VarioAltitudeQueue[varioAltitudeQueuePointer] ;
// FrskyHubData[FR_VSPD] = vptr->VarioSpeed ;
#endif
}
void store_hub_data( uint8_t index, uint16_t value ) ;
void store_indexed_hub_data( uint8_t index, uint16_t value )
{
index = pgm_read_byte( &Fr_indices[index] ) & 0x7F ;
store_hub_data( index, value ) ;
}
NOINLINE void store_cell_data( uint8_t battnumber, uint16_t cell )
{
FrskyVolts[battnumber] = ( cell & 0x0FFF ) / 10 ;
}
void store_hub_data( uint8_t index, uint16_t value )
{
if ( index == FR_ALT_BARO )
{
value *= 10 ;
if ( AltitudeDecimals )
{
WholeAltitude = value ;
index = FR_TRASH ;
}
}
if ( index == FR_ALT_BAROd )
{
AltitudeDecimals |= 1 ;
if ( value > 9 )
{
AltitudeDecimals |= 2 ;
}
if ( AltitudeDecimals & 2 )
{
value /= 10 ;
}
FrskyHubData[FR_ALT_BARO] = WholeAltitude + ( (WholeAltitude > 0) ? value : -value ) ;
}
if ( index == FR_SPORT_ALT )
{
index = FR_ALT_BARO ; // For max and min
FrskyHubData[FR_ALT_BARO] = value ;
}
if ( index < HUBDATALENGTH )
{
if ( !g_model.FrSkyGpsAlt )
{
FrskyHubData[index] = value ;
}
else
{
if ( index != FR_ALT_BARO )
{
FrskyHubData[index] = value ; /* ReSt */
}
if ( index == FR_GPS_ALT )
{
FrskyHubData[FR_ALT_BARO] = FrskyHubData[FR_GPS_ALT] * 10 ; // Copy Gps Alt instead
index = FR_ALT_BARO ; // For max and min
}
}
#if defined(VARIO)
if ( index == FR_ALT_BARO )
{
evalVario( value, 0 ) ;
}
#endif
if ( index == FR_CURRENT ) // FAS current
{
FrskyHubData[index] -= g_model.frsky.FASoffset ;
}
if ( index < HUBMINMAXLEN )
{
struct t_hub_max_min *maxMinPtr = &FrskyHubMaxMin ;
FORCE_INDIRECT( maxMinPtr) ;
int16_t value = FrskyHubData[index] ;
if ( maxMinPtr->hubMax[index] < value )
{ maxMinPtr->hubMax[index] = value ;
}
int16_t min = maxMinPtr->hubMin[index] ;
if (!min || min > value)
{ maxMinPtr->hubMin[index] = value ;
}
}
if ( index == FR_CELL_V ) // Cell Voltage
{
// It appears the cell voltage bytes are in the wrong order
// uint8_t battnumber = ( FrskyHubData[6] >> 12 ) & 0x000F ;
uint8_t battnumber = ((uint8_t)value >> 4 ) & 0x000F ;
if (FrskyBattCells < battnumber+1)
{
if (battnumber+1>=6)
{
FrskyBattCells=6;
}
else
{
FrskyBattCells=battnumber+1;
}
}
store_cell_data( battnumber, ( ( value & 0x0F ) << 8 ) + (value >> 8) ) ;
}
if ( index == FR_RPM ) // RPM
{
uint32_t x ;
x = FrskyHubData[FR_RPM] ;
x *= 60 ;
uint8_t b = g_model.numBlades ;
if ( b == 0 )
{
b = 1 ;
g_model.numBlades = b ;
}
FrskyHubData[FR_RPM] = x / b ;
}
if ( index == FR_V_AMPd ) // RPM
{
FrskyHubData[FR_VOLTS] = (FrskyHubData[FR_V_AMP] * 10 + value) * 21 / 11 ;
}
}
}
void frsky_proc_user_byte( uint8_t byte )
{
struct t_frksy_user *fUserPtr = &Frsky_user ;
FORCE_INDIRECT( fUserPtr ) ;
if (g_model.FrSkyUsrProto == 0) // FrSky Hub
{
if ( fUserPtr->state == 0 )
{ // Waiting for 0x5E
if ( byte == 0x5E )
{
fUserPtr->state = 1 ;
if ( fUserPtr->ready )
{
fUserPtr->ready = 0 ;
store_indexed_hub_data( fUserPtr->id, ( fUserPtr->hibyte << 8 ) | fUserPtr->lobyte ) ;
}
}
}
else
{ // In a packet
if ( byte == 0x5E )
{ //
fUserPtr->state = 1 ;
}
else
{
if ( byte == 0x5D )
{
fUserPtr->stuff = 1 ; // Byte stuffing active
}
else
{
if ( fUserPtr->stuff )
{
fUserPtr->stuff = 0 ;
byte ^= 0x60 ; // Unstuff
}
if ( fUserPtr->state == 1 )
{
if ( byte > 57 )
{
byte -= 17 ; // Move voltage-amp sensors
} // 58->41, 59->42
if ( byte == 48 )
{
byte = FR_VSPD ; // Move Vario
}
if ( byte > sizeof(Fr_indices) )
{
byte = 0 ; // Use a discard item
}
fUserPtr->id = byte ;
fUserPtr->state = 2 ;
}
else if ( fUserPtr->state == 2 )
{
fUserPtr->lobyte = byte ;
fUserPtr->state = 3 ;
}
else
{
fUserPtr->hibyte = byte ;
fUserPtr->ready = 1 ;
fUserPtr->state = 0 ;
}
}
}
}
}
else // if (g_model.FrSkyUsrProto == 1) // WS How High
{
if ( frskyUsrStreaming < (FRSKY_USR_TIMEOUT10ms - 10)) // At least 100mS passed since last data received
{
fUserPtr->lobyte = byte ;
}
else
{
int16_t value ;
value = ( byte << 8 ) | fUserPtr->lobyte ;
store_hub_data( FR_ALT_BARO, value ) ; // Store altitude info
#if defined(VARIO)
evalVario( value, 0 ) ;
#endif
}
}
}
static uint8_t frskyPushValue( uint8_t i, uint8_t value);
/*
Called from somewhere in the main loop or a low prioirty interrupt
routine perhaps. This funtcion processes Fr-Sky telemetry data packets
assembled byt he USART0_RX_vect) ISR function (below) and stores
extracted data in global variables for use by other parts of the program.
Packets can be any of the following:
- A1/A2/RSSI telemtry data
- Alarm level/mode/threshold settings for Ch1A, Ch1B, Ch2A, Ch2B
- User Data packets
*/
//uint8_t LinkAveCount ;
void processFrskyPacket(uint8_t *packet)
{
// What type of packet?
switch (packet[0])
{
case A22PKT:
case A21PKT:
case A12PKT:
case A11PKT:
{
struct FrskyAlarm *alarmptr ;
alarmptr = &frskyAlarms[(packet[0]-A22PKT)] ;
alarmptr->value = packet[1];
alarmptr->greater = packet[2] & 0x01;
alarmptr->level = packet[3] & 0x03;
}
break;
case LINKPKT: // A1/A2/RSSI values
// From a scope, this seems to be sent every about every 35mS
// LinkAveCount += 1 ;
frskyTelemetry[0].set(packet[1], FR_A1_COPY ); //FrskyHubData[] = frskyTelemetry[0].value ;
frskyTelemetry[1].set(packet[2], FR_A2_COPY ); //FrskyHubData[] = frskyTelemetry[1].value ;
frskyTelemetry[2].set(packet[3], FR_RXRSI_COPY ); //FrskyHubData[] = frskyTelemetry[2].value ;
frskyTelemetry[3].set(packet[4] / 2, FR_TXRSI_COPY ); //FrskyHubData[] = frskyTelemetry[3].value ;
// if ( LinkAveCount > 15 )
// {
// LinkAveCount = 0 ;
// }
// frskyRSSI[0].set(packet[3]);
// frskyRSSI[1].set(packet[4] / 2);
break;
case RSSIRXPKT :
frskyRSSIlevel[1] = packet[1] ;
frskyRSSItype[1] = packet[3] ;
break ;
case RSSITXPKT :
frskyRSSIlevel[0] = packet[1] ;
frskyRSSItype[0] = packet[3] ;
break ;
case USRPKT: // User Data packet
{
uint8_t i, j ;
i = (packet[1] & 0x07) + 3 ; // User bytes end
j = 3 ; // Index to user bytes
while ( j < i )
{
frsky_proc_user_byte( packet[j] ) ;
frskyUsrStreaming = FRSKY_USR_TIMEOUT10ms ; // reset counter only if valid frsky packets are being detected
j += 1 ;
}
}
break;
// Support native FrSky Sensor Hub protocol
#if defined(N2F)
case FRSKYHUB: //!!!!!!!!!!!!!!!!!!!!!! Found Start of Packet= 0x5E
{
uint8_t i, j ;
i = 4 ; // User bytes end
j = 0 ; // Index to user bytes
while ( j < i )
{
frsky_proc_user_byte( packet[j] ) ;
// reset counter only if valid frsky packets are being detected
j += 1 ;
}
frskyUsrStreaming = FRSKY_USR_TIMEOUT10ms;
}
break; //!!!!!!!!!!!!!!!!!!
#endif
}
// FrskyRxBufferReady = 0;
frskyStreaming = FRSKY_TIMEOUT10ms; // reset counter only if valid frsky packets are being detected
}
// Receive buffer state machine state defs
#define frskyDataIdle 0
#define frskyDataStart 1
#define frskyDataInFrame 2
#define frskyDataXOR 3
static bool checkSportPacket()
{
uint8_t *packet = frskyRxBuffer ;
uint16_t crc = 0 ;
for ( uint8_t i=1; i<FRSKY_SPORT_PACKET_SIZE; i++)
{
crc += packet[i]; //0-1FF
crc += crc >> 8; //0-100
crc &= 0x00ff;
crc += crc >> 8; //0-0FF
crc &= 0x00ff;
}
return (crc == 0x00ff) ;
}
void processSportPacket()
{
uint8_t *packet = frskyRxBuffer ;
uint8_t prim = packet[1];
// uint16_t appId = *((uint16_t *)(packet+2)) ;
if ( !checkSportPacket() )
{
return;
}
frskyStreaming = FRSKY_TIMEOUT10ms * 3 ; // reset counter only if valid frsky packets are being detected
if ( prim == DATA_FRAME )
{
if ( packet[3] == 0xF1 )
{ // Receiver specific
uint8_t value = packet[4] ;
switch ( packet[2] )
{
case 1 :
frskyTelemetry[2].set(value, FR_RXRSI_COPY ); //FrskyHubData[] = frskyTelemetry[2].value ;
break ;
case 2 :
case 4 : // Battery from X8R
frskyTelemetry[0].set(value, FR_A1_COPY ); //FrskyHubData[] = frskyTelemetry[0].value ;
break ;
case 3 :
frskyTelemetry[1].set(value, FR_A2_COPY ); //FrskyHubData[] = frskyTelemetry[1].value ;
break ;
case 5 : // SWR
frskyTelemetry[3].set(value, FR_TXRSI_COPY ); //FrskyHubData[] = frskyTelemetry[3].value ;
break ;
}
}
else if ( packet[3] == 0 )
{ // old sensors
frskyUsrStreaming = 255 ; //FRSKY_USR_TIMEOUT10ms ; // reset counter only if valid frsky packets are being detected
uint16_t value = (*((uint16_t *)(packet+4))) ;
store_indexed_hub_data( packet[2], value ) ;
}
else
{ // new sensors
frskyUsrStreaming = 255 ; //FRSKY_USR_TIMEOUT10ms ; // reset counter only if valid frsky packets are being detected
uint8_t id = (packet[3] << 4) | ( packet[2] >> 4 ) ;
uint32_t value = (*((uint32_t *)(packet+4))) ;
// SportId = id ;
// SportValue = value ;
switch ( id )
{
case ALT_ID_8 :
value = (int32_t)value / 10 ;
store_hub_data( FR_SPORT_ALT, value ) ;
break ;
case VARIO_ID_8 :
store_hub_data( FR_VSPD, value ) ;
break ;
#if defined(CPUM128) || defined(CPUM2561)
case BETA_ALT_ID_8 :
value = (int32_t)value >> 8 ;
value = (int32_t)value / 10 ;
store_hub_data( FR_SPORT_ALT, value ) ;
break ;
#endif
// case BETA_VARIO_ID_8 :
// value = (int32_t)value >> 8 ;
// store_hub_data( FR_VSPD, value ) ;
// break ;
case CELLS_ID_8 :
{
uint8_t battnumber = value ;
uint16_t cell ;
FrskyBattCells = battnumber >> 4 ;
battnumber &= 0x0F ;
value >>= 8 ;
cell = value ;
store_cell_data( battnumber, cell ) ;
battnumber += 1 ;
value >>= 12 ;
cell = value ;
store_cell_data( battnumber, cell ) ;
}
break ;
case CURR_ID_8 :
store_hub_data( FR_CURRENT, value ) ;
break ;
case VFAS_ID_8 :
store_hub_data( FR_VOLTS, value / 10 ) ;
break ;
}
}
}
}
#ifndef N2F
#define PRIVATE_COUNT 4
#define PRIVATE_VALUE 5
#else
#define frskyDataIgnore 4
#define PRIVATE_COUNT 5
#define PRIVATE_VALUE 6
#endif
/*
Receive serial (RS-232) characters, detecting and storing each Fr-Sky
0x7e-framed packet as it arrives. When a complete packet has been
received, process its data into storage variables. NOTE: This is an
interrupt routine and should not get too lengthy. I originally had
the buffer being checked in the perMain function (because per10ms
isn't quite often enough for data streaming at 9600baud) but alas
that scheme lost packets also. So each packet is parsed as it arrives,
directly at the ISR function (through a call to frskyProcessPacket).
If this proves a problem in the future, then I'll just have to implement
a second buffer to receive data while one buffer is being processed (slowly).
*/
uint8_t Private_count ;
uint8_t Private_position ;
//extern uint8_t TrotCount ;
//extern uint8_t TezRotary ;
// debug
//uint8_t Uerror ;
//uint8_t Uecount ;
#ifndef SIMU
ISR(USART0_RX_vect)
{
uint8_t stat;
uint8_t data;
static uint8_t numPktBytes = 0;
static uint8_t dataState = frskyDataIdle;
UCSR0B &= ~(1 << RXCIE0); // disable Interrupt
sei() ;
stat = UCSR0A; // USART control and Status Register 0 A
/*
bit 7 6 5 4 3 2 1 0
RxC0 TxC0 UDRE0 FE0 DOR0 UPE0 U2X0 MPCM0
RxC0: Receive complete
TXC0: Transmit Complete
UDRE0: USART Data Register Empty
FE0: Frame Error
DOR0: Data OverRun
UPE0: USART Parity Error
U2X0: Double Tx Speed
PCM0: MultiProcessor Comms Mode
*/
// rh = UCSR0B; //USART control and Status Register 0 B
/*
bit 7 6 5 4 3 2 1 0
RXCIE0 TxCIE0 UDRIE0 RXEN0 TXEN0 UCSZ02 RXB80 TXB80
RxCIE0: Receive Complete int enable
TXCIE0: Transmit Complete int enable
UDRIE0: USART Data Register Empty int enable
RXEN0: Rx Enable
TXEN0: Tx Enable
UCSZ02: Character Size bit 2
RXB80: Rx data bit 8
TXB80: Tx data bit 8
*/
data = UDR0; // USART data register 0
uint8_t numbytes = numPktBytes ;
if (stat & ((1 << FE0) | (1 << DOR0) | (1 << UPE0)))
{ // discard buffer and start fresh on any comms error
// FrskyRxBufferReady = 0;
numbytes = 0;
// Uerror = stat & ((1 << FE0) | (1 << DOR0) | (1 << UPE0)) ;
// Uecount += 1 ;
}
else
{
// if (FrskyRxBufferReady == 0) // can't get more data if the buffer hasn't been cleared
// {
switch (dataState)
{
case frskyDataStart:
//-------------------------------------------------------------------------------------
#ifdef N2F // This code supports the native FrySky Sensor Hub protocol
if (data == HUB_START_STOP) break; // Remain in userDataStart if possible 0x5e,0x5e doublet found.
frskyRxBuffer[numbytes++] = data; //ID of field.
dataState = frskyDataInFrame;
break;
case frskyDataInFrame:
if (numbytes > 4) // failure, restart
{
numbytes = 0;
dataState = frskyDataIdle;
break;
}
//---------------
if (data == BYTESTUFF)
{
dataState = frskyDataXOR; // XOR next byte
break;
}
//---------------
//following BYTECANCEL = 0x18 the next byte is sent twice. Ignore the next byte
if (data == BYTECANCEL)
{
dataState = frskyDataIgnore; // ignore next byte
break;
}
//----------------
if (data == HUB_START_STOP) // end of field 0x5e detected, is used as start of next field
{
processFrskyPacket(frskyRxBuffer); // FrskyRxBufferReady = 1; packet is "5E ID Byte Byte "
numbytes = 0;
dataState = frskyDataStart;
frskyRxBuffer[numbytes++] = data; // put starting 5E into buffer
break;
}
//----------------
frskyRxBuffer[numbytes++] = data;
break;
case frskyDataXOR:
if (numbytes < 19)
frskyRxBuffer[numbytes++] = data ^ STUFF_MASK;
dataState = frskyDataInFrame;
break;
case frskyDataIgnore:
dataState = frskyDataInFrame;
break; // ignore this byyte
case frskyDataIdle:
if (data == HUB_START_STOP) // 0x5E
{
numbytes = 0;
frskyRxBuffer[numbytes++] = data; // put 5E into buffer
dataState = frskyDataStart;
}
break;
//---------------------------------------------------------------------------------
#else // This is the original FrySky decoding
if (data == START_STOP)
{
if ( FrskyTelemetryType ) // SPORT
{
dataState = frskyDataInFrame ;
numbytes = 0;
}
break ; // Remain in userDataStart if possible 0x7e,0x7e doublet found.
}
dataState = frskyDataInFrame;
if (numbytes < 19)
frskyRxBuffer[numbytes++] = data;
break;
case frskyDataInFrame:
if (data == BYTESTUFF)
{
dataState = frskyDataXOR; // XOR next byte
break;
}
if (data == START_STOP) // end of frame detected
{
if ( FrskyTelemetryType ) // SPORT
{
dataState = frskyDataInFrame ;
numbytes = 0;
}
else
{
processFrskyPacket(frskyRxBuffer); // FrskyRxBufferReady = 1;
dataState = frskyDataIdle ;
}
break;
}
if (numbytes < 19)
frskyRxBuffer[numbytes++] = data;
break;
case frskyDataXOR:
dataState = frskyDataInFrame;
if (numbytes < 19)
frskyRxBuffer[numbytes++] = data ^ STUFF_MASK;
break;
case frskyDataIdle:
if (data == START_STOP)
{
numbytes = 0;
dataState = frskyDataStart;
}
else if (data == PRIVATE)
{
dataState = PRIVATE_COUNT ;
}
break;
case PRIVATE_COUNT :
dataState = PRIVATE_VALUE ;
Private_count = data ; // Count of bytes to receive
Private_position = 0 ;
break;
case PRIVATE_VALUE :
if ( Private_position == 0 )
{
// Process first private data byte
// PC6, PC7
if ( ( data & 0x3F ) == 0 ) // Check byte is valid
{
DDRC |= 0xC0 ; // Set as outputs
PORTC = ( PORTC & 0x3F ) | ( data & 0xC0 ) ; // update outputs
}
}
if(Private_position==1) {
Rotary.TrotCount = data;
}
if(Private_position==2) { // rotary encoder switch
Rotary.TezRotary = data;
}
Private_position++;
if ( Private_position == Private_count )
{
dataState = frskyDataIdle;
}
break;
//---------------------------------------------------------------------------------
#endif
} // switch
// } // if (FrskyRxBufferReady == 0)
}
if ( FrskyTelemetryType ) // SPORT
{
if (numbytes >= FRSKY_SPORT_PACKET_SIZE)
{
processSportPacket() ;
numbytes = 0 ;
dataState = frskyDataIdle;
}
}
numPktBytes = numbytes ;
cli() ;
UCSR0B |= (1 << RXCIE0); // enable Interrupt
}
#endif
/*
USART0 (transmit) Data Register Emtpy ISR
Usef to transmit FrSky data packets, which are buffered in frskyTXBuffer.
*/
#ifndef SIMU
ISR(USART0_UDRE_vect)
{
struct t_frskyTx *pftx = &FrskyTx ;
FORCE_INDIRECT(pftx) ;
if ( pftx->frskyTxBufferCount > 0)
{
pftx->frskyTxBufferCount--;
UDR0 = pftx->frskyTxBuffer[pftx->frskyTxISRIndex++];
} else
UCSR0B &= ~(1 << UDRIE0); // disable UDRE0 interrupt
}
/******************************************/
static void frskyTransmitBuffer()
{
FrskyTx.frskyTxISRIndex = 0;
UCSR0B |= (1 << UDRIE0); // enable UDRE0 interrupt
}
#endif
uint8_t FrskyAlarmSendState = 0 ;
uint8_t FrskyDelay = 0 ;
//uint8_t FrskyRSSIsend = 0 ;
#ifndef SIMU
static void FRSKY10mspoll(void)
{
#if defined(CPUM128) || defined(CPUM2561)
if ( g_eeGeneral.FrskyPins == 0 )
return ;
#endif
if (FrskyDelay)
{
FrskyDelay -= 1 ;
return ;
}
if (FrskyTx.frskyTxBufferCount)
{
return; // we only have one buffer. If it's in use, then we can't send yet.
}
// Now send a packet
{
uint8_t i ;
uint8_t j = 1 ;
for ( i = 0 ; i < 7 ; i += 1, j <<= 1 )
{
if ( FrskyAlarmSendState & j )
{
break ;
}
}
FrskyAlarmSendState &= ~j ;
if ( i < 4 )
{
uint8_t channel = 1 - (i / 2);
uint8_t alarm = 1 - (i % 2);
// FRSKY_setTxPacket( A22PKT + i, g_eeGeneral.frskyinternalalarm ? 0 :g_model.frsky.channels[channel].alarms_value[alarm],
// ALARM_GREATER(channel, alarm), ALARM_LEVEL(channel, alarm) ) ;
FRSKY_setTxPacket( A22PKT + i, g_model.frsky.channels[channel].opt.alarm.alarms_value[alarm],
ALARM_GREATER(channel, alarm), g_eeGeneral.frskyinternalalarm ? 0 :ALARM_LEVEL(channel, alarm) ) ;
}
else if( i < 6 )
{
i &= 1 ;
FRSKY_setTxPacket( RSSITXPKT+i, frskyRSSIlevel[i], 0, frskyRSSItype[i] ) ;
}
else if (i == 6)
{
// Send packet requesting all RSSIalarm settings be sent back to us
FRSKY_setTxPacket( RSSI_REQUEST, 0, 0, 0 ) ;
}
else
{
return ;
}
FrskyDelay = 5 ; // 50mS
frskyTransmitBuffer();
}
}
#endif
// uint8_t i = 0;
// for (int alarm=0; alarm<2; alarm++) {
// frskyTxBuffer[i++] = START_STOP; // Start of packet
// frskyTxBuffer[i++] = (RSSI1PKT-alarm); // f7 - f6
// frskyPushValue(i, g_eeGeneral.frskyRssiAlarms[alarm].value+50-(10*i));
// {
// uint8_t *ptr ;
// ptr = &frskyTxBuffer[i] ;
// *ptr++ = 0x00 ;
// *ptr++ = g_eeGeneral.frskyRssiAlarms[alarm].level;
// *ptr++ = 0x00 ;
// *ptr++ = 0x00 ;
// *ptr++ = 0x00 ;
// *ptr++ = 0x00 ;
// *ptr++ = 0x00 ;
// *ptr++ = START_STOP; // End of packet
// i += 8 ;
// }
// }
// frskyTxBufferCount = i;
void FRSKY_setTxPacket( uint8_t type, uint8_t value, uint8_t p1, uint8_t p2 )
{
uint8_t i = 0;
FrskyTx.frskyTxBuffer[i++] = START_STOP; // Start of packet
FrskyTx.frskyTxBuffer[i++] = type ;