I'm trying to erase some sectors from the flash, so that I can write to them.
I'm using a setup of dual bank mode.
For the setup I'm doing this:
HAL_FLASH_Unlock();
HAL_FLASH_OB_Unlock();
FLASH->OPTCR &= ~FLASH_OPTCR_nDBANK_Msk;
HAL_FLASH_OB_Lock();
HAL_FLASH_Lock();
Following it:
I'm doing flash erase by the following function that takes a sector and erase it.
but it fails, and the address of the sectors that are earsed are not 0xFFFFFFF
secbool flash_erase_sectors(const uint8_t *sectors, int len,
void (*progress)(int pos, int len)) {
ensure(flash_unlock_write(), NULL);
FLASH_EraseInitTypeDef EraseInitStruct;
FLASH_OBProgramInitTypeDef OBInit;
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
EraseInitStruct.NbSectors = 1;
if (progress) {
progress(0, len);
}
__HAL_FLASH_ART_DISABLE();
__HAL_FLASH_ART_RESET();
__HAL_FLASH_ART_ENABLE();
/* Allow Access to option bytes sector */
HAL_FLASH_OB_Unlock();
/* Get the Dual bank configuration status */
HAL_FLASHEx_OBGetConfig(&OBInit);
/* Allow Access to option bytes sector */
HAL_FLASH_OB_Lock();
if((OBInit.USERConfig & OB_NDBANK_SINGLE_BANK) == OB_NDBANK_DUAL_BANK){
/* We don't support the DUAL BANK MODE for now, so return error */
}
/* Unlock the Flash to enable the flash control register access *************/
ensure(flash_unlock_write(), NULL);
/* Allow Access to option bytes sector */
HAL_FLASH_OB_Unlock();
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);
HAL_FLASH_OB_Lock();
return secfalse;
}
}
if (progress) {
progress(i + 1, len);
}
}
HAL_FLASH_OB_Lock();
ensure(flash_lock_write(), NULL);
return sectrue;
}
User contributions licensed under CC BY-SA 3.0