Go Down

Topic: Watchdog timer (Read 2136 times) previous topic - next topic

krzysiek

Hi,

I think it's not possible to use Watchdog timer on Arduino Duo (with Arduino framework), regarding data sheet (17.4, p.268):

"The Watchdog Mode Register (WDT_MR) can be written only once. Only a processor reset
resets it. Writing the WDT_MR register reloads the timer with the newly programmed mode
parameters."

and watchdog is disabled at startup in init() function:

"// Disable watchdog
WDT_Disable(WDT);"

This makes it impossible to turn it again. It would be nice to have possibility to turn it on, for now it's some limitation here.

Regards

gallegojm

Hi,
I think it would be possible, but you have to modify init() in
arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x\variant.cpp
replacing instruction WDT_Disable(WDT); by somes calls to fonctions of wdt.c in
arduino-1.5.2\hardware\arduino\sam\system\libsam\source\wdt.c

May be the most easy would be to skip instruction WDT_Disable(WDT); and configure watchdog at the beginning your setup().
According to comment at the beginning of wdt.c you have 16 seconds to perform this configuration. Would it be enough for reprogramming the Due in order to modify your sketch?

Personally, I am not the valiant enough to try it  :smiley-eek:

See the following topic: http://arduino.cc/forum/index.php/topic,152885.0.html

Kind Regards.

gallegojm

First, it is necessary to comment line 353 of file variant.cpp in subdirectory arduino-1.5.2\hardware\arduino\sam\variants\arduino_due_x\

Code: [Select]
  // Disable watchdog
  // WDT_Disable(WDT);

Second, insert at the beginning of the loop() function a call to function WDT_Restart() in order to restart the watchdog' timer each time the loop is executed.
An example:

Code: [Select]
int waitTime = 0;

void setup()
{
  Serial.begin( 9600 );
}

void loop()
{
  // Restart watchdog
  WDT_Restart( WDT );
  Serial.print( "Following function last " );
  Serial.print( waitTime );
  Serial.println( " seconds" );
  delay( waitTime * 1000 );
  waitTime ++;
}

By default, watchdog period is set to its maximum value, 16 seconds.
So, when variable waitTime reach the value of 16, watchdog' counter reach 0 before the delay() function finish and a reset occurs.

Optionally, you can reduce the period of the watchdog with a call to WDT_Enable(). This function modify the Watchdog Timer Mode Register. This can be done only once after reset and the maximum value 4096 is equivalent to 4096 / 256 = 16 seconds.
In the following example, the variable waitTime would never reach a value higher than 5:
Code: [Select]
int waitTime = 0;

void setup()
{
  // Variable wdp_ms hold the periode in 256 th of seconds of watchdog
  // It must be greater than 3 et less or equal to 4096
  // The following value set a periode of 4,5 seconds (256 x 4,5 = 1152)
  uint32_t wdp_ms = 1152 ;
  WDT_Enable( WDT, 0x2000 | wdp_ms | ( wdp_ms << 16 ));
  Serial.begin( 9600 );
}

void loop()
{
  // Restart watchdog
  WDT_Restart( WDT );
  Serial.print( "Following function last " );
  Serial.print( waitTime );
  Serial.println( " seconds" );
  delay( waitTime * 1000 );
  waitTime ++;
}


On the other hand, once you have modified the file variant.cpp, you have to insert a call to WDT_Disable() at the beginning of your sketches than don't use the watchdog:

Code: [Select]
void setup()
{
  // Disable watchdog
  WDT_Disable( WDT );
  ....
}


Go Up