internal_flash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file internal_flash.c
  3. * @brief 内部 Flash 擦写驱动(H743,Sector2~15 = APP 区;Sector0~1 永不触碰)
  4. *
  5. * 基于 HAL: 编程走 HAL_FLASH_Program(FLASHWORD, 32B/次), 擦除走 HAL_FLASHEx_Erase。
  6. * Bank 边界: Sector2~7 属 Bank1, Sector8~15 属 Bank2 —— 跨 bank 擦除分两次。
  7. * 编程/读回前后处理 D-Cache 一致性(Cortex-M7 Cache 必须处理)。
  8. */
  9. #include "internal_flash.h"
  10. #include "boot_layout.h"
  11. #include "boot_crc.h"
  12. #include "Int_flash.h"
  13. #include "stm32h7xx_hal.h"
  14. #include <string.h>
  15. #define IF_BUF_SIZE 4096U
  16. static uint8_t s_buf[IF_BUF_SIZE] __attribute__((aligned(32)));
  17. /* 覆盖 [APP_BASE, APP_BASE+len) 所需最后扇区号(相对 0x08000000) */
  18. static uint32_t sector_last_for(uint32_t len)
  19. {
  20. uint32_t nsec = (len + INTF_SECTOR_SIZE - 1U) / INTF_SECTOR_SIZE;
  21. uint32_t first = APP_BASE_ADDR / INTF_SECTOR_SIZE; /* =2 */
  22. uint32_t last = first + nsec - 1U;
  23. if (last > APP_LAST_SECTOR_IDX) last = APP_LAST_SECTOR_IDX;
  24. return last;
  25. }
  26. static uint8_t erase_bank(uint32_t bank, uint32_t first_sec, uint32_t nsec)
  27. {
  28. FLASH_EraseInitTypeDef e;
  29. uint32_t err = 0xFFFFFFFFU;
  30. e.TypeErase = FLASH_TYPEERASE_SECTORS;
  31. e.Banks = bank;
  32. e.Sector = first_sec;
  33. e.NbSectors = nsec;
  34. e.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  35. if (HAL_FLASHEx_Erase(&e, &err) != HAL_OK) return 1;
  36. return (err == 0xFFFFFFFFU) ? 0 : 2;
  37. }
  38. uint8_t int_flash_erase_app(uint32_t len)
  39. {
  40. uint32_t last;
  41. if (len == 0 || len > APP_SIZE_MAX) return 1;
  42. HAL_FLASH_Unlock();
  43. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2);
  44. last = sector_last_for(len);
  45. if (last <= 7U)
  46. {
  47. if (erase_bank(FLASH_BANK_1, 2U, last - 2U + 1U)) goto fail;
  48. }
  49. else
  50. {
  51. if (erase_bank(FLASH_BANK_1, 2U, 6U)) goto fail; /* 2..7 */
  52. if (erase_bank(FLASH_BANK_2, 8U, last - 8U + 1U)) goto fail; /* 8..last */
  53. }
  54. HAL_FLASH_Lock();
  55. return 0;
  56. fail:
  57. HAL_FLASH_Lock();
  58. return 3;
  59. }
  60. /* 让 CPU 读到 Flash 最新内容: 清 + 无效化覆盖区 D-Cache */
  61. static void cache_refresh(uint32_t addr, uint32_t len)
  62. {
  63. SCB_CleanDCache();
  64. SCB_InvalidateDCache_by_Addr((void *)(addr & ~31UL),
  65. (int32_t)(((len + 31UL) & ~31UL) + 32UL));
  66. __DSB();
  67. }
  68. uint8_t int_flash_program(uint32_t dst, const uint8_t *src, uint32_t len)
  69. {
  70. uint32_t off;
  71. if ((dst & 31U) != 0U) return 1;
  72. if (((uint32_t)src & 3U) != 0U) return 1;
  73. HAL_FLASH_Unlock();
  74. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2);
  75. for (off = 0; off < len; off += 32U)
  76. {
  77. /* HAL 每次编程一个 32B flashword; 尾段不足 32B 时 src 侧必须可安全读满 32B
  78. * —— 调用方保证 len 已按 32B 补齐。 */
  79. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, dst + off,
  80. (uint32_t)(src + off)) != HAL_OK)
  81. {
  82. HAL_FLASH_Lock();
  83. return 2;
  84. }
  85. }
  86. HAL_FLASH_Lock();
  87. cache_refresh(dst, (len + 31U) & ~31U);
  88. return 0;
  89. }
  90. uint32_t int_flash_crc32(uint32_t addr, uint32_t len)
  91. {
  92. uint32_t crc = boot_crc32_init();
  93. uint32_t done = 0;
  94. cache_refresh(addr, len);
  95. while (done < len)
  96. {
  97. uint32_t n = len - done;
  98. if (n > IF_BUF_SIZE) n = IF_BUF_SIZE;
  99. crc = boot_crc32_update(crc, (const uint8_t *)(addr + done), n);
  100. done += n;
  101. }
  102. return boot_crc32_finish(crc);
  103. }
  104. void int_flash_read(uint32_t addr, uint8_t *out, uint32_t len)
  105. {
  106. cache_refresh(addr, len);
  107. memcpy(out, (const void *)addr, len);
  108. }
  109. /* NOR -> 内部 Flash 整段烧录(4KB 块; 末段补 0xFF 到 32B 边界, 但偏移按实际推进) */
  110. uint8_t int_flash_program_from_nor(uint32_t dst, uint32_t src_nor, uint32_t len)
  111. {
  112. uint32_t off = 0;
  113. while (off < len)
  114. {
  115. uint32_t n = len - off;
  116. uint32_t nprog;
  117. if (n > IF_BUF_SIZE) n = IF_BUF_SIZE;
  118. if (Int_flash_read(s_buf, src_nor + off, n) != 0) return 1;
  119. /* 末段补齐 32B 后整字编程, 非末段本就 4KB 对齐 */
  120. nprog = (off + n >= len) ? ((n + 31U) & ~31U) : n;
  121. if (nprog > n) memset(s_buf + n, 0xFF, nprog - n);
  122. if (int_flash_program(dst + off, s_buf, nprog) != 0) return 2;
  123. off += n;
  124. }
  125. return 0;
  126. }
  127. /* 内部 Flash -> NOR 整段备份 */
  128. uint8_t int_flash_backup_to_nor(uint32_t src, uint32_t dst_nor, uint32_t len)
  129. {
  130. uint32_t off = 0;
  131. while (off < len)
  132. {
  133. uint32_t n = len - off;
  134. if (n > IF_BUF_SIZE) n = IF_BUF_SIZE;
  135. int_flash_read(src + off, s_buf, n);
  136. if (Int_flash_write(s_buf, dst_nor + off, n) != 0) return 1;
  137. off += n;
  138. }
  139. return 0;
  140. }