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)
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/>.
23 #include "drivers/time.h"
25 #include "io/serial.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
42 MSP_HEADER_V2_OVER_V1
,
43 MSP_PAYLOAD_V2_OVER_V1
,
44 MSP_CHECKSUM_V2_OVER_V1
,
47 MSP_PAYLOAD_V2_NATIVE
,
48 MSP_CHECKSUM_V2_NATIVE
,
59 MSP_EVALUATE_NON_MSP_DATA
,
61 } mspEvaluateNonMspData_e
;
65 MSP_PENDING_BOOTLOADER_ROM
,
67 MSP_PENDING_BOOTLOADER_FLASH
,
68 } mspPendingSystemRequest_e
;
70 #define MSP_PORT_INBUF_SIZE 192
71 #define MSP_PORT_OUTBUF_SIZE_MIN 320
74 #define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
75 #define MSP_PORT_DATAFLASH_INFO_SIZE 16
76 #define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE)
78 #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.
81 typedef struct __attribute__((packed
)) {
86 typedef struct __attribute__((packed
)) {
90 typedef struct __attribute__((packed
)) {
96 #define MSP_MAX_HEADER_SIZE 9
99 typedef struct mspPort_s
{
100 struct serialPort_s
*port
; // null when port unused.
101 timeMs_t lastActivityMs
;
102 mspPendingSystemRequest_e pendingRequest
;
104 mspPacketType_e packetType
;
105 uint8_t inBuf
[MSP_PORT_INBUF_SIZE
];
108 mspVersion_e mspVersion
;
109 uint_fast16_t offset
;
110 uint_fast16_t dataSize
;
113 bool sharedWithTelemetry
;
114 mspDescriptor_t descriptor
;
117 void mspSerialInit(void);
118 bool mspSerialWaiting(void);
119 void mspSerialProcess(mspEvaluateNonMspData_e evaluateNonMspData
, mspProcessCommandFnPtr mspProcessCommandFn
, mspProcessReplyFnPtr mspProcessReplyFn
);
120 void mspSerialAllocatePorts(void);
121 void mspSerialReleasePortIfAllocated(struct serialPort_s
*serialPort
);
122 void mspSerialReleaseSharedTelemetryPorts(void);
123 mspDescriptor_t
getMspSerialPortDescriptor(const uint8_t portIdentifier
);
124 int mspSerialPush(serialPortIdentifier_e port
, uint8_t cmd
, uint8_t *data
, int datalen
, mspDirection_e direction
, mspVersion_e mspVersion
);
125 uint32_t mspSerialTxBytesFree(void);