Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / io / serial.h
blobaa9d709472e96ab650e780a106aeec40435580ab
1 /*
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/>.
18 #pragma once
20 #include <stdint.h>
21 #include <stdbool.h>
23 #include "config/parameter_group.h"
24 #include "drivers/serial.h"
26 typedef enum {
27 PORTSHARING_UNUSED = 0,
28 PORTSHARING_NOT_SHARED,
29 PORTSHARING_SHARED
30 } portSharing_e;
32 typedef enum {
33 FUNCTION_NONE = 0,
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 FUNCTION_GIMBAL = (1 << 26), // 67108864
61 FUNCTION_GIMBAL_HEADTRACKER = (1 << 27), // 134217728
62 } serialPortFunction_e;
64 #define FUNCTION_VTX_MSP FUNCTION_MSP_OSD
66 typedef enum {
67 BAUD_AUTO = 0,
68 BAUD_1200,
69 BAUD_2400,
70 BAUD_4800,
71 BAUD_9600,
72 BAUD_19200,
73 BAUD_38400,
74 BAUD_57600,
75 BAUD_115200,
76 BAUD_230400,
77 BAUD_250000,
78 BAUD_460800,
79 BAUD_921600,
80 BAUD_1000000,
81 BAUD_1500000,
82 BAUD_2000000,
83 BAUD_2470000,
85 BAUD_MIN = BAUD_AUTO,
86 BAUD_MAX = BAUD_2470000,
87 } baudRate_e;
89 extern const uint32_t baudRates[];
91 // serial port identifiers are now fixed, these values are used by MSP commands.
92 typedef enum {
93 SERIAL_PORT_NONE = -1,
94 SERIAL_PORT_USART1 = 0,
95 SERIAL_PORT_USART2,
96 SERIAL_PORT_USART3,
97 SERIAL_PORT_USART4,
98 SERIAL_PORT_USART5,
99 SERIAL_PORT_USART6,
100 SERIAL_PORT_USART7,
101 SERIAL_PORT_USART8,
102 SERIAL_PORT_USB_VCP = 20,
103 SERIAL_PORT_SOFTSERIAL1 = 30,
104 SERIAL_PORT_SOFTSERIAL2,
105 SERIAL_PORT_IDENTIFIER_MAX = SERIAL_PORT_SOFTSERIAL2
106 } serialPortIdentifier_e;
108 extern const serialPortIdentifier_e serialPortIdentifiers[SERIAL_PORT_COUNT];
111 // runtime
113 typedef struct serialPortUsage_s {
114 serialPortIdentifier_e identifier;
115 serialPort_t *serialPort;
116 serialPortFunction_e function;
117 } serialPortUsage_t;
119 serialPort_t *findSharedSerialPort(uint32_t functionMask, serialPortFunction_e sharedWithFunction);
120 serialPort_t *findNextSharedSerialPort(uint32_t functionMask, serialPortFunction_e sharedWithFunction);
123 // configuration
125 typedef struct serialPortConfig_s {
126 uint32_t functionMask;
127 serialPortIdentifier_e identifier;
128 uint8_t msp_baudrateIndex;
129 uint8_t gps_baudrateIndex;
130 uint8_t peripheral_baudrateIndex;
131 uint8_t telemetry_baudrateIndex; // not used for all telemetry systems, e.g. HoTT only works at 19200.
132 } serialPortConfig_t;
134 typedef struct serialConfig_s {
135 serialPortConfig_t portConfigs[SERIAL_PORT_COUNT];
136 uint8_t reboot_character; // which byte is used to reboot. Default 'R', could be changed carefully to something else.
137 } serialConfig_t;
139 PG_DECLARE(serialConfig_t, serialConfig);
141 typedef void serialConsumer(uint8_t);
144 // configuration
146 void serialInit(bool softserialEnabled);
147 void serialRemovePort(serialPortIdentifier_e identifier);
148 uint8_t serialGetAvailablePortCount(void);
149 bool serialIsPortAvailable(serialPortIdentifier_e identifier);
150 bool isSerialConfigValid(const serialConfig_t *serialConfig);
151 serialPortConfig_t *serialFindPortConfiguration(serialPortIdentifier_e identifier);
152 bool doesConfigurationUsePort(serialPortIdentifier_e portIdentifier);
153 serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function);
154 serialPortConfig_t *findNextSerialPortConfig(serialPortFunction_e function);
156 portSharing_e determinePortSharing(const serialPortConfig_t *portConfig, serialPortFunction_e function);
157 bool isSerialPortShared(const serialPortConfig_t *portConfig, uint32_t functionMask, serialPortFunction_e sharedWithFunction);
159 serialPortUsage_t *findSerialPortUsageByIdentifier(serialPortIdentifier_e identifier);
160 int findSerialPortIndexByIdentifier(serialPortIdentifier_e identifier);
162 // runtime
164 serialPort_t *openSerialPort(
165 serialPortIdentifier_e identifier,
166 serialPortFunction_e function,
167 serialReceiveCallbackPtr callback,
168 void *rxCallbackData,
169 uint32_t baudrate,
170 portMode_t mode,
171 portOptions_t options
173 void closeSerialPort(serialPort_t *serialPort);
175 void waitForSerialPortToFinishTransmitting(serialPort_t *serialPort);
177 baudRate_e lookupBaudRateIndex(uint32_t baudRate);
181 // msp/cli/bootloader
183 void serialPassthrough(serialPort_t *left, serialPort_t *right, serialConsumer *leftC, serialConsumer *rightC);