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/>.
20 #include "drivers/time.h"
21 #include "io/serial.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
37 MSP_HEADER_V2_OVER_V1
,
38 MSP_PAYLOAD_V2_OVER_V1
,
39 MSP_CHECKSUM_V2_OVER_V1
,
42 MSP_PAYLOAD_V2_NATIVE
,
43 MSP_CHECKSUM_V2_NATIVE
,
49 MSP_EVALUATE_NON_MSP_DATA
,
51 } mspEvaluateNonMspData_e
;
55 MSP_PENDING_BOOTLOADER
,
57 } mspPendingSystemRequest_e
;
59 #define MSP_PORT_INBUF_SIZE 192
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!
65 #define MSP_PORT_OUTBUF_SIZE 512
68 typedef struct __attribute__((packed
)) {
73 typedef struct __attribute__((packed
)) {
77 typedef struct __attribute__((packed
)) {
83 #define MSP_MAX_HEADER_SIZE 9
86 typedef struct mspPort_s
{
87 struct serialPort_s
*port
; // null when port unused.
88 timeMs_t lastActivityMs
;
89 mspPendingSystemRequest_e pendingRequest
;
91 uint8_t inBuf
[MSP_PORT_INBUF_SIZE
];
93 uint_fast16_t dataSize
;
94 mspVersion_e mspVersion
;
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
);