Set blackbox file handler to NULL after closing file
[inav.git] / src / main / msp / msp_serial.h
blob67487606da071fb3cadc043bac759213fb83adb4
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 "drivers/time.h"
21 #include "io/serial.h"
22 #include "msp/msp.h"
24 // Each MSP port requires state and a receive buffer, revisit this default if someone needs more than 3 MSP ports.
25 #define MAX_MSP_PORT_COUNT 3
27 typedef enum {
28 MSP_IDLE,
29 MSP_HEADER_START,
30 MSP_HEADER_M,
31 MSP_HEADER_X,
33 MSP_HEADER_V1,
34 MSP_PAYLOAD_V1,
35 MSP_CHECKSUM_V1,
37 MSP_HEADER_V2_OVER_V1,
38 MSP_PAYLOAD_V2_OVER_V1,
39 MSP_CHECKSUM_V2_OVER_V1,
41 MSP_HEADER_V2_NATIVE,
42 MSP_PAYLOAD_V2_NATIVE,
43 MSP_CHECKSUM_V2_NATIVE,
45 MSP_COMMAND_RECEIVED
46 } mspState_e;
48 typedef enum {
49 MSP_EVALUATE_NON_MSP_DATA,
50 MSP_SKIP_NON_MSP_DATA
51 } mspEvaluateNonMspData_e;
53 typedef enum {
54 MSP_PENDING_NONE,
55 MSP_PENDING_BOOTLOADER,
56 MSP_PENDING_CLI
57 } mspPendingSystemRequest_e;
59 #define MSP_PORT_INBUF_SIZE 192
60 #ifdef USE_FLASHFS
61 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
62 #define MSP_PORT_DATAFLASH_INFO_SIZE 16
63 #define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE) // WARNING! Must fit in stack!
64 #else
65 #define MSP_PORT_OUTBUF_SIZE 512
66 #endif
68 typedef struct __attribute__((packed)) {
69 uint8_t size;
70 uint8_t cmd;
71 } mspHeaderV1_t;
73 typedef struct __attribute__((packed)) {
74 uint16_t size;
75 } mspHeaderJUMBO_t;
77 typedef struct __attribute__((packed)) {
78 uint8_t flags;
79 uint16_t cmd;
80 uint16_t size;
81 } mspHeaderV2_t;
83 #define MSP_MAX_HEADER_SIZE 9
85 struct serialPort_s;
86 typedef struct mspPort_s {
87 struct serialPort_s *port; // null when port unused.
88 timeMs_t lastActivityMs;
89 mspPendingSystemRequest_e pendingRequest;
90 mspState_e c_state;
91 uint8_t inBuf[MSP_PORT_INBUF_SIZE];
92 uint_fast16_t offset;
93 uint_fast16_t dataSize;
94 mspVersion_e mspVersion;
95 uint8_t cmdFlags;
96 uint16_t cmdMSP;
97 uint8_t checksum1;
98 uint8_t checksum2;
99 } mspPort_t;
102 void mspSerialInit(void);
103 void resetMspPort(mspPort_t *mspPortToReset, serialPort_t *serialPort);
104 void mspSerialProcess(mspEvaluateNonMspData_e evaluateNonMspData, mspProcessCommandFnPtr mspProcessCommandFn);
105 void mspSerialProcessOnePort(mspPort_t * const mspPort, mspEvaluateNonMspData_e evaluateNonMspData, mspProcessCommandFnPtr mspProcessCommandFn);
106 void mspSerialAllocatePorts(void);
107 void mspSerialReleasePortIfAllocated(struct serialPort_s *serialPort);
108 int mspSerialPushPort(uint16_t cmd, const uint8_t *data, int datalen, mspPort_t *mspPort, mspVersion_e version);
109 int mspSerialPush(uint8_t cmd, const uint8_t *data, int datalen);
110 int mspSerialPushVersion(uint8_t cmd, const uint8_t *data, int datalen, mspVersion_e version);
111 uint32_t mspSerialTxBytesFree(serialPort_t *port);
112 mspPort_t * mspSerialPortFind(const struct serialPort_s *serialPort);