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)
47 RX_FRAME_COMPLETE
= (1 << 0),
48 RX_FRAME_FAILSAFE
= (1 << 1),
49 RX_FRAME_PROCESSING_REQUIRED
= (1 << 2),
50 RX_FRAME_DROPPED
= (1 << 3)
55 SERIALRX_SPEKTRUM2048
= 1,
59 SERIALRX_XBUS_MODE_B
= 5,
60 SERIALRX_XBUS_MODE_B_RJ01
= 6,
62 SERIALRX_JETIEXBUS
= 8,
65 SERIALRX_TARGET_CUSTOM
= 11,
69 SERIALRX_SPEKTRUM1024
= 15
72 #define MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT 12
73 #define MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT 8
74 #define MAX_SUPPORTED_RC_CHANNEL_COUNT 18
76 #define NON_AUX_CHANNEL_COUNT 4
77 #define MAX_AUX_CHANNEL_COUNT (MAX_SUPPORTED_RC_CHANNEL_COUNT - NON_AUX_CHANNEL_COUNT)
79 #if MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT > MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
80 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PARALLEL_PWM_CHANNEL_COUNT
82 #define MAX_SUPPORTED_RX_PARALLEL_PWM_OR_PPM_CHANNEL_COUNT MAX_SUPPORTED_RC_PPM_CHANNEL_COUNT
85 extern const char rcChannelLetters
[];
87 extern float rcData
[MAX_SUPPORTED_RC_CHANNEL_COUNT
]; // interval [1000;2000]
89 #define RSSI_SCALE_MIN 1
90 #define RSSI_SCALE_MAX 255
92 #define RSSI_SCALE_DEFAULT 100
95 RX_FAILSAFE_MODE_AUTO
= 0,
96 RX_FAILSAFE_MODE_HOLD
,
98 RX_FAILSAFE_MODE_INVALID
99 } rxFailsafeChannelMode_e
;
101 #define RX_FAILSAFE_MODE_COUNT 3
104 RX_FAILSAFE_TYPE_FLIGHT
= 0,
106 } rxFailsafeChannelType_e
;
108 #define RX_FAILSAFE_TYPE_COUNT 2
110 typedef struct rxFailsafeChannelConfig_s
{
111 uint8_t mode
; // See rxFailsafeChannelMode_e
113 } rxFailsafeChannelConfig_t
;
115 PG_DECLARE_ARRAY(rxFailsafeChannelConfig_t
, MAX_SUPPORTED_RC_CHANNEL_COUNT
, rxFailsafeChannelConfigs
);
117 typedef struct rxChannelRangeConfig_s
{
120 } rxChannelRangeConfig_t
;
122 PG_DECLARE_ARRAY(rxChannelRangeConfig_t
, NON_AUX_CHANNEL_COUNT
, rxChannelRangeConfigs
);
124 struct rxRuntimeState_s
;
125 typedef float (*rcReadRawDataFnPtr
)(const struct rxRuntimeState_s
*rxRuntimeState
, uint8_t chan
); // used by receiver driver to return channel data
126 typedef uint8_t (*rcFrameStatusFnPtr
)(struct rxRuntimeState_s
*rxRuntimeState
);
127 typedef bool (*rcProcessFrameFnPtr
)(const struct rxRuntimeState_s
*rxRuntimeState
);
128 typedef timeUs_t
rcGetFrameTimeUsFn(void); // used to retrieve the timestamp in microseconds for the last channel data frame
131 RX_PROVIDER_NONE
= 0,
132 RX_PROVIDER_PARALLEL_PWM
,
140 typedef struct rxRuntimeState_s
{
141 rxProvider_t rxProvider
;
142 SerialRXType serialrxProvider
;
143 uint8_t channelCount
; // number of RC channels as reported by current input driver
144 rcReadRawDataFnPtr rcReadRawFn
;
145 rcFrameStatusFnPtr rcFrameStatusFn
;
146 rcProcessFrameFnPtr rcProcessFrameFn
;
147 rcGetFrameTimeUsFn
*rcFrameTimeUsFn
;
148 uint16_t *channelData
;
150 timeUs_t lastRcFrameTimeUs
;
154 RSSI_SOURCE_NONE
= 0,
156 RSSI_SOURCE_RX_CHANNEL
,
157 RSSI_SOURCE_RX_PROTOCOL
,
159 RSSI_SOURCE_FRAME_ERRORS
,
160 RSSI_SOURCE_RX_PROTOCOL_CRSF
,
163 extern rssiSource_e rssiSource
;
167 LQ_SOURCE_RX_PROTOCOL_CRSF
,
168 LQ_SOURCE_RX_PROTOCOL_GHST
,
169 } linkQualitySource_e
;
171 extern linkQualitySource_e linkQualitySource
;
173 extern rxRuntimeState_t rxRuntimeState
; //!!TODO remove this extern, only needed once for channelCount
176 void rxProcessPending(bool state
);
177 bool rxUpdateCheck(timeUs_t currentTimeUs
, timeDelta_t currentDeltaTimeUs
);
178 void rxFrameCheck(timeUs_t currentTimeUs
, timeDelta_t currentDeltaTimeUs
);
179 bool rxIsReceivingSignal(void);
180 bool rxAreFlightChannelsValid(void);
181 bool calculateRxChannelsAndUpdateFailsafe(timeUs_t currentTimeUs
);
185 void parseRcChannels(const char *input
, struct rxConfig_s
*rxConfig
);
187 #define RSSI_MAX_VALUE 1023
189 void setRssiDirect(uint16_t newRssi
, rssiSource_e source
);
190 void setRssi(uint16_t rssiValue
, rssiSource_e source
);
191 void setRssiMsp(uint8_t newMspRssi
);
192 void updateRSSI(timeUs_t currentTimeUs
);
193 uint16_t getRssi(void);
194 uint8_t getRssiPercent(void);
195 bool isRssiConfigured(void);
197 #define LINK_QUALITY_MAX_VALUE 1023
199 uint16_t rxGetLinkQuality(void);
200 void setLinkQualityDirect(uint16_t linkqualityValue
);
201 uint16_t rxGetLinkQualityPercent(void);
203 #ifdef USE_RX_RSSI_DBM
204 int16_t getRssiDbm(void);
205 void setRssiDbm(int16_t newRssiDbm
, rssiSource_e source
);
206 void setRssiDbmDirect(int16_t newRssiDbm
, rssiSource_e source
);
207 int8_t getActiveAntenna(void);
208 void setActiveAntenna(int8_t antenna
);
209 #endif //USE_RX_RSSI_DBM
212 int16_t getRsnr(void);
213 void setRsnr(int16_t newRsnr
);
214 void setRsnrDirect(int16_t newRsnr
);
217 void rxSetRfMode(uint8_t rfModeValue
);
218 uint8_t rxGetRfMode(void);
220 void rxSetUplinkTxPwrMw(uint16_t uplinkTxPwrMwValue
);
221 uint16_t rxGetUplinkTxPwrMw(void);
223 void resetAllRxChannelRangeConfigurations(rxChannelRangeConfig_t
*rxChannelRangeConfig
);
225 void suspendRxSignal(void);
226 void resumeRxSignal(void);
228 timeDelta_t
rxGetFrameDelta(timeDelta_t
*frameAgeUs
);
230 timeUs_t
rxFrameTimeUs(void);