2019-09-23 05:14 AM
I tried two methods - my first code is as follows:
FLASH_OBProgramInitTypeDef flashUserConfig = {0};
flashUserConfig.OptionType = OPTIONBYTE_USER;
flashUserConfig.USERType = OB_USER_IWDG_STOP;
flashUserConfig.USERConfig = OB_IWDG_STOP_FREEZE;
HAL_FLASHEx_OBProgram(&flashUserConfig);
flashUserConfig.OptionType = OPTIONBYTE_USER;
flashUserConfig.USERType = OB_USER_IWDG_STDBY;
flashUserConfig.USERConfig = OB_IWDG_STDBY_FREEZE;
HAL_FLASHEx_OBProgram(&flashUserConfig);
This didn't work so I tried this:
__HAL_LOCK(&pFlash);
FLASH->KEYR = (uint32_t)0x45670123;
FLASH->KEYR = (uint32_t)0xCDEF89AB;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); // Clear the FLASH's pending flags.
FLASH->OPTKEYR = (uint32_t)0x08192A3B;
FLASH->OPTKEYR = (uint32_t)0x4C5D6E7F;
pFlash.ErrorCode = HAL_FLASH_ERROR_NONE;
if( (status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)) != HAL_OK)
{
return status;
}
if(mode == HAL_FREEZE)
{
FLASH->OPTR &= (uint32_t)~(1 << 17);
FLASH->OPTR &= (uint32_t)~(1 << 18);
}
else if(mode == HAL_RUN)
{
FLASH->OPTR |= (uint32_t)(1 << (17));
FLASH->OPTR |= (uint32_t)(1 << (18));
}
else
{
return status;
}
FLASH->CR |= (uint32_t)(1 << (17));
if( (status = FLASH_WaitForLastOperation((uint32_t)FLASH_TIMEOUT_VALUE)) != HAL_OK )
{
return status;
}
FLASH->CR &= (uint32_t)~(1 << 17);
FLASH->CR |= (uint32_t)0x40000000;
FLASH->CR |= (uint32_t)0x80000000;
__HAL_UNLOCK(&pFlash);
return HAL_OK;
I printed the value of FLASH->OPTR before and after the second set of operations, and these are the values I see:
1111 1111 1110 1111 1111 1000 1010 1010
1111 1111 1110 1001 1111 1000 1010 1010
which looks to me like the appropriate bits are turned off for FREEZE mode for stdby and stop modes. My application still crashes because of IWDG watchdog.
I originally used the HAL_FLASH functions (HAL_FLASH_Unlock, HAL_FLASH_OB_Unlock, SET_BIT, CLEAR_BIT, etc.) to change the values of the bits in the second function that I wrote but was not seeing any changes in the registers when I printed the value of FLASH->OPTR, so I used a more hands on approach and changed the bits myself.
I would very much appreciate any help after this frustrating experience of several days.