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)
44 #define DEFAULT_SERVO_MIN 1000
45 #define DEFAULT_SERVO_MIDDLE 1500
46 #define DEFAULT_SERVO_MAX 2000
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)
57 SERIALRX_SPEKTRUM1024
= 0,
58 SERIALRX_SPEKTRUM2048
= 1,
62 SERIALRX_XBUS_MODE_B
= 5,
63 SERIALRX_XBUS_MODE_B_RJ01
= 6,
65 SERIALRX_JETIEXBUS
= 8,
68 SERIALRX_TARGET_CUSTOM
= 11,
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
84 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
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
97 RX_FAILSAFE_MODE_AUTO
= 0,
98 RX_FAILSAFE_MODE_HOLD
,
100 RX_FAILSAFE_MODE_INVALID
101 } rxFailsafeChannelMode_e
;
103 #define RX_FAILSAFE_MODE_COUNT 3
106 RX_FAILSAFE_TYPE_FLIGHT
= 0,
108 } rxFailsafeChannelType_e
;
110 #define RX_FAILSAFE_TYPE_COUNT 2
112 typedef struct rxFailsafeChannelConfig_s
{
113 uint8_t mode
; // See rxFailsafeChannelMode_e
115 } rxFailsafeChannelConfig_t
;
117 PG_DECLARE_ARRAY(rxFailsafeChannelConfig_t
, MAX_SUPPORTED_RC_CHANNEL_COUNT
, rxFailsafeChannelConfigs
);
119 typedef struct rxChannelRangeConfig_s
{
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
133 RX_PROVIDER_NONE
= 0,
134 RX_PROVIDER_PARALLEL_PWM
,
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
;
152 timeUs_t lastRcFrameTimeUs
;
156 RSSI_SOURCE_NONE
= 0,
158 RSSI_SOURCE_RX_CHANNEL
,
159 RSSI_SOURCE_RX_PROTOCOL
,
161 RSSI_SOURCE_FRAME_ERRORS
,
162 RSSI_SOURCE_RX_PROTOCOL_CRSF
,
165 extern rssiSource_e rssiSource
;
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
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
);
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);