Line data Source code
1 : /*
2 : Please contribute your ideas! See https://ardupilot.club/dev for details
3 :
4 : This program is free software: you can redistribute it and/or modify
5 : it under the terms of the GNU General Public License as published by
6 : the Free Software Foundation, either version 3 of the License, or
7 : (at your option) any later version.
8 :
9 : This program is distributed in the hope that it will be useful,
10 : but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : GNU General Public License for more details.
13 :
14 : You should have received a copy of the GNU General Public License
15 : along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 : /*
18 : SerialManager allows defining the protocol and baud rates for the available
19 : serial ports and provides helper functions so objects (like a gimbal) can
20 : find which serial port they should use
21 : */
22 : #pragma once
23 :
24 : #include "AP_SerialManager_config.h"
25 :
26 : #if AP_SERIALMANAGER_ENABLED
27 :
28 : #include <AP_Param/AP_Param.h>
29 :
30 : // serial ports registered by AP_Networking will use IDs starting at 21 for the first port
31 : #define AP_SERIALMANAGER_NET_PORT_1 21 // NET_P1_*
32 :
33 : // serial ports registered by AP_DroneCAN will use IDs starting at 41/51 for the first port
34 : #define AP_SERIALMANAGER_CAN_D1_PORT_1 41 // CAN_D1_UC_S1_*
35 : #define AP_SERIALMANAGER_CAN_D2_PORT_1 51 // CAN_D2_UC_S1_*
36 :
37 : // serial device simulation ports registered by AP_Scripting will use IDs starting at 61 for the first port
38 : #define AP_SERIALMANAGER_SCR_PORT_1 61 // SCR_SDEV1_*
39 :
40 : #define AP_SERIALMANAGER_CONSOLE_BUFSIZE_RX 128
41 : #define AP_SERIALMANAGER_CONSOLE_BUFSIZE_TX 512
42 :
43 : // mavlink default baud rates and buffer sizes
44 : #define AP_SERIALMANAGER_MAVLINK_BAUD 57600
45 : #define AP_SERIALMANAGER_MAVLINK_BUFSIZE_RX 128
46 : #define AP_SERIALMANAGER_MAVLINK_BUFSIZE_TX 256
47 :
48 : // LTM buffer sizes
49 : #define AP_SERIALMANAGER_LTM_BUFSIZE_RX 0
50 : #define AP_SERIALMANAGER_LTM_BUFSIZE_TX 32
51 :
52 : // FrSky default baud rates, use default buffer sizes
53 : #define AP_SERIALMANAGER_FRSKY_D_BAUD 9600
54 : #define AP_SERIALMANAGER_FRSKY_SPORT_BAUD 57600
55 : #define AP_SERIALMANAGER_FRSKY_BUFSIZE_RX 0
56 : #define AP_SERIALMANAGER_FRSKY_BUFSIZE_TX 0
57 :
58 : // GPS default baud rates and buffer sizes
59 : // we need a 256 byte buffer for some GPS types (eg. UBLOX)
60 : #define AP_SERIALMANAGER_GPS_BAUD 230400
61 : #define AP_SERIALMANAGER_GPS_BUFSIZE_RX 256
62 : #define AP_SERIALMANAGER_GPS_BUFSIZE_TX 16
63 :
64 : // AlexMos Gimbal protocol default baud rates and buffer sizes
65 : #define AP_SERIALMANAGER_ALEXMOS_BAUD 115200
66 : #define AP_SERIALMANAGER_ALEXMOS_BUFSIZE_RX 128
67 : #define AP_SERIALMANAGER_ALEXMOS_BUFSIZE_TX 128
68 :
69 : #define AP_SERIALMANAGER_GIMBAL_BAUD 115200
70 : #define AP_SERIALMANAGER_GIMBAL_BUFSIZE_RX 128
71 : #define AP_SERIALMANAGER_GIMBAL_BUFSIZE_TX 128
72 :
73 : #define AP_SERIALMANAGER_ROBOTIS_BUFSIZE_RX 128
74 : #define AP_SERIALMANAGER_ROBOTIS_BUFSIZE_TX 128
75 :
76 : // MegaSquirt EFI protocol
77 : #define AP_SERIALMANAGER_EFI_MS_BAUD 115
78 : #define AP_SERIALMANAGER_EFI_MS_BUFSIZE_RX 512
79 : #define AP_SERIALMANAGER_EFI_MS_BUFSIZE_TX 16
80 :
81 : #define AP_SERIALMANAGER_SLCAN_BAUD 115200
82 : #define AP_SERIALMANAGER_SLCAN_BUFSIZE_RX 128
83 : #define AP_SERIALMANAGER_SLCAN_BUFSIZE_TX 128
84 :
85 : // MSP protocol default buffer sizes
86 : #define AP_SERIALMANAGER_MSP_BUFSIZE_RX 128
87 : #define AP_SERIALMANAGER_MSP_BUFSIZE_TX 256
88 : #define AP_SERIALMANAGER_MSP_BAUD 115200
89 :
90 : // IMU OUT protocol
91 : #define AP_SERIALMANAGER_IMUOUT_BAUD 921600
92 : #define AP_SERIALMANAGER_IMUOUT_BUFSIZE_RX 128
93 : #define AP_SERIALMANAGER_IMUOUT_BUFSIZE_TX 2048
94 :
95 : class AP_SerialManager {
96 : public:
97 : AP_SerialManager();
98 :
99 : /* Do not allow copies */
100 : CLASS_NO_COPY(AP_SerialManager);
101 :
102 : enum SerialProtocol {
103 : SerialProtocol_None = -1,
104 : SerialProtocol_Console = 0, // unused
105 : SerialProtocol_MAVLink = 1,
106 : SerialProtocol_MAVLink2 = 2, // do not use - use MAVLink and provide instance of 1
107 : SerialProtocol_FrSky_D = 3, // FrSky D protocol (D-receivers)
108 : SerialProtocol_FrSky_SPort = 4, // FrSky SPort protocol (X-receivers)
109 : SerialProtocol_GPS = 5,
110 : SerialProtocol_GPS2 = 6, // do not use - use GPS and provide instance of 1
111 : SerialProtocol_AlexMos = 7,
112 : SerialProtocol_Gimbal = 8, // SToRM32, Siyi custom serial protocols
113 : SerialProtocol_Rangefinder = 9,
114 : SerialProtocol_FrSky_SPort_Passthrough = 10, // FrSky SPort Passthrough (OpenTX) protocol (X-receivers)
115 : SerialProtocol_Lidar360 = 11, // Lightware SF40C, TeraRanger Tower or RPLidarA2
116 : SerialProtocol_Aerotenna_USD1 = 12, // USD1 support - deprecated, users should use Rangefinder
117 : SerialProtocol_Beacon = 13,
118 : SerialProtocol_Volz = 14, // Volz servo protocol
119 : SerialProtocol_Sbus1 = 15,
120 : SerialProtocol_ESCTelemetry = 16,
121 : SerialProtocol_Devo_Telem = 17,
122 : SerialProtocol_OpticalFlow = 18,
123 : SerialProtocol_Robotis = 19,
124 : SerialProtocol_NMEAOutput = 20,
125 : SerialProtocol_WindVane = 21,
126 : SerialProtocol_SLCAN = 22,
127 : SerialProtocol_RCIN = 23,
128 : SerialProtocol_EFI = 24, // EFI serial protocol
129 : SerialProtocol_LTM_Telem = 25,
130 : SerialProtocol_RunCam = 26,
131 : SerialProtocol_Hott = 27,
132 : SerialProtocol_Scripting = 28,
133 : SerialProtocol_CRSF = 29,
134 : SerialProtocol_Generator = 30,
135 : SerialProtocol_Winch = 31,
136 : SerialProtocol_MSP = 32,
137 : SerialProtocol_DJI_FPV = 33,
138 : SerialProtocol_AirSpeed = 34,
139 : SerialProtocol_ADSB = 35,
140 : SerialProtocol_AHRS = 36,
141 : SerialProtocol_SmartAudio = 37,
142 : SerialProtocol_FETtecOneWire = 38,
143 : SerialProtocol_Torqeedo = 39,
144 : SerialProtocol_AIS = 40,
145 : SerialProtocol_CoDevESC = 41,
146 : SerialProtocol_MSP_DisplayPort = 42,
147 : SerialProtocol_MAVLinkHL = 43,
148 : SerialProtocol_Tramp = 44,
149 : SerialProtocol_DDS_XRCE = 45,
150 : SerialProtocol_IMUOUT = 46,
151 : // Reserving Serial Protocol 47 for SerialProtocol_IQ
152 : SerialProtocol_PPP = 48,
153 : SerialProtocol_IBUS_Telem = 49, // i-BUS telemetry data, ie via sensor port of FS-iA6B
154 : SerialProtocol_IOMCU = 50, // IOMCU
155 : SerialProtocol_NumProtocols // must be the last value
156 : };
157 :
158 : // get singleton instance
159 83743330 : static AP_SerialManager *get_singleton(void) {
160 83743330 : return _singleton;
161 : }
162 :
163 : // init_console - initialise console at default baud rate
164 : void init_console();
165 :
166 : // init - initialise serial ports
167 : void init();
168 :
169 : bool pre_arm_checks(char *failure_msg, const uint8_t failure_msg_len);
170 :
171 : // find_serial - searches available serial ports that allows the given protocol
172 : // instance should be zero if searching for the first instance, 1 for the second, etc
173 : // returns uart on success, nullptr if a serial port cannot be found
174 : // note that the SERIALn_OPTIONS are applied if the port is found
175 : AP_HAL::UARTDriver *find_serial(enum SerialProtocol protocol, uint8_t instance) const;
176 :
177 : // have_serial - return true if we have the corresponding serial protocol configured
178 : bool have_serial(enum SerialProtocol protocol, uint8_t instance) const;
179 :
180 : // find_baudrate - searches available serial ports for the first instance that allows the given protocol
181 : // instance should be zero if searching for the first instance, 1 for the second, etc
182 : // returns the baudrate of that protocol on success, 0 if a serial port cannot be found
183 : uint32_t find_baudrate(enum SerialProtocol protocol, uint8_t instance) const;
184 :
185 : // find_portnum - find port number (SERIALn index) for a protocol and instance, -1 for not found
186 : int8_t find_portnum(enum SerialProtocol protocol, uint8_t instance) const;
187 :
188 : // get the passthru ports if enabled
189 : bool get_passthru(AP_HAL::UARTDriver *&port1, AP_HAL::UARTDriver *&port2, uint8_t &timeout_s,
190 : uint32_t &baud1, uint32_t &baud2);
191 :
192 : // disable passthru by settings SERIAL_PASS2 to -1
193 : void disable_passthru(void);
194 :
195 : // get Serial Port
196 : AP_HAL::UARTDriver *get_serial_by_id(uint8_t id);
197 :
198 : // accessors for AP_Periph to set baudrate and type
199 : void set_protocol_and_baud(uint8_t sernum, enum SerialProtocol protocol, uint32_t baudrate);
200 :
201 : void set_and_default_baud(enum SerialProtocol protocol, uint8_t instance, uint32_t _baud);
202 :
203 : static uint32_t map_baudrate(int32_t rate);
204 :
205 : // parameter var table
206 : static const struct AP_Param::GroupInfo var_info[];
207 :
208 : class UARTState {
209 : friend class AP_SerialManager;
210 : public:
211 : bool option_enabled(uint16_t option) const {
212 : return (options & option) == option;
213 : }
214 : // returns a baudrate such as 9600. May map from a special
215 : // parameter value like "57" to "57600":
216 14917 : uint32_t baudrate() const {
217 14917 : return AP_SerialManager::map_baudrate(baud);
218 : }
219 2790963 : AP_SerialManager::SerialProtocol get_protocol() const {
220 2790963 : return AP_SerialManager::SerialProtocol(protocol.get());
221 : }
222 : AP_Int32 baud;
223 : AP_Int32 options;
224 : AP_Int8 protocol;
225 :
226 : // serial index number
227 : uint8_t idx;
228 :
229 : #if HAL_LOGGING_ENABLED && HAL_UART_STATS_ENABLED
230 : AP_HAL::UARTDriver::StatsTracker stats;
231 : #endif
232 :
233 : // Enum for device type used in get_device_id
234 : enum class DeviceType {
235 : UNKNOWN,
236 : UART,
237 : NETWORKING,
238 : CANBUS,
239 : SCRIPTING,
240 : };
241 :
242 : // Return a device id for this port
243 : uint32_t get_device_id() const;
244 : };
245 :
246 : // get a state from serial index
247 : const UARTState *get_state_by_id(uint8_t id) const;
248 :
249 : // search through managed serial connections looking for the
250 : // instance-nth UART which is running protocol protocol.
251 : // protocol_match is used to determine equivalence of one protocol
252 : // to another, e.g. MAVLink2 is considered MAVLink1 for finding
253 : // mavlink1 protocol instances.
254 : const UARTState *find_protocol_instance(enum SerialProtocol protocol,
255 : uint8_t instance) const;
256 :
257 : // disable an option on a serial port:
258 0 : void disable_option(uint8_t instance, uint32_t option) {
259 0 : if (instance >= ARRAY_SIZE(state)) {
260 0 : return;
261 : }
262 0 : state[instance].options.set_and_notify(state[instance].options & ~option);
263 : }
264 :
265 : #if AP_SERIALMANAGER_REGISTER_ENABLED
266 : /*
267 : a class for a externally registered port
268 : used by AP_Networking
269 : */
270 : class RegisteredPort : public AP_HAL::UARTDriver {
271 : public:
272 0 : uint32_t bw_in_bytes_per_second() const override { return state.baudrate()/10; }
273 0 : uint32_t get_baud_rate() const override { return state.baudrate(); }
274 : RegisteredPort *next;
275 : UARTState state;
276 : };
277 : RegisteredPort *registered_ports;
278 : HAL_Semaphore port_sem;
279 :
280 : // register an externally managed port
281 : void register_port(RegisteredPort *port);
282 :
283 : #if HAL_LOGGING_ENABLED && HAL_UART_STATS_ENABLED
284 : // Log UART message for each registered serial port
285 : void registered_ports_log();
286 : uint32_t registered_ports_last_log_ms;
287 : #endif
288 :
289 : #endif // AP_SERIALMANAGER_REGISTER_ENABLED
290 :
291 :
292 : private:
293 : static AP_SerialManager *_singleton;
294 :
295 : // array of uart info. See comment above about
296 : // SERIALMANAGER_MAX_PORTS
297 : UARTState state[SERIALMANAGER_MAX_PORTS];
298 :
299 : // pass-through serial support
300 : AP_Int8 passthru_port1;
301 : AP_Int8 passthru_port2;
302 : AP_Int8 passthru_timeout;
303 :
304 : // protocol_match - returns true if the protocols match
305 : bool protocol_match(enum SerialProtocol protocol1, enum SerialProtocol protocol2) const;
306 :
307 : // setup any special options
308 : void set_options(uint16_t i);
309 :
310 : bool init_console_done;
311 :
312 : void convert_parameters();
313 : };
314 :
315 : namespace AP {
316 : AP_SerialManager &serialmanager();
317 : };
318 :
319 : #endif // AP_SERIALMANAGER_ENABLED
|