Cleaned up implementation of MSP port releasing when shared.
[betaflight.git] / src / main / msp / msp_serial.h
blob1ae0aa71386cef01174c2d93bdff1ada40560038
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"
24 #include "interface/msp.h"
26 // Each MSP port requires state and a receive buffer, revisit this default if someone needs more than 3 MSP ports.
27 #define MAX_MSP_PORT_COUNT 3
29 typedef enum {
30 MSP_IDLE,
31 MSP_HEADER_START,
32 MSP_HEADER_M,
33 MSP_HEADER_X,
35 MSP_HEADER_V1,
36 MSP_PAYLOAD_V1,
37 MSP_CHECKSUM_V1,
39 MSP_HEADER_V2_OVER_V1,
40 MSP_PAYLOAD_V2_OVER_V1,
41 MSP_CHECKSUM_V2_OVER_V1,
43 MSP_HEADER_V2_NATIVE,
44 MSP_PAYLOAD_V2_NATIVE,
45 MSP_CHECKSUM_V2_NATIVE,
47 MSP_COMMAND_RECEIVED
48 } mspState_e;
50 typedef enum {
51 MSP_PACKET_COMMAND,
52 MSP_PACKET_REPLY
53 } mspPacketType_e;
55 typedef enum {
56 MSP_EVALUATE_NON_MSP_DATA,
57 MSP_SKIP_NON_MSP_DATA
58 } mspEvaluateNonMspData_e;
60 typedef enum {
61 MSP_PENDING_NONE,
62 MSP_PENDING_BOOTLOADER,
63 MSP_PENDING_CLI
64 } mspPendingSystemRequest_e;
66 #define MSP_PORT_INBUF_SIZE 192
67 #ifdef USE_FLASHFS
68 #ifdef STM32F1
69 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 1024
70 #else
71 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
72 #endif
73 #define MSP_PORT_DATAFLASH_INFO_SIZE 16
74 #define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE)
75 #else
76 #define MSP_PORT_OUTBUF_SIZE 256
77 #endif
79 typedef struct __attribute__((packed)) {
80 uint8_t size;
81 uint8_t cmd;
82 } mspHeaderV1_t;
84 typedef struct __attribute__((packed)) {
85 uint16_t size;
86 } mspHeaderJUMBO_t;
88 typedef struct __attribute__((packed)) {
89 uint8_t flags;
90 uint16_t cmd;
91 uint16_t size;
92 } mspHeaderV2_t;
94 #define MSP_MAX_HEADER_SIZE 9
96 struct serialPort_s;
97 typedef struct mspPort_s {
98 struct serialPort_s *port; // null when port unused.
99 timeMs_t lastActivityMs;
100 mspPendingSystemRequest_e pendingRequest;
101 mspState_e c_state;
102 mspPacketType_e packetType;
103 uint8_t inBuf[MSP_PORT_INBUF_SIZE];
104 uint16_t cmdMSP;
105 uint8_t cmdFlags;
106 mspVersion_e mspVersion;
107 uint_fast16_t offset;
108 uint_fast16_t dataSize;
109 uint8_t checksum1;
110 uint8_t checksum2;
111 } mspPort_t;
113 void mspSerialInit(void);
114 bool mspSerialWaiting(void);
115 void mspSerialProcess(mspEvaluateNonMspData_e evaluateNonMspData, mspProcessCommandFnPtr mspProcessCommandFn, mspProcessReplyFnPtr mspProcessReplyFn);
116 void mspSerialAllocatePorts(void);
117 void mspSerialReleasePortIfAllocated(struct serialPort_s *serialPort);
118 void mspSerialReleaseSharedTelemetryPorts(void);
119 int mspSerialPush(uint8_t cmd, uint8_t *data, int datalen, mspDirection_e direction);
120 uint32_t mspSerialTxBytesFree(void);