cancel
Showing results for 
Search instead for 
Did you mean: 

IWDT refresh and reset

pranathi1091
Associate II
Posted on March 27, 2013 at 06:42

Hello All,

I am using stm32f2 series. I have initilized Independent watch dog timer.

My intention is , when there is some probelm in the peripherals, i need to reset the device (microcontroller) after few seconds.

So my question is Refreshing a watchdog timer will do this or not?

Here is my initialization code

 

void WatchDogInit(void)

{

 /*Clear all Reset Flags */

   RCC_ClearFlag();

 /*Enable write access to IWDG_PR and IWDG_RLR registers*/

  IWDG->KR = IWDG_WRITACCESS_ENABLE;

  /*IWDG counter clock using  pre scalar 8 - LSI/8*/

  IWDG->PR = IWDG_PR_PR_0; /*Bit 0 in PR register*/

   /*Set counter reload value to obtain 1 sec IWDG TimeOut.*/

  IWDG->RLR = IWDT_RELOAD_VALUE;

  /*Reloads IWDG counter with value defined in the reload register*/

  IWDG->KR = KR_KEY_RELOAD;

  

  

}

I am not sure whether the below code make sense

void WatchDogRefresh(void)

{

 /*Writing 0xAAAA in the Key register prevents watchdog reset*/

 IWDG->KR = KR_KEY_RELOAD;

}

Coula anyone pls let me know how to implement this

Thanks

#s #watchdog-timer #wwdg
9 REPLIES 9
frankmeyer9
Associate II
Posted on March 27, 2013 at 10:45

My intention is , when there is some probelm in the peripherals, i need to reset the device (microcontroller) after few seconds.

 

This is the second-best solution, IMHO.

I would investigate that possible problems, and deal with them in a specific way. You can reset individual peripherals, for instance.

Resetting the whole MCU is more the sledge hammer kind of solution.

And basically, a watchdog is used to regain control once the code runs wild, taking unforeseen execution paths. It is an additional safety measure to catch problems which are assumed not to happen by design.

pranathi1091
Associate II
Posted on March 28, 2013 at 05:27

Thanks fm.

a watchdog is used to regain control once the code runs wild, taking unforeseen execution paths.

 

 

My question is how do i acheive this using Watchdog?

Reloading a value to counter will reset the watchdog timer or how is it?

I am bit new to this concept

Please help

frankmeyer9
Associate II
Posted on March 28, 2013 at 15:23

Basically correct.

For a 'normal' watchdog, you define a period the watchdog timer is running, until he expired and does reset the MCU.

Under normal circumstances, you regularly 'kick' the watchdog, restarting this timer. This interval should of course be shorter than the watchdog period.

My company uses such a feature in a commercial product (with a 8 bit MCU), with a period of about 250ms, where the watchdog is kicked each millisecond.

In this case, you just need to kick it before the period expires. The STM32's have also Windowed Watchdog Timer, were you can define a certain window to kick it. If you are too early or too late, it resets.

Watchdog timer have certain other features. Most often, the have an independant clock source, and can't be stopped once started.

There are examples in the standard peripheral library, and a more detailed explanation in the reference manual for your MCU variant.

I hope this gets you started.

pranathi1091
Associate II
Posted on March 29, 2013 at 12:07

Thanks for the reply

As u said i have configured Windows watch dog. Since i need to reset in case of software faults.

And here it is..

void WatchDogInit(void)

{

/*when the WWDG counter falls to 63, the WWDG reset occurs*/

/*Enable Windowed WtchDog Timer*/

RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG,ENABLE);

/* WWDG clock counter = (PCLK1 (16MHz)/4096)/8 = 488 Hz (~2048 us) */

WWDG_SetPrescaler(WWDG_Prescaler_8);

/* Set Window value to 80; WWDG counter should be refreshed only when the counter

is below 80 (and greater than 64) otherwise a reset will be generated */

WWDG_SetWindowValue(0x75);

