Skip to content

Commit edfadc7

Browse files
authored
Merge pull request #409 from klutvott123/update-msp-protocol
Update MSP protocol
2 parents 6fb8bf6 + e704adb commit edfadc7

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/SCRIPTS/BF/MSP/common.lua

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ local MSP_STARTFLAG = bit32.lshift(1,4)
66
local mspSeq = 0
77
local mspRemoteSeq = 0
88
local mspRxBuf = {}
9+
local mspRxSize = 0
910
local mspRxCRC = 0
1011
local mspRxReq = 0
1112
local 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)
6863
end
6964

7065
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)
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

src/SCRIPTS/BF/MSP/sp.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ local REPLY_FRAME_ID = 0x32
77
local lastSensorId, lastFrameId, lastDataId, lastValue
88

99
protocol.mspSend = function(payload)
10-
local dataId = payload[1] + bit32.lshift(payload[2],8)
11-
local value = payload[3] + bit32.lshift(payload[4],8)
12-
+ bit32.lshift(payload[5],16) + bit32.lshift(payload[6],24)
10+
local dataId = payload[1] + bit32.lshift(payload[2], 8)
11+
local value = 0
12+
for i = 3, #payload do
13+
value = value + bit32.lshift(payload[i], (i - 3) * 8)
14+
end
1315
return protocol.push(LOCAL_SENSOR_ID, REQUEST_FRAME_ID, dataId, value)
1416
end
1517

0 commit comments

Comments
 (0)