0

我正在尝试擦除 STM32F779II 的闪存。

启动文件以该部分开头:

{
  FLASH (rx)      : ORIGIN = 0x08000000, LENGTH = 1664K
  DTCMRAM (xrw)   : ORIGIN = 0x20000000, LENGTH = 128K
  SRAM1 (xrw)     : ORIGIN = 0x20020000, LENGTH = 368K
  SRAM2 (xrw)     : ORIGIN = 0x2007C000, LENGTH = 16K
  MEMORY_B1 (rx)  : ORIGIN = 0x60000000, LENGTH = 0K
}

所以我要离开这三个部分。

我正在尝试清除扇区号 22。但出现写保护错误。

这就是我听到它的方式。

Sectors 是一个包含一个元素的数组,它是22地址 为 0x081C0000的扇区号

secbool flash_erase_sectors(const uint8_t *sectors, int len,
                            void (*progress)(int pos, int len)) {
  ensure(flash_unlock_write(), NULL);
  HAL_FLASH_OB_Unlock();


  FLASH_EraseInitTypeDef EraseInitStruct;
  EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  EraseInitStruct.NbSectors = 1;
  if (progress) {
    progress(0, len);
  }
  for (int i = 0; i < len; i++) {
    EraseInitStruct.Sector = sectors[i];
    uint32_t SectorError;
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
      ensure(flash_lock_write(), NULL);
      return secfalse;
    }
    // check whether the sector was really deleted (contains only 0xFF)
    const uint32_t addr_start = FLASH_SECTOR_TABLE[sectors[i]],
                   addr_end = FLASH_SECTOR_TABLE[sectors[i] + 1];
    for (uint32_t addr = addr_start; addr < addr_end; addr += 4) {
      if (*((const uint32_t *)addr) != 0xFFFFFFFF) {
        ensure(flash_lock_write(), NULL);
        return secfalse;
      }
    }
    if (progress) {
      progress(i + 1, len);
    }
  }
  ensure(flash_lock_write(), NULL);
  return sectrue;
}
4

0 回答 0