/* Enable WWDG and set counter value to 127, WWDG timeout = ~2048 us * 64 = 131.072 ms

In this case the refresh window is: ~2048 * (127-117) = 20.48 ms < refresh window < ~2048 * 64 = 131.072 ms

*/

WWDG_Enable(0x7F);

}

But the problem I facing now is , As soon as I enable Windowed Wtchdog , it resets the device.

I am not sure what have i missed.

I have set additional count value i CFR register as well as the count value.

Please check the configuration and let me know if i wrong ..

frankmeyer9
Associate II
Posted on March 29, 2013 at 12:28

I think I not actually recommended a Windowed Watchdog for your purpose, but just mentioned this variant. It makes sense if your software is based on highly predictable intervals.

If you kick an WWDG too early, it also resets the MCU. A source of trouble could be this:

/* WWDG clock counter = (PCLK1 (16MHz)/4096)/8 = 488 Hz (~2048 us) */
WWDG_SetPrescaler(WWDG_Prescaler_8);

Are you sure that you clock the core with 16MHz ? The rest looks pretty similiar to the Library example, and should work ...
pranathi1091
Associate II
Posted on April 01, 2013 at 06:28

YEs ,  the system clock is 64Mhz, and  PCLK1 = HCLK / 4

If you kick an WWDG too early, it also resets the MCU.

 

 

To early in the sense? Shouldnt be at the startup of the system, thats when i initialioze the pheripherals?

Here is the code snippet where i kick the watch dog timer

void SysInit (void)

{

 /*System clock initialization*/

 RCC_Configuration();

 /*GPIO port pin configuration*/

 GPIO_Configuration();

  /*SPI initialization*/

 SPIInit();

 /*Disable Interrupt*/

 InterruptDisable();

 /*Peripheral interrupt handler configuration*/

 NVIC_Configuration();

 /*Timer 2 Configuration*/

 Timer_Configuration();

 /*USART initialization*/

 UartInit();

 /*WatchDog Initialization*/

 WatchDogInit();

 /*ADC Initialization*/

 AdcInit();

 /*I2c Initialization*/

 I2CInit(); }

frankmeyer9
Associate II
Posted on April 02, 2013 at 08:57

As described in section 19.3 of the reference manual, you need to kick the WWDG (write to the CR register), when the counter value is below the window register value, and greater than 0x40.

And this part might be of interest for you:

''Controlling the downcounter

This downcounter is free-running: It counts down even if the watchdog is disabled. When the

watchdog is enabled, the T6 bit must be set to prevent generating an immediate reset.''

I'm still not sure why you want/need the WWDG. I guess the IWDG would suffice for your purpose, too.

pranathi1091
Associate II
Posted on April 03, 2013 at 06:03

Thank u .

I need to reset when my software hangs , or i hve some while which will be waiting for some duration, after this duration i want the device to be reset. Since WWDG is meant for Software faults , i thought of using WWDG. Can this be done using IWDT? If so that would be really easire for me. I need no worry much about these counters .

Thanks

Pranathi

frankmeyer9
Associate II
Posted on April 03, 2013 at 09:37

Sure it can be done with the IWDT.

Both the IWDT and WWDT are basically safety measures to catch unexpected software faults. With the IWDT, you just need to access the watchdog (''kick'' it), before the configured time expires. With the WWDT, you need to access it before the configured time expires, and a certain time after you accessed it the last time. That results a certain time window to operate without a WWDT reset. Following an generalised example from a commercial device.

// ***** Scheduler, forever loop *****************
for (;;)
{
// -------> feed the watchdog everytime --------
mCLRWDT; // kick watchdog
do_this();
do_that();
manage_this();
another_function(); 
}

The actual functions are replaced with pseudo-calls here. That code runs on a slightly underpowered 8-bit MCU. As you can see, it cycles through the function calls in a permanent fashion, kicking the watchdog first. All functions are assumed to take less than one millisecond, while the watchdog is configured for about 500ms. The watchdog will reset the device only if the software got stuck somewhere unexpectedly. Even a small 8 bit MCU with less than 1k of RAM represents a FSM with a vast amount of states, which is uneconomical to catch all during software testing.