cancel
Showing results for 
Search instead for 
Did you mean: 

Watchdog and erasing Flash

EGonc.1
Associate II

I'm trying to use the watchdog on a STM32F401.

The watchdog initialization is as follows:

 hwwdg.Instance = WWDG;

 hwwdg.Init.Prescaler = WWDG_PRESCALER_8;

 hwwdg.Init.Window = 127;

 hwwdg.Init.Counter = 127;

 hwwdg.Init.EWIMode = WWDG_EWI_DISABLE;

In normal operation the watchdog works fine.

When I evoke the HAL_FLASHEx_Erase function the processor goes into a reset, due to, I think, the watchdog expiring.

I tried to place a call to HAL_WWDG_Refresh() in almost every line of this function, and the functions that it calls but still it resets.

Any ideas about this?

thanks

Any ideas

2 REPLIES 2
EGonc.1
Associate II

HAL_StatusTypeDef FLASH_WaitForLastOperation(uint32_t Timeout)

 uint32_t tickstart = 0U;

  

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 /* Clear Error Code */

 pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;

  

 /* Wait for the FLASH operation to complete by polling on BUSY flag to be reset.

   Even if the FLASH operation fails, the BUSY flag will be reset and an error

   flag will be set */

 /* Get tick */

 tickstart = HAL_GetTick();

 while(__HAL_FLASH_GET_FLAG(FLASH_FLAG_BSY) != RESET) 

 { 

{ extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

if(Timeout != HAL_MAX_DELAY)

  {

   if((Timeout == 0U)||((HAL_GetTick() - tickstart ) > Timeout))

   {

    return HAL_TIMEOUT;

   }

  } 

 }

 /* Check FLASH End of Operation flag */

 if (__HAL_FLASH_GET_FLAG(FLASH_FLAG_EOP) != RESET)

 {

  /* Clear FLASH End of Operation pending bit */

  __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP);

 }

#if defined(FLASH_SR_RDERR)  

 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \

              FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR | FLASH_FLAG_RDERR)) != RESET)

#else

 if(__HAL_FLASH_GET_FLAG((FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | \

              FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR)) != RESET)

#endif /* FLASH_SR_RDERR */

 {

  /*Save the error code*/

  { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

  FLASH_SetErrorCode();

  return HAL_ERROR;

 }

 /* If there is no error flag set */

 return HAL_OK;

  

}  

----------------------------------------------------------------------------------------------------------------------------------------------------

void FLASH_Erase_Sector(uint32_t Sector, uint8_t VoltageRange)

{

 uint32_t tmp_psize = 0U;

 /* Check the parameters */

 assert_param(IS_FLASH_SECTOR(Sector));

 assert_param(IS_VOLTAGERANGE(VoltageRange));

  

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 if(VoltageRange == FLASH_VOLTAGE_RANGE_1)

 {

   tmp_psize = FLASH_PSIZE_BYTE;

 }

 else if(VoltageRange == FLASH_VOLTAGE_RANGE_2)

 {

  tmp_psize = FLASH_PSIZE_HALF_WORD;

 }

 else if(VoltageRange == FLASH_VOLTAGE_RANGE_3)

 {

  tmp_psize = FLASH_PSIZE_WORD;

 }

 else

 {

  tmp_psize = FLASH_PSIZE_DOUBLE_WORD;

 }

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 /* If the previous operation is completed, proceed to erase the sector */

 CLEAR_BIT(FLASH->CR, FLASH_CR_PSIZE);

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 FLASH->CR |= tmp_psize;

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 CLEAR_BIT(FLASH->CR, FLASH_CR_SNB);

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 FLASH->CR |= FLASH_CR_SER | (Sector << FLASH_CR_SNB_Pos);

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

 FLASH->CR |= FLASH_CR_STRT;

 { extern WWDG_HandleTypeDef hwwdg; HAL_WWDG_Refresh(&hwwdg); }

}

Uwe Bonnes
Principal III

Best is to not start Watchdog when flashing. Otherwise watchdog timeout needs to be comfordable longer than the longes expected erase time. Byte masse erase time is typcal 4 and max 8 seconds, see the datasheet.