Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / msp / msp_serial.h
blob43f708f43d5996a8805b8effe91de92fe8b100ab
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 MSP_IDLE,
34 MSP_HEADER_START,
35 MSP_HEADER_M,
36 MSP_HEADER_X,
38 MSP_HEADER_V1,
39 MSP_PAYLOAD_V1,
40 MSP_CHECKSUM_V1,
42 MSP_HEADER_V2_OVER_V1,
43 MSP_PAYLOAD_V2_OVER_V1,
44 MSP_CHECKSUM_V2_OVER_V1,
46 MSP_HEADER_V2_NATIVE,
47 MSP_PAYLOAD_V2_NATIVE,
48 MSP_CHECKSUM_V2_NATIVE,
50 MSP_COMMAND_RECEIVED
51 } mspState_e;
53 typedef enum {
54 MSP_PACKET_COMMAND,
55 MSP_PACKET_REPLY
56 } mspPacketType_e;
58 typedef enum {
59 MSP_EVALUATE_NON_MSP_DATA,
60 MSP_SKIP_NON_MSP_DATA
61 } mspEvaluateNonMspData_e;
63 typedef enum {
64 MSP_PENDING_NONE,
65 MSP_PENDING_BOOTLOADER_ROM,
66 MSP_PENDING_CLI,
67 MSP_PENDING_BOOTLOADER_FLASH,
68 } mspPendingSystemRequest_e;
70 #define MSP_PORT_INBUF_SIZE 192
71 #ifdef USE_FLASHFS
72 #ifdef STM32F1
73 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 1024
74 #else
75 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
76 #endif
77 #define MSP_PORT_DATAFLASH_INFO_SIZE 16
78 #define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE)
79 #else
80 #define MSP_PORT_OUTBUF_SIZE 320 // As of 2021/08/10 MSP_BOXNAMES generates a 307 byte response for page 1.
81 #endif
83 typedef struct __attribute__((packed)) {
84 uint8_t size;
85 uint8_t cmd;
86 } mspHeaderV1_t;
88 typedef struct __attribute__((packed)) {
89 uint16_t size;
90 } mspHeaderJUMBO_t;
92 typedef struct __attribute__((packed)) {
93 uint8_t flags;
94 uint16_t cmd;
95 uint16_t size;
96 } mspHeaderV2_t;
98 #define MSP_MAX_HEADER_SIZE 9
100 struct serialPort_s;
101 typedef struct mspPort_s {
102 struct serialPort_s *port; // null when port unused.
103 timeMs_t lastActivityMs;
104 mspPendingSystemRequest_e pendingRequest;
105 mspState_e c_state;
106 mspPacketType_e packetType;
107 uint8_t inBuf[MSP_PORT_INBUF_SIZE];
108 uint16_t cmdMSP;
109 uint8_t cmdFlags;
110 mspVersion_e mspVersion;
111 uint_fast16_t offset;
112 uint_fast16_t dataSize;
113 uint8_t checksum1;
114 uint8_t checksum2;
115 bool sharedWithTelemetry;
116 mspDescriptor_t descriptor;
117 } mspPort_t;
119 void mspSerialInit(void);
120 bool mspSerialWaiting(void);
121 void mspSerialProcess(mspEvaluateNonMspData_e evaluateNonMspData, mspProcessCommandFnPtr mspProcessCommandFn, mspProcessReplyFnPtr mspProcessReplyFn);
122 void mspSerialAllocatePorts(void);
123 void mspSerialReleasePortIfAllocated(struct serialPort_s *serialPort);
124 void mspSerialReleaseSharedTelemetryPorts(void);
125 int mspSerialPush(serialPortIdentifier_e port, uint8_t cmd, uint8_t *data, int datalen, mspDirection_e direction);
126 uint32_t mspSerialTxBytesFree(void);