Finding the elapsed time since Gadgeteer mainboard powered up

Dec 7, 2014 at 10:40 PM
I am working on a Gadgeteer module driver, and have a need to determine the elapsed time since the mainboard powered up. Ideally, I would like to know how long ago the mainboard powered up, but the elapsed time since the last mainboard reset would be my second choice.

DateTime.Now is not a reliable source, because another module (e.g., a real-time clock module) might have already set the date and time before my code is called.

I know that Windows WMI has LastBootUpTime, but, of course, that's not available here.

Is there a simple way to get this information in the Gadgeteer/NETMF environment?

Thanks.
Dec 7, 2014 at 11:06 PM
Have you tried Microsoft.SPOT.Hardware.Utility.GetMachineTime() from Microsoft.SPOT.Native assembly?
Dec 7, 2014 at 11:35 PM
Thanks. This looks like exactly what I need. But is it okay/legal for a Gadgeteer module driver to call something in this Microsoft.SPOT.Native assembly?
Dec 7, 2014 at 11:37 PM
Yes. .NET Gadgeteer itself calls .NET Micro Framework assemblies.
Dec 8, 2014 at 12:35 AM
How about PowerState.Uptime
Dec 8, 2014 at 1:03 AM
Edited Dec 8, 2014 at 1:04 AM
PowerState.Uptime calls GetMachineTime() - execution engine start time.
GetMachineTime() typically reads values from processor's counters.
Difference might be during soft restart where counters are not cleared but engine is re-initialized (not tested).
Marked as answer by kengr on 12/7/2014 at 8:14 PM
Dec 8, 2014 at 3:14 AM
Edited Dec 13, 2014 at 10:40 PM
Thanks very much for this information. I really appreciate it. For some reason, I've been having a tougher time digging out this sort of information in NETMF than in .NET.

PowerState.Uptime seems to reset whenever the application is restarted (e.g., starting a debug session).

Utility.GetMachineTime seems to reset whenever the mainboard's physical reset button is pressed.

I'm still looking for a way, if possible, to know how long it has been since power was applied (that doesn't get reset when the mainboard reset button is pressed). The module I'm working on requires some special initialization after power has been applied, but doesn't need this extra effort if reset has been pressed (as long as power to the module hasn't been interrupted).

If that isn't possible, then Utility.GetMachineTime will do, and I will just treat each reset as if it were a power-up.