0

I'm trying to erase flash for STM32F779II.

The start up file starts with that section:

{
  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
}

So I'm leaving that three sections.

I'm trying to clear sector number 22. but I get Write Protection Error.

Here is how I'm earsing it.

Sectors is an array with one element, which is 22 sector number which has address 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;
}
andre_lamothe
  • 2,171
  • 2
  • 41
  • 74
  • **Examine each flash register before and after each flash operation, check their values using the reference manual.** If you can find any bits that are different from what the reference manual says, you can expand your question with their full values, noting what is different from the expected, and at which point in the program or the library has it occured. Then reveal the source of the `ensure()` and `flash_unlock_write()` functions. If you do both, that would make an answerable question. – followed Monica to Codidact Sep 04 '19 at 11:25
  • This is the same question as [on Electrical Engineering](https://electronics.stackexchange.com/questions/456100/write-protection-error-on-stm32f779ii-earsing-flash). – the busybee Sep 04 '19 at 16:53

0 Answers0