00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <LPC21xx.h>
00010 #include <sysdefs.h>
00011 #include <system.h>
00012 #include <sysdefs.h>
00013 #include <lpcRTC.h>
00014 #include <time.h>
00015 #include <sys/times.h>
00016
00017 #define CCR_CLKEN BIT(0)
00018 #define CCR_CTCRST BIT(1)
00019 #define CCR_CTTEST0 BIT(2)
00020 #define CCR_CTTEST1 BIT(3)
00021 #define CCR_CLKSRC BIT(4)
00022 #define T0_MS_DIV (PCLK / 10000)
00023
00024
00025 long systemTickCounter;
00026
00027 static inline void sleepClock(void)
00028 {
00029 RTCCCR = (CCR_CLKEN | CCR_CLKSRC);
00030
00031 PCONP &= ~PCRTC;
00032 }
00033
00034 static inline void awakenClock(void)
00035 {
00036 RTCCCR = (CCR_CLKEN | CCR_CLKSRC);
00037
00038 PCONP |= PCRTC;
00039 }
00040
00041 static void startMsTimer (void)
00042 {
00043
00044 Timer0VectAddr = (uint32_t)timer0ISR;
00045 Timer0VectCntl = VIC_ENABLE | VIC_TIMER0;
00046 VICIntSelect &= ~VIC_BIT(VIC_TIMER0);
00047 VICIntEnable = VIC_BIT(VIC_TIMER0);
00048 T0TCR = TCR_RESET;
00049 T0PC = 0;
00050 T0PR = T0_MS_DIV;
00051 T0CCR = 0;
00052 T0EMR = 0;
00053 T0MR0 = 9;
00054 T0MCR = 3;
00055 T0TCR = TCR_ENABLE;
00056
00057 systemTickCounter = 0;
00058 }
00059
00060 void setElapsed (int HowLong, struct TIMER * timer, timertype TimerType)
00061 {
00062 timer->HowLong = HowLong;
00063 timer->Type = TimerType;
00064 if (timer->Type == TimerInMilli) timer->SetTime = times (0);
00065 else timer->SetTime = time (0);
00066 }
00067
00068 bool timeElapsed (struct TIMER * timer)
00069 {
00070 time_t TestTimerNow, TestTimeStart;
00071 if (timer->Type == TimerInMilli) TestTimerNow = times (0);
00072 else TestTimerNow = time (0);
00073 TestTimeStart = timer->SetTime;
00074 if (((TestTimeStart>TestTimerNow) ? (TestTimerNow + (~TestTimeStart)) : (TestTimerNow - TestTimeStart)) > timer->HowLong) {
00075 return True;
00076 }
00077 return False;
00078 }
00079
00080 void initRTC(void)
00081 {
00082 #ifdef HAS_CLOCK
00083 bool nonsense = False;
00084 rtcCTIME0_t ctime0;
00085 rtcCTIME1_t ctime1;
00086 rtcCTIME2_t ctime2;
00087 struct tm newTime;
00088 time_t resetTime;
00089 awakenClock();
00090
00091
00092 ctime0 = RTCCTIME0; ctime1 = RTCCTIME1; ctime2 = RTCCTIME2;
00093
00094 if ((ctime0.seconds < 0) || (ctime0.seconds > 59)) nonsense = True;
00095 if ((ctime0.minutes < 0) || (ctime0.minutes > 59)) nonsense = True;
00096 if ((ctime0.hours < 0) || (ctime0.hours > 23)) nonsense = True;
00097 if ((ctime1.dayOfMonth < 1) || (ctime1.dayOfMonth > 31)) nonsense = True;
00098 if ((ctime1.month < 1) || (ctime1.month > 12)) nonsense = True;
00099 if ((ctime1.year < 1980) || (ctime1.year > 2050)) nonsense = True;
00100 if ((ctime0.dayOfWeek < 0) || (ctime0.dayOfWeek > 6)) nonsense = True;
00101 if ((ctime2.dayOfYear < 0) || (ctime2.dayOfYear > 366)) nonsense = True;
00102 sleepClock ();
00103 if (nonsense) {
00104
00105 resetTime = 1136073600l;
00106 localtime_r (&resetTime, &newTime);
00107 newTime.tm_year += 1900;
00108 newTime.tm_mon += 1;
00109 newTime.tm_yday += 1;
00110 writeRTC (&newTime);
00111 }
00112 #endif
00113
00114 startMsTimer();
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124 void readRTC (struct tm *theTime)
00125 {
00126 #ifdef HAS_CLOCK
00127 rtcCTIME0_t ctime0;
00128 rtcCTIME1_t ctime1;
00129 rtcCTIME2_t ctime2;
00130 awakenClock();
00131
00132 ctime0 = RTCCTIME0; ctime1 = RTCCTIME1; ctime2 = RTCCTIME2;
00133
00134 theTime->tm_sec = ctime0.seconds;
00135 theTime->tm_min = ctime0.minutes;
00136 theTime->tm_hour = ctime0.hours;
00137 theTime->tm_mday = ctime1.dayOfMonth;
00138 theTime->tm_mon = ctime1.month;
00139 theTime->tm_year = ctime1.year;
00140 theTime->tm_wday = ctime0.dayOfWeek;
00141 theTime->tm_yday = ctime2.dayOfYear;
00142 theTime->tm_isdst = False;
00143 sleepClock ();
00144 #endif
00145 }
00146
00147 void writeRTC (struct tm *newTime)
00148 {
00149 #ifdef HAS_CLOCK
00150 awakenClock();
00151
00152 RTCTCR->seconds = newTime->tm_sec;
00153 RTCTCR->minutes = newTime->tm_min;
00154 RTCTCR->hours = newTime->tm_hour;
00155 RTCTCR->dayOfMonth = newTime->tm_mday;
00156 RTCTCR->month = newTime->tm_mon;
00157 RTCTCR->year = newTime->tm_year;
00158 RTCTCR->dayOfWeek = newTime->tm_wday;
00159 RTCTCR->dayOfYear = newTime->tm_yday;
00160 sleepClock ();
00161 #endif
00162 }
00163