Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / rx / rx.h
blobf92c6f691daad296713fc7bf5c7e1d7f7203653b
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 #define DEFAULT_SERVO_MIN 1000
45 #define DEFAULT_SERVO_MIDDLE 1500
46 #define DEFAULT_SERVO_MAX 2000
48 typedef enum {
49 RX_FRAME_PENDING = 0,
50 RX_FRAME_COMPLETE = (1 << 0),
51 RX_FRAME_FAILSAFE = (1 << 1),
52 RX_FRAME_PROCESSING_REQUIRED = (1 << 2),
53 RX_FRAME_DROPPED = (1 << 3)
54 } rxFrameState_e;
56 typedef enum {
57 SERIALRX_SPEKTRUM1024 = 0,
58 SERIALRX_SPEKTRUM2048 = 1,
59 SERIALRX_SBUS = 2,
60 SERIALRX_SUMD = 3,
61 SERIALRX_SUMH = 4,
62 SERIALRX_XBUS_MODE_B = 5,
63 SERIALRX_XBUS_MODE_B_RJ01 = 6,
64 SERIALRX_IBUS = 7,
65 SERIALRX_JETIEXBUS = 8,
66 SERIALRX_CRSF = 9,
67 SERIALRX_SRXL = 10,
68 SERIALRX_TARGET_CUSTOM = 11,
69 SERIALRX_FPORT = 12,
70 SERIALRX_SRXL2 = 13,
71 SERIALRX_GHST = 14
72 } SerialRXType;
74 #define MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT 12
75 #define MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT 8
76 #define MAX_SUPPORTED_RC_CHANNEL_COUNT 18
78 #define NON_AUX_CHANNEL_COUNT 4
79 #define MAX_AUX_CHANNEL_COUNT (MAX_SUPPORTED_RC_CHANNEL_COUNT - NON_AUX_CHANNEL_COUNT)
81 #if MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT > MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
82 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT
83 #else
84 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
85 #endif
87 extern const char rcChannelLetters[];
89 extern float rcData[MAX_SUPPORTED_RC_CHANNEL_COUNT]; // interval [1000;2000]
91 #define RSSI_SCALE_MIN 1
92 #define RSSI_SCALE_MAX 255
94 #define RSSI_SCALE_DEFAULT 100
96 typedef enum {
97 RX_FAILSAFE_MODE_AUTO = 0,
98 RX_FAILSAFE_MODE_HOLD,
99 RX_FAILSAFE_MODE_SET,
100 RX_FAILSAFE_MODE_INVALID
101 } rxFailsafeChannelMode_e;
103 #define RX_FAILSAFE_MODE_COUNT 3
105 typedef enum {
106 RX_FAILSAFE_TYPE_FLIGHT = 0,
107 RX_FAILSAFE_TYPE_AUX
108 } rxFailsafeChannelType_e;
110 #define RX_FAILSAFE_TYPE_COUNT 2
112 typedef struct rxFailsafeChannelConfig_s {
113 uint8_t mode; // See rxFailsafeChannelMode_e
114 uint8_t step;
115 } rxFailsafeChannelConfig_t;
117 PG_DECLARE_ARRAY(rxFailsafeChannelConfig_t, MAX_SUPPORTED_RC_CHANNEL_COUNT, rxFailsafeChannelConfigs);
119 typedef struct rxChannelRangeConfig_s {
120 uint16_t min;
121 uint16_t max;
122 } rxChannelRangeConfig_t;
124 PG_DECLARE_ARRAY(rxChannelRangeConfig_t, NON_AUX_CHANNEL_COUNT, rxChannelRangeConfigs);
126 struct rxRuntimeState_s;
127 typedef float (*rcReadRawDataFnPtr)(const struct rxRuntimeState_s *rxRuntimeState, uint8_t chan); // used by receiver driver to return channel data
128 typedef uint8_t (*rcFrameStatusFnPtr)(struct rxRuntimeState_s *rxRuntimeState);
129 typedef bool (*rcProcessFrameFnPtr)(const struct rxRuntimeState_s *rxRuntimeState);
130 typedef timeUs_t rcGetFrameTimeUsFn(void); // used to retrieve the timestamp in microseconds for the last channel data frame
132 typedef enum {
133 RX_PROVIDER_NONE = 0,
134 RX_PROVIDER_PARALLEL_PWM,
135 RX_PROVIDER_PPM,
136 RX_PROVIDER_SERIAL,
137 RX_PROVIDER_MSP,
138 RX_PROVIDER_SPI,
139 } rxProvider_t;
141 typedef struct rxRuntimeState_s {
142 rxProvider_t rxProvider;
143 SerialRXType serialrxProvider;
144 uint8_t channelCount; // number of RC channels as reported by current input driver
145 uint16_t rxRefreshRate;
146 rcReadRawDataFnPtr rcReadRawFn;
147 rcFrameStatusFnPtr rcFrameStatusFn;
148 rcProcessFrameFnPtr rcProcessFrameFn;
149 rcGetFrameTimeUsFn *rcFrameTimeUsFn;
150 uint16_t *channelData;
151 void *frameData;
152 timeUs_t lastRcFrameTimeUs;
153 } rxRuntimeState_t;
155 typedef enum {
156 RSSI_SOURCE_NONE = 0,
157 RSSI_SOURCE_ADC,
158 RSSI_SOURCE_RX_CHANNEL,
159 RSSI_SOURCE_RX_PROTOCOL,
160 RSSI_SOURCE_MSP,
161 RSSI_SOURCE_FRAME_ERRORS,
162 RSSI_SOURCE_RX_PROTOCOL_CRSF,
163 } rssiSource_e;
165 extern rssiSource_e rssiSource;
167 typedef enum {
168 LQ_SOURCE_NONE = 0,
169 LQ_SOURCE_RX_PROTOCOL_CRSF,
170 LQ_SOURCE_RX_PROTOCOL_GHST,
171 } linkQualitySource_e;
173 extern linkQualitySource_e linkQualitySource;
175 extern rxRuntimeState_t rxRuntimeState; //!!TODO remove this extern, only needed once for channelCount
177 void rxInit(void);
178 void rxProcessPending(bool state);
179 bool rxUpdateCheck(timeUs_t currentTimeUs, timeDelta_t currentDeltaTimeUs);
180 bool rxIsReceivingSignal(void);
181 bool rxAreFlightChannelsValid(void);
182 bool calculateRxChannelsAndUpdateFailsafe(timeUs_t currentTimeUs);
184 struct rxConfig_s;
186 void parseRcChannels(const char *input, struct rxConfig_s *rxConfig);
188 #define RSSI_MAX_VALUE 1023
190 void setRssiDirect(uint16_t newRssi, rssiSource_e source);
191 void setRssi(uint16_t rssiValue, rssiSource_e source);
192 void setRssiMsp(uint8_t newMspRssi);
193 void updateRSSI(timeUs_t currentTimeUs);
194 uint16_t getRssi(void);
195 uint8_t getRssiPercent(void);
196 bool isRssiConfigured(void);
198 #define LINK_QUALITY_MAX_VALUE 1023
200 uint16_t rxGetLinkQuality(void);
201 void setLinkQualityDirect(uint16_t linkqualityValue);
202 uint16_t rxGetLinkQualityPercent(void);
204 int16_t getRssiDbm(void);
205 void setRssiDbm(int16_t newRssiDbm, rssiSource_e source);
206 void setRssiDbmDirect(int16_t newRssiDbm, rssiSource_e source);
208 void rxSetRfMode(uint8_t rfModeValue);
209 uint8_t rxGetRfMode(void);
211 void rxSetUplinkTxPwrMw(uint16_t uplinkTxPwrMwValue);
212 uint16_t rxGetUplinkTxPwrMw(void);
214 void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t *rxChannelRangeConfig);
216 void suspendRxPwmPpmSignal(void);
217 void resumeRxPwmPpmSignal(void);
219 uint16_t rxGetRefreshRate(void);
221 timeDelta_t rxGetFrameDelta(timeDelta_t *frameAgeUs);
223 timeUs_t rxFrameTimeUs(void);