cpu: lpc2387: further rtc cleanup

dev/timer
Oleg Hahm 9 years ago
parent 587920cc49
commit 849580a9c9

@ -28,10 +28,8 @@ static rtc_alarm_cb_t _cb;
/* Argument to alarm callback */
static void *_cb_arg;
/**
* @brief epoch time in hour granularity
*/
static volatile time_t epoch;
/* internal function to set time based on time_t */
static void _rtc_set(time_t time);
void RTC_IRQHandler(void) __attribute__((interrupt("IRQ")));
@ -39,7 +37,7 @@ void rtc_init(void)
{
PCONP |= BIT9;
RTC_AMR = 0xff; /* disable alarm irq */
RTC_CIIR = IMHOUR; /* enable increase irq */
RTC_CIIR = 0; /* disable increase irq */
RTC_CISS = 0; /* disable subsecond irq */
INTWAKE |= BIT15; /* rtc irq wakes up mcu from power down */
@ -49,12 +47,11 @@ void rtc_init(void)
/* initialize clock with valid unix compatible values
* If RTC_YEAR contains an value larger unix time_t we must reset. */
if (RTC_YEAR > 2037) {
rtc_reset();
_rtc_set(0);
}
DEBUG("%2lu.%2lu.%4lu %2lu:%2lu:%2lu epoch %lu\n",
RTC_DOM, RTC_MONTH, RTC_YEAR, RTC_HOUR, RTC_MIN, RTC_SEC,
epoch);
DEBUG("%2lu.%2lu.%4lu %2lu:%2lu:%2lu\n",
RTC_DOM, RTC_MONTH, RTC_YEAR, RTC_HOUR, RTC_MIN, RTC_SEC);
}
/**
@ -154,9 +151,6 @@ void rtc_poweron(void)
RTC_ILR = (ILR_RTSSF | ILR_RTCCIF | ILR_RTCALF); /* clear interrupt flags */
RTC_CCR |= CCR_CLKEN; /* enable clock */
install_irq(RTC_INT, &RTC_IRQHandler, IRQP_RTC); /* install interrupt handler */
time_t now = rtc_time(NULL);
epoch = now - (now % 3600);
}
void rtc_poweroff(void)
@ -173,13 +167,9 @@ void RTC_IRQHandler(void)
if (RTC_ILR & ILR_RTSSF) {
/* sub second interrupt (does not need flag-clearing) */
}
else if (RTC_ILR & ILR_RTCCIF) {
/* counter increase interrupt */
RTC_ILR |= ILR_RTCCIF;
epoch += 60 * 60; /* add 1 hour */
}
else if (RTC_ILR & ILR_RTCALF) {
RTC_ILR |= ILR_RTCALF;
@ -194,47 +184,9 @@ void RTC_IRQHandler(void)
VICVectAddr = 0; /* Acknowledge Interrupt */
}
time_t rtc_time(struct timeval *time)
{
uint32_t sec;
uint32_t usec;
uint32_t min;
usec = (RTC_CTC >> 1);
sec = RTC_SEC;
min = RTC_MIN;
while (usec != (RTC_CTC >> 1)) {
usec = (RTC_CTC >> 1);
sec = RTC_SEC;
min = RTC_MIN;
}
sec += min * 60; /* add number of minutes */
sec += epoch; /* add precalculated epoch in hour granularity */
if (time != NULL) {
usec = usec * 15625;
usec >>= 9;
time->tv_sec = sec;
time->tv_usec = usec;
}
return sec;
}
void rtc_set(time_t time)
static void _rtc_set(time_t time)
{
struct tm *localt;
localt = localtime(&time); /* convert seconds to broken-down time */
rtc_set_time(localt);
epoch = time - localt->tm_sec - localt->tm_min * 60;
}
/* set clock to start of unix epoch */
void rtc_reset(void)
{
rtc_set(0);
epoch = 0;
}

Loading…
Cancel
Save