2 * This file is free software: you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
7 * This file is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 * See the GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 * Code by Andrew Tridgell and Siddharth Bharat Purohit
19 #include "AP_RCProtocol_config.h"
21 #include <AP_HAL/AP_HAL.h>
22 #include <AP_Common/AP_Common.h>
23 #if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
24 #include <GCS_MAVLink/GCS_MAVLink.h>
27 #define MAX_RCIN_CHANNELS 18
28 #define MIN_RCIN_CHANNELS 5
30 class AP_RCProtocol_Backend
;
36 #if AP_RCPROTOCOL_PPMSUM_ENABLED
39 #if AP_RCPROTOCOL_IBUS_ENABLED
42 #if AP_RCPROTOCOL_SBUS_ENABLED
45 #if AP_RCPROTOCOL_SBUS_NI_ENABLED
48 #if AP_RCPROTOCOL_DSM_ENABLED
51 #if AP_RCPROTOCOL_SUMD_ENABLED
54 #if AP_RCPROTOCOL_SRXL_ENABLED
57 #if AP_RCPROTOCOL_SRXL2_ENABLED
60 #if AP_RCPROTOCOL_CRSF_ENABLED
63 #if AP_RCPROTOCOL_ST24_ENABLED
66 #if AP_RCPROTOCOL_FPORT_ENABLED
69 #if AP_RCPROTOCOL_FPORT2_ENABLED
72 #if AP_RCPROTOCOL_FASTSBUS_ENABLED
75 #if AP_RCPROTOCOL_DRONECAN_ENABLED
78 #if AP_RCPROTOCOL_GHST_ENABLED
81 #if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
84 #if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED
87 #if AP_RCPROTOCOL_UDP_ENABLED
90 #if AP_RCPROTOCOL_FDM_ENABLED
93 #if AP_RCPROTOCOL_RADIO_ENABLED
96 NONE
//last enum always is None
99 // return protocol name as a string
100 static const char *protocol_name_from_protocol(rcprotocol_t protocol
);
102 #if AP_RCPROTOCOL_ENABLED
106 friend class AP_RCProtocol_Backend
;
109 bool valid_serial_prot() const
111 return _valid_serial_prot
;
113 bool should_search(uint32_t now_ms
) const;
114 void process_pulse(uint32_t width_s0
, uint32_t width_s1
);
115 void process_pulse_list(const uint32_t *widths
, uint16_t n
, bool need_swap
);
116 bool process_byte(uint8_t byte
, uint32_t baudrate
);
117 void process_handshake(uint32_t baudrate
);
120 bool failsafe_active() const {
121 return _failsafe_active
;
123 void set_failsafe_active(bool active
) {
124 _failsafe_active
= active
;
127 void disable_for_pulses(enum rcprotocol_t protocol
) {
128 _disabled_for_pulses
|= (1U<<(uint8_t)protocol
);
131 #if !defined(__clang__)
132 // in the case we've disabled most backends then the "return true" in
133 // the following method can never be reached, and the compiler gets
135 #pragma GCC diagnostic push
136 #pragma GCC diagnostic ignored "-Wswitch-unreachable"
139 // for protocols without strong CRCs we require 3 good frames to lock on
140 bool requires_3_frames(enum rcprotocol_t p
) {
142 #if AP_RCPROTOCOL_DSM_ENABLED
145 #if AP_RCPROTOCOL_FASTSBUS_ENABLED
148 #if AP_RCPROTOCOL_SBUS_ENABLED
151 #if AP_RCPROTOCOL_SBUS_NI_ENABLED
154 #if AP_RCPROTOCOL_PPMSUM_ENABLED
157 #if AP_RCPROTOCOL_FPORT_ENABLED
160 #if AP_RCPROTOCOL_FPORT2_ENABLED
163 #if AP_RCPROTOCOL_CRSF_ENABLED
166 #if AP_RCPROTOCOL_GHST_ENABLED
170 #if AP_RCPROTOCOL_IBUS_ENABLED
173 #if AP_RCPROTOCOL_SUMD_ENABLED
176 #if AP_RCPROTOCOL_SRXL_ENABLED
179 #if AP_RCPROTOCOL_SRXL2_ENABLED
182 #if AP_RCPROTOCOL_ST24_ENABLED
185 #if AP_RCPROTOCOL_DRONECAN_ENABLED
188 #if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
191 #if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED
194 #if AP_RCPROTOCOL_UDP_ENABLED
197 #if AP_RCPROTOCOL_FDM_ENABLED
200 #if AP_RCPROTOCOL_RADIO_ENABLED
208 #if !defined(__clang__)
209 #pragma GCC diagnostic pop
212 uint8_t num_channels();
213 uint16_t read(uint8_t chan
);
214 void read(uint16_t *pwm
, uint8_t n
);
216 void start_bind(void);
217 int16_t get_RSSI(void) const;
218 int16_t get_rx_link_quality(void) const;
220 // return protocol name as a string
221 const char *protocol_name(void) const;
223 // return detected protocol
224 enum rcprotocol_t
protocol_detected(void) const {
225 return _detected_protocol
;
228 // add a UART for RCIN
229 void add_uart(AP_HAL::UARTDriver
* uart
);
230 bool has_uart() const { return added
.uart
!= nullptr; }
232 // set allowed RC protocols
233 void set_rc_protocols(uint32_t mask
) {
234 rc_protocols_mask
= mask
;
239 void apply_to_uart(AP_HAL::UARTDriver
*uart
) const;
247 // return true if we are decoding a byte stream, instead of pulses
248 bool using_uart(void) const {
249 return _detected_with_bytes
;
252 // handle mavlink radio
253 #if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
254 void handle_radio_rc_channels(const mavlink_radio_rc_channels_t
* packet
);
258 void check_added_uart(void);
260 // return true if a specific protocol is enabled
261 bool protocol_enabled(enum rcprotocol_t protocol
) const;
263 // explicitly investigate a backend for data, as opposed to
264 // feeding the backend a byte (or pulse-train) at a time and
265 // having them make an "add_input" callback):
266 bool detect_async_protocol(rcprotocol_t protocol
);
268 enum rcprotocol_t _detected_protocol
= NONE
;
269 uint16_t _disabled_for_pulses
;
270 bool _detected_with_bytes
;
271 AP_RCProtocol_Backend
*backend
[NONE
];
273 uint32_t _last_input_ms
;
274 bool _failsafe_active
;
275 bool _valid_serial_prot
;
277 // optional additional uart
279 AP_HAL::UARTDriver
*uart
;
281 uint32_t last_config_change_ms
;
285 // allowed RC protocols mask (first bit means "all")
286 uint32_t rc_protocols_mask
;
288 #endif // AP_RCPROTCOL_ENABLED
292 #if AP_RCPROTOCOL_ENABLED
297 #include "AP_RCProtocol_Backend.h"
298 #endif // AP_RCProtocol_enabled