2 * This file is part of Cleanflight.
4 * Cleanflight 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.
9 * Cleanflight 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.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #include "config/parameter_group.h"
24 #include "drivers/serial.h"
27 PORTSHARING_UNUSED
= 0,
28 PORTSHARING_NOT_SHARED
,
34 FUNCTION_MSP
= (1 << 0), // 1
35 FUNCTION_GPS
= (1 << 1), // 2
36 FUNCTION_UNUSED_3
= (1 << 2), // 4 //Was FUNCTION_TELEMETRY_FRSKY
37 FUNCTION_TELEMETRY_HOTT
= (1 << 3), // 8
38 FUNCTION_TELEMETRY_LTM
= (1 << 4), // 16
39 FUNCTION_TELEMETRY_SMARTPORT
= (1 << 5), // 32
40 FUNCTION_RX_SERIAL
= (1 << 6), // 64
41 FUNCTION_BLACKBOX
= (1 << 7), // 128
42 FUNCTION_TELEMETRY_MAVLINK
= (1 << 8), // 256
43 FUNCTION_TELEMETRY_IBUS
= (1 << 9), // 512
44 FUNCTION_RCDEVICE
= (1 << 10), // 1024
45 FUNCTION_VTX_SMARTAUDIO
= (1 << 11), // 2048
46 FUNCTION_VTX_TRAMP
= (1 << 12), // 4096
47 FUNCTION_UNUSED_1
= (1 << 13), // 8192: former\ UAV_INTERCONNECT
48 FUNCTION_OPTICAL_FLOW
= (1 << 14), // 16384
49 FUNCTION_LOG
= (1 << 15), // 32768
50 FUNCTION_RANGEFINDER
= (1 << 16), // 65536
51 FUNCTION_VTX_FFPV
= (1 << 17), // 131072
52 FUNCTION_ESCSERIAL
= (1 << 18), // 262144: this is used for both SERIALSHOT and ESC_SENSOR telemetry
53 FUNCTION_TELEMETRY_SIM
= (1 << 19), // 524288
54 FUNCTION_FRSKY_OSD
= (1 << 20), // 1048576
55 FUNCTION_DJI_HD_OSD
= (1 << 21), // 2097152
56 FUNCTION_SERVO_SERIAL
= (1 << 22), // 4194304
57 FUNCTION_TELEMETRY_SMARTPORT_MASTER
= (1 << 23), // 8388608
58 FUNCTION_UNUSED_2
= (1 << 24), // 16777216
59 FUNCTION_MSP_OSD
= (1 << 25), // 33554432
60 } serialPortFunction_e
;
62 #define FUNCTION_VTX_MSP FUNCTION_MSP_OSD
84 BAUD_MAX
= BAUD_2470000
,
87 extern const uint32_t baudRates
[];
89 // serial port identifiers are now fixed, these values are used by MSP commands.
91 SERIAL_PORT_NONE
= -1,
92 SERIAL_PORT_USART1
= 0,
100 SERIAL_PORT_USB_VCP
= 20,
101 SERIAL_PORT_SOFTSERIAL1
= 30,
102 SERIAL_PORT_SOFTSERIAL2
,
103 SERIAL_PORT_IDENTIFIER_MAX
= SERIAL_PORT_SOFTSERIAL2
104 } serialPortIdentifier_e
;
106 extern const serialPortIdentifier_e serialPortIdentifiers
[SERIAL_PORT_COUNT
];
111 typedef struct serialPortUsage_s
{
112 serialPortIdentifier_e identifier
;
113 serialPort_t
*serialPort
;
114 serialPortFunction_e function
;
117 serialPort_t
*findSharedSerialPort(uint32_t functionMask
, serialPortFunction_e sharedWithFunction
);
118 serialPort_t
*findNextSharedSerialPort(uint32_t functionMask
, serialPortFunction_e sharedWithFunction
);
123 typedef struct serialPortConfig_s
{
124 uint32_t functionMask
;
125 serialPortIdentifier_e identifier
;
126 uint8_t msp_baudrateIndex
;
127 uint8_t gps_baudrateIndex
;
128 uint8_t peripheral_baudrateIndex
;
129 uint8_t telemetry_baudrateIndex
; // not used for all telemetry systems, e.g. HoTT only works at 19200.
130 } serialPortConfig_t
;
132 typedef struct serialConfig_s
{
133 serialPortConfig_t portConfigs
[SERIAL_PORT_COUNT
];
134 uint8_t reboot_character
; // which byte is used to reboot. Default 'R', could be changed carefully to something else.
137 PG_DECLARE(serialConfig_t
, serialConfig
);
139 typedef void serialConsumer(uint8_t);
144 void serialInit(bool softserialEnabled
);
145 void serialRemovePort(serialPortIdentifier_e identifier
);
146 uint8_t serialGetAvailablePortCount(void);
147 bool serialIsPortAvailable(serialPortIdentifier_e identifier
);
148 bool isSerialConfigValid(const serialConfig_t
*serialConfig
);
149 serialPortConfig_t
*serialFindPortConfiguration(serialPortIdentifier_e identifier
);
150 bool doesConfigurationUsePort(serialPortIdentifier_e portIdentifier
);
151 serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e function
);
152 serialPortConfig_t
*findNextSerialPortConfig(serialPortFunction_e function
);
154 portSharing_e
determinePortSharing(const serialPortConfig_t
*portConfig
, serialPortFunction_e function
);
155 bool isSerialPortShared(const serialPortConfig_t
*portConfig
, uint32_t functionMask
, serialPortFunction_e sharedWithFunction
);
157 serialPortUsage_t
*findSerialPortUsageByIdentifier(serialPortIdentifier_e identifier
);
158 int findSerialPortIndexByIdentifier(serialPortIdentifier_e identifier
);
162 serialPort_t
*openSerialPort(
163 serialPortIdentifier_e identifier
,
164 serialPortFunction_e function
,
165 serialReceiveCallbackPtr callback
,
166 void *rxCallbackData
,
169 portOptions_t options
171 void closeSerialPort(serialPort_t
*serialPort
);
173 void waitForSerialPortToFinishTransmitting(serialPort_t
*serialPort
);
175 baudRate_e
lookupBaudRateIndex(uint32_t baudRate
);
179 // msp/cli/bootloader
181 void serialPassthrough(serialPort_t
*left
, serialPort_t
*right
, serialConsumer
*leftC
, serialConsumer
*rightC
);