updates
[inav.git] / src / main / io / serial.h
blob7766679106bc143aa0bfba620076af3c895def18
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 } serialPortFunction_e;
62 #define FUNCTION_VTX_MSP FUNCTION_MSP_OSD
64 typedef enum {
65 BAUD_AUTO = 0,
66 BAUD_1200,
67 BAUD_2400,
68 BAUD_4800,
69 BAUD_9600,
70 BAUD_19200,
71 BAUD_38400,
72 BAUD_57600,
73 BAUD_115200,
74 BAUD_230400,
75 BAUD_250000,
76 BAUD_460800,
77 BAUD_921600,
78 BAUD_1000000,
79 BAUD_1500000,
80 BAUD_2000000,
81 BAUD_2470000,
83 BAUD_MIN = BAUD_AUTO,
84 BAUD_MAX = BAUD_2470000,
85 } baudRate_e;
87 extern const uint32_t baudRates[];
89 // serial port identifiers are now fixed, these values are used by MSP commands.
90 typedef enum {
91 SERIAL_PORT_NONE = -1,
92 SERIAL_PORT_USART1 = 0,
93 SERIAL_PORT_USART2,
94 SERIAL_PORT_USART3,
95 SERIAL_PORT_USART4,
96 SERIAL_PORT_USART5,
97 SERIAL_PORT_USART6,
98 SERIAL_PORT_USART7,
99 SERIAL_PORT_USART8,
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];
109 // runtime
111 typedef struct serialPortUsage_s {
112 serialPortIdentifier_e identifier;
113 serialPort_t *serialPort;
114 serialPortFunction_e function;
115 } serialPortUsage_t;
117 serialPort_t *findSharedSerialPort(uint32_t functionMask, serialPortFunction_e sharedWithFunction);
118 serialPort_t *findNextSharedSerialPort(uint32_t functionMask, serialPortFunction_e sharedWithFunction);
121 // configuration
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.
135 } serialConfig_t;
137 PG_DECLARE(serialConfig_t, serialConfig);
139 typedef void serialConsumer(uint8_t);
142 // configuration
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);
160 // runtime
162 serialPort_t *openSerialPort(
163 serialPortIdentifier_e identifier,
164 serialPortFunction_e function,
165 serialReceiveCallbackPtr callback,
166 void *rxCallbackData,
167 uint32_t baudrate,
168 portMode_t mode,
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);