2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
22 * Based on https://github.com/ExpressLRS/ExpressLRS
23 * Thanks to AlessandroAU, original creator of the ExpressLRS project.
31 #include "drivers/io_types.h"
32 #include "drivers/time.h"
34 #define ELRS_CRC_LEN 256
35 #define ELRS_CRC14_POLY 0x2E57
37 #define ELRS_NR_SEQUENCE_ENTRIES 256
39 #define ELRS_RX_TX_BUFF_SIZE 8
41 #define ELRS_TELEMETRY_TYPE_LINK 0x01
42 #define ELRS_TELEMETRY_TYPE_DATA 0x02
43 #define ELRS_MSP_BIND 0x09
44 #define ELRS_MSP_MODEL_ID 0x0A
45 #define ELRS_MSP_SET_RX_CONFIG 45
47 #define ELRS_MODELMATCH_MASK 0x3F
49 #define FREQ_HZ_TO_REG_VAL_900(freq) ((uint32_t)(freq / SX127x_FREQ_STEP))
50 #define FREQ_HZ_TO_REG_VAL_24(freq) ((uint32_t)(freq / SX1280_FREQ_STEP))
52 #define ELRS_RATE_MAX 4
53 #define ELRS_BINDING_RATE_24 3
54 #define ELRS_BINDING_RATE_900 2
56 #define ELRS_MAX_CHANNELS 16
57 #define ELRS_RSSI_CHANNEL 15
58 #define ELRS_LQ_CHANNEL 14
60 #define ELRS_CONFIG_CHECK_MS 200
61 #define ELRS_LINK_STATS_CHECK_MS 100
62 #define ELRS_CONSIDER_CONNECTION_GOOD_MS 1000
64 #define ELRS_MODE_CYCLE_MULTIPLIER_SLOW 10
78 #if !defined(USE_RX_SX127X) && !defined(USE_RX_SX1280)
108 } elrsRfRate_e
; // Max value of 16 since only 4 bits have been assigned in the sync package.
110 typedef struct elrsModSettings_s
{
112 elrsRfRate_e enumRate
; // Max value of 16 since only 4 bits have been assigned in the sync package.
116 uint32_t interval
; // interval in us seconds that corresponds to that frequency
117 elrsTlmRatio_e tlmInterval
; // every X packets is a response TLM packet, should be a power of 2
118 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.
122 typedef struct elrsRfPerfParams_s
{
124 elrsRfRate_e enumRate
; // Max value of 16 since only 4 bits have been assigned in the sync package.
125 int32_t sensitivity
; // expected RF sensitivity based on
126 uint32_t toa
; // time on air in microseconds
127 uint32_t disconnectTimeoutMs
; // Time without a packet before receiver goes to disconnected (ms)
128 uint32_t rxLockTimeoutMs
; // Max time to go from tentative -> connected state on receiver (ms)
129 uint32_t syncPktIntervalDisconnected
; // how often to send the SYNC_PACKET packet (ms) when there is no response from RX
130 uint32_t syncPktIntervalConnected
; // how often to send the SYNC_PACKET packet (ms) when there we have a connection
131 } elrsRfPerfParams_t
;
133 typedef bool (*elrsRxInitFnPtr
)(IO_t resetPin
, IO_t busyPin
);
134 typedef void (*elrsRxConfigFnPtr
)(const uint8_t bw
, const uint8_t sf
, const uint8_t cr
, const uint32_t freq
, const uint8_t preambleLen
, const bool iqInverted
);
135 typedef void (*elrsRxStartReceivingFnPtr
)(void);
136 typedef uint8_t (*elrsRxISRFnPtr
)(timeUs_t
*timeStamp
);
137 typedef void (*elrsRxTransmitDataFnPtr
)(const uint8_t *data
, const uint8_t length
);
138 typedef void (*elrsRxReceiveDataFnPtr
)(uint8_t *data
, const uint8_t length
);
139 typedef void (*elrsRxGetRFlinkInfoFnPtr
)(int8_t *rssi
, int8_t *snr
);
140 typedef void (*elrsRxSetFrequencyFnPtr
)(const uint32_t freq
);
141 typedef void (*elrsRxHandleFreqCorrectionFnPtr
)(int32_t offset
, const uint32_t freq
);
143 extern elrsModSettings_t airRateConfig
[][ELRS_RATE_MAX
];
144 extern elrsRfPerfParams_t rfPerfConfig
[][ELRS_RATE_MAX
];
146 void generateCrc14Table(void);
147 uint16_t calcCrc14(uint8_t *data
, uint8_t len
, uint16_t crc
);
149 uint32_t getInitialFreq(const int32_t freqCorrection
);
150 uint8_t getFHSSNumEntries(void);
151 uint8_t FHSSgetCurrIndex(void);
152 void FHSSsetCurrIndex(const uint8_t value
);
153 uint32_t FHSSgetNextFreq(const int32_t freqCorrection
);
154 void FHSSrandomiseFHSSsequence(const uint8_t UID
[], const elrsFreqDomain_e dom
);
155 uint8_t tlmRatioEnumToValue(const elrsTlmRatio_e enumval
);
156 uint16_t rateEnumToHz(const elrsRfRate_e eRate
);
157 uint16_t txPowerIndexToValue(const uint8_t index
);
163 void lqNewPeriod(void);
164 bool lqPeriodIsSet(void);
165 void lqIncrease(void);
168 uint16_t convertSwitch1b(const uint16_t val
);
169 uint16_t convertSwitch3b(const uint16_t val
);
170 uint16_t convertSwitchNb(const uint16_t val
, const uint16_t max
);
171 uint16_t convertAnalog(const uint16_t val
);
172 uint8_t hybridWideNonceToSwitchIndex(const uint8_t nonce
);