Position hold depends on GPS (#14101)
[betaflight.git] / src / main / msp / msp_serial.h
bloba080408fc796018c6c5acdeb710213f9085db19b
1 /*
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)
8 * any later version.
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/>.
21 #pragma once
23 #include "drivers/time.h"
25 #include "io/serial.h"
27 #include "msp/msp.h"
29 // Each MSP port requires state and a receive buffer, revisit this default if someone needs more than 3 MSP ports.
30 #define MAX_MSP_PORT_COUNT 3
32 typedef enum {
33 PORT_IDLE,
34 PORT_MSP_PACKET,
35 PORT_CLI_ACTIVE,
36 PORT_CLI_CMD
37 } mspPortState_e;
39 typedef enum {
40 MSP_IDLE,
41 MSP_HEADER_START,
42 MSP_HEADER_M,
43 MSP_HEADER_X,
45 MSP_HEADER_V1,
46 MSP_PAYLOAD_V1,
47 MSP_CHECKSUM_V1,
49 MSP_HEADER_V2_OVER_V1,
50 MSP_PAYLOAD_V2_OVER_V1,
51 MSP_CHECKSUM_V2_OVER_V1,
53 MSP_HEADER_V2_NATIVE,
54 MSP_PAYLOAD_V2_NATIVE,
55 MSP_CHECKSUM_V2_NATIVE,
57 MSP_COMMAND_RECEIVED
58 } mspPacketState_e;
60 typedef enum {
61 MSP_PACKET_COMMAND,
62 MSP_PACKET_REPLY
63 } mspPacketType_e;
65 typedef enum {
66 MSP_EVALUATE_NON_MSP_DATA,
67 MSP_SKIP_NON_MSP_DATA
68 } mspEvaluateNonMspData_e;
70 typedef enum {
71 MSP_PENDING_NONE,
72 MSP_PENDING_BOOTLOADER_ROM,
73 MSP_PENDING_CLI,
74 MSP_PENDING_BOOTLOADER_FLASH,
75 } mspPendingSystemRequest_e;
77 #define MSP_PORT_INBUF_SIZE 192
78 #define MSP_PORT_OUTBUF_SIZE_MIN 512 // As of 2021/08/10 MSP_BOXNAMES generates a 307 byte response for page 1. There has been overflow issues with 320 byte buffer.
80 #ifdef USE_FLASHFS
81 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
82 #define MSP_PORT_DATAFLASH_INFO_SIZE 16
83 #define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE)
84 #else
85 #define MSP_PORT_OUTBUF_SIZE MSP_PORT_OUTBUF_SIZE_MIN // As of 2021/08/10 MSP_BOXNAMES generates a 307 byte response for page 1.
86 #endif
88 typedef struct __attribute__((packed)) {
89 uint8_t size;
90 uint8_t cmd;
91 } mspHeaderV1_t;
93 typedef struct __attribute__((packed)) {
94 uint16_t size;
95 } mspHeaderJUMBO_t;
97 typedef struct __attribute__((packed)) {
98 uint8_t flags;
99 uint16_t cmd;
100 uint16_t size;
101 } mspHeaderV2_t;
103 #define MSP_MAX_HEADER_SIZE 9
105 struct serialPort_s;
106 typedef struct mspPort_s {
107 struct serialPort_s *port; // null when port unused.
108 timeMs_t lastActivityMs;
109 mspPendingSystemRequest_e pendingRequest;
110 mspPortState_e portState;
111 mspPacketState_e packetState;
112 mspPacketType_e packetType;
113 uint8_t inBuf[MSP_PORT_INBUF_SIZE];
114 uint16_t cmdMSP;
115 uint8_t cmdFlags;
116 mspVersion_e mspVersion;
117 uint_fast16_t offset;
118 uint_fast16_t dataSize;
119 uint8_t checksum1;
120 uint8_t checksum2;
121 bool sharedWithTelemetry;
122 mspDescriptor_t descriptor;
123 } mspPort_t;
125 void mspSerialInit(void);
126 bool mspSerialWaiting(void);
127 void mspSerialProcess(mspEvaluateNonMspData_e evaluateNonMspData, mspProcessCommandFnPtr mspProcessCommandFn, mspProcessReplyFnPtr mspProcessReplyFn);
128 void mspSerialAllocatePorts(void);
129 void mspSerialReleasePortIfAllocated(struct serialPort_s *serialPort);
130 void mspSerialReleaseSharedTelemetryPorts(void);
131 mspDescriptor_t getMspSerialPortDescriptor(const serialPortIdentifier_e portIdentifier);
132 int mspSerialPush(serialPortIdentifier_e port, uint8_t cmd, uint8_t *data, int datalen, mspDirection_e direction, mspVersion_e mspVersion);
133 uint32_t mspSerialTxBytesFree(void);