Skip to main content
Anvi
Associate III
Oct 30, 2020 7:11 AM UTC
Solved

Flash Sector Erase - STM32F207

Hello, I currently have this code setup to erase a specific sector of the STM32F207 internal flash:

static void eraseSector(uint32_t sector)
{
 FLASH_EraseInitTypeDef eraseInitStruct = { 0 };
 HAL_StatusTypeDef status = HAL_OK;
 
 if( HAL_FLASH_Unlock() != HAL_OK )
 {
 printDebug("Error unlocking Flash\r\n");
 }
 
 eraseInitStruct.TypeErase = TYPEERASE_SECTORS;
 eraseInitStruct.VoltageRange = VOLTAGE_RANGE_3;
 eraseInitStruct.Sector = sector;
 eraseInitStruct.NbSectors = 1;
 
 status = HAL_FLASHEx_Erase_IT(&eraseInitStruct);
 if (status != HAL_OK)
 {
 print("Unable to erase Sector: %d\n", status);
 }
 else
 {
 print("Flash sector %d erased. Status: %d\n", sector, status);
 }
 
 /* Wait for the erase operation to complete */
 osSemaphoreWait(FlashOperation_sem, osWaitForever);
 
 if( HAL_FLASH_Lock() != HAL_OK )
 {
 print("Error locking Flash\n");
 }
}

When this code executes, the application resets whenever the FLASH_CR Start bit is set in the FLASH_Erase_Sector() (called from the HAL_FLASHEx_Erase_IT function). I've attempted this with multiple unused flash sectors and they all crash.

I've also attempted directly call the FLASH_Erase_Sector() and the same persists.

This topic has been closed for replies.
Uwe Bonnes
Uwe BonnesBest answer
Chief
Oct 30, 2020 10:42 AM UTC

Maybe some watchdog running?

3 replies

Uwe Bonnes
Uwe BonnesBest answer
Chief
Oct 30, 2020 10:42 AM UTC

Maybe some watchdog running?

Anvi
AnviAuthor
Associate III
Oct 30, 2020 6:24 PM UTC

It was indeed the watchdog. Terrible oversight on my part. Thanks for the help!

TDK
Oct 30, 2020 1:37 PM UTC

After the chip resets unexpectedly, look in the RCC_CSR to see the reason for the reset.

"If you feel a post has answered your question, please click ""Accept as Solution""."