About to add watchdog I am.
The code described on page 85 of the Beginner’s Guide didn’t quite work for me when I tried it and I would like to know if the following example correctly implements the watchdog function. The main changes from the Beginner’s Guide code were the syntax used to reference the GHI watchdog class.
Since I’m going to be inserting the watchdog function into a large piece of code that does a number of functions, I wanted to make sure I’ve understood the concept and syntax. Please advise if you see errors or poor coding if you have a minute. It does seem to work on the Panda II on which it is currently deployed.
Thanks in advance.
// 5/12/11 - Watchdog test program - adapted from Beginner's Guide page 85
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware.LowLevel;
using GHIElectronics.NETMF.FEZ;
namespace Watchdog
{
public class Program
{
public static void Main()
{
// Timeout 5 seconds
int i = 0;
uint timeout = 1000 * 5;
// Enable Watchdog using GHI code
GHIElectronics.NETMF.Hardware.LowLevel.Watchdog.Enable(timeout);
// Start a time counter reset thread
WDTCounterReset = new Thread(WDTCounterResetLoop);
GHIElectronics.NETMF.Hardware.LowLevel.Watchdog.ResetCounter();
// dummy program starts here - just print integers 1-20 & then execute some bad code to throw an exception
while (true)
{
while (i < 20)
{
Debug.Print(" ticking right along ... " + i.ToString());
i++;
}
// start of bad code block used to throw an unhandled exception
Debug.Print(" now try to throw exception" + i.ToString());
// byte dc = 0, Dcm = 0;
uint dknt = 0;
byte[] wb = new byte[4];
byte[] rb = new byte[3]; // byte[3] throws an unhandled exception, 4 is ok
wb[0] = 10;
GHIElectronics.NETMF.Hardware.BatteryRAM.Read(dknt, rb, 0, 4);
Debug.Print("BRAM location " + dknt.ToString() + " = " + rb[0].ToString());
i = 0;
//end of bad code block
}
// If we exit the program,
// the thread will stop working and the system will reset!
}
static Thread WDTCounterReset;
static void WDTCounterResetLoop()
{
while (true)
{
// reset time counter every 3 seconds
Thread.Sleep(3000);
GHIElectronics.NETMF.Hardware.LowLevel.Watchdog.ResetCounter();
// next line was from Beginner's Guide example - doesn't point to GHI code for watch dog
// Watchdog.ResetCounter();
}
}
}
}