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*
// 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:
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:
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:
void setup()
{
// Disable watchdog
WDT_Disable( WDT );
....
}