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)
77 extern const uint32_t baudRates
[];
79 // serial port identifiers are now fixed, these values are used by MSP commands.
82 SERIAL_PORT_NONE
= -1,
83 SERIAL_PORT_USART1
= 0,
92 SERIAL_PORT_USB_VCP
= 20,
93 SERIAL_PORT_SOFTSERIAL1
= 30,
94 SERIAL_PORT_SOFTSERIAL2
,
95 SERIAL_PORT_IDENTIFIER_MAX
= SERIAL_PORT_SOFTSERIAL2
,
96 } serialPortIdentifier_e
;
98 extern const serialPortIdentifier_e serialPortIdentifiers
[SERIAL_PORT_COUNT
];
100 #define SERIAL_PORT_IDENTIFIER_TO_INDEX(x) (((x) < RESOURCE_SOFT_OFFSET) ? (x) : (RESOURCE_SOFT_OFFSET + ((x) - SERIAL_PORT_SOFTSERIAL1)))
102 #define SERIAL_PORT_IDENTIFIER_TO_UARTDEV(x) ((x) - SERIAL_PORT_USART1 + UARTDEV_1)
107 typedef struct serialPortUsage_s
{
108 serialPort_t
*serialPort
;
109 serialPortFunction_e function
;
110 serialPortIdentifier_e identifier
;
113 serialPort_t
*findSharedSerialPort(uint16_t functionMask
, serialPortFunction_e sharedWithFunction
);
118 typedef struct serialPortConfig_s
{
119 uint32_t functionMask
;
120 serialPortIdentifier_e identifier
;
121 uint8_t msp_baudrateIndex
;
122 uint8_t gps_baudrateIndex
;
123 uint8_t blackbox_baudrateIndex
;
124 uint8_t telemetry_baudrateIndex
; // not used for all telemetry systems, e.g. HoTT only works at 19200.
125 } serialPortConfig_t
;
127 typedef struct serialConfig_s
{
128 serialPortConfig_t portConfigs
[SERIAL_PORT_COUNT
];
129 uint16_t serial_update_rate_hz
;
130 uint8_t reboot_character
; // which byte is used to reboot. Default 'R', could be changed carefully to something else.
133 PG_DECLARE(serialConfig_t
, serialConfig
);
135 typedef void serialConsumer(uint8_t);
140 void serialInit(bool softserialEnabled
, serialPortIdentifier_e serialPortToDisable
);
141 void serialRemovePort(serialPortIdentifier_e identifier
);
142 uint8_t serialGetAvailablePortCount(void);
143 bool serialIsPortAvailable(serialPortIdentifier_e identifier
);
144 bool isSerialConfigValid(const serialConfig_t
*serialConfig
);
145 const serialPortConfig_t
*serialFindPortConfiguration(serialPortIdentifier_e identifier
);
146 serialPortConfig_t
*serialFindPortConfigurationMutable(serialPortIdentifier_e identifier
);
147 bool doesConfigurationUsePort(serialPortIdentifier_e portIdentifier
);
148 const serialPortConfig_t
*findSerialPortConfig(serialPortFunction_e function
);
149 const serialPortConfig_t
*findNextSerialPortConfig(serialPortFunction_e function
);
151 portSharing_e
determinePortSharing(const serialPortConfig_t
*portConfig
, serialPortFunction_e function
);
152 bool isSerialPortShared(const serialPortConfig_t
*portConfig
, uint16_t functionMask
, serialPortFunction_e sharedWithFunction
);
154 void pgResetFn_serialConfig(serialConfig_t
*serialConfig
); //!!TODO remove need for this
155 serialPortUsage_t
*findSerialPortUsageByIdentifier(serialPortIdentifier_e identifier
);
156 int findSerialPortIndexByIdentifier(serialPortIdentifier_e identifier
);
160 serialPort_t
*openSerialPort(
161 serialPortIdentifier_e identifier
,
162 serialPortFunction_e function
,
163 serialReceiveCallbackPtr rxCallback
,
164 void *rxCallbackData
,
167 portOptions_e options
169 void closeSerialPort(serialPort_t
*serialPort
);
171 void waitForSerialPortToFinishTransmitting(serialPort_t
*serialPort
);
173 baudRate_e
lookupBaudRateIndex(uint32_t baudRate
);
177 // msp/cli/bootloader
179 void serialPassthrough(serialPort_t
*left
, serialPort_t
*right
, serialConsumer
*leftC
, serialConsumer
*rightC
);