|
| 1 | +local MSP_BOARD_INFO = 4 |
| 2 | + |
| 3 | +local boardInfoReceived = false |
| 4 | + |
| 5 | +local boardIdentifier = "" |
| 6 | +local hardwareRevision = 0 |
| 7 | +local boardType = 0 |
| 8 | +local targetCapabilities = 0 |
| 9 | +local targetName = "" |
| 10 | +local boardName = "" |
| 11 | +local manufacturerId = "" |
| 12 | +local signature = {} |
| 13 | +local mcuTypeId = 255 |
| 14 | +local configurationState = 0 |
| 15 | +local gyroSampleRateHz = 0 |
| 16 | +local configurationProblems = 0 |
| 17 | +local spiRegisteredDeviceCount = 0 |
| 18 | +local i2cRegisteredDeviceCount = 0 |
| 19 | + |
| 20 | +local lastRunTS = 0 |
| 21 | +local INTERVAL = 100 |
| 22 | + |
| 23 | +local function processMspReply(cmd, payload) |
| 24 | + if cmd == MSP_BOARD_INFO then |
| 25 | + local length |
| 26 | + local i = 1 |
| 27 | + length = 4 |
| 28 | + for c = 1, 4 do |
| 29 | + boardIdentifier = boardIdentifier..string.char(payload[i]) |
| 30 | + i = i + 1 |
| 31 | + end |
| 32 | + for idx = 1, 2 do |
| 33 | + local raw_val = bit32.lshift(payload[i], (idx-1)*8) |
| 34 | + hardwareRevision = bit32.bor(hardwareRevision, raw_val) |
| 35 | + i = i + 1 |
| 36 | + end |
| 37 | + if apiVersion >= 1.035 then |
| 38 | + boardType = payload[i] |
| 39 | + end |
| 40 | + i = i + 1 |
| 41 | + if apiVersion >= 1.037 then |
| 42 | + targetCapabilities = payload[i] |
| 43 | + i = i + 1 |
| 44 | + length = payload[i] |
| 45 | + i = i + 1 |
| 46 | + for c = 1, length do |
| 47 | + targetName = targetName..string.char(payload[i]) |
| 48 | + i = i + 1 |
| 49 | + end |
| 50 | + end |
| 51 | + if apiVersion >= 1.039 then |
| 52 | + length = payload[i] |
| 53 | + i = i + 1 |
| 54 | + for c = 1, length do |
| 55 | + boardName = boardName..string.char(payload[i]) |
| 56 | + i = i + 1 |
| 57 | + end |
| 58 | + length = payload[i] |
| 59 | + i = i + 1 |
| 60 | + for c = 1, length do |
| 61 | + manufacturerId = manufacturerId..string.char(payload[i]) |
| 62 | + i = i + 1 |
| 63 | + end |
| 64 | + length = 32 |
| 65 | + for c = 1, 32 do |
| 66 | + signature[#signature + 1] = payload[i] |
| 67 | + i = i + 1 |
| 68 | + end |
| 69 | + end |
| 70 | + i = i + 1 |
| 71 | + if apiVersion >= 1.041 then |
| 72 | + mcuTypeId = payload[i] |
| 73 | + end |
| 74 | + i = i + 1 |
| 75 | + if apiVersion >= 1.042 then |
| 76 | + configurationState = payload[i] |
| 77 | + end |
| 78 | + if apiVersion >= 1.043 then |
| 79 | + for idx = 1, 2 do |
| 80 | + local raw_val = bit32.lshift(payload[i], (idx-1)*8) |
| 81 | + gyroSampleRateHz = bit32.bor(gyroSampleRateHz, raw_val) |
| 82 | + i = i + 1 |
| 83 | + end |
| 84 | + for idx = 1, 4 do |
| 85 | + local raw_val = bit32.lshift(payload[i], (idx-1)*8) |
| 86 | + configurationProblems = bit32.bor(configurationProblems, raw_val) |
| 87 | + i = i + 1 |
| 88 | + end |
| 89 | + end |
| 90 | + if apiVersion >= 1.044 then |
| 91 | + spiRegisteredDeviceCount = payload[i] |
| 92 | + i = i + 1 |
| 93 | + i2cRegisteredDeviceCount = payload[i] |
| 94 | + end |
| 95 | + boardInfoReceived = true |
| 96 | + end |
| 97 | +end |
| 98 | + |
| 99 | +local function getBoardInfo() |
| 100 | + if lastRunTS + INTERVAL < getTime() then |
| 101 | + lastRunTS = getTime() |
| 102 | + if not boardInfoReceived then |
| 103 | + protocol.mspRead(MSP_BOARD_INFO) |
| 104 | + end |
| 105 | + end |
| 106 | + mspProcessTxQ() |
| 107 | + processMspReply(mspPollReply()) |
| 108 | + if boardInfoReceived then |
| 109 | + local f = io.open("BOARD_INFO/"..mcuId..".lua", 'w') |
| 110 | + io.write(f, "return {", "\n") |
| 111 | + io.write(f, " boardIdentifier = "..boardIdentifier..",", "\n") |
| 112 | + io.write(f, " hardwareRevision = "..tostring(hardwareRevision)..",", "\n") |
| 113 | + io.write(f, " boardType = "..tostring(boardType)..",", "\n") |
| 114 | + io.write(f, " targetCapabilities = "..tostring(targetCapabilities)..",", "\n") |
| 115 | + io.write(f, " targetName = "..targetName..",", "\n") |
| 116 | + io.write(f, " boardName = "..boardName..",", "\n") |
| 117 | + io.write(f, " manufacturerId = "..manufacturerId..",", "\n") |
| 118 | + local signatureString = " signature = { " |
| 119 | + for i = 1, #signature do |
| 120 | + signatureString = signatureString..tostring(signature[i])..", " |
| 121 | + end |
| 122 | + signatureString = signatureString.."}," |
| 123 | + io.write(f, signatureString, "\n") |
| 124 | + io.write(f, " mcuTypeId = "..tostring(mcuTypeId)..",", "\n") |
| 125 | + io.write(f, " configurationState = "..tostring(configurationState)..",", "\n") |
| 126 | + io.write(f, " gyroSampleRateHz = "..tostring(gyroSampleRateHz)..",", "\n") |
| 127 | + io.write(f, " configurationProblems = "..tostring(configurationProblems)..",", "\n") |
| 128 | + io.write(f, " spiRegisteredDeviceCount = "..tostring(spiRegisteredDeviceCount)..",", "\n") |
| 129 | + io.write(f, " i2cRegisteredDeviceCount = "..tostring(i2cRegisteredDeviceCount)..",", "\n") |
| 130 | + io.write(f, "}", "\n") |
| 131 | + io.close(f) |
| 132 | + assert(loadScript("BOARD_INFO/"..mcuId..".lua", 'c')) |
| 133 | + end |
| 134 | + return boardInfoReceived |
| 135 | +end |
| 136 | + |
| 137 | +return { f = getBoardInfo, t = "Downloading board info" } |
0 commit comments