Version 1.0 bump
[inav/snaewe.git] / src / main / io / serial.h
blobb5dd13f010137c1e9f9ada171dff65d2d4da6c6e
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 typedef enum {
21 PORTSHARING_UNUSED = 0,
22 PORTSHARING_NOT_SHARED,
23 PORTSHARING_SHARED
24 } portSharing_e;
26 typedef enum {
27 FUNCTION_NONE = 0,
28 FUNCTION_MSP = (1 << 0), // 1
29 FUNCTION_GPS = (1 << 1), // 2
30 FUNCTION_TELEMETRY_FRSKY = (1 << 2), // 4
31 FUNCTION_TELEMETRY_HOTT = (1 << 3), // 8
32 FUNCTION_TELEMETRY_LTM = (1 << 4), // 16
33 FUNCTION_TELEMETRY_SMARTPORT = (1 << 5), // 32
34 FUNCTION_RX_SERIAL = (1 << 6), // 64
35 FUNCTION_BLACKBOX = (1 << 7), // 128
36 } serialPortFunction_e;
38 typedef enum {
39 BAUD_AUTO = 0,
40 BAUD_9600,
41 BAUD_19200,
42 BAUD_38400,
43 BAUD_57600,
44 BAUD_115200,
45 BAUD_230400,
46 BAUD_250000,
47 } baudRate_e;
49 extern const uint32_t baudRates[];
51 // serial port identifiers are now fixed, these values are used by MSP commands.
52 typedef enum {
53 SERIAL_PORT_NONE = -1,
54 SERIAL_PORT_USART1 = 0,
55 SERIAL_PORT_USART2,
56 SERIAL_PORT_USART3,
57 SERIAL_PORT_USART4,
58 SERIAL_PORT_USB_VCP = 20,
59 SERIAL_PORT_SOFTSERIAL1 = 30,
60 SERIAL_PORT_SOFTSERIAL2,
61 SERIAL_PORT_IDENTIFIER_MAX = SERIAL_PORT_SOFTSERIAL2
62 } serialPortIdentifier_e;
64 extern const serialPortIdentifier_e serialPortIdentifiers[SERIAL_PORT_COUNT];
67 // runtime
69 typedef struct serialPortUsage_s {
70 serialPortIdentifier_e identifier;
71 serialPort_t *serialPort;
72 serialPortFunction_e function;
73 } serialPortUsage_t;
75 serialPort_t *findSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction);
76 serialPort_t *findNextSharedSerialPort(uint16_t functionMask, serialPortFunction_e sharedWithFunction);
79 // configuration
81 typedef struct serialPortConfig_s {
82 serialPortIdentifier_e identifier;
83 uint16_t functionMask;
84 uint8_t msp_baudrateIndex;
85 uint8_t gps_baudrateIndex;
86 uint8_t blackbox_baudrateIndex;
87 uint8_t telemetry_baudrateIndex; // not used for all telemetry systems, e.g. HoTT only works at 19200.
88 } serialPortConfig_t;
90 typedef struct serialConfig_s {
91 uint8_t reboot_character; // which byte is used to reboot. Default 'R', could be changed carefully to something else.
92 serialPortConfig_t portConfigs[SERIAL_PORT_COUNT];
93 } serialConfig_t;
97 // configuration
99 void serialRemovePort(serialPortIdentifier_e identifier);
100 uint8_t serialGetAvailablePortCount(void);
101 bool serialIsPortAvailable(serialPortIdentifier_e identifier);
102 bool isSerialConfigValid(serialConfig_t *serialConfig);
103 serialPortConfig_t *serialFindPortConfiguration(serialPortIdentifier_e identifier);
104 bool doesConfigurationUsePort(serialPortIdentifier_e portIdentifier);
105 serialPortConfig_t *findSerialPortConfig(serialPortFunction_e function);
106 serialPortConfig_t *findNextSerialPortConfig(serialPortFunction_e function);
108 portSharing_e determinePortSharing(serialPortConfig_t *portConfig, serialPortFunction_e function);
109 bool isSerialPortShared(serialPortConfig_t *portConfig, uint16_t functionMask, serialPortFunction_e sharedWithFunction);
114 // runtime
116 serialPort_t *openSerialPort(
117 serialPortIdentifier_e identifier,
118 serialPortFunction_e function,
119 serialReceiveCallbackPtr callback,
120 uint32_t baudrate,
121 portMode_t mode,
122 portOptions_t options
124 void closeSerialPort(serialPort_t *serialPort);
126 void waitForSerialPortToFinishTransmitting(serialPort_t *serialPort);
128 baudRate_e lookupBaudRateIndex(uint32_t baudRate);
132 // msp/cli/bootloader
134 void evaluateOtherData(serialPort_t *serialPort, uint8_t receivedChar);
135 void handleSerial(void);