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.

Dependents:   LAB4WEEK1 Motor

Revision:
2:cbcdd97f3a6d
Parent:
1:6d3d16f4b27c
Child:
3:524ad47afdc7
--- a/TimeUtilities.cpp	Sun May 29 20:34:27 2011 +0000
+++ b/TimeUtilities.cpp	Wed Jun 08 11:36:11 2011 +0000
@@ -15,6 +15,12 @@
 ///
 /// @author David Smart, Smartware Computing
 ///
+/// Version History
+/// 20110601
+/// \li discovered the CCALEN flag to enable calibration
+/// 20110529
+/// \li original version
+///
 #ifndef WIN32
 // embedded build
 #include "mbed.h"
@@ -29,8 +35,9 @@
 #ifdef WIN32
 // Fake it out for Win32
 struct LPC {
-    unsigned long GPREG0;
-    unsigned long CALIBRATION;
+    unsigned long CCR;          // Clock Control register
+    unsigned long GPREG0;       // General Purpose Register - Battery backed
+    unsigned long CALIBRATION;  // Calibration Register
 };
 struct LPC X;
 struct LPC * LPC_RTC = &X;
@@ -44,22 +51,27 @@
     return (i >= 0) ? 1 : -1;
 }
 
+
 RealTimeClock::RealTimeClock() {
     GetTimeOffsetStore();
 }
 
+
 RealTimeClock::~RealTimeClock() {
 }
 
+
 void RealTimeClock::GetTimeString(char *buf, time_t tValue) {
     GetTimeOffsetStore();       // Load the time zone offset values from the battery ram
     GetTimeString(buf, tValue, tzOffsetHr, tzOffsetMin);
 }
 
+
 void RealTimeClock::GetTimeString(char *buf, int hOffset, int mOffset) {
     GetTimeString(buf, 0, hOffset, mOffset);
 }
 
+
 void RealTimeClock::GetTimeString(char *buf, time_t tValue, int hOffset, int mOffset) {
     time_t ctTime;
 
@@ -71,6 +83,7 @@
     sprintf(buf + strlen(buf), " (tzo: %2i:%02i)", hOffset, mOffset);
 }
 
+
 int32_t RealTimeClock::GetTimeCalibration() {
     int32_t calvalue = LPC_RTC->CALIBRATION & 0x3FFFF;
 
@@ -80,28 +93,38 @@
     return calvalue;
 }
 
+
 void RealTimeClock::SetTimeCalibration(int32_t calibration) {
-    if (calibration < 0) {
-        calibration = (-calibration & 0x1FFFF) | 0x20000;
+    if (calibration) {
+        if (calibration < 0) {
+            calibration = (-calibration & 0x1FFFF) | 0x20000;
+        }
+        LPC_RTC->CCR = 0x000001; //(LPC_RTC->CCR & 0x0003);   // Clear CCALEN to enable it
+    } else {
+        LPC_RTC->CCR = 0x000011; //(LPC_RTC->CCR & 0x0003) | 0x0010;   // Set CCALEN to disable it
     }
     LPC_RTC->CALIBRATION = calibration;
 }
 
+
 void RealTimeClock::SetTimeOffset(int offsetHour, int offsetMinute) {
     tzOffsetHr = offsetHour;
     tzOffsetMin = offsetMinute;
     SetTimeOffsetStore();
 }
 
+
 void RealTimeClock::SetTimeOffsetStore() {
     LPC_RTC->GPREG0 = tzOffsetHr * 256 + tzOffsetMin;
 }
 
+
 void RealTimeClock::GetTimeOffsetStore() {
     tzOffsetHr = (int32_t)LPC_RTC->GPREG0 / 256;
     tzOffsetMin = (LPC_RTC->GPREG0 & 0xFF);
 }
 
+
 // MM/DD/YYYY HH:MM:SS [(+/-hh:mm)]
 bool RealTimeClock::SetTime(char * timestring) {
     bool success = false;