Skip to main content
MMoli.7
Associate
January 21, 2019
Question

STM32L4 IWDG freeze in STOP mode

  • January 21, 2019
  • 6 replies
  • 2695 views

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,

This topic has been closed for replies.

6 replies

Technical Moderator
January 21, 2019

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

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
MMoli.7
MMoli.7Author
Associate
January 21, 2019

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
MMoli.7Author
Associate
January 21, 2019

Dupe

MMoli.7
MMoli.7Author
Associate
January 21, 2019

Dupe

MMoli.7
MMoli.7Author
Associate
January 23, 2019

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
March 28, 2019

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();