2018-01-17 15:31:11 -04:00
|
|
|
/*
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-07-20 01:25:40 -03:00
|
|
|
*
|
2018-01-17 15:31:11 -04:00
|
|
|
* Code by Andrew Tridgell and Siddharth Bharat Purohit
|
|
|
|
*/
|
|
|
|
|
2023-05-03 10:08:00 -03:00
|
|
|
#include "AP_RCProtocol_config.h"
|
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
#include "AP_RCProtocol.h"
|
2023-05-03 10:08:00 -03:00
|
|
|
|
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
#include "AP_RCProtocol_PPMSum.h"
|
|
|
|
#include "AP_RCProtocol_DSM.h"
|
2019-07-06 04:27:43 -03:00
|
|
|
#include "AP_RCProtocol_IBUS.h"
|
2018-01-17 15:31:11 -04:00
|
|
|
#include "AP_RCProtocol_SBUS.h"
|
2018-07-12 11:49:32 -03:00
|
|
|
#include "AP_RCProtocol_SUMD.h"
|
2018-07-13 11:49:42 -03:00
|
|
|
#include "AP_RCProtocol_SRXL.h"
|
2020-03-23 16:20:00 -03:00
|
|
|
#include "AP_RCProtocol_SRXL2.h"
|
2020-04-30 16:34:51 -03:00
|
|
|
#include "AP_RCProtocol_CRSF.h"
|
2018-07-13 11:49:42 -03:00
|
|
|
#include "AP_RCProtocol_ST24.h"
|
2019-12-02 01:13:14 -04:00
|
|
|
#include "AP_RCProtocol_FPort.h"
|
2020-10-26 03:32:36 -03:00
|
|
|
#include "AP_RCProtocol_FPort2.h"
|
2023-05-18 05:20:22 -03:00
|
|
|
#include "AP_RCProtocol_DroneCAN.h"
|
2023-12-13 04:47:51 -04:00
|
|
|
#include "AP_RCProtocol_GHST.h"
|
2024-03-06 06:32:50 -04:00
|
|
|
#include "AP_RCProtocol_MAVLinkRadio.h"
|
2024-03-05 01:50:57 -04:00
|
|
|
#include "AP_RCProtocol_Joystick_SFML.h"
|
2019-08-27 04:40:22 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2020-08-12 23:27:43 -03:00
|
|
|
#include <RC_Channel/RC_Channel.h>
|
2018-10-31 21:21:03 -03:00
|
|
|
|
2023-05-03 10:08:00 -03:00
|
|
|
#include <AP_Vehicle/AP_Vehicle_Type.h>
|
|
|
|
|
2020-01-28 22:16:54 -04:00
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
void AP_RCProtocol::init()
|
|
|
|
{
|
2023-05-15 22:55:57 -03:00
|
|
|
#if AP_RCPROTOCOL_PPMSUM_ENABLED
|
|
|
|
backend[AP_RCProtocol::PPMSUM] = new AP_RCProtocol_PPMSum(*this);
|
|
|
|
#endif
|
2023-05-15 23:03:39 -03:00
|
|
|
#if AP_RCPROTOCOL_IBUS_ENABLED
|
2019-07-06 04:27:43 -03:00
|
|
|
backend[AP_RCProtocol::IBUS] = new AP_RCProtocol_IBUS(*this);
|
2023-05-15 23:03:39 -03:00
|
|
|
#endif
|
2023-04-27 21:38:54 -03:00
|
|
|
#if AP_RCPROTOCOL_SBUS_ENABLED
|
2021-11-05 02:39:56 -03:00
|
|
|
backend[AP_RCProtocol::SBUS] = new AP_RCProtocol_SBUS(*this, true, 100000);
|
2023-04-27 21:38:54 -03:00
|
|
|
#endif
|
2021-11-05 02:39:56 -03:00
|
|
|
#if AP_RCPROTOCOL_FASTSBUS_ENABLED
|
|
|
|
backend[AP_RCProtocol::FASTSBUS] = new AP_RCProtocol_SBUS(*this, true, 200000);
|
|
|
|
#endif
|
2023-05-15 23:21:28 -03:00
|
|
|
#if AP_RCPROTOCOL_DSM_ENABLED
|
2018-01-17 15:31:11 -04:00
|
|
|
backend[AP_RCProtocol::DSM] = new AP_RCProtocol_DSM(*this);
|
2023-05-15 23:21:28 -03:00
|
|
|
#endif
|
2023-05-15 23:08:53 -03:00
|
|
|
#if AP_RCPROTOCOL_SUMD_ENABLED
|
2018-07-12 11:49:32 -03:00
|
|
|
backend[AP_RCProtocol::SUMD] = new AP_RCProtocol_SUMD(*this);
|
2023-05-15 23:08:53 -03:00
|
|
|
#endif
|
2023-03-14 08:18:29 -03:00
|
|
|
#if AP_RCPROTOCOL_SRXL_ENABLED
|
2018-07-13 11:49:42 -03:00
|
|
|
backend[AP_RCProtocol::SRXL] = new AP_RCProtocol_SRXL(*this);
|
2023-03-14 08:18:29 -03:00
|
|
|
#endif
|
2023-04-27 21:38:54 -03:00
|
|
|
#if AP_RCPROTOCOL_SBUS_NI_ENABLED
|
2021-11-05 02:39:56 -03:00
|
|
|
backend[AP_RCProtocol::SBUS_NI] = new AP_RCProtocol_SBUS(*this, false, 100000);
|
2023-04-27 21:38:54 -03:00
|
|
|
#endif
|
2023-04-26 04:19:07 -03:00
|
|
|
#if AP_RCPROTOCOL_SRXL2_ENABLED
|
2020-03-23 16:20:00 -03:00
|
|
|
backend[AP_RCProtocol::SRXL2] = new AP_RCProtocol_SRXL2(*this);
|
2023-04-26 04:19:07 -03:00
|
|
|
#endif
|
2023-04-26 10:46:08 -03:00
|
|
|
#if AP_RCPROTOCOL_CRSF_ENABLED
|
2020-04-30 16:34:51 -03:00
|
|
|
backend[AP_RCProtocol::CRSF] = new AP_RCProtocol_CRSF(*this);
|
2023-04-26 10:46:08 -03:00
|
|
|
#endif
|
2022-04-16 23:54:08 -03:00
|
|
|
#if AP_RCPROTOCOL_FPORT2_ENABLED
|
2020-12-24 02:01:31 -04:00
|
|
|
backend[AP_RCProtocol::FPORT2] = new AP_RCProtocol_FPort2(*this, true);
|
2020-04-04 07:40:38 -03:00
|
|
|
#endif
|
2023-05-15 22:35:46 -03:00
|
|
|
#if AP_RCPROTOCOL_ST24_ENABLED
|
2018-07-13 11:49:42 -03:00
|
|
|
backend[AP_RCProtocol::ST24] = new AP_RCProtocol_ST24(*this);
|
2023-05-15 22:35:46 -03:00
|
|
|
#endif
|
2022-04-16 23:54:08 -03:00
|
|
|
#if AP_RCPROTOCOL_FPORT_ENABLED
|
2019-12-02 01:13:14 -04:00
|
|
|
backend[AP_RCProtocol::FPORT] = new AP_RCProtocol_FPort(*this, true);
|
2022-04-16 23:54:08 -03:00
|
|
|
#endif
|
2023-05-18 05:20:22 -03:00
|
|
|
#if AP_RCPROTOCOL_DRONECAN_ENABLED
|
|
|
|
backend[AP_RCProtocol::DRONECAN] = new AP_RCProtocol_DroneCAN(*this);
|
|
|
|
#endif
|
2023-12-13 04:47:51 -04:00
|
|
|
#if AP_RCPROTOCOL_GHST_ENABLED
|
|
|
|
backend[AP_RCProtocol::GHST] = new AP_RCProtocol_GHST(*this);
|
|
|
|
#endif
|
2024-03-06 06:32:50 -04:00
|
|
|
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
|
|
|
|
backend[AP_RCProtocol::MAVLINK_RADIO] = new AP_RCProtocol_MAVLinkRadio(*this);
|
|
|
|
#endif
|
2024-03-05 01:50:57 -04:00
|
|
|
#if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED
|
|
|
|
backend[AP_RCProtocol::JOYSTICK_SFML] = new AP_RCProtocol_Joystick_SFML(*this);
|
|
|
|
#endif
|
2018-01-17 15:31:11 -04:00
|
|
|
}
|
|
|
|
|
2018-11-04 23:56:37 -04:00
|
|
|
AP_RCProtocol::~AP_RCProtocol()
|
|
|
|
{
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(backend); i++) {
|
2018-11-04 23:56:37 -04:00
|
|
|
if (backend[i] != nullptr) {
|
|
|
|
delete backend[i];
|
|
|
|
backend[i] = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 19:33:18 -03:00
|
|
|
bool AP_RCProtocol::should_search(uint32_t now_ms) const
|
|
|
|
{
|
2023-03-19 06:26:16 -03:00
|
|
|
#if AP_RC_CHANNEL_ENABLED && !APM_BUILD_TYPE(APM_BUILD_UNKNOWN)
|
2023-06-11 02:13:42 -03:00
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE && !rc().option_is_enabled(RC_Channels::Option::MULTI_RECEIVER_SUPPORT)) {
|
2021-10-10 19:33:18 -03:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-06 17:08:46 -04:00
|
|
|
#else
|
|
|
|
// on IOMCU don't allow protocol to change once detected
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-10 19:33:18 -03:00
|
|
|
#endif
|
|
|
|
return (now_ms - _last_input_ms >= 200);
|
|
|
|
}
|
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
void AP_RCProtocol::process_pulse(uint32_t width_s0, uint32_t width_s1)
|
|
|
|
{
|
2018-11-03 21:09:29 -03:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2021-10-10 19:33:18 -03:00
|
|
|
bool searching = should_search(now);
|
2020-08-12 23:27:43 -03:00
|
|
|
|
2023-03-19 06:26:16 -03:00
|
|
|
#if AP_RC_CHANNEL_ENABLED
|
2020-08-12 23:27:43 -03:00
|
|
|
rc_protocols_mask = rc().enabled_protocols();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE &&
|
|
|
|
!protocol_enabled(_detected_protocol)) {
|
|
|
|
_detected_protocol = AP_RCProtocol::NONE;
|
|
|
|
}
|
|
|
|
|
2018-11-03 21:09:29 -03:00
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE && _detected_with_bytes && !searching) {
|
2018-11-01 00:44:55 -03:00
|
|
|
// we're using byte inputs, discard pulses
|
|
|
|
return;
|
|
|
|
}
|
2018-01-17 19:12:16 -04:00
|
|
|
// first try current protocol
|
2018-11-03 21:09:29 -03:00
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE && !searching) {
|
2018-01-17 19:12:16 -04:00
|
|
|
backend[_detected_protocol]->process_pulse(width_s0, width_s1);
|
|
|
|
if (backend[_detected_protocol]->new_input()) {
|
|
|
|
_new_input = true;
|
2018-11-02 07:13:21 -03:00
|
|
|
_last_input_ms = now;
|
2018-01-17 19:12:16 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise scan all protocols
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(backend); i++) {
|
2018-11-02 08:25:05 -03:00
|
|
|
if (_disabled_for_pulses & (1U << i)) {
|
|
|
|
// this protocol is disabled for pulse input
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-17 15:31:11 -04:00
|
|
|
if (backend[i] != nullptr) {
|
2020-08-12 23:27:43 -03:00
|
|
|
if (!protocol_enabled(rcprotocol_t(i))) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-13 04:24:01 -03:00
|
|
|
const uint32_t frame_count = backend[i]->get_rc_frame_count();
|
|
|
|
const uint32_t input_count = backend[i]->get_rc_input_count();
|
2018-01-17 15:31:11 -04:00
|
|
|
backend[i]->process_pulse(width_s0, width_s1);
|
2020-08-13 04:24:01 -03:00
|
|
|
const uint32_t frame_count2 = backend[i]->get_rc_frame_count();
|
|
|
|
if (frame_count2 > frame_count) {
|
|
|
|
if (requires_3_frames((rcprotocol_t)i) && frame_count2 < 3) {
|
2018-11-02 19:49:17 -03:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-08 04:07:42 -04:00
|
|
|
_new_input = (input_count != backend[i]->get_rc_input_count());
|
2018-01-17 15:31:11 -04:00
|
|
|
_detected_protocol = (enum AP_RCProtocol::rcprotocol_t)i;
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t j = 0; j < ARRAY_SIZE(backend); j++) {
|
2020-08-13 04:24:01 -03:00
|
|
|
if (backend[j]) {
|
|
|
|
backend[j]->reset_rc_frame_count();
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 07:13:21 -03:00
|
|
|
_last_input_ms = now;
|
2018-11-01 00:44:55 -03:00
|
|
|
_detected_with_bytes = false;
|
2018-11-05 02:13:46 -04:00
|
|
|
break;
|
2018-07-12 11:49:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 02:52:57 -03:00
|
|
|
/*
|
|
|
|
process an array of pulses. n must be even
|
|
|
|
*/
|
|
|
|
void AP_RCProtocol::process_pulse_list(const uint32_t *widths, uint16_t n, bool need_swap)
|
|
|
|
{
|
|
|
|
if (n & 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
while (n) {
|
|
|
|
uint32_t widths0 = widths[0];
|
|
|
|
uint32_t widths1 = widths[1];
|
|
|
|
if (need_swap) {
|
|
|
|
uint32_t tmp = widths1;
|
|
|
|
widths1 = widths0;
|
|
|
|
widths0 = tmp;
|
|
|
|
}
|
|
|
|
widths1 -= widths0;
|
|
|
|
process_pulse(widths0, widths1);
|
|
|
|
widths += 2;
|
|
|
|
n -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 01:13:14 -04:00
|
|
|
bool AP_RCProtocol::process_byte(uint8_t byte, uint32_t baudrate)
|
2018-07-12 11:49:32 -03:00
|
|
|
{
|
2018-11-03 21:09:29 -03:00
|
|
|
uint32_t now = AP_HAL::millis();
|
2021-10-10 19:33:18 -03:00
|
|
|
bool searching = should_search(now);
|
2020-08-12 23:27:43 -03:00
|
|
|
|
2023-03-19 06:26:16 -03:00
|
|
|
#if AP_RC_CHANNEL_ENABLED
|
2020-08-12 23:27:43 -03:00
|
|
|
rc_protocols_mask = rc().enabled_protocols();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE &&
|
|
|
|
!protocol_enabled(_detected_protocol)) {
|
|
|
|
_detected_protocol = AP_RCProtocol::NONE;
|
|
|
|
}
|
|
|
|
|
2018-11-03 21:09:29 -03:00
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE && !_detected_with_bytes && !searching) {
|
2018-11-01 00:44:55 -03:00
|
|
|
// we're using pulse inputs, discard bytes
|
2019-12-02 01:13:14 -04:00
|
|
|
return false;
|
2018-11-01 00:44:55 -03:00
|
|
|
}
|
2020-05-09 05:20:33 -03:00
|
|
|
|
2018-07-12 11:49:32 -03:00
|
|
|
// first try current protocol
|
2018-11-03 21:09:29 -03:00
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE && !searching) {
|
2018-11-02 06:42:29 -03:00
|
|
|
backend[_detected_protocol]->process_byte(byte, baudrate);
|
2018-07-12 11:49:32 -03:00
|
|
|
if (backend[_detected_protocol]->new_input()) {
|
|
|
|
_new_input = true;
|
2018-11-02 07:13:21 -03:00
|
|
|
_last_input_ms = now;
|
2018-07-12 11:49:32 -03:00
|
|
|
}
|
2019-12-02 01:13:14 -04:00
|
|
|
return true;
|
2018-07-12 11:49:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise scan all protocols
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(backend); i++) {
|
2018-07-12 11:49:32 -03:00
|
|
|
if (backend[i] != nullptr) {
|
2020-08-12 23:27:43 -03:00
|
|
|
if (!protocol_enabled(rcprotocol_t(i))) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-08-13 04:24:01 -03:00
|
|
|
const uint32_t frame_count = backend[i]->get_rc_frame_count();
|
|
|
|
const uint32_t input_count = backend[i]->get_rc_input_count();
|
2018-11-02 06:42:29 -03:00
|
|
|
backend[i]->process_byte(byte, baudrate);
|
2020-08-13 04:24:01 -03:00
|
|
|
const uint32_t frame_count2 = backend[i]->get_rc_frame_count();
|
|
|
|
if (frame_count2 > frame_count) {
|
|
|
|
if (requires_3_frames((rcprotocol_t)i) && frame_count2 < 3) {
|
2018-11-02 19:49:17 -03:00
|
|
|
continue;
|
|
|
|
}
|
2018-11-08 04:07:42 -04:00
|
|
|
_new_input = (input_count != backend[i]->get_rc_input_count());
|
2018-07-12 11:49:32 -03:00
|
|
|
_detected_protocol = (enum AP_RCProtocol::rcprotocol_t)i;
|
2018-11-02 07:13:21 -03:00
|
|
|
_last_input_ms = now;
|
2018-11-01 00:44:55 -03:00
|
|
|
_detected_with_bytes = true;
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t j = 0; j < ARRAY_SIZE(backend); j++) {
|
2020-08-13 04:24:01 -03:00
|
|
|
if (backend[j]) {
|
|
|
|
backend[j]->reset_rc_frame_count();
|
|
|
|
}
|
|
|
|
}
|
2020-01-28 22:16:54 -04:00
|
|
|
// stop decoding pulses to save CPU
|
|
|
|
hal.rcin->pulse_input_enable(false);
|
2023-05-21 05:45:43 -03:00
|
|
|
return true;
|
2018-01-17 15:31:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-02 01:13:14 -04:00
|
|
|
return false;
|
2018-01-17 15:31:11 -04:00
|
|
|
}
|
|
|
|
|
2020-05-09 05:20:33 -03:00
|
|
|
// handshake if nothing else has succeeded so far
|
|
|
|
void AP_RCProtocol::process_handshake( uint32_t baudrate)
|
|
|
|
{
|
|
|
|
// if we ever succeeded before then do not handshake
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE || _last_input_ms > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise handshake all protocols
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(backend); i++) {
|
2020-05-09 05:20:33 -03:00
|
|
|
if (backend[i] != nullptr) {
|
|
|
|
backend[i]->process_handshake(baudrate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 04:40:22 -03:00
|
|
|
/*
|
|
|
|
check for bytes from an additional uart. This is used to support RC
|
|
|
|
protocols from SERIALn_PROTOCOL
|
|
|
|
*/
|
2021-11-10 20:34:49 -04:00
|
|
|
void AP_RCProtocol::SerialConfig::apply_to_uart(AP_HAL::UARTDriver *uart) const
|
|
|
|
{
|
|
|
|
uart->configure_parity(parity);
|
|
|
|
uart->set_stop_bits(stop_bits);
|
|
|
|
if (invert_rx) {
|
|
|
|
uart->set_options(uart->get_options() | AP_HAL::UARTDriver::OPTION_RXINV);
|
|
|
|
} else {
|
|
|
|
uart->set_options(uart->get_options() & ~AP_HAL::UARTDriver::OPTION_RXINV);
|
|
|
|
}
|
|
|
|
uart->begin(baud, 128, 128);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const AP_RCProtocol::SerialConfig serial_configs[] {
|
|
|
|
// BAUD PRTY STOP INVERT-RX
|
|
|
|
// inverted and uninverted 115200 8N1:
|
|
|
|
{ 115200, 0, 1, false },
|
|
|
|
{ 115200, 0, 1, true },
|
|
|
|
// SBUS settings, even parity, 2 stop bits:
|
|
|
|
{ 100000, 2, 2, true },
|
2021-11-05 02:39:56 -03:00
|
|
|
#if AP_RCPROTOCOL_FASTSBUS_ENABLED
|
|
|
|
// FastSBUS:
|
|
|
|
{ 200000, 2, 2, true },
|
|
|
|
#endif
|
2024-01-02 14:20:46 -04:00
|
|
|
#if AP_RCPROTOCOL_CRSF_ENABLED || AP_RCPROTOCOL_GHST_ENABLED
|
2021-11-10 20:34:49 -04:00
|
|
|
// CrossFire:
|
|
|
|
{ 416666, 0, 1, false },
|
2023-07-01 10:58:32 -03:00
|
|
|
// CRSFv3 can negotiate higher rates which are sticky on soft reboot
|
|
|
|
{ 2000000, 0, 1, false },
|
2023-12-13 04:47:51 -04:00
|
|
|
#endif
|
2021-11-10 20:34:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(ARRAY_SIZE(serial_configs) > 1, "must have at least one serial config");
|
|
|
|
|
2019-08-27 04:40:22 -03:00
|
|
|
void AP_RCProtocol::check_added_uart(void)
|
|
|
|
{
|
|
|
|
if (!added.uart) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t now = AP_HAL::millis();
|
2021-10-10 19:33:18 -03:00
|
|
|
bool searching = should_search(now);
|
2019-08-27 04:40:22 -03:00
|
|
|
if (!searching && !_detected_with_bytes) {
|
|
|
|
// not using this uart
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!added.opened) {
|
|
|
|
added.opened = true;
|
2021-11-10 20:34:49 -04:00
|
|
|
added.last_config_change_ms = AP_HAL::millis();
|
|
|
|
serial_configs[added.config_num].apply_to_uart(added.uart);
|
2019-08-27 04:40:22 -03:00
|
|
|
}
|
2023-03-19 06:26:16 -03:00
|
|
|
#if AP_RC_CHANNEL_ENABLED
|
2021-01-03 17:35:13 -04:00
|
|
|
rc_protocols_mask = rc().enabled_protocols();
|
|
|
|
#endif
|
2021-11-10 20:34:49 -04:00
|
|
|
const uint32_t current_baud = serial_configs[added.config_num].baud;
|
|
|
|
process_handshake(current_baud);
|
2020-05-09 05:20:33 -03:00
|
|
|
|
2019-08-27 04:40:22 -03:00
|
|
|
uint32_t n = added.uart->available();
|
|
|
|
n = MIN(n, 255U);
|
|
|
|
for (uint8_t i=0; i<n; i++) {
|
|
|
|
int16_t b = added.uart->read();
|
|
|
|
if (b >= 0) {
|
2021-11-10 20:34:49 -04:00
|
|
|
process_byte(uint8_t(b), current_baud);
|
2019-08-27 04:40:22 -03:00
|
|
|
}
|
|
|
|
}
|
2021-12-20 00:34:46 -04:00
|
|
|
if (searching) {
|
2021-11-10 20:34:49 -04:00
|
|
|
if (now - added.last_config_change_ms > 1000) {
|
|
|
|
// change configs if not detected once a second
|
|
|
|
added.config_num++;
|
|
|
|
if (added.config_num >= ARRAY_SIZE(serial_configs)) {
|
|
|
|
added.config_num = 0;
|
2019-12-02 01:13:14 -04:00
|
|
|
}
|
2019-08-27 04:40:22 -03:00
|
|
|
added.opened = false;
|
|
|
|
}
|
2022-01-15 11:26:47 -04:00
|
|
|
// power loss on CRSF requires re-bootstrap because the baudrate is reset to the default. The CRSF side will
|
|
|
|
// drop back down to 416k if it has received 200 incorrect characters (or none at all)
|
|
|
|
} else if (_detected_protocol != AP_RCProtocol::NONE
|
|
|
|
// protocols that want to be able to renegotiate should return false in is_rx_active()
|
|
|
|
&& !backend[_detected_protocol]->is_rx_active()
|
|
|
|
&& now - added.last_config_change_ms > 1000) {
|
|
|
|
added.opened = false;
|
2019-08-27 04:40:22 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 19:38:58 -03:00
|
|
|
void AP_RCProtocol::update()
|
|
|
|
{
|
|
|
|
check_added_uart();
|
|
|
|
}
|
|
|
|
|
2024-02-05 01:02:44 -04:00
|
|
|
// explicitly investigate a backend for data, as opposed to feeding
|
|
|
|
// the backend a byte (or pulse-train) at a time and having them make
|
|
|
|
// an "add_input" callback):
|
|
|
|
bool AP_RCProtocol::detect_async_protocol(rcprotocol_t protocol)
|
|
|
|
{
|
|
|
|
auto *p = backend[protocol];
|
|
|
|
if (p == nullptr) {
|
|
|
|
// backend is not allocated?!
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_detected_protocol == protocol) {
|
|
|
|
// we are using this protocol already, see if there is new
|
|
|
|
// data. Caller will handle the case where we stop presenting
|
|
|
|
// data
|
|
|
|
return p->new_input();
|
|
|
|
}
|
|
|
|
|
|
|
|
// we are not the currently in-use protocol.
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
// see if another backend is providing data:
|
|
|
|
if (!should_search(now)) {
|
|
|
|
// apparently, yes
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-03-05 18:21:59 -04:00
|
|
|
#if AP_RC_CHANNEL_ENABLED
|
|
|
|
rc_protocols_mask = rc().enabled_protocols();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!protocol_enabled(protocol)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-02-05 01:02:44 -04:00
|
|
|
// nobody is providing data; can we provide data?
|
|
|
|
if (!p->new_input()) {
|
|
|
|
// we can't provide data
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can provide data, change the detected protocol to be us:
|
|
|
|
_detected_protocol = protocol;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-17 15:31:11 -04:00
|
|
|
bool AP_RCProtocol::new_input()
|
|
|
|
{
|
2019-08-27 04:40:22 -03:00
|
|
|
// if we have an extra UART from a SERIALn_PROTOCOL then check it for data
|
|
|
|
check_added_uart();
|
|
|
|
|
2018-04-10 03:14:54 -03:00
|
|
|
// run update function on backends
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(backend); i++) {
|
2018-04-10 03:14:54 -03:00
|
|
|
if (backend[i] != nullptr) {
|
|
|
|
backend[i]->update();
|
|
|
|
}
|
|
|
|
}
|
2023-05-18 05:20:22 -03:00
|
|
|
|
2024-02-05 01:02:44 -04:00
|
|
|
// iterate through backends which don't do either of pulse or uart
|
|
|
|
// input, and thus won't update_new_input
|
|
|
|
const rcprotocol_t pollable[] {
|
2023-05-18 05:20:22 -03:00
|
|
|
#if AP_RCPROTOCOL_DRONECAN_ENABLED
|
2024-02-05 01:02:44 -04:00
|
|
|
AP_RCProtocol::DRONECAN,
|
2024-03-06 06:32:50 -04:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
|
|
|
|
AP_RCProtocol::MAVLINK_RADIO,
|
2024-03-05 01:50:57 -04:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED
|
|
|
|
AP_RCProtocol::JOYSTICK_SFML,
|
2024-02-05 01:02:44 -04:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
for (const auto protocol : pollable) {
|
2024-02-22 19:20:57 -04:00
|
|
|
if (!detect_async_protocol(protocol)) {
|
2024-02-05 01:02:44 -04:00
|
|
|
continue;
|
2023-05-18 05:20:22 -03:00
|
|
|
}
|
2024-02-22 19:20:57 -04:00
|
|
|
_new_input = true;
|
2024-02-05 01:02:44 -04:00
|
|
|
_last_input_ms = AP_HAL::millis();
|
|
|
|
break;
|
2023-05-18 05:20:22 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = _new_input;
|
|
|
|
_new_input = false;
|
2018-01-17 15:31:11 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t AP_RCProtocol::num_channels()
|
|
|
|
{
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE) {
|
|
|
|
return backend[_detected_protocol]->num_channels();
|
|
|
|
}
|
2018-01-17 18:02:42 -04:00
|
|
|
return 0;
|
2018-01-17 15:31:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t AP_RCProtocol::read(uint8_t chan)
|
|
|
|
{
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE) {
|
|
|
|
return backend[_detected_protocol]->read(chan);
|
|
|
|
}
|
2018-01-17 18:02:42 -04:00
|
|
|
return 0;
|
2018-01-17 15:31:11 -04:00
|
|
|
}
|
2018-04-10 03:14:54 -03:00
|
|
|
|
2020-01-28 22:16:54 -04:00
|
|
|
void AP_RCProtocol::read(uint16_t *pwm, uint8_t n)
|
|
|
|
{
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE) {
|
|
|
|
backend[_detected_protocol]->read(pwm, n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 03:47:12 -04:00
|
|
|
int16_t AP_RCProtocol::get_RSSI(void) const
|
|
|
|
{
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE) {
|
|
|
|
return backend[_detected_protocol]->get_RSSI();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2021-07-13 13:45:08 -03:00
|
|
|
int16_t AP_RCProtocol::get_rx_link_quality(void) const
|
|
|
|
{
|
|
|
|
if (_detected_protocol != AP_RCProtocol::NONE) {
|
|
|
|
return backend[_detected_protocol]->get_rx_link_quality();
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2018-04-10 03:14:54 -03:00
|
|
|
/*
|
|
|
|
ask for bind start on supported receivers (eg spektrum satellite)
|
|
|
|
*/
|
|
|
|
void AP_RCProtocol::start_bind(void)
|
|
|
|
{
|
2023-01-24 01:56:17 -04:00
|
|
|
for (uint8_t i = 0; i < ARRAY_SIZE(backend); i++) {
|
2018-04-10 03:14:54 -03:00
|
|
|
if (backend[i] != nullptr) {
|
|
|
|
backend[i]->start_bind();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 19:49:56 -03:00
|
|
|
|
2023-05-03 10:08:00 -03:00
|
|
|
#endif // AP_RCPROTOCOL_ENABLED
|
|
|
|
|
2018-11-02 19:49:56 -03:00
|
|
|
/*
|
|
|
|
return protocol name
|
|
|
|
*/
|
2018-11-05 01:44:00 -04:00
|
|
|
const char *AP_RCProtocol::protocol_name_from_protocol(rcprotocol_t protocol)
|
2018-11-02 19:49:56 -03:00
|
|
|
{
|
2018-11-05 01:44:00 -04:00
|
|
|
switch (protocol) {
|
2023-05-15 22:55:57 -03:00
|
|
|
#if AP_RCPROTOCOL_PPMSUM_ENABLED
|
|
|
|
case PPMSUM:
|
2018-11-02 19:49:56 -03:00
|
|
|
return "PPM";
|
2023-05-15 22:55:57 -03:00
|
|
|
#endif
|
2023-05-15 23:03:39 -03:00
|
|
|
#if AP_RCPROTOCOL_IBUS_ENABLED
|
2019-07-06 04:27:43 -03:00
|
|
|
case IBUS:
|
|
|
|
return "IBUS";
|
2023-05-15 23:03:39 -03:00
|
|
|
#endif
|
2023-04-27 21:38:54 -03:00
|
|
|
#if AP_RCPROTOCOL_SBUS_ENABLED
|
2018-11-02 19:49:56 -03:00
|
|
|
case SBUS:
|
2023-05-02 02:41:26 -03:00
|
|
|
return "SBUS";
|
2023-04-27 21:38:54 -03:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_SBUS_NI_ENABLED
|
2018-11-02 19:49:56 -03:00
|
|
|
case SBUS_NI:
|
|
|
|
return "SBUS";
|
2023-05-02 02:41:26 -03:00
|
|
|
#endif
|
2021-11-05 02:39:56 -03:00
|
|
|
#if AP_RCPROTOCOL_FASTSBUS_ENABLED
|
|
|
|
case FASTSBUS:
|
|
|
|
return "FastSBUS";
|
|
|
|
#endif
|
2023-05-15 23:21:28 -03:00
|
|
|
#if AP_RCPROTOCOL_DSM_ENABLED
|
2018-11-02 19:49:56 -03:00
|
|
|
case DSM:
|
|
|
|
return "DSM";
|
2023-05-15 23:21:28 -03:00
|
|
|
#endif
|
2023-05-15 23:08:53 -03:00
|
|
|
#if AP_RCPROTOCOL_SUMD_ENABLED
|
2018-11-02 19:49:56 -03:00
|
|
|
case SUMD:
|
|
|
|
return "SUMD";
|
2023-05-15 23:08:53 -03:00
|
|
|
#endif
|
2023-03-14 08:18:29 -03:00
|
|
|
#if AP_RCPROTOCOL_SRXL_ENABLED
|
2018-11-02 19:49:56 -03:00
|
|
|
case SRXL:
|
|
|
|
return "SRXL";
|
2023-03-14 08:18:29 -03:00
|
|
|
#endif
|
2023-04-26 04:19:07 -03:00
|
|
|
#if AP_RCPROTOCOL_SRXL2_ENABLED
|
2020-03-23 16:20:00 -03:00
|
|
|
case SRXL2:
|
|
|
|
return "SRXL2";
|
2023-04-26 04:19:07 -03:00
|
|
|
#endif
|
2023-04-26 10:46:08 -03:00
|
|
|
#if AP_RCPROTOCOL_CRSF_ENABLED
|
2020-04-30 16:34:51 -03:00
|
|
|
case CRSF:
|
|
|
|
return "CRSF";
|
2023-04-26 10:46:08 -03:00
|
|
|
#endif
|
2023-05-15 22:35:46 -03:00
|
|
|
#if AP_RCPROTOCOL_ST24_ENABLED
|
2018-11-02 19:49:56 -03:00
|
|
|
case ST24:
|
|
|
|
return "ST24";
|
2023-05-15 22:35:46 -03:00
|
|
|
#endif
|
2022-04-16 23:54:08 -03:00
|
|
|
#if AP_RCPROTOCOL_FPORT_ENABLED
|
2019-12-02 01:13:14 -04:00
|
|
|
case FPORT:
|
|
|
|
return "FPORT";
|
2022-04-16 23:54:08 -03:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_FPORT2_ENABLED
|
2020-10-26 03:32:36 -03:00
|
|
|
case FPORT2:
|
|
|
|
return "FPORT2";
|
2023-05-18 05:20:22 -03:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_DRONECAN_ENABLED
|
|
|
|
case DRONECAN:
|
|
|
|
return "DroneCAN";
|
2023-12-13 04:47:51 -04:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_GHST_ENABLED
|
|
|
|
case GHST:
|
|
|
|
return "GHST";
|
2024-03-06 06:32:50 -04:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
|
|
|
|
case MAVLINK_RADIO:
|
|
|
|
return "MAVRadio";
|
2024-03-05 01:50:57 -04:00
|
|
|
#endif
|
|
|
|
#if AP_RCPROTOCOL_JOYSTICK_SFML_ENABLED
|
|
|
|
case JOYSTICK_SFML:
|
|
|
|
return "SFML";
|
2022-04-16 23:54:08 -03:00
|
|
|
#endif
|
2018-11-02 19:49:56 -03:00
|
|
|
case NONE:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-11-05 01:44:00 -04:00
|
|
|
|
2023-05-03 10:08:00 -03:00
|
|
|
#if AP_RCPROTOCOL_ENABLED
|
2018-11-05 01:44:00 -04:00
|
|
|
/*
|
|
|
|
return protocol name
|
|
|
|
*/
|
|
|
|
const char *AP_RCProtocol::protocol_name(void) const
|
|
|
|
{
|
|
|
|
return protocol_name_from_protocol(_detected_protocol);
|
|
|
|
}
|
2019-08-27 04:40:22 -03:00
|
|
|
|
|
|
|
/*
|
|
|
|
add a uart to decode
|
|
|
|
*/
|
|
|
|
void AP_RCProtocol::add_uart(AP_HAL::UARTDriver* uart)
|
|
|
|
{
|
|
|
|
added.uart = uart;
|
2021-11-10 20:36:56 -04:00
|
|
|
added.uart->set_flow_control(AP_HAL::UARTDriver::FLOW_CONTROL_DISABLE);
|
2019-08-27 04:40:22 -03:00
|
|
|
}
|
|
|
|
|
2020-08-12 23:27:43 -03:00
|
|
|
// return true if a specific protocol is enabled
|
|
|
|
bool AP_RCProtocol::protocol_enabled(rcprotocol_t protocol) const
|
|
|
|
{
|
2024-03-05 18:21:59 -04:00
|
|
|
if ((rc_protocols_mask & 1U) != 0) {
|
2020-08-12 23:27:43 -03:00
|
|
|
// all protocols enabled
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return ((1U<<(uint8_t(protocol)+1)) & rc_protocols_mask) != 0;
|
|
|
|
}
|
|
|
|
|
2024-03-06 06:32:50 -04:00
|
|
|
#if AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
|
|
|
|
void AP_RCProtocol::handle_radio_rc_channels(const mavlink_radio_rc_channels_t* packet)
|
|
|
|
{
|
|
|
|
if (backend[AP_RCProtocol::MAVLINK_RADIO] == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
backend[AP_RCProtocol::MAVLINK_RADIO]->update_radio_rc_channels(packet);
|
|
|
|
};
|
|
|
|
#endif // AP_RCPROTOCOL_MAVLINK_RADIO_ENABLED
|
|
|
|
|
2019-08-27 04:40:22 -03:00
|
|
|
namespace AP {
|
|
|
|
AP_RCProtocol &RC()
|
|
|
|
{
|
|
|
|
static AP_RCProtocol rcprot;
|
|
|
|
return rcprot;
|
|
|
|
}
|
|
|
|
};
|
2023-05-03 10:08:00 -03:00
|
|
|
|
|
|
|
#endif // AP_RCPROTOCOL_ENABLED
|