Difference between revisions of "Watchdog Demo"

From Nutwiki
Jump to: navigation, search
m (Test Environments)
 
m (1 revision imported)
 
(No difference)

Latest revision as of 17:03, 27 October 2016

Test Environments

Hardware Comments Nut/OS
4.6.3
Nut/OS
4.6.4
Nut/OS
4.7.4
Nut/OS
4.8.0
Ethernut 1.3 H Watchdog waits too long
see example 2.
Watchdog waits too long
see example 2.
Binaries
Watchdog waits too long
see example 2.
Binaries
Watchdog waits too long
see example 2.
Binaries
Ethernut 2.1 B Watchdog waits too long
see example 2.
Watchdog waits too long
see example 2.
Binaries
Watchdog waits too long
see example 2.
Binaries
Watchdog waits too long
see example 2.
Binaries
Ethernut 3.0 E OK OK
Binaries
OK
Binaries
OK
Binaries
EIR 1.0 Does not work due tue problems
with the AT91SAM7SE watchdog hardware.
Compiler: ARM-GCC 4.2.2 ; AVR-GCC 4.3.0

Description

This program demonstrates how the hardware watchdog can be used within Nut/OS applications to reset the processor, if a process takes longer as it should.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>
  3. include <dev/watchdog.h>
  4. include <sys/timer.h>

int main(void) {

   u_long baud = 115200;
   int sltime = 500;
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   NutWatchDogStart(700, 0);
   printf("\nWatchdog Started\n\n");
   for (;;) {
       NutWatchDogRestart();
       printf("Watchdog restarted\n");
       NutSleep(sltime);
   }
   return 0;

} </source>

Output

Watchdog Started
Watchdog restarted Watchdog restarted Watchdog restarted ...(infinitive)

Details

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>
  3. include <dev/watchdog.h>
  4. include <sys/timer.h>

</source> The first four lines include the necessary libraries board.h (which contains information about board specific things) and stdio.h (the standard I/O library), watchdog.h and timer.h.

within int main(void), the first function that will be executed,

<source lang="c"> u_long baud = 115200; int sltime = 500;

NutRegisterDevice(&DEV_DEBUG, 0, 0); freopen(DEV_DEBUG_NAME, "w", stdout); _ioctl(_fileno(stdout), UART_SETSPEED, &baud); </source>

u_long baud = 115200 defines a variable named "baud" of the type u_long (32bit unsigned integer) and assigns the value "115200" to it.

int sltime = 500 defines a variable of the type int (integer), calls it "sltime" and assigns the value "500" to it.

The following three lines register the UART device, open it, assign stdout to it and set the baudrate. Now the UART (Serial Port) is our standard out and every output will be redirected to there.

<source lang="c"> NutWatchDogStart(700, 0); </source>

starts the Hardware Watchdog. The function has two parameters, the first one sets the watchdog time out in ms, the second one sets the hardware specific mode, 0 sets the default mode. In this mode, the watch dog will reset the CPU if not restarted within the specified time out period.

In other words, the watchdog sets a countdown, starting at 700 (the first parameter), counting down to 0. If it does not get restarted within that period of time, it will reset the CPU. If it gets restarted, the countdown begins at "700".

<source lang="c"> for (;;) </source> creates an infinitive loop.

Within that loop, the functions

<source lang="c"> NutWatchDogRestart(); </source>

followed by

<source lang="c"> NutSleep(sltime); </source>

(parameter sets time to sleep in ms) are called. The variable sltime is set to "500", so NutSleep(sltime) halts the program for 500ms.

After that 500ms the loop executes again, starting by restarting the watchdog.


In other words, the watchdog gets restarted every 500ms.

So - Nothing happens. The watchdog is set to a time out of 700ms. If it gets restarted within that period of time, nothing happens.

Description

The following program demonstrates the case of a watchdog time out, causing a CPU reset.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>
  3. include <dev/watchdog.h>
  4. include <sys/timer.h>

int main (void) {

   u_long baud = 115200;
   int sltime = 500;
   NutRegisterDevice(&DEV_DEBUG, 0, 0);
   freopen(DEV_DEBUG_NAME, "w", stdout);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   
   NutWatchDogStart(700, 0);
   printf("\nWatchdog Started\n\n");
 
   for(;;) {
       NutWatchDogRestart();
       printf("Watchdog restarted");
       printf(", SleepTime = %d\n", sltime);                     
       sltime = sltime + 50;
       NutSleep(sltime);
   }
   return 0;

} </source>

Output (Ethernut 3.0 E)

Watchdog Started
Watchdog restarted, SleepTime = 500
Watchdog restarted, SleepTime = 550
Watchdog restarted, SleepTime = 600
Watchdog restarted, SleepTime = 650
Watchdog restarted, SleepTime = 700

Watchdog Started Watchdog restarted, SleepTime = 500 ...

Output (Ethernut 2.1 B)

Watchdog Started
Watchdog restarted, SleepTime = 500 Watchdog restarted, SleepTime = 550 Watchdog restarted, SleepTime = 600 Watchdog restarted, SleepTime = 650 Watchdog restarted, SleepTime = 700 Watchdog restarted, SleepTime = 750 Watchdog restarted, SleepTime = 800 Watchdog restarted, SleepTime = 850 Watchdog restarted, SleepTime = 900

Output (Ethernut 1.3 H)

Watchdog Started
Watchdog restarted, SleepTime = 500 Watchdog restarted, SleepTime = 550 Watchdog restarted, SleepTime = 600 Watchdog restarted, SleepTime = 650 Watchdog restarted, SleepTime = 700 Watchdog restarted, SleepTime = 750 Watchdog restarted, SleepTime = 800 Watchdog restarted, SleepTime = 850

Details

In this example there are two lines added to the previous example: "sltime = sltime + 50" and "printf(", SleepTime = %d\n", sltime)" .

<source lang="c"> sltime = sltime + 50; </source>

simply adds "50" to the value of sltime, in every cycle of the loop.

<source lang="c"> printf(", SleepTime = %d\n", sltime); </source>

just displays the actual value of sltime.


So, sltime increases by 50, everytime the loop cycles. Because sltime is our parameter of the NutSleep funtion, every cycle of the loop, the programm is halted for an additional 50ms.

After 5 cycles, "sltime = sltime + 50" is called the fifth time, and sets sltime to 750.

Seeing that, NutSleep halts the program for 750ms.


While that 750ms the watchdog already counted down from 700 to 0 and causes a CPU reset.

See also