firmware.js 3.5 KB

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