@@ -6,6 +6,7 @@ local MSP_STARTFLAG = bit32.lshift(1,4)
66local mspSeq = 0
77local mspRemoteSeq = 0
88local mspRxBuf = {}
9+ local mspRxSize = 0
910local mspRxCRC = 0
1011local mspRxReq = 0
1112local mspStarted = false
@@ -37,12 +38,6 @@ function mspProcessTxQ()
3738 end
3839 if i <= protocol .maxTxBufferSize then
3940 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
4641 protocol .mspSend (payload )
4742 mspTxBuf = {}
4843 mspTxIdx = 1
@@ -68,25 +63,30 @@ function mspSendRequest(cmd, payload)
6863end
6964
7065function 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 )
7472 idx = idx + 1
75- if err_flag then
76- -- error flag set
73+ if err then
7774 mspStarted = false
7875 return nil
7976 end
80- local start = (bit32.band (head ,0x10 ) ~= 0 )
81- local seq = bit32.band (head ,0x0F )
8277 if start then
83- -- start flag set
8478 mspRxBuf = {}
8579 mspRxSize = payload [idx ]
86- mspRxCRC = bit32.bxor (mspRxSize ,mspLastReq )
87- mspRxReq = mspLastReq
80+ mspRxReq = mspLastReq
8881 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
9090 elseif not mspStarted then
9191 return nil
9292 elseif bit32.band (mspRemoteSeq + 1 , 0x0F ) ~= seq then
@@ -95,7 +95,7 @@ function mspReceivedReply(payload)
9595 end
9696 while (idx <= protocol .maxRxBufferSize ) and (# mspRxBuf < mspRxSize ) do
9797 mspRxBuf [# mspRxBuf + 1 ] = payload [idx ]
98- mspRxCRC = bit32.bxor (mspRxCRC ,payload [idx ])
98+ mspRxCRC = bit32.bxor (mspRxCRC , payload [idx ])
9999 idx = idx + 1
100100 end
101101 if idx > protocol .maxRxBufferSize then
@@ -104,7 +104,7 @@ function mspReceivedReply(payload)
104104 end
105105 mspStarted = false
106106 -- check CRC
107- if mspRxCRC ~= payload [idx ] then
107+ if mspRxCRC ~= payload [idx ] and version == 0 then
108108 return nil
109109 end
110110 return mspRxBuf
0 commit comments