This is a library for managing the mbed real time clock, including functions for setting and getting the rtc in text format, getting and setting the timezone offset, and getting and setting the calibration register, both directly, and by using an adjustment API to automatically set the calibration value.
Diff: TimeUtilities.cpp
- Revision:
- 6:a517fee06e2e
- Parent:
- 5:fbbdf57675c3
- Child:
- 7:812452a275fa
diff -r fbbdf57675c3 -r a517fee06e2e TimeUtilities.cpp --- a/TimeUtilities.cpp Sun Oct 30 23:42:43 2011 +0000 +++ b/TimeUtilities.cpp Wed Nov 02 01:32:07 2011 +0000 @@ -20,7 +20,7 @@ #include "mbed.h" #endif -#define VERSION "1.03" +#define VERSION "1.04" #include "TimeUtilities.h" #include <time.h> @@ -43,7 +43,6 @@ static int tzOffsetHr = 0; ///!< time zone offset hours to print time in local time static int tzOffsetMin = 0; ///!< time zone offset minutes to print time in local time -static time_t timeWhenLastSet = 0; static const char ver[] = VERSION; @@ -55,7 +54,6 @@ RealTimeClock::RealTimeClock() { GetTimeOffsetStore(); - GetTimeLastSetStore(); } @@ -70,10 +68,6 @@ return time(NULL); } -time_t RealTimeClock::GetTimeValueWhenSet() { - return timeWhenLastSet; -} - void RealTimeClock::GetTimeString(char *buf, time_t tValue) { GetTimeOffsetStore(); // Load the time zone offset values from the battery ram @@ -138,12 +132,12 @@ tzOffsetMin = (LPC_RTC->GPREG0 & 0xFF); } -void RealTimeClock::SetTimeLastSetStore() { - LPC_RTC->GPREG1 = timeWhenLastSet; +void RealTimeClock::SetTimeLastSet(time_t t) { + LPC_RTC->GPREG1 = t; } -void RealTimeClock::GetTimeLastSetStore() { - timeWhenLastSet = LPC_RTC->GPREG1; +time_t RealTimeClock::GetTimeLastSet() { + return LPC_RTC->GPREG1; } // MM/DD/YYYY HH:MM:SS [(+/-hh:mm)] @@ -187,8 +181,7 @@ seconds = mktime(t); seconds = seconds - (time_t)(tzOffsetHr * 3600 + tzOffsetMin * 60); set_time(seconds); - timeWhenLastSet = seconds; - SetTimeLastSetStore(); + SetTimeLastSet(seconds); } } } @@ -199,18 +192,16 @@ } bool RealTimeClock::AdjustBySeconds(int32_t adjustSeconds) { - if (timeWhenLastSet != 0) { + time_t lastSet = GetTimeLastSet(); + + if (lastSet != 0) { time_t seconds = time(NULL); // get "now" according to the rtc - int32_t delta = seconds - timeWhenLastSet; + int32_t delta = seconds - lastSet; int32_t curCal = GetTimeCalibration(); int32_t calMAX = 131071; int32_t secPerDay = 86400; float errSecPerDay; - // Make the clock correct - seconds = seconds + adjustSeconds; - set_time(seconds); - // Convert the current calibration and the adjustment into // the new calibration value // assume it is +10sec and it has been 2days, then the adjustment @@ -219,6 +210,12 @@ // delta = now - then (number of elapsed seconds) if (adjustSeconds != 0 && delta != 0) { int32_t calFactor; + + // Make the clock correct + seconds = seconds + adjustSeconds; + set_time(seconds); + SetTimeLastSet(seconds); + // Compute the calibration factor errSecPerDay = (float)adjustSeconds / ((float)(delta)/secPerDay); calFactor = (int32_t)((float)secPerDay/errSecPerDay); if (abs(calFactor) < calMAX)