I’ve been working this issue for a couple of weeks now and GHI has helped tremendously in trying to establish if there is or is not a problem when the RTC gets into unknown state and stops ticking.
The potential problem arises if a power interruption puts the RTC in an indeterminate state until it is reset either during the boot sequence or by program. This is important when your application needs to get right back up and running when a user replaces a backup battery or some other problem puts the RTC in an indeterminate state.
The problem I’ve experienced with two Panda II’s I’m testing is that when they default to a startup date of 1/1/1977, the RTC may not tick even after resetting it to a current date and time. I was advised to test this by writing a short program that would use a button (LDR in this case) to reset the RTC when it was pushed. To monitor this the program starts up by printing out RTC time and local tine every two seconds, and of course they should be in synch. When the LDR button is pushed both clocks reset to
04/19/2011 12:00:00 and if all is well they keep ticking along in lockstep ad infinitum.
The problem I am observing in the two Panda’s I’m testing is that sometimes after installing and removing a backup battery, ultracapacitor, or even just powering down, the Panda comes back up with the default time in an indeterminate state and even setting the RTC with this program won’t get it ticking again. Right now I have one Panda running this program and a LDR button push gets the right times and ticks, and the other Panda with the identical program that will not get a ticking RTC no matter what I push.
Now it’s possible that I have the only two Pandas that exhibit this behavior or it may even be that time relativity is even more exaggerated than Einstein thought here in the Idaho outback, or it may also be that I’m making some incredibly stupid mistake, but I frequently see the RTC ticking stop when I run my tests.
If anyone is interested or bored and would like to verify or disprove this behavior please load the following program and check their results against what I’ve described. The problem won’t show up unless the RTC gets into an indeterminate state so it may require installing some form of backup Vbatt and the removing it to get the Panda’s RTC in an indeterminate state, or it may enter this state simply when system power is removed with no Vbatt. I’ve not found a reliable way to get into this mode.
If nothing else the code snippet below may be useful for illustrating how to use the LDR button for a user initiated interrupt. It was adapted from section 8.2.1 in the Beginner’s Guide to .NETMF, and shows some changes that had to made to the code in the Guide.
Thanks to anyone who wants to try explain what might cause this behavior or who finds an error in how I’ve described the problem . Here’s the code followed by a screen shot of the Visual Studio output for the Panda that currently won’t tick:
/* RTC_Test_button - C# .NETMF SOLUTION
* Written to verify RTC operation
* Depending on its initial state a RTC may or may not increment properly.
* Setting the RTC should establish a known state. This short program tests
* the RTC and local clock for any timekeeping anamolies in a Panda II.
*/
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
namespace RTC_Test_button
{
public class Program
{
public static void Main()
{
// the next line can be uncommented if you want start with a known RTC setting
// RealTimeClock.SetTime(new DateTime(2011, 04, 19, 09, 0, 0, 0));
Utility.SetLocalTime(RealTimeClock.GetTime());
// set up the LDR button to generate an interrupt on high edge
InterruptPort IntButton = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeHigh);
// add an interrupt handler to the pin
IntButton.OnInterrupt += new NativeEventHandler(IntButton_OnInterrupt);
// start a loop to print out times every two seconds
while (true)
{
// print out local time and RTC time every two seconds
Debug.Print(" here's local time: " + DateTime.Now.ToString());
Debug.Print(" here's RTC time: " + RealTimeClock.GetTime().ToString());
Debug.Print("");
Thread.Sleep(2000);
}
}
static void IntButton_OnInterrupt(uint port, uint state, DateTime time)
{
// LDR button has been pushed - reset clocks to known value
RealTimeClock.SetTime(new DateTime(2011, 04, 19, 12, 0, 0, 0));
Utility.SetLocalTime(RealTimeClock.GetTime());
Debug.Print(" RTC and local time have been reset to 04/19/2011 12:00:00 ");
Debug.Print(" here's local time: " + DateTime.Now.ToString());
Debug.Print(" here's RTC time: " + RealTimeClock.GetTime().ToString());
Debug.Print("");
}
}
}