📄 rtcfuncs.c
字号:
/************************************************************* * RealTime awareness functions. Provide for elapsed time * intervals as well as reading data & time from RTC. * * (C) 2005 - Tom Walsh <tom@openhardware.net> * May be used for hobby or commercial purposes as long as * this notice remains intact.*************************************************************/#include <LPC21xx.h>#include <sysdefs.h>#include <system.h>#include <sysdefs.h>#include <lpcRTC.h>#include <time.h>#include <sys/times.h>#define CCR_CLKEN BIT(0)#define CCR_CTCRST BIT(1)#define CCR_CTTEST0 BIT(2)#define CCR_CTTEST1 BIT(3)#define CCR_CLKSRC BIT(4)#define T0_MS_DIV (PCLK / 10000)long systemTickCounter;static inline void sleepClock(void){// place RTC on 32kHz xtal and disconnect power. RTCCCR = (CCR_CLKEN | CCR_CLKSRC); // power off the clock registers. PCONP &= ~PCRTC;}static inline void awakenClock(void){// prepare clock for interactive use. RTCCCR = (CCR_CLKEN | CCR_CLKSRC); // power up the clock registers. PCONP |= PCRTC;}static void startMsTimer (void){// tick timer, 1ms heartbeat for the system. // connect VIC to Timer0 Timer0VectAddr = (uint32_t)timer0ISR; // address of the ISR Timer0VectCntl = VIC_ENABLE | VIC_TIMER0; VICIntSelect &= ~VIC_BIT(VIC_TIMER0); // Timer0 selected as IRQ VICIntEnable = VIC_BIT(VIC_TIMER0); // Timer0 interrupt enabled T0TCR = TCR_RESET; // reset & disable timer 0 T0PC = 0; T0PR = T0_MS_DIV; T0CCR = 0; // timer mode. T0EMR = 0; // no external match. T0MR0 = 9; T0MCR = 3; // reset and interrupt on terminal count. T0TCR = TCR_ENABLE; // init the system ticks. systemTickCounter = 0;}void setElapsed (int HowLong, struct TIMER * timer, timertype TimerType){ /* set for a TimeElapsed event. */ timer->HowLong = HowLong; timer->Type = TimerType; if (timer->Type == TimerInMilli) timer->SetTime = times (0); else timer->SetTime = time (0);}bool timeElapsed (struct TIMER * timer){ /* return True if preset interval has expired. */time_t TestTimerNow, TestTimeStart; if (timer->Type == TimerInMilli) TestTimerNow = times (0); else TestTimerNow = time (0); TestTimeStart = timer->SetTime; if (((TestTimeStart>TestTimerNow) ? (TestTimerNow + (~TestTimeStart)) : (TestTimerNow - TestTimeStart)) > timer->HowLong) { return True; } return False;}void initRTC(void){#ifdef HAS_CLOCKbool nonsense = False;rtcCTIME0_t ctime0;rtcCTIME1_t ctime1;rtcCTIME2_t ctime2;struct tm newTime;time_t resetTime; awakenClock(); // see if clock contains nonsense values. // grab time as consolidated to avoid misses. ctime0 = RTCCTIME0; ctime1 = RTCCTIME1; ctime2 = RTCCTIME2; // leisurely tear the packed time apart into individual time. if ((ctime0.seconds < 0) || (ctime0.seconds > 59)) nonsense = True; if ((ctime0.minutes < 0) || (ctime0.minutes > 59)) nonsense = True; if ((ctime0.hours < 0) || (ctime0.hours > 23)) nonsense = True; if ((ctime1.dayOfMonth < 1) || (ctime1.dayOfMonth > 31)) nonsense = True; if ((ctime1.month < 1) || (ctime1.month > 12)) nonsense = True; if ((ctime1.year < 1980) || (ctime1.year > 2050)) nonsense = True; if ((ctime0.dayOfWeek < 0) || (ctime0.dayOfWeek > 6)) nonsense = True; if ((ctime2.dayOfYear < 0) || (ctime2.dayOfYear > 366)) nonsense = True; sleepClock (); if (nonsense) { // set the clock to Jan 1, 2006 00:00:00 resetTime = 1136073600l; localtime_r (&resetTime, &newTime); newTime.tm_year += 1900; newTime.tm_mon += 1; newTime.tm_yday += 1; writeRTC (&newTime); }#endif // start realtime heartbeat (1ms period). startMsTimer();}/************************************************** * * readRTC & writeRTC are supporting functions * for the newlib stubs. ***************************************************/void readRTC (struct tm *theTime){// read clock registers and return tm structure.#ifdef HAS_CLOCKrtcCTIME0_t ctime0;rtcCTIME1_t ctime1;rtcCTIME2_t ctime2; awakenClock(); // grab time as consolidated to avoid misses. ctime0 = RTCCTIME0; ctime1 = RTCCTIME1; ctime2 = RTCCTIME2; // leisurely tear the packed time apart into individual time. theTime->tm_sec = ctime0.seconds; theTime->tm_min = ctime0.minutes; theTime->tm_hour = ctime0.hours; theTime->tm_mday = ctime1.dayOfMonth; theTime->tm_mon = ctime1.month; theTime->tm_year = ctime1.year; theTime->tm_wday = ctime0.dayOfWeek; theTime->tm_yday = ctime2.dayOfYear; theTime->tm_isdst = False; sleepClock ();#endif}void writeRTC (struct tm *newTime){// set clock to new values.#ifdef HAS_CLOCK awakenClock(); // grab time as consolidated to avoid misses. RTCTCR->seconds = newTime->tm_sec; RTCTCR->minutes = newTime->tm_min; RTCTCR->hours = newTime->tm_hour; RTCTCR->dayOfMonth = newTime->tm_mday; RTCTCR->month = newTime->tm_mon; RTCTCR->year = newTime->tm_year; RTCTCR->dayOfWeek = newTime->tm_wday; RTCTCR->dayOfYear = newTime->tm_yday; sleepClock ();#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -