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
*/
2024-02-27 07:54:14 -04:00
# include "AP_SerialManager_config.h"
# if AP_SERIALMANAGER_ENABLED
2015-08-11 03:28:45 -03:00
# include <AP_HAL/AP_HAL.h>
2019-03-22 22:21:52 -03:00
# include <AP_Math/AP_Math.h>
2019-08-27 04:41:21 -03:00
# include <AP_RCProtocol/AP_RCProtocol.h>
2020-08-04 17:40:15 -03:00
# include <AP_MSP/AP_MSP.h>
2023-11-27 23:18:14 -04:00
# include <AP_InertialSensor/AP_InertialSensor.h>
2015-08-11 03:28:45 -03:00
# include "AP_SerialManager.h"
2023-05-10 20:27:13 -03:00
# include <GCS_MAVLink/GCS.h>
2015-01-19 09:32:24 -04:00
extern const AP_HAL : : HAL & hal ;
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL0_PROTOCOL
# define DEFAULT_SERIAL0_PROTOCOL SerialProtocol_MAVLink2
# endif
# ifndef DEFAULT_SERIAL0_BAUD
# define DEFAULT_SERIAL0_BAUD AP_SERIALMANAGER_CONSOLE_BAUD
2023-04-08 14:34:08 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL0_PROTOCOL
# error "Please use DEFAULT_SERIAL0_PROTOCOL"
# endif
2023-04-08 14:34:08 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL1_PROTOCOL
# define DEFAULT_SERIAL1_PROTOCOL SerialProtocol_MAVLink2
# endif
# ifndef DEFAULT_SERIAL1_BAUD
# define DEFAULT_SERIAL1_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL1_OPTIONS
# define DEFAULT_SERIAL1_OPTIONS 0
2019-08-27 04:41:21 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL1_PROTOCOL
# error "Please use DEFAULT_SERIAL1_PROTOCOL"
# endif
2019-08-27 04:41:21 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL2_PROTOCOL
# define DEFAULT_SERIAL2_PROTOCOL SerialProtocol_MAVLink2
# endif
# ifndef DEFAULT_SERIAL2_BAUD
# define DEFAULT_SERIAL2_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL2_OPTIONS
# define DEFAULT_SERIAL2_OPTIONS 0
2019-08-27 04:41:21 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL2_PROTOCOL
# error "Please use DEFAULT_SERIAL2_PROTOCOL"
# endif
2019-08-27 04:41:21 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL3_PROTOCOL
# define DEFAULT_SERIAL3_PROTOCOL SerialProtocol_GPS
# endif
# ifndef DEFAULT_SERIAL3_BAUD
# define DEFAULT_SERIAL3_BAUD AP_SERIALMANAGER_GPS_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL3_OPTIONS
# define DEFAULT_SERIAL3_OPTIONS 0
2019-08-27 04:41:21 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL3_PROTOCOL
# error "Please use DEFAULT_SERIAL3_PROTOCOL"
# endif
2019-08-27 04:41:21 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL4_PROTOCOL
# define DEFAULT_SERIAL4_PROTOCOL SerialProtocol_GPS
# endif
# ifndef DEFAULT_SERIAL4_BAUD
# define DEFAULT_SERIAL4_BAUD AP_SERIALMANAGER_GPS_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL4_OPTIONS
# define DEFAULT_SERIAL4_OPTIONS 0
2016-04-13 08:24:42 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL4_PROTOCOL
# error "Please use DEFAULT_SERIAL4_PROTOCOL"
# endif
2016-04-13 08:24:42 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL5_PROTOCOL
# define DEFAULT_SERIAL5_PROTOCOL SerialProtocol_None
# endif
# ifndef DEFAULT_SERIAL5_BAUD
# define DEFAULT_SERIAL5_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL5_OPTIONS
# define DEFAULT_SERIAL5_OPTIONS 0
2018-06-27 08:33:24 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL5_PROTOCOL
# error "Please use DEFAULT_SERIAL5_PROTOCOL"
# endif
2018-06-27 08:33:24 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL6_PROTOCOL
# define DEFAULT_SERIAL6_PROTOCOL SerialProtocol_None
# endif
# ifndef DEFAULT_SERIAL6_BAUD
# define DEFAULT_SERIAL6_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL6_OPTIONS
# define DEFAULT_SERIAL6_OPTIONS 0
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL6_PROTOCOL
# error "Please use DEFAULT_SERIAL6_PROTOCOL"
# endif
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL7_PROTOCOL
# define DEFAULT_SERIAL7_PROTOCOL SerialProtocol_None
# endif
# ifndef DEFAULT_SERIAL7_BAUD
# define DEFAULT_SERIAL7_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL7_OPTIONS
# define DEFAULT_SERIAL7_OPTIONS 0
2019-07-12 01:59:02 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL7_PROTOCOL
# error "Please use DEFAULT_SERIAL7_PROTOCOL"
# endif
2019-07-12 01:59:02 -03:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL8_PROTOCOL
# define DEFAULT_SERIAL8_PROTOCOL SerialProtocol_None
# endif
# ifndef DEFAULT_SERIAL8_BAUD
# define DEFAULT_SERIAL8_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL8_OPTIONS
# define DEFAULT_SERIAL8_OPTIONS 0
2020-11-26 02:49:00 -04:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL8_PROTOCOL
# error "Please use DEFAULT_SERIAL8_PROTOCOL"
# endif
2020-11-26 02:49:00 -04:00
2023-05-03 21:30:51 -03:00
# ifndef DEFAULT_SERIAL9_PROTOCOL
# define DEFAULT_SERIAL9_PROTOCOL SerialProtocol_None
# endif
# ifndef DEFAULT_SERIAL9_BAUD
# define DEFAULT_SERIAL9_BAUD AP_SERIALMANAGER_MAVLINK_BAUD / 1000
# endif
# ifndef DEFAULT_SERIAL9_OPTIONS
# define DEFAULT_SERIAL9_OPTIONS 0
2021-11-05 00:17:40 -03:00
# endif
2023-07-06 09:25:30 -03:00
# ifdef HAL_SERIAL9_PROTOCOL
# error "Please use DEFAULT_SERIAL9_PROTOCOL"
# endif
2021-11-05 00:17:40 -03:00
2015-10-25 14:03:46 -03:00
const AP_Param : : GroupInfo AP_SerialManager : : var_info [ ] = {
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL0
2015-01-19 09:32:24 -04:00
// @Param: 0_BAUD
// @DisplayName: Serial0 baud rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate used on the USB console. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2022-12-29 08:47:33 -04:00
// @Values: 1:1200,2:2400,4:4800,9:9600,19:19200,38:38400,57:57600,111:111100,115:115200,230:230400,256:256000,460:460800,500:500000,921:921600,1500:1500000,2000:2000000
2015-01-19 09:32:24 -04:00
// @User: Standard
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 0_BAUD " , 0 , AP_SerialManager , state [ 0 ] . baud , DEFAULT_SERIAL0_BAUD / 1000 ) ,
2015-01-19 09:32:24 -04:00
2016-05-16 00:34:32 -03:00
// @Param: 0_PROTOCOL
// @DisplayName: Console protocol selection
// @Description: Control what protocol to use on the console.
2024-05-19 21:09:58 -03:00
// @Values: 1:MAVLink1, 2:MAVLink2
2016-05-16 00:34:32 -03:00
// @User: Standard
2017-05-12 10:36:55 -03:00
// @RebootRequired: True
2018-07-18 00:19:38 -03:00
AP_GROUPINFO ( " 0_PROTOCOL " , 11 , AP_SerialManager , state [ 0 ] . protocol , SerialProtocol_MAVLink2 ) ,
2019-11-14 23:50:57 -04:00
# endif
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL1
2015-01-19 09:32:24 -04:00
// @Param: 1_PROTOCOL
// @DisplayName: Telem1 protocol selection
2015-02-23 19:19:42 -04:00
// @Description: Control what protocol to use on the Telem1 port. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-11-27 23:18:14 -04:00
// @Values: -1:None, 1:MAVLink1, 2:MAVLink2, 3:Frsky D, 4:Frsky SPort, 5:GPS, 7:Alexmos Gimbal Serial, 8:Gimbal, 9:Rangefinder, 10:FrSky SPort Passthrough (OpenTX), 11:Lidar360, 13:Beacon, 14:Volz servo out, 15:SBus servo out, 16:ESC Telemetry, 17:Devo Telemetry, 18:OpticalFlow, 19:RobotisServo, 20:NMEA Output, 21:WindVane, 22:SLCAN, 23:RCIN, 24:EFI Serial, 25:LTM, 26:RunCam, 27:HottTelem, 28:Scripting, 29:Crossfire VTX, 30:Generator, 31:Winch, 32:MSP, 33:DJI FPV, 34:AirSpeed, 35:ADSB, 36:AHRS, 37:SmartAudio, 38:FETtecOneWire, 39:Torqeedo, 40:AIS, 41:CoDevESC, 42:DisplayPort, 43:MAVLink High Latency, 44:IRC Tramp, 45:DDS XRCE, 46:IMUDATA
2015-01-19 09:32:24 -04:00
// @User: Standard
2017-05-12 10:36:55 -03:00
// @RebootRequired: True
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 1_PROTOCOL " , 1 , AP_SerialManager , state [ 1 ] . protocol , DEFAULT_SERIAL1_PROTOCOL ) ,
2015-01-19 09:32:24 -04:00
// @Param: 1_BAUD
// @DisplayName: Telem1 Baud Rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate used on the Telem1 port. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2022-12-29 08:47:33 -04:00
// @Values: 1:1200,2:2400,4:4800,9:9600,19:19200,38:38400,57:57600,111:111100,115:115200,230:230400,256:256000,460:460800,500:500000,921:921600,1500:1500000,2000:2000000
2015-01-19 09:32:24 -04:00
// @User: Standard
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 1_BAUD " , 2 , AP_SerialManager , state [ 1 ] . baud , DEFAULT_SERIAL1_BAUD ) ,
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
# if HAL_HAVE_SERIAL2
2015-01-19 09:32:24 -04:00
// @Param: 2_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2015-01-19 09:32:24 -04:00
// @DisplayName: Telemetry 2 protocol selection
2015-02-23 19:19:42 -04:00
// @Description: Control what protocol to use on the Telem2 port. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 2_PROTOCOL " , 3 , AP_SerialManager , state [ 2 ] . protocol , DEFAULT_SERIAL2_PROTOCOL ) ,
2015-01-19 09:32:24 -04:00
// @Param: 2_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2015-01-19 09:32:24 -04:00
// @DisplayName: Telemetry 2 Baud Rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate of the Telem2 port. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 2_BAUD " , 4 , AP_SerialManager , state [ 2 ] . baud , DEFAULT_SERIAL2_BAUD ) ,
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
# if HAL_HAVE_SERIAL3
2015-01-19 09:32:24 -04:00
// @Param: 3_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2015-01-19 09:32:24 -04:00
// @DisplayName: Serial 3 (GPS) protocol selection
2015-02-23 19:19:42 -04:00
// @Description: Control what protocol Serial 3 (GPS) should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 3_PROTOCOL " , 5 , AP_SerialManager , state [ 3 ] . protocol , DEFAULT_SERIAL3_PROTOCOL ) ,
2015-01-19 09:32:24 -04:00
// @Param: 3_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2015-01-19 09:32:24 -04:00
// @DisplayName: Serial 3 (GPS) Baud Rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate used for the Serial 3 (GPS). Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 3_BAUD " , 6 , AP_SerialManager , state [ 3 ] . baud , DEFAULT_SERIAL3_BAUD ) ,
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
# if HAL_HAVE_SERIAL4
2015-01-19 09:32:24 -04:00
// @Param: 4_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2015-01-19 09:32:24 -04:00
// @DisplayName: Serial4 protocol selection
2015-02-23 19:19:42 -04:00
// @Description: Control what protocol Serial4 port should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 4_PROTOCOL " , 7 , AP_SerialManager , state [ 4 ] . protocol , DEFAULT_SERIAL4_PROTOCOL ) ,
2015-01-19 09:32:24 -04:00
// @Param: 4_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2015-01-19 09:32:24 -04:00
// @DisplayName: Serial 4 Baud Rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate used for Serial4. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 4_BAUD " , 8 , AP_SerialManager , state [ 4 ] . baud , DEFAULT_SERIAL4_BAUD ) ,
2019-11-14 23:50:57 -04:00
# endif
2016-04-19 00:48:57 -03:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL5
2016-04-19 00:48:57 -03:00
// @Param: 5_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2016-04-19 21:03:45 -03:00
// @DisplayName: Serial5 protocol selection
// @Description: Control what protocol Serial5 port should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 5_PROTOCOL " , 9 , AP_SerialManager , state [ 5 ] . protocol , DEFAULT_SERIAL5_PROTOCOL ) ,
2016-04-19 00:48:57 -03:00
// @Param: 5_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2016-04-19 21:03:45 -03:00
// @DisplayName: Serial 5 Baud Rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate used for Serial5. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 5_BAUD " , 10 , AP_SerialManager , state [ 5 ] . baud , DEFAULT_SERIAL5_BAUD ) ,
2019-11-14 23:50:57 -04:00
# endif
2015-01-19 09:32:24 -04:00
2016-05-16 00:34:32 -03:00
// index 11 used by 0_PROTOCOL
2018-06-27 08:33:24 -03:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL6
2018-06-27 08:33:24 -03:00
// @Param: 6_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2018-06-27 08:33:24 -03:00
// @DisplayName: Serial6 protocol selection
// @Description: Control what protocol Serial6 port should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 6_PROTOCOL " , 12 , AP_SerialManager , state [ 6 ] . protocol , DEFAULT_SERIAL6_PROTOCOL ) ,
2018-06-27 08:33:24 -03:00
// @Param: 6_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2018-06-27 08:33:24 -03:00
// @DisplayName: Serial 6 Baud Rate
2019-01-19 02:32:03 -04:00
// @Description: The baud rate used for Serial6. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 6_BAUD " , 13 , AP_SerialManager , state [ 6 ] . baud , DEFAULT_SERIAL6_BAUD ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL1
2018-11-10 05:45:01 -04:00
// @Param: 1_OPTIONS
// @DisplayName: Telem1 options
2018-11-20 03:26:42 -04:00
// @Description: Control over UART options. The InvertRX option controls invert of the receive pin. The InvertTX option controls invert of the transmit pin. The HalfDuplex option controls half-duplex (onewire) mode, where both transmit and receive is done on the transmit wire. The Swap option allows the RX and TX pins to be swapped on STM32F7 based boards.
2023-05-15 09:12:24 -03:00
// @Bitmask: 0:InvertRX, 1:InvertTX, 2:HalfDuplex, 3:SwapTXRX, 4: RX_PullDown, 5: RX_PullUp, 6: TX_PullDown, 7: TX_PullUp, 8: RX_NoDMA, 9: TX_NoDMA, 10: Don't forward mavlink to/from, 11: DisableFIFO, 12: Ignore Streamrate
2018-11-10 05:45:01 -04:00
// @User: Advanced
// @RebootRequired: True
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 1_OPTIONS " , 14 , AP_SerialManager , state [ 1 ] . options , DEFAULT_SERIAL1_OPTIONS ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL2
2018-11-10 05:45:01 -04:00
// @Param: 2_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2018-11-10 05:45:01 -04:00
// @DisplayName: Telem2 options
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 2_OPTIONS " , 15 , AP_SerialManager , state [ 2 ] . options , DEFAULT_SERIAL2_OPTIONS ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL3
2018-11-10 05:45:01 -04:00
// @Param: 3_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2018-11-10 05:45:01 -04:00
// @DisplayName: Serial3 options
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 3_OPTIONS " , 16 , AP_SerialManager , state [ 3 ] . options , DEFAULT_SERIAL3_OPTIONS ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL4
2018-11-10 05:45:01 -04:00
// @Param: 4_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2018-11-10 05:45:01 -04:00
// @DisplayName: Serial4 options
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 4_OPTIONS " , 17 , AP_SerialManager , state [ 4 ] . options , DEFAULT_SERIAL4_OPTIONS ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL5
2018-11-10 05:45:01 -04:00
// @Param: 5_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2018-11-10 05:45:01 -04:00
// @DisplayName: Serial5 options
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 5_OPTIONS " , 18 , AP_SerialManager , state [ 5 ] . options , DEFAULT_SERIAL5_OPTIONS ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL6
2018-11-10 05:45:01 -04:00
// @Param: 6_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2018-11-10 05:45:01 -04:00
// @DisplayName: Serial6 options
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 6_OPTIONS " , 19 , AP_SerialManager , state [ 6 ] . options , DEFAULT_SERIAL6_OPTIONS ) ,
2019-11-14 23:50:57 -04:00
# endif
2018-11-10 05:45:01 -04:00
2018-12-19 07:24:57 -04:00
// @Param: _PASS1
// @DisplayName: Serial passthru first port
// @Description: This sets one side of pass-through between two serial ports. Once both sides are set then all data received on either port will be passed to the other port
2018-12-29 00:15:45 -04:00
// @Values: -1:Disabled,0:Serial0,1:Serial1,2:Serial2,3:Serial3,4:Serial4,5:Serial5,6:Serial6
2018-12-19 07:24:57 -04:00
// @User: Advanced
AP_GROUPINFO ( " _PASS1 " , 20 , AP_SerialManager , passthru_port1 , 0 ) ,
// @Param: _PASS2
// @DisplayName: Serial passthru second port
// @Description: This sets one side of pass-through between two serial ports. Once both sides are set then all data received on either port will be passed to the other port
2018-12-29 00:15:45 -04:00
// @Values: -1:Disabled,0:Serial0,1:Serial1,2:Serial2,3:Serial3,4:Serial4,5:Serial5,6:Serial6
2018-12-19 07:24:57 -04:00
// @User: Advanced
AP_GROUPINFO ( " _PASS2 " , 21 , AP_SerialManager , passthru_port2 , - 1 ) ,
// @Param: _PASSTIMO
// @DisplayName: Serial passthru timeout
// @Description: This sets a timeout for serial pass-through in seconds. When the pass-through is enabled by setting the SERIAL_PASS1 and SERIAL_PASS2 parameters then it remains in effect until no data comes from the first port for SERIAL_PASSTIMO seconds. This allows the port to revent to its normal usage (such as MAVLink connection to a GCS) when it is no longer needed. A value of 0 means no timeout.
// @Range: 0 120
// @Units: s
// @User: Advanced
AP_GROUPINFO ( " _PASSTIMO " , 22 , AP_SerialManager , passthru_timeout , 15 ) ,
2019-07-12 01:59:02 -03:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL7
2019-07-12 01:59:02 -03:00
// @Param: 7_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2019-07-12 01:59:02 -03:00
// @DisplayName: Serial7 protocol selection
// @Description: Control what protocol Serial7 port should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 7_PROTOCOL " , 23 , AP_SerialManager , state [ 7 ] . protocol , DEFAULT_SERIAL7_PROTOCOL ) ,
2019-07-12 01:59:02 -03:00
// @Param: 7_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2019-07-12 01:59:02 -03:00
// @DisplayName: Serial 7 Baud Rate
// @Description: The baud rate used for Serial7. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 7_BAUD " , 24 , AP_SerialManager , state [ 7 ] . baud , DEFAULT_SERIAL7_BAUD ) ,
2019-07-12 01:59:02 -03:00
// @Param: 7_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2019-07-12 01:59:02 -03:00
// @DisplayName: Serial7 options
AP_GROUPINFO ( " 7_OPTIONS " , 25 , AP_SerialManager , state [ 7 ] . options , 0 ) ,
2019-11-14 23:50:57 -04:00
# endif
2020-11-26 02:49:00 -04:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL8
2020-11-26 02:49:00 -04:00
// @Param: 8_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2020-11-26 02:49:00 -04:00
// @DisplayName: Serial8 protocol selection
// @Description: Control what protocol Serial8 port should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 8_PROTOCOL " , 26 , AP_SerialManager , state [ 8 ] . protocol , DEFAULT_SERIAL8_PROTOCOL ) ,
2020-11-26 02:49:00 -04:00
// @Param: 8_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2020-11-26 02:49:00 -04:00
// @DisplayName: Serial 8 Baud Rate
2021-09-26 10:42:27 -03:00
// @Description: The baud rate used for Serial8. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 8_BAUD " , 27 , AP_SerialManager , state [ 8 ] . baud , DEFAULT_SERIAL8_BAUD ) ,
2020-11-26 02:49:00 -04:00
// @Param: 8_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2020-11-26 02:49:00 -04:00
// @DisplayName: Serial8 options
AP_GROUPINFO ( " 8_OPTIONS " , 28 , AP_SerialManager , state [ 8 ] . options , 0 ) ,
# endif
2021-11-05 00:17:40 -03:00
2023-09-24 14:39:37 -03:00
# if HAL_HAVE_SERIAL9
2021-11-05 00:17:40 -03:00
// @Param: 9_PROTOCOL
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_PROTOCOL
2021-11-05 00:17:40 -03:00
// @DisplayName: Serial9 protocol selection
// @Description: Control what protocol Serial9 port should be used for. Note that the Frsky options require external converter hardware. See the wiki for details.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 9_PROTOCOL " , 29 , AP_SerialManager , state [ 9 ] . protocol , DEFAULT_SERIAL9_PROTOCOL ) ,
2021-11-05 00:17:40 -03:00
// @Param: 9_BAUD
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_BAUD
2021-11-05 00:17:40 -03:00
// @DisplayName: Serial 9 Baud Rate
// @Description: The baud rate used for Serial8. Most stm32-based boards can support rates of up to 1500. If you setup a rate you cannot support and then can't connect to your board you should load a firmware from a different vehicle type. That will reset all your parameters to defaults.
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 9_BAUD " , 30 , AP_SerialManager , state [ 9 ] . baud , DEFAULT_SERIAL9_BAUD ) ,
2021-11-05 00:17:40 -03:00
// @Param: 9_OPTIONS
2022-12-29 22:10:57 -04:00
// @CopyFieldsFrom: SERIAL1_OPTIONS
2021-11-05 00:17:40 -03:00
// @DisplayName: Serial9 options
2023-05-03 21:30:51 -03:00
AP_GROUPINFO ( " 9_OPTIONS " , 31 , AP_SerialManager , state [ 9 ] . options , DEFAULT_SERIAL9_OPTIONS ) ,
2021-11-05 00:17:40 -03:00
# endif
2022-12-29 22:10:57 -04:00
2015-01-19 09:32:24 -04:00
AP_GROUPEND
} ;
2017-11-02 23:27:29 -03:00
// singleton instance
2019-02-10 01:02:55 -04:00
AP_SerialManager * AP_SerialManager : : _singleton ;
2017-11-02 23:27:29 -03:00
2015-01-19 09:32:24 -04:00
// Constructor
AP_SerialManager : : AP_SerialManager ( )
{
2019-02-10 01:02:55 -04:00
_singleton = this ;
2015-01-19 09:32:24 -04:00
// setup parameter defaults
AP_Param : : setup_object_defaults ( this , var_info ) ;
}
// init_console - initialise console at default baud rate
void AP_SerialManager : : init_console ( )
{
// initialise console immediately at default size and baud
2019-11-14 23:50:57 -04:00
# if SERIALMANAGER_NUM_PORTS > 0
2020-12-10 20:56:28 -04:00
if ( ! init_console_done ) {
init_console_done = true ;
2023-05-03 21:30:51 -03:00
hal . serial ( 0 ) - > begin ( DEFAULT_SERIAL0_BAUD ,
2020-12-10 20:56:28 -04:00
AP_SERIALMANAGER_CONSOLE_BUFSIZE_RX ,
AP_SERIALMANAGER_CONSOLE_BUFSIZE_TX ) ;
}
2019-11-14 23:50:57 -04:00
# endif
2015-01-19 09:32:24 -04:00
}
// init - // init - initialise serial ports
void AP_SerialManager : : init ( )
{
2018-12-19 07:24:57 -04:00
// always reset passthru port2 on boot
passthru_port2 . set_and_save_ifchanged ( - 1 ) ;
2020-01-07 18:08:17 -04:00
# ifdef HAL_OTG1_CONFIG
/*
prevent users from changing USB protocol to other than
MAVLink . This fixes an issue where users trying to get SLCAN
change SERIAL0_PROTOCOL to 22 and find they can no longer connect
*/
if ( state [ 0 ] . protocol ! = SerialProtocol_MAVLink & &
state [ 0 ] . protocol ! = SerialProtocol_MAVLink2 ) {
state [ 0 ] . protocol . set ( SerialProtocol_MAVLink2 ) ;
}
# endif
2020-12-10 20:56:28 -04:00
init_console ( ) ;
2015-01-19 09:32:24 -04:00
// initialise serial ports
for ( uint8_t i = 1 ; i < SERIALMANAGER_NUM_PORTS ; i + + ) {
2020-12-10 20:56:28 -04:00
auto * uart = hal . serial ( i ) ;
2017-08-13 02:47:26 -03:00
2023-11-15 23:41:16 -04:00
state [ i ] . idx = i ;
2020-12-10 20:56:28 -04:00
if ( uart ! = nullptr ) {
2021-10-03 14:55:32 -03:00
set_options ( i ) ;
2015-01-19 09:32:24 -04:00
switch ( state [ i ] . protocol ) {
2016-04-19 00:48:57 -03:00
case SerialProtocol_None :
2021-12-21 04:12:58 -04:00
# if HAL_GCS_ENABLED
2021-11-05 01:34:12 -03:00
// disable RX and TX pins in case they are shared
2021-12-21 04:12:58 -04:00
// with another peripheral (eg. RCIN pin). We
// don't do this if GCS is not enabled as in that
// case we don't have serialmanager parameters and
// this would prevent AP_Periph from using a GPS
2021-11-05 01:34:12 -03:00
uart - > disable_rxtx ( ) ;
2021-12-21 04:12:58 -04:00
# endif
2016-04-19 00:48:57 -03:00
break ;
2015-01-19 09:32:24 -04:00
case SerialProtocol_Console :
2015-03-27 22:44:43 -03:00
case SerialProtocol_MAVLink :
2015-01-19 09:32:24 -04:00
case SerialProtocol_MAVLink2 :
2022-01-25 22:50:59 -04:00
case SerialProtocol_MAVLinkHL :
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2015-01-29 00:35:42 -04:00
AP_SERIALMANAGER_MAVLINK_BUFSIZE_RX ,
AP_SERIALMANAGER_MAVLINK_BUFSIZE_TX ) ;
2015-01-19 09:32:24 -04:00
break ;
2016-05-03 15:35:45 -03:00
case SerialProtocol_FrSky_D :
2016-04-08 16:31:13 -03:00
// Note baudrate is hardcoded to 9600
2022-07-04 23:35:31 -03:00
state [ i ] . baud . set_and_default ( AP_SERIALMANAGER_FRSKY_D_BAUD / 1000 ) ; // update baud param in case user looks at it
2016-05-03 15:35:45 -03:00
// begin is handled by AP_Frsky_telem library
2015-01-19 09:32:24 -04:00
break ;
2016-05-03 15:35:45 -03:00
case SerialProtocol_FrSky_SPort :
2016-05-03 18:29:30 -03:00
case SerialProtocol_FrSky_SPort_Passthrough :
2016-04-08 16:31:13 -03:00
// Note baudrate is hardcoded to 57600
2022-07-04 23:35:31 -03:00
state [ i ] . baud . set_and_default ( AP_SERIALMANAGER_FRSKY_SPORT_BAUD / 1000 ) ; // update baud param in case user looks at it
2015-01-27 07:52:37 -04:00
// begin is handled by AP_Frsky_telem library
2015-01-19 09:32:24 -04:00
break ;
case SerialProtocol_GPS :
case SerialProtocol_GPS2 :
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2015-01-29 00:35:42 -04:00
AP_SERIALMANAGER_GPS_BUFSIZE_RX ,
AP_SERIALMANAGER_GPS_BUFSIZE_TX ) ;
2015-01-19 09:32:24 -04:00
break ;
case SerialProtocol_AlexMos :
// Note baudrate is hardcoded to 115200
2022-07-04 23:35:31 -03:00
state [ i ] . baud . set_and_default ( AP_SERIALMANAGER_ALEXMOS_BAUD / 1000 ) ; // update baud param in case user looks at it
2020-12-10 20:56:28 -04:00
uart - > begin ( AP_SERIALMANAGER_ALEXMOS_BAUD ,
2015-01-29 00:35:42 -04:00
AP_SERIALMANAGER_ALEXMOS_BUFSIZE_RX ,
AP_SERIALMANAGER_ALEXMOS_BUFSIZE_TX ) ;
2015-01-19 09:32:24 -04:00
break ;
2023-05-15 09:06:22 -03:00
case SerialProtocol_Gimbal :
2015-05-26 03:57:05 -03:00
// Note baudrate is hardcoded to 115200
2023-05-15 09:06:22 -03:00
state [ i ] . baud . set_and_default ( AP_SERIALMANAGER_GIMBAL_BAUD / 1000 ) ; // update baud param in case user looks at it
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2023-05-15 09:06:22 -03:00
AP_SERIALMANAGER_GIMBAL_BUFSIZE_RX ,
AP_SERIALMANAGER_GIMBAL_BUFSIZE_TX ) ;
2015-05-26 03:57:05 -03:00
break ;
2021-10-25 18:41:10 -03:00
case SerialProtocol_Aerotenna_USD1 :
2018-02-27 12:00:32 -04:00
state [ i ] . protocol . set_and_save ( SerialProtocol_Rangefinder ) ;
2016-11-14 17:46:11 -04:00
break ;
2017-11-02 23:27:29 -03:00
case SerialProtocol_Volz :
// Note baudrate is hardcoded to 115200
2022-07-04 23:35:31 -03:00
state [ i ] . baud . set_and_default ( AP_SERIALMANAGER_VOLZ_BAUD ) ; // update baud param in case user looks at it
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2017-11-02 23:27:29 -03:00
AP_SERIALMANAGER_VOLZ_BUFSIZE_RX ,
AP_SERIALMANAGER_VOLZ_BUFSIZE_TX ) ;
2020-12-10 20:56:28 -04:00
uart - > set_unbuffered_writes ( true ) ;
uart - > set_flow_control ( AP_HAL : : UARTDriver : : FLOW_CONTROL_DISABLE ) ;
2017-11-02 23:27:29 -03:00
break ;
2017-08-21 17:31:51 -03:00
case SerialProtocol_Sbus1 :
2022-07-04 23:35:31 -03:00
state [ i ] . baud . set_and_default ( AP_SERIALMANAGER_SBUS1_BAUD / 1000 ) ; // update baud param in case user looks at it
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2017-08-21 17:31:51 -03:00
AP_SERIALMANAGER_SBUS1_BUFSIZE_RX ,
AP_SERIALMANAGER_SBUS1_BUFSIZE_TX ) ;
2020-12-10 20:56:28 -04:00
uart - > configure_parity ( 2 ) ; // enable even parity
uart - > set_stop_bits ( 2 ) ;
uart - > set_unbuffered_writes ( true ) ;
uart - > set_flow_control ( AP_HAL : : UARTDriver : : FLOW_CONTROL_DISABLE ) ;
2017-08-21 17:31:51 -03:00
break ;
2018-04-02 01:21:20 -03:00
case SerialProtocol_ESCTelemetry :
// ESC telemetry protocol from BLHeli32 ESCs. Note that baudrate is hardcoded to 115200
2022-07-04 23:35:31 -03:00
state [ i ] . baud . set_and_default ( 115200 / 1000 ) ;
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) , 30 , 30 ) ;
2020-12-10 20:56:28 -04:00
uart - > set_flow_control ( AP_HAL : : UARTDriver : : FLOW_CONTROL_DISABLE ) ;
2018-04-02 01:21:20 -03:00
break ;
2018-12-18 23:28:18 -04:00
case SerialProtocol_Robotis :
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2018-12-18 23:28:18 -04:00
AP_SERIALMANAGER_ROBOTIS_BUFSIZE_RX ,
AP_SERIALMANAGER_ROBOTIS_BUFSIZE_TX ) ;
2020-12-10 20:56:28 -04:00
uart - > set_unbuffered_writes ( true ) ;
uart - > set_flow_control ( AP_HAL : : UARTDriver : : FLOW_CONTROL_DISABLE ) ;
2018-12-18 23:28:18 -04:00
break ;
2019-03-02 12:51:38 -04:00
2019-04-20 03:13:23 -03:00
case SerialProtocol_SLCAN :
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2019-04-20 03:13:23 -03:00
AP_SERIALMANAGER_SLCAN_BUFSIZE_RX ,
AP_SERIALMANAGER_SLCAN_BUFSIZE_TX ) ;
break ;
2023-05-03 10:08:00 -03:00
# if AP_RCPROTOCOL_ENABLED
2019-08-27 04:41:21 -03:00
case SerialProtocol_RCIN :
2022-10-21 15:50:45 -03:00
if ( ! AP : : RC ( ) . has_uart ( ) ) {
2022-10-15 10:16:35 -03:00
AP : : RC ( ) . add_uart ( uart ) ;
}
2022-10-21 15:50:45 -03:00
2019-08-27 04:41:21 -03:00
break ;
2019-08-27 08:08:34 -03:00
# endif
2019-08-27 04:41:21 -03:00
2021-12-18 21:36:14 -04:00
case SerialProtocol_EFI :
state [ i ] . baud . set_default ( AP_SERIALMANAGER_EFI_MS_BAUD ) ;
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2019-11-08 19:30:39 -04:00
AP_SERIALMANAGER_EFI_MS_BUFSIZE_RX ,
AP_SERIALMANAGER_EFI_MS_BUFSIZE_TX ) ;
2020-12-10 20:56:28 -04:00
uart - > set_flow_control ( AP_HAL : : UARTDriver : : FLOW_CONTROL_DISABLE ) ;
2019-11-08 19:30:39 -04:00
break ;
2019-12-05 01:01:19 -04:00
case SerialProtocol_Generator :
break ;
2020-08-04 17:40:15 -03:00
# if HAL_MSP_ENABLED
case SerialProtocol_MSP :
case SerialProtocol_DJI_FPV :
2021-07-02 06:21:39 -03:00
case SerialProtocol_MSP_DisplayPort :
2020-09-01 17:40:22 -03:00
// baudrate defaults to 115200
state [ i ] . baud . set_default ( AP_SERIALMANAGER_MSP_BAUD / 1000 ) ;
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ,
2020-09-01 17:40:22 -03:00
AP_SERIALMANAGER_MSP_BUFSIZE_RX ,
AP_SERIALMANAGER_MSP_BUFSIZE_TX ) ;
2020-12-10 20:56:28 -04:00
uart - > set_flow_control ( AP_HAL : : UARTDriver : : FLOW_CONTROL_DISABLE ) ;
2020-08-04 17:40:15 -03:00
// Note init is handled by AP_MSP
break ;
# endif
2023-11-27 23:18:14 -04:00
# if AP_SERIALMANAGER_IMUOUT_ENABLED
case SerialProtocol_IMUOUT :
uart - > begin ( state [ i ] . baudrate ( ) ,
AP_SERIALMANAGER_IMUOUT_BUFSIZE_RX ,
AP_SERIALMANAGER_IMUOUT_BUFSIZE_TX ) ;
AP : : ins ( ) . set_imu_out_uart ( uart ) ;
uart - > set_unbuffered_writes ( true ) ;
break ;
# endif
2023-12-09 16:11:24 -04:00
# if AP_NETWORKING_BACKEND_PPP
case SerialProtocol_PPP :
uart - > begin ( state [ i ] . baudrate ( ) ,
AP_SERIALMANAGER_PPP_BUFSIZE_RX ,
AP_SERIALMANAGER_PPP_BUFSIZE_TX ) ;
break ;
# endif
2019-03-02 12:51:38 -04:00
default :
2022-07-11 04:57:40 -03:00
uart - > begin ( state [ i ] . baudrate ( ) ) ;
2015-01-19 09:32:24 -04:00
}
}
}
}
2018-04-18 03:13:02 -03:00
const AP_SerialManager : : UARTState * AP_SerialManager : : find_protocol_instance ( enum SerialProtocol protocol , uint8_t instance ) const
2015-01-19 09:32:24 -04:00
{
2015-03-27 22:44:43 -03:00
uint8_t found_instance = 0 ;
2015-01-19 09:32:24 -04:00
// search for matching protocol
for ( uint8_t i = 0 ; i < SERIALMANAGER_NUM_PORTS ; i + + ) {
2015-03-27 22:44:43 -03:00
if ( protocol_match ( protocol , ( enum SerialProtocol ) state [ i ] . protocol . get ( ) ) ) {
if ( found_instance = = instance ) {
2018-04-18 03:13:02 -03:00
return & state [ i ] ;
2015-03-27 22:44:43 -03:00
}
found_instance + + ;
2015-01-29 00:35:42 -04:00
}
}
2023-11-15 23:41:16 -04:00
# if AP_SERIALMANAGER_REGISTER_ENABLED
for ( auto p = registered_ports ; p ; p = p - > next ) {
if ( protocol_match ( protocol , ( enum SerialProtocol ) p - > state . protocol . get ( ) ) ) {
if ( found_instance = = instance ) {
return & p - > state ;
}
found_instance + + ;
}
}
# endif
2015-01-29 00:35:42 -04:00
// if we got this far we did not find the uart
2016-10-30 02:24:21 -03:00
return nullptr ;
2015-01-29 00:35:42 -04:00
}
2018-04-18 03:13:02 -03:00
// find_serial - searches available serial ports for the first instance that allows the given protocol
// instance should be zero if searching for the first instance, 1 for the second, etc
// returns uart on success, nullptr if a serial port cannot be found
AP_HAL : : UARTDriver * AP_SerialManager : : find_serial ( enum SerialProtocol protocol , uint8_t instance ) const
{
const struct UARTState * _state = find_protocol_instance ( protocol , instance ) ;
if ( _state = = nullptr ) {
return nullptr ;
}
2023-11-15 23:41:16 -04:00
const uint8_t serial_idx = _state - > idx ;
2021-07-07 19:25:07 -03:00
// set options before any user does begin()
AP_HAL : : UARTDriver * port = hal . serial ( serial_idx ) ;
2023-11-15 23:41:16 -04:00
# if AP_SERIALMANAGER_REGISTER_ENABLED
if ( port = = nullptr ) {
// look for a registered port
for ( auto p = registered_ports ; p ; p = p - > next ) {
if ( p - > state . idx = = serial_idx ) {
port = p ;
break ;
}
}
}
# endif
2021-07-07 19:25:07 -03:00
if ( port ) {
port - > set_options ( _state - > options ) ;
}
return port ;
2018-04-18 03:13:02 -03:00
}
2021-12-20 00:33:32 -04:00
// have_serial - return true if we have the given serial protocol configured
bool AP_SerialManager : : have_serial ( enum SerialProtocol protocol , uint8_t instance ) const
{
return find_protocol_instance ( protocol , instance ) ! = nullptr ;
}
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 baudrate on success, 0 if a serial port cannot be found
2015-04-06 18:47:11 -03:00
uint32_t AP_SerialManager : : find_baudrate ( enum SerialProtocol protocol , uint8_t instance ) const
2015-01-29 00:35:42 -04:00
{
2018-04-18 03:13:02 -03:00
const struct UARTState * _state = find_protocol_instance ( protocol , instance ) ;
if ( _state = = nullptr ) {
return 0 ;
2015-01-19 09:32:24 -04:00
}
2022-07-25 01:41:09 -03:00
return _state - > baudrate ( ) ;
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 AP_SerialManager : : find_portnum ( enum SerialProtocol protocol , uint8_t instance ) const
{
const struct UARTState * _state = find_protocol_instance ( protocol , instance ) ;
if ( _state = = nullptr ) {
return - 1 ;
}
2023-11-15 23:41:16 -04:00
return int8_t ( _state - > idx ) ;
2020-12-29 21:04:27 -04:00
}
2019-01-04 05:51:05 -04:00
// get_serial_by_id - gets serial by serial id
AP_HAL : : UARTDriver * AP_SerialManager : : get_serial_by_id ( uint8_t id )
{
if ( id < SERIALMANAGER_NUM_PORTS ) {
2020-12-10 20:56:28 -04:00
return hal . serial ( id ) ;
2019-01-04 05:51:05 -04:00
}
2023-11-15 23:41:16 -04:00
# if AP_SERIALMANAGER_REGISTER_ENABLED
for ( auto p = registered_ports ; p ; p = p - > next ) {
if ( p - > state . idx = = id ) {
return ( AP_HAL : : UARTDriver * ) p ;
}
}
# endif
2019-01-04 05:51:05 -04:00
return nullptr ;
}
2023-11-16 02:44:30 -04:00
/*
get a UARTState by index
*/
const AP_SerialManager : : UARTState * AP_SerialManager : : get_state_by_id ( uint8_t id ) const
{
if ( id < SERIALMANAGER_NUM_PORTS ) {
return & state [ id ] ;
}
# if AP_SERIALMANAGER_REGISTER_ENABLED
for ( auto p = registered_ports ; p ; p = p - > next ) {
if ( p - > state . idx = = id ) {
return & p - > state ;
}
}
# endif
return nullptr ;
}
2015-01-29 00:47:36 -04:00
/*
2019-01-19 02:33:55 -04:00
* map from a 16 bit EEPROM baud rate to a real baud rate . For
* stm32 - based boards we can do 1.5 MBit , although 921600 is more
* reliable .
2015-01-29 00:47:36 -04:00
*/
2019-11-26 20:06:04 -04:00
uint32_t AP_SerialManager : : map_baudrate ( int32_t rate )
2015-01-29 00:47:36 -04:00
{
if ( rate < = 0 ) {
rate = 57 ;
}
switch ( rate ) {
case 1 : return 1200 ;
case 2 : return 2400 ;
case 4 : return 4800 ;
case 9 : return 9600 ;
case 19 : return 19200 ;
case 38 : return 38400 ;
case 57 : return 57600 ;
case 100 : return 100000 ;
case 111 : return 111100 ;
case 115 : return 115200 ;
case 230 : return 230400 ;
2019-01-08 10:44:46 -04:00
case 256 : return 256000 ;
2015-01-29 00:47:36 -04:00
case 460 : return 460800 ;
case 500 : return 500000 ;
case 921 : return 921600 ;
case 1500 : return 1500000 ;
2022-12-29 08:47:33 -04:00
case 2000 : return 2000000 ;
2015-01-29 00:47:36 -04:00
}
if ( rate > 2000 ) {
// assume it is a direct baudrate. This allows for users to
// set an exact baudrate as long as it is over 2000 baud
return ( uint32_t ) rate ;
}
// otherwise allow any other kbaud rate
return rate * 1000 ;
}
2015-03-27 22:44:43 -03:00
// protocol_match - returns true if the protocols match
bool AP_SerialManager : : protocol_match ( enum SerialProtocol protocol1 , enum SerialProtocol protocol2 ) const
{
// check for obvious match
if ( protocol1 = = protocol2 ) {
return true ;
}
// mavlink match
2022-01-25 22:50:59 -04:00
if ( ( ( protocol1 = = SerialProtocol_MAVLink ) | | ( protocol1 = = SerialProtocol_MAVLink2 ) | | ( protocol1 = = SerialProtocol_MAVLinkHL ) ) & &
( ( protocol2 = = SerialProtocol_MAVLink ) | | ( protocol2 = = SerialProtocol_MAVLink2 ) | | ( protocol2 = = SerialProtocol_MAVLinkHL ) ) ) {
2015-03-27 22:44:43 -03:00
return true ;
}
// gps match
if ( ( ( protocol1 = = SerialProtocol_GPS ) | | ( protocol1 = = SerialProtocol_GPS2 ) ) & &
2015-04-06 19:28:03 -03:00
( ( protocol2 = = SerialProtocol_GPS ) | | ( protocol2 = = SerialProtocol_GPS2 ) ) ) {
2015-03-27 22:44:43 -03:00
return true ;
}
return false ;
}
2017-11-20 21:10:02 -04:00
2018-11-10 05:45:01 -04:00
// setup any special options
2020-01-02 21:51:02 -04:00
void AP_SerialManager : : set_options ( uint16_t i )
2018-11-10 05:45:01 -04:00
{
struct UARTState & opt = state [ i ] ;
// pass through to HAL
2020-12-10 20:56:28 -04:00
if ( ! hal . serial ( i ) - > set_options ( opt . options ) ) {
2022-03-21 06:40:43 -03:00
DEV_PRINTF ( " Unable to setup options for Serial%u \n " , i ) ;
2018-11-10 05:45:01 -04:00
}
}
2018-12-19 07:24:57 -04:00
// get the passthru ports if enabled
2020-11-14 22:15:19 -04:00
bool AP_SerialManager : : get_passthru ( AP_HAL : : UARTDriver * & port1 , AP_HAL : : UARTDriver * & port2 , uint8_t & timeout_s ,
2023-11-16 02:44:30 -04:00
uint32_t & baud1 , uint32_t & baud2 )
2018-12-19 07:24:57 -04:00
{
if ( passthru_port2 < 0 | |
2023-11-16 02:44:30 -04:00
passthru_port1 < 0 ) {
return false ;
}
port1 = get_serial_by_id ( passthru_port1 ) ;
port2 = get_serial_by_id ( passthru_port2 ) ;
if ( port1 = = nullptr | | port2 = = nullptr ) {
return false ;
}
const auto * state1 = get_state_by_id ( passthru_port1 ) ;
const auto * state2 = get_state_by_id ( passthru_port2 ) ;
if ( ! state1 | | ! state2 ) {
2018-12-19 07:24:57 -04:00
return false ;
}
2023-11-16 02:44:30 -04:00
baud1 = state1 - > baudrate ( ) ;
baud2 = state2 - > baudrate ( ) ;
2018-12-19 07:24:57 -04:00
timeout_s = MAX ( passthru_timeout , 0 ) ;
return true ;
}
// disable passthru by settings SERIAL_PASS2 to -1
void AP_SerialManager : : disable_passthru ( void )
{
passthru_port2 . set_and_notify ( - 1 ) ;
}
2019-10-27 21:30:58 -03:00
// accessor for AP_Periph to set baudrate and type
void AP_SerialManager : : set_protocol_and_baud ( uint8_t sernum , enum SerialProtocol protocol , uint32_t baudrate )
{
2022-02-21 23:19:15 -04:00
if ( sernum < SERIALMANAGER_NUM_PORTS ) {
2019-10-27 21:30:58 -03:00
state [ sernum ] . protocol . set ( protocol ) ;
state [ sernum ] . baud . set ( baudrate ) ;
}
}
2023-11-15 23:41:16 -04:00
# if AP_SERIALMANAGER_REGISTER_ENABLED
/*
register an external network port . It is up to the caller to use a unique id field
using AP_SERIALMANAGER_NET_PORT_1 as the base id for NET_P1_ *
*/
void AP_SerialManager : : register_port ( RegisteredPort * port )
{
2023-11-21 20:33:33 -04:00
const auto idx = port - > state . idx ;
WITH_SEMAPHORE ( port_sem ) ;
/*
maintain the list in ID order
*/
if ( registered_ports = = nullptr | |
registered_ports - > state . idx > = idx ) {
port - > next = registered_ports ;
registered_ports = port ;
return ;
}
for ( auto p = registered_ports ; p ; p = p - > next ) {
if ( p - > next = = nullptr | | p - > next - > state . idx > = idx ) {
port - > next = p - > next ;
p - > next = port ;
break ;
}
}
2023-11-15 23:41:16 -04:00
}
# endif // AP_SERIALMANAGER_REGISTER_ENABLED
2017-11-20 21:10:02 -04:00
namespace AP {
AP_SerialManager & serialmanager ( )
{
2019-02-10 01:02:55 -04:00
return * AP_SerialManager : : get_singleton ( ) ;
2017-11-20 21:10:02 -04:00
}
}
2024-02-27 07:54:14 -04:00
# endif // AP_SERIALMANAGER_ENABLED