2014-08-27 18:47:31 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008-2010, Freie Universitaet Berlin (FUB). All rights reserved.
|
2014-11-20 18:33:23 +01:00
|
|
|
* Copyright 2014 INRIA
|
2014-08-27 18:47:31 +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
|
|
|
|
2017-06-22 15:43:17 +02:00
|
|
|
/**
|
2020-07-16 16:25:45 +02:00
|
|
|
* @ingroup cpu_lpc23xx
|
2017-06-22 15:43:17 +02:00
|
|
|
* @ingroup drivers_periph_rtc
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Peripheral UART driver implementation
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
/* cpu */
|
2014-11-20 17:55:28 +01:00
|
|
|
#include "periph/rtc.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
#include "VIC.h"
|
2020-07-16 16:24:36 +02:00
|
|
|
#include "lpc23xx.h"
|
2010-09-22 15:10:42 +02:00
|
|
|
|
2020-10-22 11:34:00 +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-11-20 17:55:28 +01:00
|
|
|
/* Alarm callback */
|
|
|
|
static rtc_alarm_cb_t _cb;
|
|
|
|
|
|
|
|
/* Argument to alarm callback */
|
|
|
|
static void *_cb_arg;
|
|
|
|
|
2014-11-20 18:33:23 +01:00
|
|
|
void RTC_IRQHandler(void) __attribute__((interrupt("IRQ")));
|
|
|
|
|
|
|
|
void rtc_init(void)
|
|
|
|
{
|
2020-08-07 19:22:26 +02:00
|
|
|
PCONP |= BIT9;
|
|
|
|
RTC_AMR = 0xff; /* disable alarm irq */
|
|
|
|
RTC_CIIR = 0; /* disable increase irq */
|
|
|
|
RTC_CISS = 0; /* disable subsecond irq */
|
2014-11-20 18:33:23 +01:00
|
|
|
|
2020-08-07 19:22:26 +02:00
|
|
|
INTWAKE |= BIT15; /* rtc irq wakes up mcu from power down */
|
2014-11-20 18:33:23 +01:00
|
|
|
|
2020-08-07 19:22:26 +02:00
|
|
|
RTC_CCR = CCR_CLKSRC; /* Clock from external 32 kHz Osc. */
|
2014-11-20 18:33:23 +01:00
|
|
|
|
2019-12-05 22:30:35 +01:00
|
|
|
/* Initialize clock to a a sane and predictable default
|
|
|
|
* after cold boot or external reset.
|
|
|
|
*/
|
|
|
|
if ((RSIR == RSIR_POR) || (RSIR == (RSIR_POR | RSIR_EXTR))) {
|
2020-08-07 13:24:58 +02:00
|
|
|
struct tm localt = { .tm_year = RIOT_EPOCH - 1900 };
|
2019-12-04 03:10:16 +01:00
|
|
|
rtc_set_time(&localt);
|
2014-11-20 18:33:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 01:18:24 +02:00
|
|
|
rtc_poweron();
|
|
|
|
|
2014-11-21 22:13:51 +01:00
|
|
|
DEBUG("%2lu.%2lu.%4lu %2lu:%2lu:%2lu\n",
|
2020-08-07 19:22:26 +02:00
|
|
|
RTC_DOM, RTC_MONTH, RTC_YEAR, RTC_HOUR, RTC_MIN, RTC_SEC);
|
2014-11-20 18:33:23 +01:00
|
|
|
}
|
|
|
|
|
2010-09-22 15:10:42 +02:00
|
|
|
/**
|
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
|
|
|
*/
|
2014-11-20 17:55:28 +01:00
|
|
|
int rtc_set_time(struct tm *localt)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt == NULL) {
|
2014-11-20 17:55:28 +01:00
|
|
|
return -1;
|
2013-06-21 03:52:57 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:22:50 +02:00
|
|
|
/* normalize input */
|
|
|
|
rtc_tm_normalize(localt);
|
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
/* set clock */
|
2020-08-07 19:22:26 +02:00
|
|
|
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;
|
2013-06-21 03:52:57 +02:00
|
|
|
RTC_MONTH = localt->tm_mon + 1;
|
2020-08-07 19:22:26 +02:00
|
|
|
RTC_YEAR = localt->tm_year;
|
2014-11-20 17:55:28 +01:00
|
|
|
|
|
|
|
return 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
2014-11-20 18:33:23 +01:00
|
|
|
int rtc_get_time(struct tm *localt)
|
2013-06-21 03:52:57 +02:00
|
|
|
{
|
2020-08-07 19:22:26 +02:00
|
|
|
if (localt == NULL) {
|
|
|
|
return -1;
|
2014-11-20 18:33:23 +01:00
|
|
|
}
|
2020-08-07 19:22:26 +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 */
|
|
|
|
|
|
|
|
return 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
int rtc_set_alarm(struct tm *localt, rtc_alarm_cb_t cb, void *arg)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt != NULL) {
|
2020-08-07 19:22:26 +02:00
|
|
|
|
2019-04-17 18:22:50 +02:00
|
|
|
/* normalize input */
|
|
|
|
rtc_tm_normalize(localt);
|
|
|
|
|
2020-08-07 19:22:26 +02:00
|
|
|
RTC_ALSEC = localt->tm_sec;
|
|
|
|
RTC_ALMIN = localt->tm_min;
|
2013-06-21 03:52:57 +02:00
|
|
|
RTC_ALHOUR = localt->tm_hour;
|
2020-08-07 19:22:26 +02:00
|
|
|
RTC_ALDOM = localt->tm_mday;
|
|
|
|
RTC_ALDOW = localt->tm_wday;
|
|
|
|
RTC_ALDOY = localt->tm_yday;
|
|
|
|
RTC_ALMON = localt->tm_mon + 1;
|
2013-06-21 03:52:57 +02:00
|
|
|
RTC_ALYEAR = localt->tm_year;
|
2020-08-07 19:22:26 +02:00
|
|
|
RTC_AMR = 0; /* set which 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);
|
2020-08-07 19:22:26 +02:00
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
2020-08-07 19:22:26 +02:00
|
|
|
if (cb) {
|
2014-11-20 17:55:28 +01:00
|
|
|
_cb = cb;
|
2020-01-29 19:47:16 +01:00
|
|
|
_cb_arg = arg;
|
2013-06-21 03:52:57 +02:00
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
2020-08-07 19:22:26 +02:00
|
|
|
return 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
|
|
|
int rtc_get_alarm(struct tm *localt)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2020-08-07 19:22:26 +02:00
|
|
|
if (localt == NULL) {
|
|
|
|
return -1;
|
2013-06-21 03:52:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-07 19:22:26 +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 */
|
|
|
|
|
|
|
|
return 0;
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
|
|
|
void rtc_clear_alarm(void)
|
|
|
|
{
|
|
|
|
RTC_AMR = 0xff;
|
|
|
|
}
|
|
|
|
|
2014-11-20 18:33:23 +01:00
|
|
|
void rtc_poweron(void)
|
|
|
|
{
|
2020-08-07 19:22:26 +02:00
|
|
|
PCONP |= BIT9;
|
|
|
|
RTC_ILR = (ILR_RTSSF | ILR_RTCCIF | ILR_RTCALF); /* clear interrupt flags */
|
2014-11-20 18:33:23 +01:00
|
|
|
RTC_CCR |= CCR_CLKEN; /* enable clock */
|
|
|
|
install_irq(RTC_INT, &RTC_IRQHandler, IRQP_RTC); /* install interrupt handler */
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_poweroff(void)
|
|
|
|
{
|
|
|
|
RTC_CCR &= ~CCR_CLKEN; /* disable clock */
|
|
|
|
install_irq(RTC_INT, NULL, 0);
|
|
|
|
PCONP &= ~BIT9;
|
|
|
|
}
|
2014-11-20 17:55:28 +01:00
|
|
|
|
2013-06-21 03:52:57 +02:00
|
|
|
void RTC_IRQHandler(void)
|
2010-09-22 15:10:42 +02:00
|
|
|
{
|
2020-08-07 19:22:26 +02:00
|
|
|
if (RTC_ILR & ILR_RTCALF) {
|
|
|
|
RTC_ILR = ILR_RTCALF;
|
2014-07-31 20:46:28 +02:00
|
|
|
RTC_AMR = 0xff; /* disable alarm irq */
|
2014-11-20 17:55:28 +01:00
|
|
|
if (_cb) {
|
|
|
|
_cb(_cb_arg);
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
DEBUG("Ring\n");
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:46:28 +02:00
|
|
|
VICVectAddr = 0; /* Acknowledge Interrupt */
|
2010-09-22 15:10:42 +02:00
|
|
|
}
|