Add tlmConfirm to tlm_dl ota packet-structure (#2991)
[ExpressLRS.git] / src / include / common.h
blob660216794643e1942e5d786032e149ee2103eefd
1 #pragma once
3 #ifndef UNIT_TEST
4 #include "targets.h"
6 #if defined(RADIO_SX127X)
7 #include "SX127xDriver.h"
8 #elif defined(RADIO_LR1121)
9 #include "LR1121Driver.h"
10 #elif defined(RADIO_SX128X)
11 #include "SX1280Driver.h"
12 #else
13 #error "Radio configuration is not valid!"
14 #endif
16 #endif // UNIT_TEST
18 // Used to XOR with OtaCrcInitializer and macSeed to reduce compatibility with previous versions.
19 // It should be incremented when the OTA packet structure is modified.
20 #define OTA_VERSION_ID 4
21 #define UID_LEN 6
23 typedef enum : uint8_t
25 TLM_RATIO_STD = 0, // Use suggested ratio from ModParams
26 TLM_RATIO_NO_TLM,
27 TLM_RATIO_1_128,
28 TLM_RATIO_1_64,
29 TLM_RATIO_1_32,
30 TLM_RATIO_1_16,
31 TLM_RATIO_1_8,
32 TLM_RATIO_1_4,
33 TLM_RATIO_1_2,
34 TLM_RATIO_DISARMED, // TLM_RATIO_STD when disarmed, TLM_RATIO_NO_TLM when armed
35 } expresslrs_tlm_ratio_e;
37 typedef enum
39 connected,
40 tentative, // RX only
41 awaitingModelId, // TX only
42 disconnected,
43 MODE_STATES,
44 // States below here are special mode states
45 noCrossfire,
46 bleJoystick,
47 NO_CONFIG_SAVE_STATES,
48 wifiUpdate,
49 serialUpdate,
50 // Failure states go below here to display immediately
51 FAILURE_STATES,
52 radioFailed,
53 hardwareUndefined
54 } connectionState_e;
56 /**
57 * On the TX, tracks what to do when the Tock timer fires
58 **/
59 typedef enum
61 ttrpTransmitting, // Transmitting RC channels as normal
62 ttrpPreReceiveGap, // Has switched to Receive mode for telemetry, but in the gap between TX done and Tock
63 ttrpExpectingTelem // Still in Receive mode, Tock has fired, receiving telem as far as we know
64 } TxTlmRcvPhase_e;
66 typedef enum
68 tim_disconnected = 0,
69 tim_tentative = 1,
70 tim_locked = 2
71 } RXtimerState_e;
73 typedef enum
75 RF_DOWNLINK_INFO = 0,
76 RF_UPLINK_INFO = 1,
77 RF_AIRMODE_PARAMETERS = 2
78 } expresslrs_tlm_header_e;
80 typedef enum : uint8_t
82 // RATE_MODULATION_BAND_RATE_MODE
84 RATE_LORA_900_25HZ = 0,
85 RATE_LORA_900_50HZ,
86 RATE_LORA_900_100HZ,
87 RATE_LORA_900_100HZ_8CH,
88 RATE_LORA_900_150HZ,
89 RATE_LORA_900_200HZ,
90 RATE_LORA_900_200HZ_8CH,
91 RATE_LORA_900_250HZ,
92 RATE_LORA_900_333HZ_8CH,
93 RATE_LORA_900_500HZ,
94 RATE_LORA_900_50HZ_DVDA,
95 RATE_FSK_900_1000HZ_8CH,
97 RATE_LORA_2G4_25HZ = 20,
98 RATE_LORA_2G4_50HZ,
99 RATE_LORA_2G4_100HZ,
100 RATE_LORA_2G4_100HZ_8CH,
101 RATE_LORA_2G4_150HZ,
102 RATE_LORA_2G4_200HZ,
103 RATE_LORA_2G4_200HZ_8CH,
104 RATE_LORA_2G4_250HZ,
105 RATE_LORA_2G4_333HZ_8CH,
106 RATE_LORA_2G4_500HZ,
107 RATE_FLRC_2G4_250HZ_DVDA,
108 RATE_FLRC_2G4_500HZ_DVDA,
109 RATE_FLRC_2G4_500HZ,
110 RATE_FLRC_2G4_1000HZ,
111 RATE_FSK_2G4_250HZ_DVDA,
112 RATE_FSK_2G4_500HZ_DVDA,
113 RATE_FSK_2G4_1000HZ,
115 RATE_LORA_DUAL_100HZ_8CH = 100,
116 RATE_LORA_DUAL_150HZ,
117 } expresslrs_RFrates_e;
119 enum {
120 RADIO_TYPE_SX127x_LORA,
121 RADIO_TYPE_LR1121_LORA_900,
122 RADIO_TYPE_LR1121_LORA_2G4,
123 RADIO_TYPE_LR1121_GFSK_900,
124 RADIO_TYPE_LR1121_GFSK_2G4,
125 RADIO_TYPE_LR1121_LORA_DUAL,
126 RADIO_TYPE_SX128x_LORA,
127 RADIO_TYPE_SX128x_FLRC,
130 typedef enum : uint8_t
132 TX_RADIO_MODE_GEMINI = 0,
133 TX_RADIO_MODE_ANT_1 = 1,
134 TX_RADIO_MODE_ANT_2 = 2,
135 TX_RADIO_MODE_SWITCH = 3
136 } tx_radio_mode_e;
138 typedef enum : uint8_t
140 TX_NORMAL_MODE = 0,
141 TX_MAVLINK_MODE = 1,
142 } tx_transmission_mode_e;
144 // Value used for expresslrs_rf_pref_params_s.DynpowerUpThresholdSnr if SNR should not be used
145 #define DYNPOWER_SNR_THRESH_NONE -127
146 #define SNR_SCALE(snr) ((int8_t)((float)snr * RADIO_SNR_SCALE))
147 #define SNR_DESCALE(snrScaled) (snrScaled / RADIO_SNR_SCALE)
148 // Bound is any of the last 4 bytes nonzero (unbound is all zeroes)
149 #define UID_IS_BOUND(uid) (uid[2] != 0 || uid[3] != 0 || uid[4] != 0 || uid[5] != 0)
151 typedef struct expresslrs_rf_pref_params_s
153 uint8_t index;
154 int16_t RXsensitivity; // expected min RF sensitivity
155 uint16_t TOA; // time on air in microseconds
156 uint16_t DisconnectTimeoutMs; // Time without a packet before receiver goes to disconnected (ms)
157 uint16_t RxLockTimeoutMs; // Max time to go from tentative -> connected state on receiver (ms)
158 uint16_t SyncPktIntervalDisconnected; // how often to send the PACKET_TYPE_SYNC (ms) when there is no response from RX
159 uint16_t SyncPktIntervalConnected; // how often to send the PACKET_TYPE_SYNC (ms) when there we have a connection
160 int8_t DynpowerSnrThreshUp; // Request a raise in power if the reported (average) SNR is at or below this
161 // or DYNPOWER_UPTHRESH_SNR_NONE to use RSSI
162 int8_t DynpowerSnrThreshDn; // Like DynpowerSnrUpThreshold except to lower power
164 } expresslrs_rf_pref_params_s;
166 typedef struct expresslrs_mod_settings_s
168 uint8_t index;
169 uint8_t radio_type;
170 expresslrs_RFrates_e enum_rate;
171 uint8_t bw;
172 uint8_t sf;
173 uint8_t cr;
174 uint8_t PreambleLen;
175 #if defined(RADIO_LR1121)
176 uint8_t bw2;
177 uint8_t sf2;
178 uint8_t cr2;
179 uint8_t PreambleLen2;
180 #endif
181 expresslrs_tlm_ratio_e TLMinterval; // every X packets is a response TLM packet, should be a power of 2
182 uint8_t FHSShopInterval; // every X packets we hop to a new frequency. Max value of 16 since only 4 bits have been assigned in the sync package.
183 int32_t interval; // interval in us seconds that corresponds to that frequency
184 uint8_t PayloadLength; // Number of OTA bytes to be sent.
185 uint8_t numOfSends; // Number of packets to send.
186 } expresslrs_mod_settings_t;
188 // Limited to 16 possible ACTIONs by config storage currently
189 typedef enum : uint8_t {
190 ACTION_NONE,
191 ACTION_INCREASE_POWER,
192 ACTION_GOTO_VTX_BAND,
193 ACTION_GOTO_VTX_CHANNEL,
194 ACTION_SEND_VTX,
195 ACTION_START_WIFI,
196 ACTION_BIND,
197 ACTION_BLE_JOYSTICK,
198 ACTION_RESET_REBOOT,
200 ACTION_LAST
201 } action_e;
203 enum eServoOutputMode : uint8_t
205 som50Hz = 0, // 0: 50 Hz | modes are "Servo PWM" where the signal is 988-2012us
206 som60Hz, // 1: 60 Hz | and the mode sets the refresh interval
207 som100Hz, // 2: 100 Hz | must be mode=0 for default in config
208 som160Hz, // 3: 160Hz
209 som333Hz, // 4: 333Hz
210 som400Hz, // 5: 400Hz
211 som10KHzDuty, // 6: 10kHz duty
212 somOnOff, // 7: Digital 0/1 mode
213 somDShot, // 8: DShot300
214 somSerial, // 9: primary Serial
215 somSCL, // 10: I2C clock signal
216 somSDA, // 11: I2C data line
217 somPwm, // 12: true PWM mode (NOT SUPPORTED)
218 #if defined(PLATFORM_ESP32)
219 somSerial1RX, // 13: secondary Serial RX
220 somSerial1TX, // 14: secondary Serial TX
221 #endif
224 enum eServoOutputFailsafeMode : uint8_t
226 PWMFAILSAFE_SET_POSITION, // user customizable pulse value
227 PWMFAILSAFE_NO_PULSES, // stop pulsing
228 PWMFAILSAFE_LAST_POSITION, // continue to pulse last used value
231 enum eSerialProtocol : uint8_t
233 PROTOCOL_CRSF,
234 PROTOCOL_INVERTED_CRSF,
235 PROTOCOL_SBUS,
236 PROTOCOL_INVERTED_SBUS,
237 PROTOCOL_SUMD,
238 PROTOCOL_DJI_RS_PRO,
239 PROTOCOL_HOTT_TLM,
240 PROTOCOL_MAVLINK,
241 PROTOCOL_MSP_DISPLAYPORT,
244 #if defined(PLATFORM_ESP32)
245 enum eSerial1Protocol : uint8_t
247 PROTOCOL_SERIAL1_OFF,
248 PROTOCOL_SERIAL1_CRSF,
249 PROTOCOL_SERIAL1_INVERTED_CRSF,
250 PROTOCOL_SERIAL1_SBUS,
251 PROTOCOL_SERIAL1_INVERTED_SBUS,
252 PROTOCOL_SERIAL1_SUMD,
253 PROTOCOL_SERIAL1_DJI_RS_PRO,
254 PROTOCOL_SERIAL1_HOTT_TLM,
255 PROTOCOL_SERIAL1_TRAMP,
256 PROTOCOL_SERIAL1_SMARTAUDIO,
257 PROTOCOL_SERIAL1_MSP_DISPLAYPORT,
259 #endif
261 enum eFailsafeMode : uint8_t
263 FAILSAFE_NO_PULSES,
264 FAILSAFE_LAST_POSITION,
265 FAILSAFE_SET_POSITION
268 enum eAuxChannels : uint8_t
270 AUX1 = 4,
271 AUX2 = 5,
272 AUX3 = 6,
273 AUX4 = 7,
274 AUX5 = 8,
275 AUX6 = 9,
276 AUX7 = 10,
277 AUX8 = 11,
278 AUX9 = 12,
279 AUX10 = 13,
280 AUX11 = 14,
281 AUX12 = 15,
282 CRSF_NUM_CHANNELS = 16
285 //ELRS SPECIFIC OTA CRC
286 //Koopman formatting https://users.ece.cmu.edu/~koopman/crc/
287 #define ELRS_CRC_POLY 0x07 // 0x83
288 #define ELRS_CRC14_POLY 0x2E57 // 0x372B
290 #ifndef UNIT_TEST
291 #if defined(RADIO_SX127X)
292 #define RATE_MAX 6
293 #define RATE_BINDING RATE_LORA_900_50HZ
295 extern SX127xDriver Radio;
297 #elif defined(RADIO_LR1121)
298 #define RATE_MAX 20
299 #define RATE_BINDING RATE_LORA_900_50HZ
300 #define RATE_DUALBAND_BINDING RATE_LORA_2G4_50HZ
302 extern LR1121Driver Radio;
304 #elif defined(RADIO_SX128X)
305 #define RATE_MAX 10 // 2xFLRC + 2xDVDA + 4xLoRa + 2xFullRes
306 #define RATE_BINDING RATE_LORA_2G4_50HZ
308 extern SX1280Driver Radio;
309 #endif
310 #endif // UNIT_TEST
312 expresslrs_mod_settings_s *get_elrs_airRateConfig(uint8_t index);
313 expresslrs_rf_pref_params_s *get_elrs_RFperfParams(uint8_t index);
314 uint8_t get_elrs_HandsetRate_max(uint8_t rateIndex, uint32_t minInterval);
316 uint8_t TLMratioEnumToValue(expresslrs_tlm_ratio_e const enumval);
317 uint8_t TLMBurstMaxForRateRatio(uint16_t const rateHz, uint8_t const ratioDiv);
318 uint8_t enumRatetoIndex(expresslrs_RFrates_e const eRate);
320 extern uint8_t UID[UID_LEN];
321 extern bool connectionHasModelMatch;
322 extern bool teamraceHasModelMatch;
323 extern bool InBindingMode;
324 extern uint8_t ExpressLRS_currTlmDenom;
325 extern connectionState_e connectionState;
326 extern expresslrs_mod_settings_s *ExpressLRS_currAirRate_Modparams;
327 extern expresslrs_rf_pref_params_s *ExpressLRS_currAirRate_RFperfParams;
328 extern uint32_t ChannelData[CRSF_NUM_CHANNELS]; // Current state of channels, CRSF format
330 uint32_t uidMacSeedGet();
331 bool isDualRadio();
332 void EnterBindingModeSafely(); // defined in rx_main/tx_main
334 #if defined(RADIO_LR1121)
335 bool isSupportedRFRate(uint8_t index);
336 #else
337 inline bool isSupportedRFRate(uint8_t index) { return true; };
338 #endif