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/>.
23 #include "common/time.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)
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)
54 SERIALRX_SPEKTRUM2048
= 1,
58 SERIALRX_XBUS_MODE_B
= 5,
59 SERIALRX_XBUS_MODE_B_RJ01
= 6,
61 SERIALRX_JETIEXBUS
= 8,
64 SERIALRX_TARGET_CUSTOM
= 11,
68 SERIALRX_SPEKTRUM1024
= 15
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
81 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
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
94 RX_FAILSAFE_MODE_AUTO
= 0,
95 RX_FAILSAFE_MODE_HOLD
,
97 RX_FAILSAFE_MODE_INVALID
98 } rxFailsafeChannelMode_e
;
100 #define RX_FAILSAFE_MODE_COUNT 3
103 RX_FAILSAFE_TYPE_FLIGHT
= 0,
105 } rxFailsafeChannelType_e
;
107 #define RX_FAILSAFE_TYPE_COUNT 2
109 typedef struct rxFailsafeChannelConfig_s
{
110 uint8_t mode
; // See rxFailsafeChannelMode_e
112 } rxFailsafeChannelConfig_t
;
114 PG_DECLARE_ARRAY(rxFailsafeChannelConfig_t
, MAX_SUPPORTED_RC_CHANNEL_COUNT
, rxFailsafeChannelConfigs
);
116 typedef struct rxChannelRangeConfig_s
{
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
130 RX_PROVIDER_NONE
= 0,
131 RX_PROVIDER_PARALLEL_PWM
,
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
;
148 timeUs_t lastRcFrameTimeUs
;
152 RSSI_SOURCE_NONE
= 0,
154 RSSI_SOURCE_RX_CHANNEL
,
155 RSSI_SOURCE_RX_PROTOCOL
,
157 RSSI_SOURCE_FRAME_ERRORS
,
158 RSSI_SOURCE_RX_PROTOCOL_CRSF
,
161 extern rssiSource_e rssiSource
;
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
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
);
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
210 int16_t getRsnr(void);
211 void setRsnr(int16_t newRsnr
);
212 void setRsnrDirect(int16_t newRsnr
);
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);