-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfrsky.cpp
2295 lines (2006 loc) · 55.6 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 <[email protected]>, Bryan J.Rentoul (Gruvin) <[email protected]> and Philip Moss
*
* Adapted from jeti.cpp code by Karl Szmutny <[email protected]>
*
* 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"
#include "menus.h"
#include <stdlib.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
#define MPRIVATE 'M'
// 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.
// To allow either 0 or 0x80
#define FR_INDEX_FLAG 0
const prog_uint8_t APM Fr_indices[] =
{
HUBDATALENGTH-1,
FR_GPS_ALT | FR_INDEX_FLAG,
FR_TEMP1,
FR_RPM,
FR_FUEL,
FR_TEMP2,
FR_CELL_V,
#if defined(CPUM128) || defined(CPUM2561)
FR_VCC, // 0x07 Extra data for Mavlink via FrSky
HUBDATALENGTH-1,
#else
HUBDATALENGTH-1,HUBDATALENGTH-1,
#endif
FR_GPS_ALTd,
#if defined(CPUM128) || defined(CPUM2561)
/* Extra data 1 for Mavlink via FrSky */
FR_HOME_DIR, // 0x0A
FR_HOME_DIST, // 0x0B
FR_CPU_LOAD, // 0x0C
FR_GPS_HDOP, // 0x0D
FR_WP_NUM, // 0x0E
FR_WP_BEARING, // 0x0F
/* Extra data 1 for Mavlink via FrSky */
#else
HUBDATALENGTH-1,HUBDATALENGTH-1, // 10,11
HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,
#endif
FR_ALT_BARO | FR_INDEX_FLAG,
FR_GPS_SPEED | FR_INDEX_FLAG,
FR_GPS_LONG | FR_INDEX_FLAG,
FR_GPS_LAT | FR_INDEX_FLAG,
FR_COURSE, // 20
FR_GPS_DATMON,
FR_GPS_YEAR,
FR_GPS_HRMIN,
FR_GPS_SEC,
FR_GPS_SPEEDd,
FR_GPS_LONGd,
FR_GPS_LATd,
FR_COURSEd, // 28
#if defined(CPUM128) || defined(CPUM2561)
/* Extra data 2 for Mavlink via FrSky */
FR_BASEMODE,
FR_WP_DIST,
FR_HEALTH,
FR_MSG,
/* Extra data 2 for Mavlink via FrSky */
#else
HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,HUBDATALENGTH-1,
#endif
FR_ALT_BAROd, // 33
FR_LONG_E_W,
FR_LAT_N_S,
FR_ACCX,
FR_ACCY,
FR_ACCZ,
FR_VSPD,
FR_CURRENT, // 40
FR_V_AMP | FR_INDEX_FLAG,
FR_V_AMPd,
FR_VOLTS,
HUBDATALENGTH-1, // 44
} ;
uint8_t TxLqi ;
uint8_t TmOK ;
uint8_t AltitudeDecimals ;
int16_t WholeAltitude ;
uint8_t A1Received = 0 ;
#define FRSKY_SPORT_PACKET_SIZE 9
uint8_t frskyRxBuffer[20]; // Receive buffer. 9 bytes (full packet), worst case 18 bytes with byte-stuffing (+1)
struct t_frskyTx
{
#ifndef V2
uint8_t frskyTxBuffer[13]; // Ditto for transmit buffer
#else
uint8_t frskyTxBuffer[6]; // V2 only sends 4 bytes of private data
#endif
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] ;
#ifndef V2
uint8_t frskyRSSIlevel[2] ;
uint8_t frskyRSSItype[2] ;
#endif // V2
struct t_frsky_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 ;
#define FRSKY_VOLTS_SIZE 6
uint8_t FrskyVolts[FRSKY_VOLTS_SIZE];
uint8_t FrskyBattCells=0;
int16_t Frsky_Amp_hour_prescale ;
uint8_t FrskyTelemetryType ;
uint16_t DsmABLRFH[6] ;
#if defined(VARIO)
struct t_vario VarioData ;
#endif
#if defined(VARIO)
void evalVario(int16_t altitude_bp, uint16_t altitude_ap)
{
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 )
{
if ( index > 57 )
{
index -= 17 ; // Move voltage-amp sensors
} // 58->41, 59->42
if ( index == 48 )
{
index = 39 ; //FR_VSPD ; // Move Vario
}
if ( index == 57 )
{
index = 43 ; // FR_VOLTS ; // Move Oxsensor voltage
}
if ( index > sizeof(Fr_indices) )
{
index = 0 ; // Use a discard item
}
#if FR_INDEX_FLAG
index = pgm_read_byte( &Fr_indices[index] ) & 0x7F ;
#else
index = pgm_read_byte( &Fr_indices[index] ) ;
#endif
store_hub_data( index, value ) ;
}
#if defined(CPUM128) || defined(CPUM2561)
void store_telemetry_scaler( uint8_t index, uint16_t value )
{
switch ( index )
{
case 1 :
store_hub_data( FR_BASEMODE, value ) ;
break ;
case 2 :
store_hub_data( FR_CURRENT, value ) ;
break ;
case 3 :
store_hub_data( FR_AMP_MAH, value ) ;
break ;
case 4 :
store_hub_data( FR_VOLTS, value ) ;
break ;
case 5 :
store_hub_data( FR_FUEL, value ) ;
break ;
}
}
#endif
NOINLINE void store_cell_data( uint8_t battnumber, uint16_t cell )
{
if ( battnumber < FRSKY_VOLTS_SIZE )
{
FrskyVolts[battnumber] = ( cell & 0x0FFF ) / 10 ;
}
}
void store_hub_data( uint8_t index, uint16_t value )
{
if ( index == FR_ALT_BARO )
{
int16_t val = (int16_t)value ;
val *= 10 ;
if ( AltitudeDecimals )
{
WholeAltitude = val ;
index = FR_TRASH ;
}
value = val ;
}
if ( index == FR_ALT_BAROd )
{
int16_t val = (int16_t)value ;
AltitudeDecimals |= 1 ;
if ( ( val > 9 ) || ( val < -9 ) )
{
AltitudeDecimals |= 2 ;
}
if ( AltitudeDecimals & 2 )
{
val /= 10 ;
}
FrskyHubData[FR_ALT_BARO] = WholeAltitude + val ;
index = FR_ALT_BARO ; // For max and min
}
if ( index == FR_SPORT_ALT )
{
FrskyHubData[FR_ALT_BARO] = value ;
index = FR_ALT_BARO ; // For max and min
}
//#if defined(CPUM128) || defined(CPUM2561)
if ( index == FR_SPORT_GALT )
{
index = FR_GPS_ALT ; // For max and min
FrskyHubData[FR_GPS_ALT] = value ;
}
//#endif
if ( index < HUBDATALENGTH )
{
#ifndef NOGPSALT
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
}
}
#else
FrskyHubData[index] = value ;
#endif
#if defined(VARIO)
if ( index == FR_ALT_BARO )
{
evalVario( value, 0 ) ;
}
#endif
if ( index == FR_CURRENT ) // FAS current
{
if ( value > g_model.frsky.FASoffset )
{
value -= g_model.frsky.FASoffset ;
}
else
{
value = 0 ;
}
FrskyHubData[index] = value ;
}
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 ;
uint8_t tempCells = battnumber+1 ;
if (FrskyBattCells < tempCells)
{
if (tempCells>=FRSKY_VOLTS_SIZE)
{
FrskyBattCells=FRSKY_VOLTS_SIZE;
}
else
{
FrskyBattCells=tempCells;
}
}
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_frsky_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 )
{
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
}
}
}
#ifndef V2
static uint8_t frskyPushValue( uint8_t i, uint8_t value);
#endif // V2
/*
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
*/
void setTxLqi( uint8_t value )
{
TxLqi = ( ( TxLqi * 7 ) + value + 4 ) / 8 ; // Multi only
}
//uint8_t LinkAveCount ;
void processFrskyPacket(uint8_t *packet)
{
// What type of packet?
switch (packet[0])
{
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 ;
setTxLqi( packet[6] ) ;
// if ( LinkAveCount > 15 )
// {
// LinkAveCount = 0 ;
// }
// frskyRSSI[0].set(packet[3]);
// frskyRSSI[1].set(packet[4] / 2);
break;
#ifndef V2
case RSSIRXPKT :
frskyRSSIlevel[1] = packet[1] ;
frskyRSSItype[1] = packet[3] ;
break ;
case RSSITXPKT :
frskyRSSIlevel[0] = packet[1] ;
frskyRSSItype[0] = packet[3] ;
break ;
#endif // V2
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
}
// DSM data
//0[00] 22(0x16)
//1[01] 00
//2[02] Altitude LSB (Decimal) //In 0.1m
//3[03] Altitude MSB (Decimal)
//4[04] 1/100th of a degree second latitude (Decimal) (XX YY.SSSS)
//5[05] degree seconds latitude (Decimal)
//6[06] degree minutes latitude (Decimal)
//7[07] degrees latitude (Decimal)
//8[08] 1/100th of a degree second longitude (Decimal) (XX YY.SSSS)
//9[09] degree seconds longitude (Decimal)
//10[0A] degree minutes longitude (Decimal)
//11[0B] degrees longitude (Decimal)
//12[0C] Heading LSB (Decimal)
//13[0D] Heading MSB (Decimal) Divide by 10 for Degrees
//14[0E] Unknown
//15[0F] First bit for latitude: 1=N(+), 0=S(-);
//Second bit for longitude: 1=E(+), 0=W(-);
//Third bit for longitude over 99 degrees: 1=+-100 degrees
//0[00] 23(0x17)
//1[01] 00
//2[02] Speed LSB (Decimal)
//3[03] Speed MSB (Decimal) Divide by 10 for Knots. Multiply by 0.185 for Kph and 0.115 for Mph
//4[04] UTC Time LSB (Decimal) 1/100th sec. (HH:MM:SS.SS)
//5[05] UTC Time (Decimal) = SS
//6[06] UTC Time (Decimal) = MM
//7[07] UTC Time MSB (Decimal) = HH
//8[08] Number of Sats (Decimal)
//9[09] Altitude in 1000m (Decimal) Altitude = Value * 10000 + Altitude(0x16) (in 0.1m)
//10[0A]-15[0F] Unused (But contains Data left in buffer)
//===============================================================
//Data type = 0x03 High Current Sensor
#define DSM_AMPS 3
//0 [00] 03
//1 [01] 00
//2 [02] MSB (Hex) //16bit signed integer
//3 [03] LSB (Hex) //In 0.196791A
//4 [04] 00
//5 [05] 00
//6 [06] 00
//7 [07] 00
//8 [08] 00
//9 [09] 00
//10 [0A] 00
//11 [0B] 00
//12 [0C] 00
//13 [0D] 00
//14 [0E] 00
//15 [0F] 00
//===============================================================
//Data type = 0x0A PowerBox Sensor
#define DSM_PBOX 0x0A
//0 [00] 0x0A
//1 [01] 00
//2 [02] V1 MSB (Hex)
//3 [03] V1 LSB (Hex) //In 0.01V
//4 [04] V2 MSB (Hex)
//5 [05] V2 LSB (Hex) //In 0.01V
//6 [06] Cap1 MSB (Hex)
//7 [07] Cap1 LSB (Hex) //In 1mAh
//8 [08] Cap2 MSB (Hex)
//9 [09] Cap2 LSB (Hex) //In 1mAh
//10 [0A] 00
//11 [0B] 00
//12 [0C] 00
//13 [0D] 00
//14 [0E] 00
//15 [0F] Alarm // The fist bit is alarm V1, the second V2, the third Capacity 1, the 4th capacity 2.
//===============================================================
//Data type = 0x11 AirSpeed Sensor
#define DSM_AIRSPEED 17
//0[00] 17(0x11)
//1[01] 00
//2[02] Speed MSB (Hex)
//3[03] Speed LSB (Hex) //In 1 km/h
//4[04] Unknown
//5[05] Unknown
//6[06] Unknown
//7[07] Unknown
//8[08] Unknown
//9[09] Unknown
//10[0A] Unknown
//11[0B] Unknown
//12[0C] Unknown
//13[0D] Unknown
//14[0E] Unknown
//15[0F] Unknown
//===============================================================
//Data type = 0x12 Altimeter Sensor
#define DSM_ALT 18
//0[00] 18(0x12)
//1[01] 00
//2[02] Altitude MSB (Hex)
//3[03] Altitude LSB (Hex) 16bit signed integer, in 0.1m
//4[04] Max Altitude MSB (Hex)
//5[05] Max Altitude LSB (Hex) 16bit signed integer, in 0.1m
//6[05] Unknown
//7[07] Unknown
//8[08] Unknown
//9[09] Unknown
//10[0A] Unknown
//11[0B] Unknown
//12[0C] Unknown
//13[0D] Unknown
//14[0E] Unknown
//15[0F] Unknown
//===============================================================
//Data type = 0x14 Gforce Sensor
#define DSM_GFORCE 20
//0[00] 20(0x14)
//1[01] 00
//2[02] x MSB (Hex, signed integer)
//3[03] x LSB (Hex, signed integer) //In 0.01g
//4[04] y MSB (Hex, signed integer)
//5[05] y LSB (Hex, signed integer) //In 0.01g
//6[06] z MSB (Hex, signed integer)
//7[07] z LSB (Hex, signed integer) //In 0.01g
//8[08] x max MSB (Hex, signed integer)
//9[09] x max LSB (Hex, signed integer) //In 0.01g
//10[0A] y max MSB (Hex, signed integer)
//11[0B] y max LSB (Hex, signed integer) //In 0.01g
//12[0C] z max MSB (Hex, signed integer)
//13[0D] z max LSB (Hex, signed integer) //In 0.01g
//14[0E] z min MSB (Hex, signed integer)
//15[0F] z min LSB (Hex, signed integer) //In 0.01g
//===============================================================
//Data type = 0x15 JetCat Sensor
//0[00] 21(0x15)
//1[01] 00
//2[02] Status
//3[03] Throttle //up to 159% (the upper nibble is 0-f, the lower nibble 0-9)
//4[04] Pack_Volt LSB (Decimal)
//5[05] Pack_Volt MSB (Decimal) //In 0.01V
//6[06] Pump_Volt LSB (Decimal)
//7[07] Pump_Volt MSB (Decimal) //In 0.01V
//8[08] RPM LSB (Decimal) //Up to 999999rpm
//9[09] RPM Mid (Decimal)
//10[0A] RPM MSB (Decimal)
//11[0B] 00
//12[0C] TempEGT (Decimal) LSB
//13[0D] TempEGT (Decimal) MSB //used only lover nibble, up to 999°C
//14[0E] Off_Condition
//15[0F] 00
//===============================================================
//Data type = 7E{TM1000} or FE{TM1100}
#define DSM_VTEMP1 0x7E
#define DSM_VTEMP2 0xFE
//0[00] 7E or FE
//1[01] 00
//2[02] RPM MSB (Hex)
//3[03] RPM LSB (Hex) //RPM = 120000000 / number_of_poles(2, 4, ... 32) / gear_ratio(0.01 - 30.99) / Value
//4[04] Volt MSB (Hex)
//5[05] Volt LSB (Hex) //In 0.01V
//6[06] Temp MSB (Hex)
//7[07] Temp LSB (Hex) //Value (Decimal) is in Fahrenheit, for Celsius (Value (Decimal) - 32) * 5) / 9)
//8[08] Unknown
//9[09] Unknown
//10[0A] Unknown
//11[0B] Unknown
//12[0C] Unknown
//13[0D] Unknown // Assan when 7E type is Rx RSSI
//14[0E] Unknown
//15[0F] Unknown
//===============================================================
//Data type = 7F{TM1000} or FF{TM1100}
#define DSM_STAT1 0x7F
#define DSM_STAT2 0xFF
//0[00] 7F or FF
//1[01] 00
//2[02] A MSB (Hex)
//3[03] A LSB (Hex)
//4[04] B MSB (Hex)
//5[05] B LSB (Hex)
//6[06] L MSB (Hex)
//7[07] L LSB (Hex) //0xFFFF = NC (not connected)
//8[08] R MSB (Hex)
//9[09] R LSB (Hex) //0xFFFF = NC (not connected)
//10[0A] Frame loss MSB (Hex)
//11[0B] Frame loss LSB (Hex)
//12[0C] Holds MSB (Hex)
//13[0D] Holds LSB (Hex)
//14[0E] Receiver Volts MSB (Hex) //In 0.01V
//15[0F] Receiver Volts LSB (Hex)
//answer from TX module:
//header: 0xAA
//0x00 - no telemetry
//0x01 - telemetry packet present and telemetry block (TM1000, TM1100)
//answer in 16 bytes body from
//http://www.deviationtx.com/forum/protocol-development/1192-dsm-telemetry-support?start=60
//..
//0x1F - this byte also used as RSSI signal for receiving telemetry
//block. readed from CYRF in a TX module
//0x80 - BIND packet answer, receiver return his type
//aa bb cc dd ee......
//aa ORTX_USExxxx from main.h
//bb
//cc - max channel, AR6115e work only in 6 ch mode (0xA2)
//0xFF - sysinfo
//aa.aa.aa.aa - CYRF manufacturer ID
//bb.bb - firmware version
// if(ortxRxBuffer[1] == 0x80){//BIND packet answer
// ortxMode = ortxRxBuffer[2];
// if(ortxMode & ORTX_USE_DSMX)
// g_model.ppmNCH = DSM2_DSMX;
// else
// g_model.ppmNCH = DSM2only;
// //ortxTxBuffer[3];//power
// g_model.unused1[0] = ortxRxBuffer[4];//channels number
//===============================================================
// 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;
}
return (crc == 0x00ff) ;
}
void processSportPacket()
{
uint8_t *packet = frskyRxBuffer ;
FORCE_INDIRECT(packet) ;
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 ;
setTxLqi( packet[7] ) ; // packet[7] is TX_LQI for MULTI
break ;
case 4 : // Battery from X8R
// frskyTelemetry[0].set(value, FR_A1_COPY ); //FrskyHubData[] = frskyTelemetry[0].value ;
store_hub_data( FR_RXV, value ) ;
if ( A1Received )
{
break ;
}
case 2 :
frskyTelemetry[0].set(value, FR_A1_COPY ); //FrskyHubData[] = frskyTelemetry[0].value ;
A1Received = 1 ;
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 ;
case A3_ID_8 :
store_hub_data( FR_A3, value ) ;
break ;
case A4_ID_8 :
store_hub_data( FR_A4, value ) ;
break ;
case T1_ID_8 :
store_hub_data( FR_TEMP1, value ) ;
break ;
case T2_ID_8 :
store_hub_data( FR_TEMP2, value ) ;
break ;
case RPM_ID_8 :
store_hub_data( FR_RPM, value ) ;
break ;
#if defined(CPUM128) || defined(CPUM2561)
case ACCX_ID_8 :
store_hub_data( FR_ACCX, value ) ;
break ;
case ACCY_ID_8 :
store_hub_data( FR_ACCY, value ) ;
break ;
case ACCZ_ID_8 :
store_hub_data( FR_ACCZ, value ) ;
break ;
case FUEL_ID_8 :
store_hub_data( FR_FUEL, value ) ;
break ;
#endif // 128/2561
case GPS_ALT_ID_8 :
value = (int32_t)value / 100 ;