http://www.ethernut.de/nutwiki/index.php?title=Watchdog_Demo&feed=atom&action=historyWatchdog Demo - Revision history2024-03-28T09:18:07ZRevision history for this page on the wikiMediaWiki 1.26.2http://www.ethernut.de/nutwiki/index.php?title=Watchdog_Demo&diff=294&oldid=prevHarald: 1 revision imported2016-10-27T16:03:02Z<p>1 revision imported</p>
<table class='diff diff-contentalign-left'>
<tr style='vertical-align: top;' lang='en'>
<td colspan='1' style="background-color: white; color:black; text-align: center;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black; text-align: center;">Revision as of 16:03, 27 October 2016</td>
</tr><tr><td colspan='2' style='text-align: center;' lang='en'><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>Haraldhttp://www.ethernut.de/nutwiki/index.php?title=Watchdog_Demo&diff=293&oldid=prevDaniel: /* Test Environments */2009-03-10T17:39:31Z<p><span dir="auto"><span class="autocomment">Test Environments</span></span></p>
<p><b>New page</b></p><div>== Test Environments ==<br />
<br />
<br />
{| class="smalldiv"<br />
| style="background-color:#F6F9ED" | Hardware Comments<br />
| style="background-color:#F6F9ED" |<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.6.3'''<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.6.4'''<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.7.4'''<br />
| style="background-color:#F6F9ED" | '''Nut/OS<br>4.8.0'''<br />
|-<br />
| <span style="color:orange">Ethernut 1.3 H</span><br />
| <br />
| Watchdog waits too long<br>see example 2.<br />
| Watchdog waits too long<br>see example 2. <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut13H-nutos-4.6.4.zip '''Binaries''']<br />
| Watchdog waits too long<br>see example 2. <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut13H-nutos-4.7.4.zip '''Binaries''']<br />
| Watchdog waits too long<br>see example 2. <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut13H-nutos-4.8.0.zip '''Binaries''']<br />
|-<br />
| <span style="color:orange">Ethernut 2.1 B</span><br />
|<br />
| Watchdog waits too long<br>see example 2.<br />
| Watchdog waits too long<br>see example 2. <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut21B-nutos-4.6.4.zip '''Binaries''']<br />
| Watchdog waits too long<br>see example 2. <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut21B-nutos-4.7.4.zip '''Binaries''']<br />
| Watchdog waits too long<br>see example 2. <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut21B-nutos-4.8.0.zip '''Binaries''']<br />
|-<br />
| <span style="color:green">Ethernut 3.0 E</span><br />
|<br />
| OK<br />
| OK <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut30E-nutos-4.6.4.zip '''Binaries''']<br />
| OK <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut30E-nutos-4.7.4.zip '''Binaries''']<br />
| OK <br> [http://ethernut.de/arc/WIKI_BINARIES_ZIPPED/watchdog-enut30E-nutos-4.8.0.zip '''Binaries''']<br />
|- <br />
| <span style="color:red">EIR 1.0</span><br />
| Does not work due tue problems<br> with the AT91SAM7SE watchdog hardware.<br />
| <br />
| <br />
|<br />
|-<br />
| id="compilertd" colspan="6" |'''Compiler:''' ARM-GCC 4.2.2 ; AVR-GCC 4.3.0<br />
|}<br />
<br />
== Description ==<br />
<br />
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.<br />
<br />
== Source Code ==<br />
<br />
<source lang="c"><br />
#include <dev/board.h><br />
#include <stdio.h><br />
#include <dev/watchdog.h><br />
#include <sys/timer.h><br />
<br />
int main(void)<br />
{<br />
u_long baud = 115200;<br />
int sltime = 500;<br />
<br />
NutRegisterDevice(&DEV_DEBUG, 0, 0);<br />
freopen(DEV_DEBUG_NAME, "w", stdout);<br />
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);<br />
<br />
NutWatchDogStart(700, 0);<br />
<br />
printf("\nWatchdog Started\n\n");<br />
<br />
for (;;) {<br />
NutWatchDogRestart();<br />
printf("Watchdog restarted\n");<br />
NutSleep(sltime);<br />
}<br />
<br />
return 0;<br />
}<br />
</source><br />
<br />
== Output ==<br />
<br />
Watchdog Started<br><br />
Watchdog restarted<br />
Watchdog restarted<br />
Watchdog restarted<br />
...(infinitive)<br />
<br />
== Details ==<br />
<br />
<source lang="c"><br />
#include <dev/board.h><br />
#include <stdio.h><br />
#include <dev/watchdog.h><br />
#include <sys/timer.h><br />
</source><br />
The first four lines include the necessary libraries <code>board.h</code> (which contains information about board specific things) and <code>stdio.h</code> (the standard I/O library), <code>watchdog.h</code> and <code>timer.h</code>.<br />
<br />
within <code>int main(void)</code>, the first function that will be executed,<br />
<br />
<source lang="c"><br />
u_long baud = 115200;<br />
int sltime = 500;<br />
<br />
NutRegisterDevice(&DEV_DEBUG, 0, 0);<br />
freopen(DEV_DEBUG_NAME, "w", stdout);<br />
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);<br />
</source><br />
<br />
<code>u_long baud = 115200</code> defines a variable named "baud" of the type u_long (32bit unsigned integer) and assigns the value "115200" to it.<br />
<br />
<code>int sltime = 500</code> defines a variable of the type int (integer), calls it "<code>sltime</code>" and assigns the value "500" to it.<br />
<br />
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.<br />
<br />
<source lang="c"><br />
NutWatchDogStart(700, 0);<br />
</source><br />
<br />
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.<br />
<br />
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".<br />
<br />
<source lang="c"><br />
for (;;)<br />
</source><br />
creates an infinitive loop.<br />
<br />
Within that loop, the functions <br />
<br />
<source lang="c"><br />
NutWatchDogRestart();<br />
</source><br />
<br />
followed by <br />
<br />
<source lang="c"><br />
NutSleep(sltime);<br />
</source><br />
<br />
(parameter sets time to sleep in ms)<br />
are called.<br />
The variable <code>sltime</code> is set to "500", so <code>NutSleep(sltime)</code> halts the program for 500ms.<br />
<br />
After that 500ms the loop executes again, starting by restarting the watchdog.<br />
<br />
<br />
In other words, the watchdog gets restarted every 500ms.<br />
<br />
So - Nothing happens. The watchdog is set to a time out of 700ms. If it gets restarted within that period of time, nothing happens.<br />
<br />
== Description ==<br />
<br />
The following program demonstrates the case of a watchdog time out, causing a CPU reset.<br />
<br />
== Source Code ==<br />
<br />
<source lang="c"><br />
#include <dev/board.h><br />
#include <stdio.h><br />
#include <dev/watchdog.h><br />
#include <sys/timer.h><br />
<br />
int main (void)<br />
{<br />
u_long baud = 115200;<br />
int sltime = 500;<br />
<br />
NutRegisterDevice(&DEV_DEBUG, 0, 0);<br />
freopen(DEV_DEBUG_NAME, "w", stdout);<br />
_ioctl(_fileno(stdout), UART_SETSPEED, &baud);<br />
<br />
NutWatchDogStart(700, 0);<br />
<br />
printf("\nWatchdog Started\n\n");<br />
<br />
for(;;) {<br />
NutWatchDogRestart();<br />
printf("Watchdog restarted");<br />
printf(", SleepTime = %d\n", sltime); <br />
sltime = sltime + 50;<br />
NutSleep(sltime);<br />
}<br />
<br />
return 0;<br />
}<br />
</source><br />
<br />
== Output (Ethernut 3.0 E) ==<br />
<br />
Watchdog Started<br />
Watchdog restarted, SleepTime = 500<br />
Watchdog restarted, SleepTime = 550<br />
Watchdog restarted, SleepTime = 600<br />
Watchdog restarted, SleepTime = 650<br />
Watchdog restarted, SleepTime = 700<br />
<br><br />
Watchdog Started<br />
Watchdog restarted, SleepTime = 500<br />
...<br />
<br />
== Output (Ethernut 2.1 B) ==<br />
<br />
Watchdog Started<br><br />
Watchdog restarted, SleepTime = 500<br />
Watchdog restarted, SleepTime = 550<br />
Watchdog restarted, SleepTime = 600<br />
Watchdog restarted, SleepTime = 650<br />
Watchdog restarted, SleepTime = 700<br />
Watchdog restarted, SleepTime = 750<br />
Watchdog restarted, SleepTime = 800<br />
Watchdog restarted, SleepTime = 850<br />
Watchdog restarted, SleepTime = 900<br />
<br />
== Output (Ethernut 1.3 H) ==<br />
<br />
Watchdog Started<br><br />
Watchdog restarted, SleepTime = 500<br />
Watchdog restarted, SleepTime = 550<br />
Watchdog restarted, SleepTime = 600<br />
Watchdog restarted, SleepTime = 650<br />
Watchdog restarted, SleepTime = 700<br />
Watchdog restarted, SleepTime = 750<br />
Watchdog restarted, SleepTime = 800<br />
Watchdog restarted, SleepTime = 850<br />
<br />
== Details ==<br />
<br />
In this example there are two lines added to the previous example:<br />
"<code>sltime = sltime + 50</code>" and "<code>printf(", SleepTime = %d\n", sltime)</code>" .<br />
<br />
<source lang="c"><br />
sltime = sltime + 50;<br />
</source><br />
<br />
simply adds "50" to the value of <code>sltime</code>, in every cycle of the loop.<br />
<br />
<source lang="c"><br />
printf(", SleepTime = %d\n", sltime); <br />
</source><br />
<br />
just displays the actual value of <code>sltime</code>.<br />
<br />
<br />
So, <code>sltime</code> increases by 50, everytime the loop cycles. Because <code>sltime</code> is our parameter of the <code>NutSleep</code> funtion, every cycle of the loop, the programm is halted for an additional 50ms.<br />
<br />
After 5 cycles, "<code>sltime = sltime + 50</code>" is called the fifth time, and sets <code>sltime</code> to 750.<br />
<br />
Seeing that, <code>NutSleep</code> halts the program for 750ms.<br />
<br />
<br />
<br />
While that 750ms the watchdog already counted down from 700 to 0 and causes a CPU reset.<br />
<br />
== See also ==<br />
* [[System Reset]]<br />
* More [[Nut/OS Examples]]<br />
<br />
[[Category:Code Snippets]]<br />
<br />
{{Languages}}</div>Daniel