cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 IWDG freeze in STOP mode

MMoli.7
Associate II

Hello,

I would like to freeze the functionality of the IWDG while the microcontroller is in STOP mode, because I want stop time greater than 32 seconds. I read the documentation and I built the following code:

 FLASH_OBProgramInitTypeDef pOBInit;
 pOBInit.OptionType = OPTIONBYTE_USER;      // Option byte to be configured
 pOBInit.USERType = OB_USER_IWDG_STOP;         // User option byte(s) to be configured 
 pOBInit.USERConfig = OB_IWDG_STOP_FREEZE;  // Value of the user option byte 
 
HAL_FLASH_OB_Unlock();                       // Unlock the FLASH Option Bytes Registers access.
HAL_FLASHEx_OBProgram(&pOBInit);               // Program Option bytes.
HAL_FLASH_OB_Launch();                        //  Launch the option byte loading.
HAL_FLASH_OB_Lock();                           // Lock the FLASH Option Bytes Registers access.

But I get a HardFault when executing the unlock function.

Could someone help me with this problem?

Best regards,

6 REPLIES 6
Imen.D
ST Employee

Hello,

In STOP mode, once the watchdog is enabled, then it cannot be disabled again except by a reset. According to the reference manual: "the IWDG is started by writing to its Key register or by hardware option. Once started it cannot be stopped except by a Reset."

For more details, see Section IWDG functional description in the reference manual.

Kind Regards,

Imen

When your question is answered, please close this topic by clicking "Accept as Solution".
Thanks
Imen
MMoli.7
Associate II

Hello,

I don't want to disable the watchdog, I want to freeze during the STOP mode. In the reference manual section 32.3.4 "Depending on the IWDG_STOP and IWDG_STBY options configuration, the IWDG can

continue counting or not during the Stop mode and the Standby mode respectively.". Then if you go to

the Flash option register (FLASH_OPTR) the Bit 17 (IWDG_STOP) has this definition: Independent watchdog counter freeze in Stop mode.

In order to write this register it is needed to unlock it before, then you write de new value and launch the values writing in OBL_LAUNCH bit in FLASH_CR register.

But something is going wrong and it doesn't work.

MMoli.7
Associate II

Dupe

MMoli.7
Associate II

Dupe

MMoli.7
Associate II

Hello,

I found the solution to my problem, I forgot first to call the function "HAL_FLASH_Unlock ()" to Unlock the FLASH control register access.

But now I have another problem, if I execute the code the microcontroller does not stop resetting again and again. So if I comment the code the options remain saved and the IWDG freezes in STOP mode.

Does anyone know how to avoid this problem?

ashiz
Associate II

Hello,

It seems that you need to add the following two.

(1) Clear the FLASH's pending flags.

(2) Get the Option bytes configuration, then change pOBInit.

It looks like the following code.

FLASH_OBProgramInitTypeDef pOBInit;

 HAL_FLASH_Unlock();

 __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR); // Clear the FLASH's pending flags.

 HAL_FLASH_OB_Unlock();

 HAL_FLASHEx_OBGetConfig(&pOBInit); // Get the Option bytes configuration.

 pOBInit.OptionType = OPTIONBYTE_USER;

 pOBInit.USERType = OB_USER_IWDG_STOP;

 pOBInit.USERConfig = OB_IWDG_STOP_FREEZE;

 HAL_FLASHEx_OBProgram(&pOBInit);

 HAL_FLASH_OB_Lock();

 HAL_FLASH_Lock();