Watch Dog and Sleep

18 Mar 2011

Hi,

I wrote a function to sleep for some certain time, the code is as follows:

void WDT_IRQHandler(void)
{
  NVIC_DisableIRQ(WDT_IRQn);
  WDT_ClrTimeOutFlag();
}

int sleep(long long useconds)
{
  /* Clear timeout flag */
  if (WDT_ReadTimeOutFlag())
      WDT_ClrTimeOutFlag();
  
  /*Init watchdog timer */
  NVIC_SetPriority(WDT_IRQn, 0x10);
  WDT_Init(WDT_CLKSRC_IRC, WDT_MODE_INT_ONLY);

  NVIC_EnableIRQ(WDT_IRQn); 
  WDT_Start(useconds);
  
  CLKPWR_Sleep();

  return 0;
}

int test_sleep()
{
  LED_On(LED1);

  /* sleep for five seconds */
  sleep(5000000);
  LED_On(LED2);

  /* sleep for another five seconds */
  sleep(5000000);
  LED_Off(LED2);

  while(1);

  return 0;
}

But when I test it, I find that the first sleep(5000000) works, but the second sleep(5000000) does not. Then I checked the user manual, I found this is caused by the WDINT flag:

"WDINT The Watchdog interrupt flag is set when the Watchdog times out. This flag is cleared when any reset occurs. Once the watchdog interrupt is serviced, it can be disabled in the NVIC or the watchdog interrupt request will be generated indefinitely. the intent of the watchdog interrupt is to allow debugging watchdog activity without resetting the device when the watchdog overflows."

This means that after the first time the interrupt occurs, WDINT is set. Then when the second time sleep function is called. Because WDINT is set now, interrupt occurs immediately after NVIC_EnableIRQ(WDT_IRQn), but not five seconds later.

Is there any way to clear WDINT, so that the second sleep() can work properly?

Thanks!

Regards, Jessamine

18 Mar 2011

Hi,

May be watch dog timer is not a right timer for me to choose...

Which interrupt or timer is usually used for sleeping a certain time? I do not want to use RTC, since it is seconds precision。

Regards, Jessamine