/** * @file internal_flash.c * @brief 内部 Flash 擦写驱动(H743,Sector2~15 = APP 区;Sector0~1 永不触碰) * * 基于 HAL: 编程走 HAL_FLASH_Program(FLASHWORD, 32B/次), 擦除走 HAL_FLASHEx_Erase。 * Bank 边界: Sector2~7 属 Bank1, Sector8~15 属 Bank2 —— 跨 bank 擦除分两次。 * 编程/读回前后处理 D-Cache 一致性(Cortex-M7 Cache 必须处理)。 */ #include "internal_flash.h" #include "boot_layout.h" #include "boot_crc.h" #include "Int_flash.h" #include "stm32h7xx_hal.h" #include #define IF_BUF_SIZE 4096U static uint8_t s_buf[IF_BUF_SIZE] __attribute__((aligned(32))); /* 覆盖 [APP_BASE, APP_BASE+len) 所需最后扇区号(相对 0x08000000) */ static uint32_t sector_last_for(uint32_t len) { uint32_t nsec = (len + INTF_SECTOR_SIZE - 1U) / INTF_SECTOR_SIZE; uint32_t first = APP_BASE_ADDR / INTF_SECTOR_SIZE; /* =2 */ uint32_t last = first + nsec - 1U; if (last > APP_LAST_SECTOR_IDX) last = APP_LAST_SECTOR_IDX; return last; } static uint8_t erase_bank(uint32_t bank, uint32_t first_sec, uint32_t nsec) { FLASH_EraseInitTypeDef e; uint32_t err = 0xFFFFFFFFU; e.TypeErase = FLASH_TYPEERASE_SECTORS; e.Banks = bank; e.Sector = first_sec; e.NbSectors = nsec; e.VoltageRange = FLASH_VOLTAGE_RANGE_3; if (HAL_FLASHEx_Erase(&e, &err) != HAL_OK) return 1; return (err == 0xFFFFFFFFU) ? 0 : 2; } uint8_t int_flash_erase_app(uint32_t len) { uint32_t last; if (len == 0 || len > APP_SIZE_MAX) return 1; HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2); last = sector_last_for(len); if (last <= 7U) { if (erase_bank(FLASH_BANK_1, 2U, last - 2U + 1U)) goto fail; } else { if (erase_bank(FLASH_BANK_1, 2U, 6U)) goto fail; /* 2..7 */ if (erase_bank(FLASH_BANK_2, 8U, last - 8U + 1U)) goto fail; /* 8..last */ } HAL_FLASH_Lock(); return 0; fail: HAL_FLASH_Lock(); return 3; } /* 让 CPU 读到 Flash 最新内容: 清 + 无效化覆盖区 D-Cache */ static void cache_refresh(uint32_t addr, uint32_t len) { SCB_CleanDCache(); SCB_InvalidateDCache_by_Addr((void *)(addr & ~31UL), (int32_t)(((len + 31UL) & ~31UL) + 32UL)); __DSB(); } uint8_t int_flash_program(uint32_t dst, const uint8_t *src, uint32_t len) { uint32_t off; if ((dst & 31U) != 0U) return 1; if (((uint32_t)src & 3U) != 0U) return 1; HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS_BANK1 | FLASH_FLAG_ALL_ERRORS_BANK2); for (off = 0; off < len; off += 32U) { /* HAL 每次编程一个 32B flashword; 尾段不足 32B 时 src 侧必须可安全读满 32B * —— 调用方保证 len 已按 32B 补齐。 */ if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, dst + off, (uint32_t)(src + off)) != HAL_OK) { HAL_FLASH_Lock(); return 2; } } HAL_FLASH_Lock(); cache_refresh(dst, (len + 31U) & ~31U); return 0; } uint32_t int_flash_crc32(uint32_t addr, uint32_t len) { uint32_t crc = boot_crc32_init(); uint32_t done = 0; cache_refresh(addr, len); while (done < len) { uint32_t n = len - done; if (n > IF_BUF_SIZE) n = IF_BUF_SIZE; crc = boot_crc32_update(crc, (const uint8_t *)(addr + done), n); done += n; } return boot_crc32_finish(crc); } void int_flash_read(uint32_t addr, uint8_t *out, uint32_t len) { cache_refresh(addr, len); memcpy(out, (const void *)addr, len); } /* NOR -> 内部 Flash 整段烧录(4KB 块; 末段补 0xFF 到 32B 边界, 但偏移按实际推进) */ uint8_t int_flash_program_from_nor(uint32_t dst, uint32_t src_nor, uint32_t len) { uint32_t off = 0; while (off < len) { uint32_t n = len - off; uint32_t nprog; if (n > IF_BUF_SIZE) n = IF_BUF_SIZE; if (Int_flash_read(s_buf, src_nor + off, n) != 0) return 1; /* 末段补齐 32B 后整字编程, 非末段本就 4KB 对齐 */ nprog = (off + n >= len) ? ((n + 31U) & ~31U) : n; if (nprog > n) memset(s_buf + n, 0xFF, nprog - n); if (int_flash_program(dst + off, s_buf, nprog) != 0) return 2; off += n; } return 0; } /* 内部 Flash -> NOR 整段备份 */ uint8_t int_flash_backup_to_nor(uint32_t src, uint32_t dst_nor, uint32_t len) { uint32_t off = 0; while (off < len) { uint32_t n = len - off; if (n > IF_BUF_SIZE) n = IF_BUF_SIZE; int_flash_read(src + off, s_buf, n); if (Int_flash_write(s_buf, dst_nor + off, n) != 0) return 1; off += n; } return 0; }