2010-09-22 15:10:42 +02:00
|
|
|
/******************************************************************************
|
|
|
|
Copyright 2008-2010, Freie Universitaet Berlin (FUB). All rights reserved.
|
|
|
|
|
|
|
|
These sources were developed at the Freie Universitaet Berlin, Computer Systems
|
|
|
|
and Telematics group (http://cst.mi.fu-berlin.de).
|
|
|
|
-------------------------------------------------------------------------------
|
2014-07-31 19:53:53 +02:00
|
|
|
This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
directory for more details.
|
2010-09-22 15:10:42 +02:00
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
2014-07-31 20:46:28 +02:00
|
|
|
* @ingroup lpc2387_rtc
|
|
|
|
* @brief LPC2387 Real-Time-Clock
|
2010-09-22 15:10:42 +02:00
|
|
|
*
|
2014-07-31 20:46:28 +02:00
|
|
|
* @author Michael Baar <michael.baar@fu-berlin.de>
|
2010-09-22 15:10:42 +02:00
|
|
|
* @version $Revision: 2005 $
|
|
|
|
*
|
2014-07-31 20:46:28 +02:00
|
|
|
* @note $Id: lpc2387-rtc.c 2005 2010-03-17 10:52:03Z baar $
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-07-06 22:57:56 +02:00
|
|
|
#include "kernel_types.h"
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
/* cpu */
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "VIC.h"
|
|
|
|
#include "lpc2387.h"
|
|
|
|
#include "lpc2387-rtc.h"
|
|
|
|
#include "lpm.h"
|
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
#define PREINT_RTC 0x000001C8 /* Prescaler value, integer portion, PCLK = 15Mhz */
|
|
|
|
#define PREFRAC_RTC 0x000061C0 /* Prescaler value, fraction portion, PCLK = 15Mhz */
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-07-24 00:36:06 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "debug.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
|
|
|
/**
|
2014-07-31 20:46:28 +02:00
|
|
|
* @brief epoch time in hour granularity
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
|
|
|
static volatile time_t epoch;
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2014-07-31 20:46:28 +02:00
|
|
|
* @brief Sets the current time in broken down format directly from to RTC
|
|
|
|
* @param[in] localt Pointer to structure with time to set
|
2010-09-22 15:10:42 +02:00
|
|
|
*/
|
2010-11-04 18:16:39 +01:00
|
|
|
void
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_set_localtime(struct tm *localt)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt == NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set clock */
|
|
|
|
RTC_SEC = localt->tm_sec;
|
|
|
|
RTC_MIN = localt->tm_min;
|
|
|
|
RTC_HOUR = localt->tm_hour;
|
|
|
|
RTC_DOM = localt->tm_mday;
|
|
|
|
RTC_DOW = localt->tm_wday;
|
|
|
|
RTC_DOY = localt->tm_yday;
|
|
|
|
RTC_MONTH = localt->tm_mon + 1;
|
|
|
|
RTC_YEAR = localt->tm_year;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_set(time_t time)
|
|
|
|
{
|
|
|
|
struct tm *localt;
|
2014-07-31 20:46:28 +02:00
|
|
|
localt = localtime(&time); /* convert seconds to broken-down time */
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_set_localtime(localt);
|
|
|
|
epoch = time - localt->tm_sec - localt->tm_min * 60;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
//* set clock to start of unix epoch */
|
2010-11-04 18:16:39 +01:00
|
|
|
void rtc_reset(void)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
|
|
|
rtc_set(0);
|
2013-06-21 03:52:57 +02:00
|
|
|
epoch = 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_set_alarm(struct tm *localt, enum rtc_alarm_mask mask)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt != NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
RTC_ALSEC = localt->tm_sec;
|
|
|
|
RTC_ALMIN = localt->tm_min;
|
|
|
|
RTC_ALHOUR = localt->tm_hour;
|
|
|
|
RTC_ALDOM = localt->tm_mday;
|
|
|
|
RTC_ALDOW = localt->tm_wday;
|
|
|
|
RTC_ALDOY = localt->tm_yday;
|
|
|
|
RTC_ALMON = localt->tm_mon + 1;
|
|
|
|
RTC_ALYEAR = localt->tm_year;
|
2014-07-31 20:46:28 +02:00
|
|
|
RTC_AMR = ~mask; /* set wich alarm fields to check */
|
2013-06-21 03:52:57 +02:00
|
|
|
DEBUG("alarm set %2lu.%2lu.%4lu %2lu:%2lu:%2lu\n",
|
|
|
|
RTC_ALDOM, RTC_ALMON, RTC_ALYEAR, RTC_ALHOUR, RTC_ALMIN, RTC_ALSEC);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RTC_AMR = 0xff;
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
enum rtc_alarm_mask
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_get_alarm(struct tm *localt)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt != NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
localt->tm_sec = RTC_ALSEC;
|
|
|
|
localt->tm_min = RTC_ALMIN;
|
|
|
|
localt->tm_hour = RTC_ALHOUR;
|
|
|
|
localt->tm_mday = RTC_ALDOM;
|
|
|
|
localt->tm_wday = RTC_ALDOW;
|
|
|
|
localt->tm_yday = RTC_ALDOY;
|
|
|
|
localt->tm_mon = RTC_ALMON - 1;
|
|
|
|
localt->tm_year = RTC_ALYEAR;
|
|
|
|
localt->tm_isdst = -1; /* not available */
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
return (~RTC_AMR) & 0xff; /* return which alarm fields are checked */
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void RTC_IRQHandler(void) __attribute__((interrupt("IRQ")));
|
|
|
|
void RTC_IRQHandler(void)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-21 03:52:57 +02:00
|
|
|
lpm_begin_awake();
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (RTC_ILR & ILR_RTSSF) {
|
2013-06-21 03:52:57 +02:00
|
|
|
/* sub second interrupt (does not need flag-clearing) */
|
|
|
|
|
|
|
|
}
|
2013-06-24 22:37:35 +02:00
|
|
|
else if (RTC_ILR & ILR_RTCCIF) {
|
2013-06-21 03:52:57 +02:00
|
|
|
/* counter increase interrupt */
|
|
|
|
RTC_ILR |= ILR_RTCCIF;
|
2014-07-31 20:46:28 +02:00
|
|
|
epoch += 60 * 60; /* add 1 hour */
|
2013-06-21 03:52:57 +02:00
|
|
|
|
|
|
|
}
|
2013-06-24 22:37:35 +02:00
|
|
|
else if (RTC_ILR & ILR_RTCALF) {
|
2013-06-21 03:52:57 +02:00
|
|
|
RTC_ILR |= ILR_RTCALF;
|
2014-07-31 20:46:28 +02:00
|
|
|
RTC_AMR = 0xff; /* disable alarm irq */
|
2013-06-21 03:52:57 +02:00
|
|
|
DEBUG("Ring\n");
|
|
|
|
lpm_end_awake();
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
VICVectAddr = 0; /* Acknowledge Interrupt */
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void rtc_enable(void)
|
|
|
|
{
|
2014-07-31 20:46:28 +02:00
|
|
|
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 */
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
time_t now = rtc_time(NULL);
|
|
|
|
epoch = now - (now % 3600);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-11-04 18:16:39 +01:00
|
|
|
void rtc_init(void)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-21 03:52:57 +02:00
|
|
|
PCONP |= BIT9;
|
2014-07-31 20:46:28 +02:00
|
|
|
RTC_AMR = 0xff; /* disable alarm irq */
|
|
|
|
RTC_CIIR = IMHOUR; /* enable increase irq */
|
|
|
|
RTC_CISS = 0; /* disable subsecond irq */
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
INTWAKE |= BIT15; /* rtc irq wakes up mcu from power down */
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
RTC_CCR = CCR_CLKSRC; /* Clock from external 32 kHz Osc. */
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
/* initialize clock with valid unix compatible values
|
|
|
|
* If RTC_YEAR contains an value larger unix time_t we must reset. */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (RTC_YEAR > 2037) {
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_reset();
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
DEBUG("%2lu.%2lu.%4lu %2lu:%2lu:%2lu epoch %lu\n",
|
|
|
|
RTC_DOM, RTC_MONTH, RTC_YEAR, RTC_HOUR, RTC_MIN, RTC_SEC,
|
|
|
|
epoch);
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
time_t rtc_time(struct timeval *time)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-21 03:52:57 +02:00
|
|
|
uint32_t sec;
|
|
|
|
uint32_t usec;
|
|
|
|
uint32_t min;
|
|
|
|
|
|
|
|
usec = (RTC_CTC >> 1);
|
|
|
|
sec = RTC_SEC;
|
|
|
|
min = RTC_MIN;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (usec != (RTC_CTC >> 1)) {
|
2013-06-21 03:52:57 +02:00
|
|
|
usec = (RTC_CTC >> 1);
|
|
|
|
sec = RTC_SEC;
|
|
|
|
min = RTC_MIN;
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
sec += min * 60; /* add number of minutes */
|
|
|
|
sec += epoch; /* add precalculated epoch in hour granularity */
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (time != NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
usec = usec * 15625;
|
|
|
|
usec >>= 9;
|
|
|
|
time->tv_sec = sec;
|
|
|
|
time->tv_usec = usec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sec;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void rtc_disable(void)
|
|
|
|
{
|
2014-07-31 20:46:28 +02:00
|
|
|
RTC_CCR &= ~CCR_CLKEN; /* disable clock */
|
2013-06-21 03:52:57 +02:00
|
|
|
install_irq(RTC_INT, NULL, 0);
|
|
|
|
RTC_ILR = 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_get_localtime(struct tm *localt)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt != NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
localt->tm_sec = RTC_SEC;
|
|
|
|
localt->tm_min = RTC_MIN;
|
|
|
|
localt->tm_hour = RTC_HOUR;
|
|
|
|
localt->tm_mday = RTC_DOM;
|
|
|
|
localt->tm_wday = RTC_DOW;
|
|
|
|
localt->tm_yday = RTC_DOY;
|
|
|
|
localt->tm_mon = RTC_MONTH - 1;
|
|
|
|
localt->tm_year = RTC_YEAR;
|
|
|
|
localt->tm_isdst = -1; /* not available */
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-11-04 18:16:39 +01:00
|
|
|
void gettimeofday_r(struct _reent *r, struct timeval *ptimeval, struct timezone *ptimezone)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2014-04-30 20:34:14 +02:00
|
|
|
(void) ptimezone; /* unused */
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
r->_errno = 0;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (ptimeval != NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
rtc_time(ptimeval);
|
|
|
|
}
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|