2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
27 #include "drivers/serial.h"
30 PORTSHARING_UNUSED
= 0,
31 PORTSHARING_NOT_SHARED
,
37 FUNCTION_MSP
= (1 << 0), // 1
38 FUNCTION_GPS
= (1 << 1), // 2
39 FUNCTION_TELEMETRY_FRSKY_HUB
= (1 << 2), // 4
40 FUNCTION_TELEMETRY_HOTT
= (1 << 3), // 8
41 FUNCTION_TELEMETRY_LTM
= (1 << 4), // 16
42 FUNCTION_TELEMETRY_SMARTPORT
= (1 << 5), // 32
43 FUNCTION_RX_SERIAL
= (1 << 6), // 64
44 FUNCTION_BLACKBOX
= (1 << 7), // 128
45 FUNCTION_TELEMETRY_MAVLINK
= (1 << 9), // 512
46 FUNCTION_ESC_SENSOR
= (1 << 10), // 1024
47 FUNCTION_VTX_SMARTAUDIO
= (1 << 11), // 2048
48 FUNCTION_TELEMETRY_IBUS
= (1 << 12), // 4096
49 FUNCTION_VTX_TRAMP
= (1 << 13), // 8192
50 FUNCTION_RCDEVICE
= (1 << 14), // 16384
51 FUNCTION_LIDAR_TF
= (1 << 15), // 32768
52 FUNCTION_FRSKY_OSD
= (1 << 16), // 65536
53 } serialPortFunction_e
;
55 #define TELEMETRY_SHAREABLE_PORT_FUNCTIONS_MASK (FUNCTION_TELEMETRY_FRSKY_HUB | FUNCTION_TELEMETRY_LTM | FUNCTION_TELEMETRY_MAVLINK)
56 #define TELEMETRY_PORT_FUNCTIONS_MASK (TELEMETRY_SHAREABLE_PORT_FUNCTIONS_MASK | FUNCTION_TELEMETRY_HOTT | FUNCTION_TELEMETRY_SMARTPORT)
78 extern const uint32_t baudRates
[];
80 // serial port identifiers are now fixed, these values are used by MSP commands.
83 SERIAL_PORT_NONE
= -1,
84 SERIAL_PORT_USART1
= 0,
94 SERIAL_PORT_USB_VCP
= 20,
95 SERIAL_PORT_SOFTSERIAL1
= 30,
96 SERIAL_PORT_SOFTSERIAL2
,
97 SERIAL_PORT_LPUART1
= 40,
98 SERIAL_PORT_IDENTIFIER_MAX
= SERIAL_PORT_LPUART1
,
99 } serialPortIdentifier_e
;
101 extern const serialPortIdentifier_e serialPortIdentifiers
[SERIAL_PORT_COUNT
];
103 #define SERIAL_PORT_IDENTIFIER_TO_INDEX(x) (((x) < RESOURCE_SOFT_OFFSET) ? (x) : (RESOURCE_SOFT_OFFSET + ((x) - SERIAL_PORT_SOFTSERIAL1)))
105 #define SERIAL_PORT_IDENTIFIER_TO_UARTDEV(x) ((x) - SERIAL_PORT_USART1 + UARTDEV_1)
110 typedef struct serialPortUsage_s
{
111 serialPort_t
*serialPort
;
112 serialPortFunction_e function
;
113 serialPortIdentifier_e identifier
;
116 serialPort_t
*findSharedSerialPort(uint16_t functionMask
, serialPortFunction_e sharedWithFunction
);
121 typedef struct serialPortConfig_s
{
122 uint32_t functionMask
;
124 uint8_t msp_baudrateIndex
;
125 uint8_t gps_baudrateIndex
;
126 uint8_t blackbox_baudrateIndex
;
127 uint8_t telemetry_baudrateIndex
; // not used for all telemetry systems, e.g. HoTT only works at 19200.
128 } serialPortConfig_t
;
130 typedef struct serialConfig_s
{
131 serialPortConfig_t portConfigs
[SERIAL_PORT_COUNT
];
132 uint16_t serial_update_rate_hz
;
133 uint8_t reboot_character
; // which byte is used to reboot. Default 'R', could be changed carefully to something else.
136 PG_DECLARE(serialConfig_t
, serialConfig
);
138 typedef void serialConsumer(uint8_t);
143 void serialInit(bool softserialEnabled
, serialPortIdentifier_e serialPortToDisable
);
144 void serialRemovePort(serialPortIdentifier_e identifier
);
145 uint8_t serialGetAvailablePortCount(void);
146 bool serialIsPortAvailable(serialPortIdentifier_e identifier
);
147 bool isSerialConfigValid(const serialConfig_t
*serialConfig
);
148 const serialPortConfig_t
*serialFindPortConfiguration(serialPortIdentifier_e identifier
);
149 serialPortConfig_t
*serialFindPortConfigurationMutable(serialPortIdentifier_e identifier
);
150 bool doesConfigurationUsePort(serialPortIdentifier_e portIdentifier
);
151 const serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e function
);
152 const serialPortConfig_t
*findNextSerialPortConfig(serialPortFunction_e function
);
154 portSharing_e
determinePortSharing(const serialPortConfig_t
*portConfig
, serialPortFunction_e function
);
155 bool isSerialPortShared(const serialPortConfig_t
*portConfig
, uint16_t functionMask
, serialPortFunction_e sharedWithFunction
);
157 void pgResetFn_serialConfig(serialConfig_t
*serialConfig
); //!!TODO remove need for this
158 serialPortUsage_t
*findSerialPortUsageByIdentifier(serialPortIdentifier_e identifier
);
159 int findSerialPortIndexByIdentifier(serialPortIdentifier_e identifier
);
163 serialPort_t
*openSerialPort(
164 serialPortIdentifier_e identifier
,
165 serialPortFunction_e function
,
166 serialReceiveCallbackPtr rxCallback
,
167 void *rxCallbackData
,
170 portOptions_e options
172 void closeSerialPort(serialPort_t
*serialPort
);
174 void waitForSerialPortToFinishTransmitting(serialPort_t
*serialPort
);
176 baudRate_e
lookupBaudRateIndex(uint32_t baudRate
);
180 // msp/cli/bootloader
182 void serialPassthrough(serialPort_t
*left
, serialPort_t
*right
, serialConsumer
*leftC
, serialConsumer
*rightC
);