2010-12-13 01:29:46 +01:00
|
|
|
/******************************************************************************
|
2013-06-18 17:21:38 +02:00
|
|
|
Copyright 2010, Freie Universitaet Berlin (FUB).
|
|
|
|
Copyright 2013, INRIA.
|
2010-12-13 01:29:46 +01:00
|
|
|
|
|
|
|
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-12-13 01:29:46 +01:00
|
|
|
*******************************************************************************/
|
|
|
|
|
2013-03-13 21:56:56 +01:00
|
|
|
/**
|
|
|
|
* @ingroup rtc
|
2013-06-18 17:21:38 +02:00
|
|
|
* @file cc430-rtc.c
|
2014-07-31 20:46:28 +02:00
|
|
|
* @brief CC430 real time clock implementation
|
|
|
|
* @author Oliver Hahm <oliver.hahm@inria.fr>
|
2013-03-13 21:56:56 +01:00
|
|
|
*/
|
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
#include <string.h>
|
2013-03-30 21:44:16 +01:00
|
|
|
#include <legacymsp430.h>
|
2013-12-16 17:54:58 +01:00
|
|
|
#include "irq.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "cc430-rtc.h"
|
2010-12-13 01:29:46 +01:00
|
|
|
|
|
|
|
//static volatile time_t epoch;
|
|
|
|
static struct tm time_to_set;
|
2010-12-16 18:21:24 +01:00
|
|
|
static int set_time = 0;
|
2014-08-06 09:44:31 +02:00
|
|
|
kernel_pid_t rtc_second_pid = KERNEL_PID_UNDEF;
|
2010-12-13 01:29:46 +01:00
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_init(void)
|
|
|
|
{
|
2010-12-13 01:29:46 +01:00
|
|
|
/* Set to calendar mode */
|
|
|
|
RTCCTL1 |= RTCMODE_H;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_enable(void)
|
|
|
|
{
|
2010-12-13 01:29:46 +01:00
|
|
|
/* Set RTC operational */
|
|
|
|
RTCCTL1 &= ~RTCHOLD_H;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_disable(void)
|
|
|
|
{
|
2010-12-13 01:29:46 +01:00
|
|
|
/* Stop RTC */
|
|
|
|
RTCCTL1 |= RTCHOLD_H;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_set_localtime(struct tm *localt)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt == NULL) {
|
2013-06-21 03:52:57 +02:00
|
|
|
return;
|
2010-12-13 01:29:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy time to be set */
|
|
|
|
memcpy(&time_to_set, localt, sizeof(struct tm));
|
2010-12-16 18:21:24 +01:00
|
|
|
set_time = 1;
|
2010-12-13 01:29:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
void rtc_set(time_t time) {
|
2014-07-31 20:46:28 +02:00
|
|
|
struct tm* localt;
|
|
|
|
localt = localtime(&time); // convert seconds to broken-down time
|
|
|
|
rtc_set_localtime(localt);
|
|
|
|
epoch = time - localt->tm_sec - localt->tm_min * 60;
|
2010-12-13 01:29:46 +01:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
time_t rtc_time(void) {
|
|
|
|
time_t sec;
|
|
|
|
struct tm t;
|
|
|
|
rtc_get_localtime(&t);
|
|
|
|
sec = mktime(&t);
|
2014-07-31 20:46:28 +02:00
|
|
|
return sec;
|
2010-12-13 01:29:46 +01:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_get_localtime(struct tm *localt)
|
|
|
|
{
|
2010-12-13 01:29:46 +01:00
|
|
|
uint8_t success = 0;
|
|
|
|
uint8_t i;
|
|
|
|
uint16_t tmpyear;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (localt == NULL) {
|
2010-12-13 01:29:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (!success) {
|
|
|
|
for (i = 0; i < 8; i++) {
|
2010-12-13 01:29:46 +01:00
|
|
|
/* try again when RTC is in transition */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (!(RTCCTL1 & RTCRDY_H)) {
|
2010-12-13 01:29:46 +01:00
|
|
|
break;
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
|
|
|
switch(i) {
|
2010-12-13 01:29:46 +01:00
|
|
|
case 0:
|
|
|
|
localt->tm_sec = RTCSEC;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
case 1:
|
|
|
|
localt->tm_min = RTCMIN;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
case 2:
|
|
|
|
localt->tm_hour = RTCHOUR;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
case 3:
|
|
|
|
localt->tm_mday = RTCDAY;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
case 4:
|
|
|
|
localt->tm_wday = RTCDOW;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
case 5:
|
|
|
|
localt->tm_mon = RTCMON - 1;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
case 6:
|
|
|
|
tmpyear = RTCYEARL;
|
|
|
|
tmpyear |= (RTCYEARH << 0x08);
|
|
|
|
localt->tm_year = tmpyear - 1900;
|
|
|
|
break;
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
default:
|
|
|
|
success = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_set_alarm(struct tm *localt, rtc_alarm_mask_t mask)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mask & RTC_ALARM_MIN) {
|
2010-12-13 01:29:46 +01:00
|
|
|
RTCAMIN = localt->tm_min;
|
|
|
|
RTCAMIN |= BIT7;
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mask & RTC_ALARM_HOUR) {
|
2010-12-13 01:29:46 +01:00
|
|
|
RTCAHOUR = localt->tm_hour;
|
|
|
|
RTCAHOUR |= BIT7;
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mask & RTC_ALARM_DOW) {
|
2010-12-13 01:29:46 +01:00
|
|
|
RTCADOW = localt->tm_wday;
|
|
|
|
RTCADOW |= BIT7;
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (mask & RTC_ALARM_DOM) {
|
2010-12-13 01:29:46 +01:00
|
|
|
RTCADAY = localt->tm_mday;
|
|
|
|
RTCADAY |= BIT7;
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
RTCCTL0 |= RTCAIE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
void rtc_remove_alarm(void)
|
|
|
|
{
|
2010-12-13 01:29:46 +01:00
|
|
|
/* reset all AE bits */
|
|
|
|
RTCAHOUR &= ~BIT7;
|
|
|
|
RTCAMIN &= ~BIT7;
|
|
|
|
RTCADAY &= ~BIT7;
|
|
|
|
RTCADOW &= ~BIT7;
|
|
|
|
|
|
|
|
/* reset alarm interrupt enable */
|
|
|
|
RTCCTL0 &= ~RTCAIE;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-06-21 03:52:57 +02:00
|
|
|
interrupt(RTC_VECTOR) __attribute__((naked)) rtc_isr(void)
|
|
|
|
{
|
2010-12-13 01:29:46 +01:00
|
|
|
__enter_isr();
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
/* RTC is save to write for up to one second now */
|
2013-06-24 22:37:35 +02:00
|
|
|
if (RTCIV == RTC_RTCRDYIFG) {
|
2010-12-13 01:29:46 +01:00
|
|
|
/* disable interrupt */
|
2010-12-16 18:21:24 +01:00
|
|
|
//RTCCTL0 &= ~RTCRDYIE;
|
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (set_time) {
|
2010-12-16 18:21:24 +01:00
|
|
|
set_time = 0;
|
2013-06-21 03:52:57 +02:00
|
|
|
/* set previous set time and reset it */
|
2010-12-16 18:21:24 +01:00
|
|
|
RTCSEC = time_to_set.tm_sec;
|
|
|
|
RTCMIN = time_to_set.tm_min;
|
|
|
|
RTCHOUR = time_to_set.tm_hour;
|
|
|
|
RTCDAY = time_to_set.tm_mday;
|
|
|
|
RTCDOW = time_to_set.tm_wday;
|
|
|
|
RTCMON = time_to_set.tm_mon + 1;
|
|
|
|
RTCYEARL = (time_to_set.tm_year + 1900) & 0xFF;
|
|
|
|
RTCYEARH = (time_to_set.tm_year + 1900) >> 0x08;
|
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2014-08-06 09:44:31 +02:00
|
|
|
if (rtc_second_pid != KERNEL_PID_UNDEF) {
|
2014-04-11 17:20:23 +02:00
|
|
|
static msg_t m;
|
2010-12-16 18:21:24 +01:00
|
|
|
m.type = RTC_SECOND;
|
|
|
|
msg_send_int(&m, rtc_second_pid);
|
|
|
|
}
|
2010-12-13 01:29:46 +01:00
|
|
|
}
|
|
|
|
/* RTC alarm */
|
2013-06-24 22:37:35 +02:00
|
|
|
else if (RTCIV == RTC_RTCAIFG) {
|
2010-12-13 01:29:46 +01:00
|
|
|
}
|
2013-06-21 03:52:57 +02:00
|
|
|
|
2010-12-13 01:29:46 +01:00
|
|
|
__exit_isr();
|
|
|
|
}
|