AnsweredAssumed Answered

STM32F407 FLASH erase fail with IWDG enabled (HAL)

Question asked by Morris.Neil on May 15, 2017
Latest reply on Aug 12, 2017 by kerr.matthew

Hi,

 

I am having a problem writing to flash with the IWDG enabled, presumably I am missing something.
I have cut my project down to a minimum that reproduces the problem.

 

 

The program initialises the HAL and system clocks and then erases a flash sector (FLASH_SECTOR_2), the compiler is told not to use sector 2 & 3 so I can store parameters in them.

This gives me a successful return value and all is well.


However if I enable IWDG (HAL_IWDG_Start()), then HAL_FLASHEx_Erase() fails with HAL_FLASH_GetError() = 6.

 

I've changed the watchdog to a long period to make sure its not interrupting the erase.

 

Have I skipped over some detail? I had assumed that the watchdog being independent all I needed to do was ensure the watch period was long enough not to interrupt the flash writing.

 

 

void MX_IWDG_Init(void)
{

  hiwdg.Instance = IWDG;
  hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
  hiwdg.Init.Reload = 4095;
  HAL_IWDG_Init(&hiwdg);
}

 

 

  HAL_Init();
  SystemClock_Config();

  HAL_IWDG_Start(&hiwdg); /* Start Watchdog */

  HAL_FLASH_Unlock(); /* makes no difference */
 
  uint32_t error_sector_num = 0;
 
  FLASH_EraseInitTypeDef EraseInitStruct;
  EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  EraseInitStruct.Sector = FLASH_SECTOR_2;
  EraseInitStruct.NbSectors = 1u;
 
  /* erase flash */
  if(HAL_FLASHEx_Erase(&EraseInitStruct, &error_sector_num) != HAL_OK)
  {
    /* Error while erasing sectors */
    uint32_t error_code = HAL_FLASH_GetError();
    __ASM volatile("BKPT #01");
  }
  else
  {
    __ASM volatile("BKPT #01");   
  }

 

Thanks in advance,

Outcomes