Various fixes around Companion trainer mode (#7116)
[opentx.git] / radio / src / telemetry / frsky.h
blob8512c606c600ff5d8010afb206cb507007fefbf3
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _FRSKY_H_
22 #define _FRSKY_H_
24 #include "../definitions.h"
25 #include "telemetry_holders.h"
26 #include "../io/frsky_pxx2.h"
28 // Receive buffer state machine state enum
29 enum FrSkyDataState {
30 STATE_DATA_IDLE,
31 STATE_DATA_START,
32 STATE_DATA_IN_FRAME,
33 STATE_DATA_XOR,
36 #define FRSKY_SPORT_BAUDRATE 57600
38 #define FRSKY_D_BAUDRATE 9600
40 #define FRSKY_SPORT_PACKET_SIZE 9
42 // Enumerate FrSky packet codes
43 #define LINKPKT 0xfe
44 #define USRPKT 0xfd
45 #define A11PKT 0xfc
46 #define A12PKT 0xfb
47 #define A21PKT 0xfa
48 #define A22PKT 0xf9
49 #define ALRM_REQUEST 0xf8
50 #define RSSI1PKT 0xf7
51 #define RSSI2PKT 0xf6
52 #define RSSI_REQUEST 0xf1
54 // FrSky PRIM IDs (1 byte)
55 #define DATA_FRAME 0x10
57 // FrSky old DATA IDs (1 byte)
58 #define GPS_ALT_BP_ID 0x01
59 #define TEMP1_ID 0x02
60 #define RPM_ID 0x03
61 #define FUEL_ID 0x04
62 #define TEMP2_ID 0x05
63 #define VOLTS_ID 0x06
64 #define GPS_ALT_AP_ID 0x09
65 #define BARO_ALT_BP_ID 0x10
66 #define GPS_SPEED_BP_ID 0x11
67 #define GPS_LONG_BP_ID 0x12
68 #define GPS_LAT_BP_ID 0x13
69 #define GPS_COURS_BP_ID 0x14
70 #define GPS_DAY_MONTH_ID 0x15
71 #define GPS_YEAR_ID 0x16
72 #define GPS_HOUR_MIN_ID 0x17
73 #define GPS_SEC_ID 0x18
74 #define GPS_SPEED_AP_ID 0x19
75 #define GPS_LONG_AP_ID 0x1A
76 #define GPS_LAT_AP_ID 0x1B
77 #define GPS_COURS_AP_ID 0x1C
78 #define BARO_ALT_AP_ID 0x21
79 #define GPS_LONG_EW_ID 0x22
80 #define GPS_LAT_NS_ID 0x23
81 #define ACCEL_X_ID 0x24
82 #define ACCEL_Y_ID 0x25
83 #define ACCEL_Z_ID 0x26
84 #define CURRENT_ID 0x28
85 #define VARIO_ID 0x30
86 #define VFAS_ID 0x39
87 #define VOLTS_BP_ID 0x3A
88 #define VOLTS_AP_ID 0x3B
89 #define FRSKY_LAST_ID 0x3F
90 #define D_RSSI_ID 0xF0
91 #define D_A1_ID 0xF1
92 #define D_A2_ID 0xF2
94 #define VFAS_D_HIPREC_OFFSET 2000
96 // FrSky new DATA IDs (2 bytes)
97 #define ALT_FIRST_ID 0x0100
98 #define ALT_LAST_ID 0x010f
99 #define VARIO_FIRST_ID 0x0110
100 #define VARIO_LAST_ID 0x011f
101 #define CURR_FIRST_ID 0x0200
102 #define CURR_LAST_ID 0x020f
103 #define VFAS_FIRST_ID 0x0210
104 #define VFAS_LAST_ID 0x021f
105 #define CELLS_FIRST_ID 0x0300
106 #define CELLS_LAST_ID 0x030f
107 #define T1_FIRST_ID 0x0400
108 #define T1_LAST_ID 0x040f
109 #define T2_FIRST_ID 0x0410
110 #define T2_LAST_ID 0x041f
111 #define RPM_FIRST_ID 0x0500
112 #define RPM_LAST_ID 0x050f
113 #define FUEL_FIRST_ID 0x0600
114 #define FUEL_LAST_ID 0x060f
115 #define ACCX_FIRST_ID 0x0700
116 #define ACCX_LAST_ID 0x070f
117 #define ACCY_FIRST_ID 0x0710
118 #define ACCY_LAST_ID 0x071f
119 #define ACCZ_FIRST_ID 0x0720
120 #define ACCZ_LAST_ID 0x072f
121 #define GPS_LONG_LATI_FIRST_ID 0x0800
122 #define GPS_LONG_LATI_LAST_ID 0x080f
123 #define GPS_ALT_FIRST_ID 0x0820
124 #define GPS_ALT_LAST_ID 0x082f
125 #define GPS_SPEED_FIRST_ID 0x0830
126 #define GPS_SPEED_LAST_ID 0x083f
127 #define GPS_COURS_FIRST_ID 0x0840
128 #define GPS_COURS_LAST_ID 0x084f
129 #define GPS_TIME_DATE_FIRST_ID 0x0850
130 #define GPS_TIME_DATE_LAST_ID 0x085f
131 #define A3_FIRST_ID 0x0900
132 #define A3_LAST_ID 0x090f
133 #define A4_FIRST_ID 0x0910
134 #define A4_LAST_ID 0x091f
135 #define AIR_SPEED_FIRST_ID 0x0a00
136 #define AIR_SPEED_LAST_ID 0x0a0f
137 #define RBOX_BATT1_FIRST_ID 0x0b00
138 #define RBOX_BATT1_LAST_ID 0x0b0f
139 #define RBOX_BATT2_FIRST_ID 0x0b10
140 #define RBOX_BATT2_LAST_ID 0x0b1f
141 #define RBOX_STATE_FIRST_ID 0x0b20
142 #define RBOX_STATE_LAST_ID 0x0b2f
143 #define RBOX_CNSP_FIRST_ID 0x0b30
144 #define RBOX_CNSP_LAST_ID 0x0b3f
145 #define SD1_FIRST_ID 0x0b40
146 #define SD1_LAST_ID 0x0b4f
147 #define ESC_POWER_FIRST_ID 0x0b50
148 #define ESC_POWER_LAST_ID 0x0b5f
149 #define ESC_RPM_CONS_FIRST_ID 0x0b60
150 #define ESC_RPM_CONS_LAST_ID 0x0b6f
151 #define ESC_TEMPERATURE_FIRST_ID 0x0b70
152 #define ESC_TEMPERATURE_LAST_ID 0x0b7f
153 #define X8R_FIRST_ID 0x0c20
154 #define X8R_LAST_ID 0x0c2f
155 #define S6R_FIRST_ID 0x0c30
156 #define S6R_LAST_ID 0x0c3f
157 #define GASSUIT_TEMP1_FIRST_ID 0x0d00
158 #define GASSUIT_TEMP1_LAST_ID 0x0d0f
159 #define GASSUIT_TEMP2_FIRST_ID 0x0d10
160 #define GASSUIT_TEMP2_LAST_ID 0x0d1f
161 #define GASSUIT_SPEED_FIRST_ID 0x0d20
162 #define GASSUIT_SPEED_LAST_ID 0x0d2f
163 #define GASSUIT_RES_VOL_FIRST_ID 0x0d30
164 #define GASSUIT_RES_VOL_LAST_ID 0x0d3f
165 #define GASSUIT_RES_PERC_FIRST_ID 0x0d40
166 #define GASSUIT_RES_PERC_LAST_ID 0x0d4f
167 #define GASSUIT_FLOW_FIRST_ID 0x0d50
168 #define GASSUIT_FLOW_LAST_ID 0x0d5f
169 #define GASSUIT_MAX_FLOW_FIRST_ID 0x0d60
170 #define GASSUIT_MAX_FLOW_LAST_ID 0x0d6f
171 #define GASSUIT_AVG_FLOW_FIRST_ID 0x0d70
172 #define GASSUIT_AVG_FLOW_LAST_ID 0x0d7f
173 #define SBEC_POWER_FIRST_ID 0x0e50
174 #define SBEC_POWER_LAST_ID 0x0e5f
175 #define DIY_FIRST_ID 0x5100
176 #define DIY_LAST_ID 0x52ff
177 #define DIY_STREAM_FIRST_ID 0x5000
178 #define DIY_STREAM_LAST_ID 0x50ff
179 #define FACT_TEST_ID 0xf000
180 #define RSSI_ID 0xf101
181 #define ADC1_ID 0xf102
182 #define ADC2_ID 0xf103
183 #define BATT_ID 0xf104
184 #define RAS_ID 0xf105
185 #define XJT_VERSION_ID 0xf106
186 #define R9_PWR_ID 0xf107
187 #define SP2UART_A_ID 0xfd00
188 #define SP2UART_B_ID 0xfd01
189 #define FUEL_QTY_FIRST_ID 0x0a10
190 #define FUEL_QTY_LAST_ID 0x0a1f
192 // Default sensor data IDs (Physical IDs + CRC)
193 #define DATA_ID_VARIO 0x00 // 0
194 #define DATA_ID_FLVSS 0xA1 // 1
195 #define DATA_ID_FAS 0x22 // 2
196 #define DATA_ID_GPS 0x83 // 3
197 #define DATA_ID_RPM 0xE4 // 4
198 #define DATA_ID_SP2UH 0x45 // 5
199 #define DATA_ID_SP2UR 0xC6 // 6
201 inline bool IS_HIDDEN_TELEMETRY_VALUE(uint16_t id)
203 return (id == SP2UART_A_ID) || (id == SP2UART_B_ID) || (id == XJT_VERSION_ID) || (id == RAS_ID) || (id == FACT_TEST_ID);
206 class TelemetryData {
207 public:
208 TelemetryExpiringDecorator<TelemetryValue> swrInternal;
209 TelemetryExpiringDecorator<TelemetryValue> swrExternal;
210 TelemetryFilterDecorator<TelemetryValue> rssi;
211 uint16_t xjtVersion;
212 uint8_t varioHighPrecision:1;
213 uint8_t telemetryValid:3;
214 uint8_t spare:4;
216 void setSwr(uint8_t module, uint8_t value)
218 if (module == 0)
219 swrInternal.set(value);
220 else
221 swrExternal.set(value);
224 void clear()
226 memset(this, 0, sizeof(*this));
230 extern TelemetryData telemetryData;
232 inline uint8_t TELEMETRY_RSSI()
234 return telemetryData.rssi.value();
237 constexpr uint8_t START_STOP = 0x7E;
238 constexpr uint8_t BYTE_STUFF = 0x7D;
239 constexpr uint8_t STUFF_MASK = 0x20;
241 typedef enum {
242 TS_IDLE = 0, // waiting for 0x5e frame marker
243 TS_DATA_ID, // waiting for dataID
244 TS_DATA_LOW, // waiting for data low byte
245 TS_DATA_HIGH, // waiting for data high byte
246 TS_XOR = 0x80 // decode stuffed byte
247 } TS_STATE;
249 // FrSky D Telemetry Protocol
250 void processHubPacket(uint8_t id, int16_t value);
251 void frskyDProcessPacket(const uint8_t *packet);
253 // FrSky S.PORT Telemetry Protocol
254 void sportProcessTelemetryPacket(const uint8_t * packet);
255 void sportProcessTelemetryPacketWithoutCrc(uint8_t origin, const uint8_t * packet);
257 void telemetryWakeup();
258 void telemetryReset();
260 extern uint8_t telemetryProtocol;
261 void telemetryInit(uint8_t protocol);
263 void telemetryInterrupt10ms();
265 bool pushFrskyTelemetryData(uint8_t data); // returns true when end of frame detected
266 void processFrskyTelemetryData(uint8_t data);
268 #if defined(NO_RAS)
269 inline bool isRasValueValid()
271 return false;
273 #elif defined(PXX2)
274 inline bool isRasValueValid()
276 return true;
278 #elif defined(PCBX10)
279 inline bool isRasValueValid()
281 return false;
283 #elif defined(PCBX9DP) || defined(PCBX9E)
284 inline bool isRasValueValid()
286 return telemetryData.xjtVersion != 0x0000 && telemetryData.xjtVersion != 0x00ff;
288 #else
289 inline bool isRasValueValid()
291 return false;
293 #endif
295 constexpr uint8_t FRSKY_BAD_ANTENNA_THRESHOLD = 0x33;
297 #endif // _FRSKY_H_