frame.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const BOOTLOADER_HEAD = [0x46, 0x54]
  2. const {
  3. BYTE_ORDER_HIGH,
  4. appendCrc16Ccitt,
  5. crc16Ccitt,
  6. hasValidCrc16Ccitt
  7. } = require('../../utils/crc.js')
  8. const ACK = 0x06
  9. const NAK = 0x15
  10. const PROGRAM_CHUNK_SIZE = 128
  11. const BOOTLOADER_CRC_OPTIONS = {
  12. byteOrder: BYTE_ORDER_HIGH
  13. }
  14. function isBootloaderFrame(bytes) {
  15. return Array.isArray(bytes)
  16. && bytes.length >= 2
  17. && bytes[0] === BOOTLOADER_HEAD[0]
  18. && bytes[1] === BOOTLOADER_HEAD[1]
  19. }
  20. function getBootloaderResponseLength(bytes) {
  21. if (!isBootloaderFrame(bytes) || bytes.length < 3) return 0
  22. if (bytes[2] === 0x39) return 15
  23. if (bytes[2] === 0x19) return 9
  24. return 8
  25. }
  26. function getBootloaderExpectedLength(kind) {
  27. if (kind === 'handshake') return 15
  28. if (kind === 'flashCheck') return 9
  29. return 8
  30. }
  31. function toHex(value, length = 2) {
  32. return Number(value || 0).toString(16).toUpperCase().padStart(length, '0')
  33. }
  34. function formatBootloaderCrc(value) {
  35. return `0x${toHex(value, 4)}`
  36. }
  37. function calculateBootloaderCrc(bytes) {
  38. return crc16Ccitt(Array.prototype.slice.call(bytes || []), BOOTLOADER_CRC_OPTIONS)
  39. }
  40. function buildBootloaderFrame(payload) {
  41. return new Uint8Array(appendCrc16Ccitt(BOOTLOADER_HEAD.concat(payload), BOOTLOADER_CRC_OPTIONS))
  42. }
  43. function buildHandshakeFrame() {
  44. return buildBootloaderFrame([0x39, 0x42, 0x4C])
  45. }
  46. function buildUnlockFrame() {
  47. return buildBootloaderFrame([0x08, 0x4E, 0x00])
  48. }
  49. function buildProgramFrame(address, dataBytes, chunkSize = PROGRAM_CHUNK_SIZE) {
  50. const payload = [
  51. 0x44,
  52. address & 0xFF,
  53. (address >> 8) & 0xFF
  54. ]
  55. const data = Array.prototype.slice.call(dataBytes || []).slice(0, chunkSize)
  56. while (data.length < chunkSize) {
  57. data.push(0x00)
  58. }
  59. return buildBootloaderFrame(payload.concat(data))
  60. }
  61. function buildFlashCheckFrame() {
  62. return buildBootloaderFrame([0x19, 0x43, 0x43])
  63. }
  64. function buildPageEraseFrame(enabled) {
  65. return buildBootloaderFrame([0x08, 0x50, enabled ? 0x45 : 0x44])
  66. }
  67. function buildExitFrame() {
  68. return buildBootloaderFrame([0x08, 0x42, 0x42])
  69. }
  70. function parseAsciiField(bytes, offset, length) {
  71. const chars = []
  72. for (let index = 0; index < length; index += 1) {
  73. const byte = bytes[offset + index] & 0xFF
  74. if (byte === 0x00 || byte === 0xFF) continue
  75. if (byte >= 0x20 && byte <= 0x7E) {
  76. chars.push(String.fromCharCode(byte))
  77. }
  78. }
  79. return chars.join('').trim() || '--'
  80. }
  81. function alignBootloaderBuffer(buffer) {
  82. let headIndex = -1
  83. for (let index = 0; index < buffer.length - 1; index += 1) {
  84. if (buffer[index] === BOOTLOADER_HEAD[0] && buffer[index + 1] === BOOTLOADER_HEAD[1]) {
  85. headIndex = index
  86. break
  87. }
  88. }
  89. if (headIndex > 0) {
  90. buffer.splice(0, headIndex)
  91. } else if (headIndex < 0 && buffer.length > 1) {
  92. buffer.splice(0, buffer.length - 1)
  93. }
  94. }
  95. function parseBootloaderResponse(bytes, kind) {
  96. if (!hasValidCrc16Ccitt(bytes, BOOTLOADER_CRC_OPTIONS)) {
  97. throw new Error('Bootloader 返回帧 CRC 校验失败')
  98. }
  99. if (kind === 'handshake') {
  100. if (bytes.length !== 15 || bytes[2] !== 0x39 || bytes[3] !== 0x42 || bytes[4] !== 0x4C) {
  101. throw new Error('握手反馈帧不匹配')
  102. }
  103. const versionText = parseAsciiField(bytes, 5, 4)
  104. const chipIdText = parseAsciiField(bytes, 9, 4)
  105. return {
  106. chipId: chipIdText,
  107. chipIdText,
  108. version: versionText,
  109. versionText
  110. }
  111. }
  112. if (kind === 'unlock') {
  113. if (bytes.length !== 8 || bytes[2] !== 0x08 || bytes[3] !== 0x4E || bytes[4] !== 0x00) {
  114. throw new Error('解锁反馈帧不匹配')
  115. }
  116. return {
  117. ack: bytes[5]
  118. }
  119. }
  120. if (kind === 'program') {
  121. if (bytes.length !== 8 || bytes[2] !== 0x44) {
  122. throw new Error('编程反馈帧不匹配')
  123. }
  124. return {
  125. ack: bytes[5],
  126. address: (bytes[3] & 0xFF) | ((bytes[4] & 0xFF) << 8)
  127. }
  128. }
  129. if (kind === 'flashCheck') {
  130. if (bytes.length !== 9 || bytes[2] !== 0x19 || bytes[3] !== 0x43 || bytes[4] !== 0x43) {
  131. throw new Error('全 Flash 校验反馈帧不匹配')
  132. }
  133. const flashCrc = ((bytes[5] & 0xFF) << 8) | (bytes[6] & 0xFF)
  134. return {
  135. flashCrc,
  136. flashCrcText: formatBootloaderCrc(flashCrc)
  137. }
  138. }
  139. if (kind === 'pageErase') {
  140. if (bytes.length !== 8 || bytes[2] !== 0x08 || bytes[3] !== 0x50) {
  141. throw new Error('页擦除反馈帧不匹配')
  142. }
  143. return {
  144. ack: bytes[5],
  145. enabled: bytes[4] === 0x45
  146. }
  147. }
  148. return {}
  149. }
  150. function assertBootloaderAck(response, label) {
  151. if (!response || response.ack === ACK) return
  152. if (response.ack === NAK) throw new Error(`${label}失败:设备返回 NAK`)
  153. throw new Error(`${label}失败:未知 ACK 0x${toHex(response.ack)}`)
  154. }
  155. module.exports = {
  156. ACK,
  157. BOOTLOADER_HEAD,
  158. BOOTLOADER_CRC_OPTIONS,
  159. NAK,
  160. PROGRAM_CHUNK_SIZE,
  161. alignBootloaderBuffer,
  162. assertBootloaderAck,
  163. buildBootloaderFrame,
  164. buildExitFrame,
  165. buildFlashCheckFrame,
  166. buildHandshakeFrame,
  167. buildPageEraseFrame,
  168. buildProgramFrame,
  169. buildUnlockFrame,
  170. calculateBootloaderCrc,
  171. formatBootloaderCrc,
  172. getBootloaderExpectedLength,
  173. getBootloaderResponseLength,
  174. isBootloaderFrame,
  175. parseBootloaderResponse,
  176. toHex
  177. }