Update actions to ubuntu-latest (#14114)
[betaflight.git] / src / main / rx / rx.h
blob3cea92b6d16913b5bc7a9856523a0670adfc417f
1 /*
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)
8 * any later version.
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/>.
21 #pragma once
23 #include "common/time.h"
25 #include "pg/pg.h"
26 #include "pg/rx.h"
28 #include "drivers/io_types.h"
30 #define STICK_CHANNEL_COUNT 4
32 #define PWM_RANGE_MIN 1000
33 #define PWM_RANGE_MAX 2000
34 #define PWM_RANGE (PWM_RANGE_MAX - PWM_RANGE_MIN)
35 #define PWM_RANGE_MIDDLE (PWM_RANGE_MIN + (PWM_RANGE / 2))
37 #define PWM_PULSE_MIN 750 // minimum PWM pulse width which is considered valid
38 #define PWM_PULSE_MAX 2250 // maximum PWM pulse width which is considered valid
40 #define RXFAIL_STEP_TO_CHANNEL_VALUE(step) (PWM_PULSE_MIN + 25 * step)
41 #define CHANNEL_VALUE_TO_RXFAIL_STEP(channelValue) ((constrain(channelValue, PWM_PULSE_MIN, PWM_PULSE_MAX) - PWM_PULSE_MIN) / 25)
42 #define MAX_RXFAIL_RANGE_STEP ((PWM_PULSE_MAX - PWM_PULSE_MIN) / 25)
44 typedef enum {
45 RX_FRAME_PENDING = 0,
46 RX_FRAME_COMPLETE = (1 << 0),
47 RX_FRAME_FAILSAFE = (1 << 1),
48 RX_FRAME_PROCESSING_REQUIRED = (1 << 2),
49 RX_FRAME_DROPPED = (1 << 3)
50 } rxFrameState_e;
52 typedef enum {
53 SERIALRX_NONE = 0,
54 SERIALRX_SPEKTRUM2048 = 1,
55 SERIALRX_SBUS = 2,
56 SERIALRX_SUMD = 3,
57 SERIALRX_SUMH = 4,
58 SERIALRX_XBUS_MODE_B = 5,
59 SERIALRX_XBUS_MODE_B_RJ01 = 6,
60 SERIALRX_IBUS = 7,
61 SERIALRX_JETIEXBUS = 8,
62 SERIALRX_CRSF = 9,
63 SERIALRX_SRXL = 10,
64 SERIALRX_TARGET_CUSTOM = 11,
65 SERIALRX_FPORT = 12,
66 SERIALRX_SRXL2 = 13,
67 SERIALRX_GHST = 14,
68 SERIALRX_SPEKTRUM1024 = 15
69 } SerialRXType;
71 #define MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT 12
72 #define MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT 8
73 #define MAX_SUPPORTED_RC_CHANNEL_COUNT 18
75 #define NON_AUX_CHANNEL_COUNT 4
76 #define MAX_AUX_CHANNEL_COUNT (MAX_SUPPORTED_RC_CHANNEL_COUNT - NON_AUX_CHANNEL_COUNT)
78 #if MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT > MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
79 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT
80 #else
81 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
82 #endif
84 extern const char rcChannelLetters[];
86 extern float rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT]; // interval [1000;2000]
88 #define RSSI_SCALE_MIN 1
89 #define RSSI_SCALE_MAX 255
91 #define RSSI_SCALE_DEFAULT 100
93 typedef enum {
94 RX_FAILSAFE_MODE_AUTO = 0,
95 RX_FAILSAFE_MODE_HOLD,
96 RX_FAILSAFE_MODE_SET,
97 RX_FAILSAFE_MODE_INVALID
98 } rxFailsafeChannelMode_e;
100 #define RX_FAILSAFE_MODE_COUNT 3
102 typedef enum {
103 RX_FAILSAFE_TYPE_FLIGHT = 0,
104 RX_FAILSAFE_TYPE_AUX
105 } rxFailsafeChannelType_e;
107 #define RX_FAILSAFE_TYPE_COUNT 2
109 typedef struct rxFailsafeChannelConfig_s {
110 uint8_t mode; // See rxFailsafeChannelMode_e
111 uint8_t step;
112 } rxFailsafeChannelConfig_t;
114 PG_DECLARE_ARRAY(rxFailsafeChannelConfig_t, MAX_SUPPORTED_RC_CHANNEL_COUNT, rxFailsafeChannelConfigs);
116 typedef struct rxChannelRangeConfig_s {
117 uint16_t min;
118 uint16_t max;
119 } rxChannelRangeConfig_t;
121 PG_DECLARE_ARRAY(rxChannelRangeConfig_t, NON_AUX_CHANNEL_COUNT, rxChannelRangeConfigs);
123 struct rxRuntimeState_s;
124 typedef float (*rcReadRawDataFnPtr)(const struct rxRuntimeState_s *rxRuntimeState, uint8_t chan); // used by receiver driver to return channel data
125 typedef uint8_t (*rcFrameStatusFnPtr)(struct rxRuntimeState_s *rxRuntimeState);
126 typedef bool (*rcProcessFrameFnPtr)(const struct rxRuntimeState_s *rxRuntimeState);
127 typedef timeUs_t rcGetFrameTimeUsFn(void); // used to retrieve the timestamp in microseconds for the last channel data frame
129 typedef enum {
130 RX_PROVIDER_NONE = 0,
131 RX_PROVIDER_PARALLEL_PWM,
132 RX_PROVIDER_PPM,
133 RX_PROVIDER_SERIAL,
134 RX_PROVIDER_MSP,
135 RX_PROVIDER_SPI,
136 RX_PROVIDER_UDP,
137 } rxProvider_t;
139 typedef struct rxRuntimeState_s {
140 rxProvider_t rxProvider;
141 SerialRXType serialrxProvider;
142 uint8_t channelCount; // number of RC channels as reported by current input driver
143 rcReadRawDataFnPtr rcReadRawFn;
144 rcFrameStatusFnPtr rcFrameStatusFn;
145 rcProcessFrameFnPtr rcProcessFrameFn;
146 uint16_t *channelData;
147 void *frameData;
148 timeUs_t lastRcFrameTimeUs;
149 } rxRuntimeState_t;
151 typedef enum {
152 RSSI_SOURCE_NONE = 0,
153 RSSI_SOURCE_ADC,
154 RSSI_SOURCE_RX_CHANNEL,
155 RSSI_SOURCE_RX_PROTOCOL,
156 RSSI_SOURCE_MSP,
157 RSSI_SOURCE_FRAME_ERRORS,
158 RSSI_SOURCE_RX_PROTOCOL_CRSF,
159 } rssiSource_e;
161 extern rssiSource_e rssiSource;
163 typedef enum {
164 LQ_SOURCE_NONE = 0,
165 LQ_SOURCE_RX_PROTOCOL_CRSF,
166 LQ_SOURCE_RX_PROTOCOL_GHST,
167 } linkQualitySource_e;
169 extern linkQualitySource_e linkQualitySource;
171 extern rxRuntimeState_t rxRuntimeState; //!!TODO remove this extern, only needed once for channelCount
173 void rxInit(void);
174 void rxProcessPending(bool state);
175 bool rxUpdateCheck(timeUs_t currentTimeUs, timeDelta_t currentDeltaTimeUs);
176 void rxFrameCheck(timeUs_t currentTimeUs, timeDelta_t currentDeltaTimeUs);
177 bool isRxReceivingSignal(void);
178 bool rxAreFlightChannelsValid(void);
179 bool calculateRxChannelsAndUpdateFailsafe(timeUs_t currentTimeUs);
181 struct rxConfig_s;
183 void parseRcChannels(const char *input, struct rxConfig_s *rxConfig);
185 #define RSSI_MAX_VALUE 1023
187 void setRssiDirect(uint16_t newRssi, rssiSource_e source);
188 void setRssi(uint16_t rssiValue, rssiSource_e source);
189 void setRssiMsp(uint8_t newMspRssi);
190 void updateRSSI(timeUs_t currentTimeUs);
191 uint16_t getRssi(void);
192 uint8_t getRssiPercent(void);
193 bool isRssiConfigured(void);
195 #define LINK_QUALITY_MAX_VALUE 1023
197 uint16_t rxGetLinkQuality(void);
198 void setLinkQualityDirect(uint16_t linkqualityValue);
199 uint16_t rxGetLinkQualityPercent(void);
201 #ifdef USE_RX_RSSI_DBM
202 int16_t getRssiDbm(void);
203 void setRssiDbm(int16_t newRssiDbm, rssiSource_e source);
204 void setRssiDbmDirect(int16_t newRssiDbm, rssiSource_e source);
205 int8_t getActiveAntenna(void);
206 void setActiveAntenna(int8_t antenna);
207 #endif //USE_RX_RSSI_DBM
209 #ifdef USE_RX_RSNR
210 int16_t getRsnr(void);
211 void setRsnr(int16_t newRsnr);
212 void setRsnrDirect(int16_t newRsnr);
213 #endif //USE_RX_RSNR
215 void rxSetRfMode(uint8_t rfModeValue);
216 uint8_t rxGetRfMode(void);
218 void rxSetUplinkTxPwrMw(uint16_t uplinkTxPwrMwValue);
219 uint16_t rxGetUplinkTxPwrMw(void);
221 void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t *rxChannelRangeConfig);
223 void suspendRxSignal(void);
224 void resumeRxSignal(void);
226 timeDelta_t rxGetFrameDelta(void);
228 timeUs_t rxFrameTimeUs(void);