@@ -6,6 +6,7 @@ local MSP_STARTFLAG = bit32.lshift(1,4)
6
6
local mspSeq = 0
7
7
local mspRemoteSeq = 0
8
8
local mspRxBuf = {}
9
+ local mspRxSize = 0
9
10
local mspRxCRC = 0
10
11
local mspRxReq = 0
11
12
local mspStarted = false
@@ -37,12 +38,6 @@ function mspProcessTxQ()
37
38
end
38
39
if i <= protocol .maxTxBufferSize then
39
40
payload [i ] = mspTxCRC
40
- i = i + 1
41
- -- zero fill
42
- while i <= protocol .maxTxBufferSize do
43
- payload [i ] = 0
44
- i = i + 1
45
- end
46
41
protocol .mspSend (payload )
47
42
mspTxBuf = {}
48
43
mspTxIdx = 1
@@ -68,25 +63,30 @@ function mspSendRequest(cmd, payload)
68
63
end
69
64
70
65
function mspReceivedReply (payload )
71
- local idx = 1
72
- local head = payload [idx ]
73
- local err_flag = (bit32.band (head ,0x20 ) ~= 0 )
66
+ local idx = 1
67
+ local status = payload [idx ]
68
+ local err = bit32.btest (status , 0x80 )
69
+ local version = bit32.rshift (bit32.band (status , 0x60 ), 5 )
70
+ local start = bit32.btest (status , 0x10 )
71
+ local seq = bit32.band (status , 0x0F )
74
72
idx = idx + 1
75
- if err_flag then
76
- -- error flag set
73
+ if err then
77
74
mspStarted = false
78
75
return nil
79
76
end
80
- local start = (bit32.band (head ,0x10 ) ~= 0 )
81
- local seq = bit32.band (head ,0x0F )
82
77
if start then
83
- -- start flag set
84
78
mspRxBuf = {}
85
79
mspRxSize = payload [idx ]
86
- mspRxCRC = bit32.bxor (mspRxSize ,mspLastReq )
87
- mspRxReq = mspLastReq
80
+ mspRxReq = mspLastReq
88
81
idx = idx + 1
89
- mspStarted = true
82
+ if version == 1 then
83
+ mspRxReq = payload [idx ]
84
+ idx = idx + 1
85
+ end
86
+ mspRxCRC = bit32.bxor (mspRxSize , mspRxReq )
87
+ if mspRxReq == mspLastReq then
88
+ mspStarted = true
89
+ end
90
90
elseif not mspStarted then
91
91
return nil
92
92
elseif bit32.band (mspRemoteSeq + 1 , 0x0F ) ~= seq then
@@ -95,7 +95,7 @@ function mspReceivedReply(payload)
95
95
end
96
96
while (idx <= protocol .maxRxBufferSize ) and (# mspRxBuf < mspRxSize ) do
97
97
mspRxBuf [# mspRxBuf + 1 ] = payload [idx ]
98
- mspRxCRC = bit32.bxor (mspRxCRC ,payload [idx ])
98
+ mspRxCRC = bit32.bxor (mspRxCRC , payload [idx ])
99
99
idx = idx + 1
100
100
end
101
101
if idx > protocol .maxRxBufferSize then
@@ -104,7 +104,7 @@ function mspReceivedReply(payload)
104
104
end
105
105
mspStarted = false
106
106
-- check CRC
107
- if mspRxCRC ~= payload [idx ] then
107
+ if mspRxCRC ~= payload [idx ] and version == 0 then
108
108
return nil
109
109
end
110
110
return mspRxBuf
0 commit comments