/
msp_serial.h
123 lines (105 loc) · 3.27 KB
/
msp_serial.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* This file is part of Cleanflight and Betaflight.
*
* Cleanflight and Betaflight are free software. You can redistribute
* this software and/or modify this software under the terms of the
* GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* Cleanflight and Betaflight are distributed in the hope that they
* will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software.
*
* If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "drivers/time.h"
#include "msp/msp.h"
// Each MSP port requires state and a receive buffer, revisit this default if someone needs more than 3 MSP ports.
#define MAX_MSP_PORT_COUNT 3
typedef enum {
MSP_IDLE,
MSP_HEADER_START,
MSP_HEADER_M,
MSP_HEADER_X,
MSP_HEADER_V1,
MSP_PAYLOAD_V1,
MSP_CHECKSUM_V1,
MSP_HEADER_V2_OVER_V1,
MSP_PAYLOAD_V2_OVER_V1,
MSP_CHECKSUM_V2_OVER_V1,
MSP_HEADER_V2_NATIVE,
MSP_PAYLOAD_V2_NATIVE,
MSP_CHECKSUM_V2_NATIVE,
MSP_COMMAND_RECEIVED
} mspState_e;
typedef enum {
MSP_PACKET_COMMAND,
MSP_PACKET_REPLY
} mspPacketType_e;
typedef enum {
MSP_EVALUATE_NON_MSP_DATA,
MSP_SKIP_NON_MSP_DATA
} mspEvaluateNonMspData_e;
typedef enum {
MSP_PENDING_NONE,
MSP_PENDING_BOOTLOADER_ROM,
MSP_PENDING_CLI,
MSP_PENDING_BOOTLOADER_FLASH,
} mspPendingSystemRequest_e;
#define MSP_PORT_INBUF_SIZE 192
#ifdef USE_FLASHFS
#ifdef STM32F1
#define MSP_PORT_DATAFLASH_BUFFER_SIZE 1024
#else
#define MSP_PORT_DATAFLASH_BUFFER_SIZE 4096
#endif
#define MSP_PORT_DATAFLASH_INFO_SIZE 16
#define MSP_PORT_OUTBUF_SIZE (MSP_PORT_DATAFLASH_BUFFER_SIZE + MSP_PORT_DATAFLASH_INFO_SIZE)
#else
#define MSP_PORT_OUTBUF_SIZE 256
#endif
typedef struct __attribute__((packed)) {
uint8_t size;
uint8_t cmd;
} mspHeaderV1_t;
typedef struct __attribute__((packed)) {
uint16_t size;
} mspHeaderJUMBO_t;
typedef struct __attribute__((packed)) {
uint8_t flags;
uint16_t cmd;
uint16_t size;
} mspHeaderV2_t;
#define MSP_MAX_HEADER_SIZE 9
struct serialPort_s;
typedef struct mspPort_s {
struct serialPort_s *port; // null when port unused.
timeMs_t lastActivityMs;
mspPendingSystemRequest_e pendingRequest;
mspState_e c_state;
mspPacketType_e packetType;
uint8_t inBuf[MSP_PORT_INBUF_SIZE];
uint16_t cmdMSP;
uint8_t cmdFlags;
mspVersion_e mspVersion;
uint_fast16_t offset;
uint_fast16_t dataSize;
uint8_t checksum1;
uint8_t checksum2;
bool sharedWithTelemetry;
mspDescriptor_t descriptor;
} mspPort_t;
void mspSerialInit(void);
bool mspSerialWaiting(void);
void mspSerialProcess(mspEvaluateNonMspData_e evaluateNonMspData, mspProcessCommandFnPtr mspProcessCommandFn, mspProcessReplyFnPtr mspProcessReplyFn);
void mspSerialAllocatePorts(void);
void mspSerialReleasePortIfAllocated(struct serialPort_s *serialPort);
void mspSerialReleaseSharedTelemetryPorts(void);
int mspSerialPush(uint8_t cmd, uint8_t *data, int datalen, mspDirection_e direction);
uint32_t mspSerialTxBytesFree(void);