1
0

firmware.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const {
  2. formatBytes
  3. } = require('../../utils/binary-utils.js')
  4. const FILE_SIZES = {
  5. 16: 16 * 1024,
  6. 32: 32 * 1024
  7. }
  8. const FLASH_LAYOUTS = {
  9. 16: {
  10. capacity: FILE_SIZES[16],
  11. protectedStartAddress: 0x3F80,
  12. startAddress: 0x0400,
  13. endAddress: 0x3F80
  14. },
  15. 32: {
  16. capacity: FILE_SIZES[32],
  17. protectedStartAddress: 0x7F00,
  18. startAddress: 0x0800,
  19. endAddress: 0x7F00
  20. }
  21. }
  22. const FLASH_SIZE_TEXT = Object.keys(FILE_SIZES)
  23. .map((sizeKb) => formatBytes(FILE_SIZES[sizeKb]))
  24. .join(' 或 ')
  25. const CHIP_FLASH_SIZE_KB = {
  26. FU6572L: 32,
  27. FU6572N: 32,
  28. FU6572T: 32,
  29. FU6565N: 32,
  30. FU6565T: 32,
  31. FU6563N: 32,
  32. FU6562L: 32,
  33. FU6562LA: 32,
  34. FU6562Q: 32,
  35. FU6562S: 32,
  36. FU6562T: 32,
  37. FU6532N: 32,
  38. FU6532T: 32,
  39. FU6522L: 32,
  40. FU6522N: 32,
  41. FU6522T: 32,
  42. FU6812L2: 16,
  43. FU6812N2: 16,
  44. FU6812S2: 16,
  45. FU6812V: 16,
  46. FU6861Q2: 16,
  47. FU6861N2: 16,
  48. FU6861NF2: 16,
  49. FU6861L2: 16,
  50. FU6862L: 16,
  51. FU6862Q: 16,
  52. FU6872P: 16
  53. }
  54. const CHIP_FAMILY_FLASH_SIZE_KB = {
  55. 65: 32,
  56. 68: 16
  57. }
  58. function normalizeModel(value) {
  59. const text = String(value || '').trim()
  60. return text && text !== '--' ? text : ''
  61. }
  62. function extractChipModels(chipModel) {
  63. const model = normalizeModel(chipModel).toUpperCase()
  64. return model.match(/FU\d{4}[A-Z0-9]*/g) || []
  65. }
  66. function inferFlashSizeKb(chipModel) {
  67. const models = extractChipModels(chipModel)
  68. for (const model of models) {
  69. if (CHIP_FLASH_SIZE_KB[model]) return CHIP_FLASH_SIZE_KB[model]
  70. const family = model.slice(2, 4)
  71. if (CHIP_FAMILY_FLASH_SIZE_KB[family]) return CHIP_FAMILY_FLASH_SIZE_KB[family]
  72. }
  73. const text = normalizeModel(chipModel).toUpperCase()
  74. if (/(^|[^0-9])32\s*K(B)?([^0-9]|$)/.test(text)) return 32
  75. if (/(^|[^0-9])16\s*K(B)?([^0-9]|$)/.test(text)) return 16
  76. return null
  77. }
  78. function inferFlashSizeKbFromBytes(byteLength) {
  79. return Number(Object.keys(FILE_SIZES).find((sizeKb) => FILE_SIZES[sizeKb] === byteLength)) || null
  80. }
  81. function inferUpgradeFlashSizeKb(chipModel, byteLength) {
  82. return inferFlashSizeKb(chipModel) || inferFlashSizeKbFromBytes(byteLength)
  83. }
  84. function getFlashLayout(chipModel, byteLength) {
  85. const sizeKb = inferUpgradeFlashSizeKb(chipModel, byteLength)
  86. return sizeKb ? FLASH_LAYOUTS[sizeKb] : null
  87. }
  88. function getFirmwareValidation(byteLength, chipModel) {
  89. const chipSizeKb = inferFlashSizeKb(chipModel)
  90. const firmwareSizeKb = inferFlashSizeKbFromBytes(byteLength)
  91. const sizeKb = chipSizeKb || firmwareSizeKb
  92. const layout = sizeKb ? FLASH_LAYOUTS[sizeKb] : null
  93. const normalizedChipModel = normalizeModel(chipModel)
  94. if (!byteLength) {
  95. return {
  96. isReady: false,
  97. text: chipSizeKb
  98. ? `需要 ${formatBytes(FLASH_LAYOUTS[chipSizeKb].capacity)} .bin`
  99. : `请选择 ${FLASH_SIZE_TEXT} .bin`
  100. }
  101. }
  102. if (!layout) {
  103. return {
  104. isReady: false,
  105. text: `文件 ${formatBytes(byteLength)},应为 ${FLASH_SIZE_TEXT}`
  106. }
  107. }
  108. if (byteLength !== layout.capacity) {
  109. return {
  110. isReady: false,
  111. text: `文件 ${formatBytes(byteLength)},应为 ${formatBytes(layout.capacity)}`
  112. }
  113. }
  114. return {
  115. isReady: true,
  116. text: chipSizeKb
  117. ? `匹配 ${formatBytes(layout.capacity)}`
  118. : `${normalizedChipModel ? `${normalizedChipModel} 未识别,` : '未读到芯片型号,'}按 ${formatBytes(layout.capacity)} 尝试`
  119. }
  120. }
  121. module.exports = {
  122. CHIP_FAMILY_FLASH_SIZE_KB,
  123. CHIP_FLASH_SIZE_KB,
  124. FILE_SIZES,
  125. FLASH_LAYOUTS,
  126. FLASH_SIZE_TEXT,
  127. extractChipModels,
  128. getFirmwareValidation,
  129. getFlashLayout,
  130. inferFlashSizeKb,
  131. inferFlashSizeKbFromBytes,
  132. inferUpgradeFlashSizeKb,
  133. normalizeModel
  134. }