2015-01-19 09:32:24 -04:00
|
|
|
/*
|
2021-05-26 23:33:17 -03:00
|
|
|
Please contribute your ideas! See https://ardupilot.org/dev for details
|
2015-01-19 09:32:24 -04:00
|
|
|
|
|
|
|
This program 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 program 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/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
SerialManager allows defining the protocol and baud rates for the available
|
|
|
|
serial ports and provides helper functions so objects (like a gimbal) can
|
|
|
|
find which serial port they should use
|
|
|
|
*/
|
2016-02-17 21:25:48 -04:00
|
|
|
#pragma once
|
2015-01-19 09:32:24 -04:00
|
|
|
|
2015-08-11 03:28:45 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-03-22 22:21:52 -03:00
|
|
|
#include <AP_Param/AP_Param.h>
|
2015-01-19 09:32:24 -04:00
|
|
|
|
2019-11-14 23:50:57 -04:00
|
|
|
#ifdef HAL_UART_NUM_SERIAL_PORTS
|
2019-11-20 03:56:46 -04:00
|
|
|
#if HAL_UART_NUM_SERIAL_PORTS >= 4
|
2019-11-14 23:50:57 -04:00
|
|
|
#define SERIALMANAGER_NUM_PORTS HAL_UART_NUM_SERIAL_PORTS
|
|
|
|
#else
|
2019-11-20 03:56:46 -04:00
|
|
|
// we need a minimum of 4 to allow for a GPS due to the odd ordering
|
|
|
|
// of hal.uartB as SERIAL3
|
|
|
|
#define SERIALMANAGER_NUM_PORTS 4
|
|
|
|
#endif
|
|
|
|
#else
|
2019-11-14 23:50:57 -04:00
|
|
|
// assume max 8 ports
|
2019-07-12 01:59:02 -03:00
|
|
|
#define SERIALMANAGER_NUM_PORTS 8
|
2019-11-14 23:50:57 -04:00
|
|
|
#endif
|
2015-01-19 09:32:24 -04:00
|
|
|
|
2023-09-24 14:39:37 -03:00
|
|
|
#ifndef HAL_NUM_SERIAL_PORTS
|
|
|
|
#define HAL_NUM_SERIAL_PORTS SERIALMANAGER_NUM_PORTS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAL_HAVE_SERIAL0
|
|
|
|
#define HAL_HAVE_SERIAL0 HAL_NUM_SERIAL_PORTS > 0
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL1
|
|
|
|
#define HAL_HAVE_SERIAL1 HAL_NUM_SERIAL_PORTS > 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL2
|
|
|
|
#define HAL_HAVE_SERIAL2 HAL_NUM_SERIAL_PORTS > 2
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL3
|
|
|
|
#define HAL_HAVE_SERIAL3 HAL_NUM_SERIAL_PORTS > 3
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL4
|
|
|
|
#define HAL_HAVE_SERIAL4 HAL_NUM_SERIAL_PORTS > 4
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL5
|
|
|
|
#define HAL_HAVE_SERIAL5 HAL_NUM_SERIAL_PORTS > 5
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL6
|
|
|
|
#define HAL_HAVE_SERIAL6 HAL_NUM_SERIAL_PORTS > 6
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL7
|
|
|
|
#define HAL_HAVE_SERIAL7 HAL_NUM_SERIAL_PORTS > 7
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL8
|
|
|
|
#define HAL_HAVE_SERIAL8 HAL_NUM_SERIAL_PORTS > 8
|
|
|
|
#endif
|
|
|
|
#ifndef HAL_HAVE_SERIAL9
|
|
|
|
#define HAL_HAVE_SERIAL9 HAL_NUM_SERIAL_PORTS > 9
|
|
|
|
#endif
|
|
|
|
|
2022-02-21 23:19:15 -04:00
|
|
|
/*
|
|
|
|
array size for state[]. This needs to be at least
|
|
|
|
SERIALMANAGER_NUM_PORTS, but we want it to be the same length on
|
|
|
|
similar boards to get the ccache efficiency up. This wastes a small
|
|
|
|
amount of memory, but makes a huge difference to the build times
|
|
|
|
*/
|
|
|
|
#if SERIALMANAGER_NUM_PORTS > 10 || SERIALMANAGER_NUM_PORTS < 5
|
|
|
|
#define SERIALMANAGER_MAX_PORTS SERIALMANAGER_NUM_PORTS
|
|
|
|
#else
|
|
|
|
#define SERIALMANAGER_MAX_PORTS 10
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2015-01-19 09:32:24 -04:00
|
|
|
// console default baud rates and buffer sizes
|
2023-05-03 21:30:51 -03:00
|
|
|
#ifdef DEFAULT_SERIAL0_BAUD
|
|
|
|
#define AP_SERIALMANAGER_CONSOLE_BAUD DEFAULT_SERIAL0_BAUD
|
2015-01-19 09:32:24 -04:00
|
|
|
#else
|
2023-05-03 21:30:51 -03:00
|
|
|
#define AP_SERIALMANAGER_CONSOLE_BAUD 115200
|
2015-01-19 09:32:24 -04:00
|
|
|
#endif
|
2023-05-03 21:30:51 -03:00
|
|
|
#define AP_SERIALMANAGER_CONSOLE_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_CONSOLE_BUFSIZE_TX 512
|
2015-01-19 09:32:24 -04:00
|
|
|
|
|
|
|
// mavlink default baud rates and buffer sizes
|
|
|
|
#define AP_SERIALMANAGER_MAVLINK_BAUD 57600
|
|
|
|
#define AP_SERIALMANAGER_MAVLINK_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_MAVLINK_BUFSIZE_TX 256
|
|
|
|
|
2019-09-06 05:23:51 -03:00
|
|
|
// LTM buffer sizes
|
|
|
|
#define AP_SERIALMANAGER_LTM_BUFSIZE_RX 0
|
|
|
|
#define AP_SERIALMANAGER_LTM_BUFSIZE_TX 32
|
|
|
|
|
2016-05-03 15:35:45 -03:00
|
|
|
// FrSky default baud rates, use default buffer sizes
|
|
|
|
#define AP_SERIALMANAGER_FRSKY_D_BAUD 9600
|
2015-01-19 09:32:24 -04:00
|
|
|
#define AP_SERIALMANAGER_FRSKY_SPORT_BAUD 57600
|
|
|
|
#define AP_SERIALMANAGER_FRSKY_BUFSIZE_RX 0
|
|
|
|
#define AP_SERIALMANAGER_FRSKY_BUFSIZE_TX 0
|
|
|
|
|
|
|
|
// GPS default baud rates and buffer sizes
|
|
|
|
// we need a 256 byte buffer for some GPS types (eg. UBLOX)
|
|
|
|
#define AP_SERIALMANAGER_GPS_BAUD 38400
|
|
|
|
#define AP_SERIALMANAGER_GPS_BUFSIZE_RX 256
|
|
|
|
#define AP_SERIALMANAGER_GPS_BUFSIZE_TX 16
|
|
|
|
|
|
|
|
// AlexMos Gimbal protocol default baud rates and buffer sizes
|
|
|
|
#define AP_SERIALMANAGER_ALEXMOS_BAUD 115200
|
|
|
|
#define AP_SERIALMANAGER_ALEXMOS_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_ALEXMOS_BUFSIZE_TX 128
|
|
|
|
|
2023-05-15 09:06:22 -03:00
|
|
|
#define AP_SERIALMANAGER_GIMBAL_BAUD 115200
|
|
|
|
#define AP_SERIALMANAGER_GIMBAL_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_GIMBAL_BUFSIZE_TX 128
|
2015-05-26 03:57:05 -03:00
|
|
|
|
2017-11-02 23:27:29 -03:00
|
|
|
#define AP_SERIALMANAGER_VOLZ_BAUD 115
|
|
|
|
#define AP_SERIALMANAGER_VOLZ_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_VOLZ_BUFSIZE_TX 128
|
|
|
|
|
2018-12-18 23:28:18 -04:00
|
|
|
#define AP_SERIALMANAGER_ROBOTIS_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_ROBOTIS_BUFSIZE_TX 128
|
|
|
|
|
2019-11-08 19:30:39 -04:00
|
|
|
// MegaSquirt EFI protocol
|
|
|
|
#define AP_SERIALMANAGER_EFI_MS_BAUD 115
|
|
|
|
#define AP_SERIALMANAGER_EFI_MS_BUFSIZE_RX 512
|
|
|
|
#define AP_SERIALMANAGER_EFI_MS_BUFSIZE_TX 16
|
|
|
|
|
2017-08-21 17:31:51 -03:00
|
|
|
// SBUS servo outputs
|
|
|
|
#define AP_SERIALMANAGER_SBUS1_BAUD 100000
|
|
|
|
#define AP_SERIALMANAGER_SBUS1_BUFSIZE_RX 16
|
|
|
|
#define AP_SERIALMANAGER_SBUS1_BUFSIZE_TX 32
|
|
|
|
|
2019-04-20 03:13:23 -03:00
|
|
|
#define AP_SERIALMANAGER_SLCAN_BAUD 115200
|
|
|
|
#define AP_SERIALMANAGER_SLCAN_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_SLCAN_BUFSIZE_TX 128
|
|
|
|
|
2020-08-04 17:40:15 -03:00
|
|
|
// MSP protocol default buffer sizes
|
|
|
|
#define AP_SERIALMANAGER_MSP_BUFSIZE_RX 128
|
|
|
|
#define AP_SERIALMANAGER_MSP_BUFSIZE_TX 256
|
|
|
|
#define AP_SERIALMANAGER_MSP_BAUD 115200
|
|
|
|
|
2015-01-19 09:32:24 -04:00
|
|
|
class AP_SerialManager {
|
|
|
|
public:
|
2017-12-12 21:06:14 -04:00
|
|
|
AP_SerialManager();
|
|
|
|
|
|
|
|
/* Do not allow copies */
|
2022-09-30 06:50:43 -03:00
|
|
|
CLASS_NO_COPY(AP_SerialManager);
|
2017-12-12 21:06:14 -04:00
|
|
|
|
2015-01-19 09:32:24 -04:00
|
|
|
enum SerialProtocol {
|
2016-04-19 00:48:57 -03:00
|
|
|
SerialProtocol_None = -1,
|
2016-05-16 00:34:32 -03:00
|
|
|
SerialProtocol_Console = 0, // unused
|
2015-03-27 22:44:43 -03:00
|
|
|
SerialProtocol_MAVLink = 1,
|
2016-05-03 15:35:45 -03:00
|
|
|
SerialProtocol_MAVLink2 = 2, // do not use - use MAVLink and provide instance of 1
|
|
|
|
SerialProtocol_FrSky_D = 3, // FrSky D protocol (D-receivers)
|
|
|
|
SerialProtocol_FrSky_SPort = 4, // FrSky SPort protocol (X-receivers)
|
2015-01-19 09:32:24 -04:00
|
|
|
SerialProtocol_GPS = 5,
|
2016-05-03 15:35:45 -03:00
|
|
|
SerialProtocol_GPS2 = 6, // do not use - use GPS and provide instance of 1
|
2015-05-26 03:57:05 -03:00
|
|
|
SerialProtocol_AlexMos = 7,
|
2023-05-15 09:06:22 -03:00
|
|
|
SerialProtocol_Gimbal = 8, // SToRM32, Siyi custom serial protocols
|
2018-02-27 12:00:32 -04:00
|
|
|
SerialProtocol_Rangefinder = 9,
|
2016-05-03 18:29:30 -03:00
|
|
|
SerialProtocol_FrSky_SPort_Passthrough = 10, // FrSky SPort Passthrough (OpenTX) protocol (X-receivers)
|
2017-08-24 13:12:16 -03:00
|
|
|
SerialProtocol_Lidar360 = 11, // Lightware SF40C, TeraRanger Tower or RPLidarA2
|
2021-10-25 18:41:10 -03:00
|
|
|
SerialProtocol_Aerotenna_USD1 = 12, // USD1 support - deprecated, users should use Rangefinder
|
2017-11-02 23:27:29 -03:00
|
|
|
SerialProtocol_Beacon = 13,
|
|
|
|
SerialProtocol_Volz = 14, // Volz servo protocol
|
2018-04-02 01:21:20 -03:00
|
|
|
SerialProtocol_Sbus1 = 15,
|
2018-03-04 05:41:06 -04:00
|
|
|
SerialProtocol_ESCTelemetry = 16,
|
|
|
|
SerialProtocol_Devo_Telem = 17,
|
2018-11-23 03:40:56 -04:00
|
|
|
SerialProtocol_OpticalFlow = 18,
|
2018-12-18 23:28:18 -04:00
|
|
|
SerialProtocol_Robotis = 19,
|
2019-03-02 12:51:38 -04:00
|
|
|
SerialProtocol_NMEAOutput = 20,
|
2019-05-28 18:08:39 -03:00
|
|
|
SerialProtocol_WindVane = 21,
|
2019-04-20 03:13:23 -03:00
|
|
|
SerialProtocol_SLCAN = 22,
|
2019-08-27 04:41:21 -03:00
|
|
|
SerialProtocol_RCIN = 23,
|
2021-12-18 21:36:14 -04:00
|
|
|
SerialProtocol_EFI = 24, // EFI serial protocol
|
2019-09-06 05:23:51 -03:00
|
|
|
SerialProtocol_LTM_Telem = 25,
|
2020-01-08 16:34:07 -04:00
|
|
|
SerialProtocol_RunCam = 26,
|
2020-01-02 23:56:21 -04:00
|
|
|
SerialProtocol_Hott = 27,
|
|
|
|
SerialProtocol_Scripting = 28,
|
2020-04-18 07:55:20 -03:00
|
|
|
SerialProtocol_CRSF = 29,
|
2019-12-05 01:01:19 -04:00
|
|
|
SerialProtocol_Generator = 30,
|
2020-07-21 05:16:20 -03:00
|
|
|
SerialProtocol_Winch = 31,
|
2020-08-04 17:40:15 -03:00
|
|
|
SerialProtocol_MSP = 32,
|
2020-08-26 15:18:50 -03:00
|
|
|
SerialProtocol_DJI_FPV = 33,
|
2020-07-18 16:47:40 -03:00
|
|
|
SerialProtocol_AirSpeed = 34,
|
2020-08-21 13:23:09 -03:00
|
|
|
SerialProtocol_ADSB = 35,
|
2020-12-27 22:06:00 -04:00
|
|
|
SerialProtocol_AHRS = 36,
|
2021-01-29 15:56:47 -04:00
|
|
|
SerialProtocol_SmartAudio = 37,
|
2021-02-05 11:37:12 -04:00
|
|
|
SerialProtocol_FETtecOneWire = 38,
|
2021-07-01 01:55:47 -03:00
|
|
|
SerialProtocol_Torqeedo = 39,
|
2020-03-16 15:43:20 -03:00
|
|
|
SerialProtocol_AIS = 40,
|
2021-08-20 01:03:00 -03:00
|
|
|
SerialProtocol_CoDevESC = 41,
|
2021-07-02 06:21:39 -03:00
|
|
|
SerialProtocol_MSP_DisplayPort = 42,
|
2022-01-25 22:50:59 -04:00
|
|
|
SerialProtocol_MAVLinkHL = 43,
|
2022-06-18 13:45:46 -03:00
|
|
|
SerialProtocol_Tramp = 44,
|
2023-03-10 20:41:58 -04:00
|
|
|
SerialProtocol_DDS_XRCE = 45,
|
2020-08-26 15:18:50 -03:00
|
|
|
SerialProtocol_NumProtocols // must be the last value
|
2015-01-19 09:32:24 -04:00
|
|
|
};
|
|
|
|
|
2017-11-02 23:27:29 -03:00
|
|
|
// get singleton instance
|
2019-02-10 01:02:55 -04:00
|
|
|
static AP_SerialManager *get_singleton(void) {
|
|
|
|
return _singleton;
|
2017-11-02 23:27:29 -03:00
|
|
|
}
|
2021-07-02 06:21:39 -03:00
|
|
|
|
2015-01-19 09:32:24 -04:00
|
|
|
// init_console - initialise console at default baud rate
|
|
|
|
void init_console();
|
|
|
|
|
|
|
|
// init - initialise serial ports
|
|
|
|
void init();
|
|
|
|
|
2015-03-27 22:44:43 -03:00
|
|
|
// find_serial - searches available serial ports that allows the given protocol
|
|
|
|
// instance should be zero if searching for the first instance, 1 for the second, etc
|
2016-10-30 02:24:21 -03:00
|
|
|
// returns uart on success, nullptr if a serial port cannot be found
|
2021-12-20 00:33:32 -04:00
|
|
|
// note that the SERIALn_OPTIONS are applied if the port is found
|
2015-03-27 22:44:43 -03:00
|
|
|
AP_HAL::UARTDriver *find_serial(enum SerialProtocol protocol, uint8_t instance) const;
|
2015-01-29 00:35:42 -04:00
|
|
|
|
2021-12-20 00:33:32 -04:00
|
|
|
// have_serial - return true if we have the corresponding serial protocol configured
|
|
|
|
bool have_serial(enum SerialProtocol protocol, uint8_t instance) const;
|
|
|
|
|
2015-01-29 00:35:42 -04:00
|
|
|
// find_baudrate - searches available serial ports for the first instance that allows the given protocol
|
2015-04-06 18:47:11 -03:00
|
|
|
// instance should be zero if searching for the first instance, 1 for the second, etc
|
2015-01-29 00:35:42 -04:00
|
|
|
// returns the baudrate of that protocol on success, 0 if a serial port cannot be found
|
2015-04-06 18:47:11 -03:00
|
|
|
uint32_t find_baudrate(enum SerialProtocol protocol, uint8_t instance) const;
|
2015-01-19 09:32:24 -04:00
|
|
|
|
2020-12-29 21:04:27 -04:00
|
|
|
// find_portnum - find port number (SERIALn index) for a protocol and instance, -1 for not found
|
|
|
|
int8_t find_portnum(enum SerialProtocol protocol, uint8_t instance) const;
|
2021-07-02 06:21:39 -03:00
|
|
|
|
2018-12-19 07:24:57 -04:00
|
|
|
// get the passthru ports if enabled
|
2020-11-14 22:15:19 -04:00
|
|
|
bool get_passthru(AP_HAL::UARTDriver *&port1, AP_HAL::UARTDriver *&port2, uint8_t &timeout_s,
|
|
|
|
uint32_t &baud1, uint32_t &baud2) const;
|
2018-12-19 07:24:57 -04:00
|
|
|
|
|
|
|
// disable passthru by settings SERIAL_PASS2 to -1
|
|
|
|
void disable_passthru(void);
|
2019-01-04 05:51:05 -04:00
|
|
|
|
|
|
|
// get Serial Port
|
|
|
|
AP_HAL::UARTDriver *get_serial_by_id(uint8_t id);
|
|
|
|
|
2019-10-27 21:30:58 -03:00
|
|
|
// accessors for AP_Periph to set baudrate and type
|
|
|
|
void set_protocol_and_baud(uint8_t sernum, enum SerialProtocol protocol, uint32_t baudrate);
|
|
|
|
|
2019-11-26 20:06:04 -04:00
|
|
|
static uint32_t map_baudrate(int32_t rate);
|
|
|
|
|
2015-01-19 09:32:24 -04:00
|
|
|
// parameter var table
|
|
|
|
static const struct AP_Param::GroupInfo var_info[];
|
|
|
|
|
2022-07-11 04:57:40 -03:00
|
|
|
class UARTState {
|
|
|
|
friend class AP_SerialManager;
|
|
|
|
public:
|
|
|
|
bool option_enabled(uint16_t option) const {
|
|
|
|
return (options & option) == option;
|
|
|
|
}
|
|
|
|
// returns a baudrate such as 9600. May map from a special
|
|
|
|
// parameter value like "57" to "57600":
|
|
|
|
uint32_t baudrate() const {
|
|
|
|
return AP_SerialManager::map_baudrate(baud);
|
|
|
|
}
|
|
|
|
AP_SerialManager::SerialProtocol get_protocol() const {
|
|
|
|
return AP_SerialManager::SerialProtocol(protocol.get());
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
AP_Int32 baud;
|
|
|
|
AP_Int16 options;
|
|
|
|
AP_Int8 protocol;
|
|
|
|
};
|
|
|
|
|
|
|
|
// search through managed serial connections looking for the
|
|
|
|
// instance-nth UART which is running protocol protocol.
|
|
|
|
// protocol_match is used to determine equivalence of one protocol
|
|
|
|
// to another, e.g. MAVLink2 is considered MAVLink1 for finding
|
|
|
|
// mavlink1 protocol instances.
|
|
|
|
const UARTState *find_protocol_instance(enum SerialProtocol protocol,
|
|
|
|
uint8_t instance) const;
|
|
|
|
|
2015-01-19 09:32:24 -04:00
|
|
|
private:
|
2019-02-10 01:02:55 -04:00
|
|
|
static AP_SerialManager *_singleton;
|
2021-07-02 06:21:39 -03:00
|
|
|
|
2022-02-21 23:19:15 -04:00
|
|
|
// array of uart info. See comment above about
|
|
|
|
// SERIALMANAGER_MAX_PORTS
|
2022-07-11 04:57:40 -03:00
|
|
|
UARTState state[SERIALMANAGER_MAX_PORTS];
|
2015-01-29 00:47:36 -04:00
|
|
|
|
2018-12-19 07:24:57 -04:00
|
|
|
// pass-through serial support
|
|
|
|
AP_Int8 passthru_port1;
|
|
|
|
AP_Int8 passthru_port2;
|
|
|
|
AP_Int8 passthru_timeout;
|
|
|
|
|
2015-03-27 22:44:43 -03:00
|
|
|
// protocol_match - returns true if the protocols match
|
|
|
|
bool protocol_match(enum SerialProtocol protocol1, enum SerialProtocol protocol2) const;
|
2018-11-10 05:45:01 -04:00
|
|
|
|
|
|
|
// setup any special options
|
2020-01-02 21:51:02 -04:00
|
|
|
void set_options(uint16_t i);
|
2020-12-10 20:56:28 -04:00
|
|
|
|
|
|
|
bool init_console_done;
|
2015-01-19 09:32:24 -04:00
|
|
|
};
|
2017-11-20 21:10:02 -04:00
|
|
|
|
|
|
|
namespace AP {
|
|
|
|
AP_SerialManager &serialmanager();
|
|
|
|
};
|