# STM32 Flash erase sector 0

I have an updatable project with stm32f407. Code produced by cubeMx. Updater code check new update from internet and if is avaliable write main program on 0x08020000(sector 5) and this updater code on 0x08000000(sector 0 and sector 2 size is less than 32kb)

I am writing a code(will be like main project on sector 5) can update the updater code on sector 0

My problem is when I erase sector 0 AC6 give an error for address wrapping and STM32 stop working. I think is because of sector 0 has a special situation. there is no problem with erase other sectors. Is there a anyway to erase sector0 with a embedded program? The problem begin after delete 1 sector. I dont know what to do. (sorry for my bad English)

                              for(data_sayac=0;data_sayac<=filelength/2;data_sayac+=2)
{
// UPDATEDATA_ADDRESS defined 0x08040000 (sector 6 where my new updater data copied from internet)
// UPDATEDATA_ADDRESS2 defined 0x08000000 (sector 0 where my new updater program copied from sector 6)
if(Flash_oku_halfword(UPDATEDATA_ADDRESS2+data_sayac)!=0xFFFF)  // if sector is not empty erase first
{
HAL_FLASH_Unlock();
FLASH_Erase_Sector(Get_Sector,VOLTAGE_RANGE_3);            //erase sector
}

}


My problem is when I erase sector 0 AC6 give an error for address wrapping

Remember that the first item on page zero is the interrupt vector table.

Your problem sounds a lot like you have interrupts enabled. This would only work in case you moved the vector table to a RAM address using the SCB->VTOR register. Note that the vector table needs an address with the lowest byte all zero (256 byte aligned).

Is there a anyway to erase sector0 with a embedded program?

This usually needs a bit of preparation:

1. disable interrupts (or use VTOR above)
2. erase page
3. write new page content
4. enable interrupts

A power fail (or any other Reset) could "brick" the device, if it happened between the erase and the new write.

• Thanks that was my problem. I was using timer interrupt for ethernet. before I erase sectors I stop timer and now is working – user164200 Aug 13 '18 at 6:51