From 498edb1854c7c10132da8d152bc859a6ed6e5bd0 Mon Sep 17 00:00:00 2001 From: Oleg Hahm Date: Thu, 20 Nov 2014 17:55:28 +0100 Subject: [PATCH] cc430: lpc2387: switch to new periph/rtc interface Removes the old interface. --- boards/avsextrem/include/periph_conf.h | 5 ++ boards/chronos/include/periph_conf.h | 3 + boards/msba2/include/periph_conf.h | 5 ++ cpu/arm7_common/gettimeofday.c | 2 +- cpu/arm7_common/syscalls.c | 2 +- cpu/cc430/cc430-rtc.c | 90 +++++++++++++++----------- cpu/cc430/include/cc430-rtc.h | 14 +--- cpu/lpc2387/include/lpc2387-rtc.h | 3 +- cpu/lpc2387/rtc/lpc2387-rtc.c | 84 +++++++++++++++--------- drivers/include/rtc.h | 69 -------------------- examples/ccn-lite-relay/main.c | 2 +- sys/auto_init/auto_init.c | 3 +- 12 files changed, 125 insertions(+), 157 deletions(-) delete mode 100644 drivers/include/rtc.h diff --git a/boards/avsextrem/include/periph_conf.h b/boards/avsextrem/include/periph_conf.h index b2dabf6c26..5a721fd413 100644 --- a/boards/avsextrem/include/periph_conf.h +++ b/boards/avsextrem/include/periph_conf.h @@ -45,6 +45,11 @@ extern "C" { #define PWM_0_CH2_PIN (4) #define PWM_0_FUNC (1) +/** + * @brief Real Time Clock configuration + */ +#define RTC_NUMOF (1) + #ifdef __cplusplus } #endif diff --git a/boards/chronos/include/periph_conf.h b/boards/chronos/include/periph_conf.h index 995b093f84..7f825ab6fc 100644 --- a/boards/chronos/include/periph_conf.h +++ b/boards/chronos/include/periph_conf.h @@ -23,6 +23,9 @@ extern "C" { #endif +/** + * @brief Real Time Clock configuration + */ #define RTC_NUMOF (1) #ifdef __cplusplus diff --git a/boards/msba2/include/periph_conf.h b/boards/msba2/include/periph_conf.h index 3766bda25e..a9a7bdf3be 100644 --- a/boards/msba2/include/periph_conf.h +++ b/boards/msba2/include/periph_conf.h @@ -46,6 +46,11 @@ extern "C" { #define PWM_0_CH2_PIN (4) #define PWM_0_FUNC (1) +/** + * @brief Real Time Clock configuration + */ +#define RTC_NUMOF (1) + #ifdef __cplusplus } #endif diff --git a/cpu/arm7_common/gettimeofday.c b/cpu/arm7_common/gettimeofday.c index a0ab89df05..7ab966a401 100644 --- a/cpu/arm7_common/gettimeofday.c +++ b/cpu/arm7_common/gettimeofday.c @@ -19,7 +19,7 @@ #include "kernel_types.h" #if defined MODULE_RTC -# include "rtc.h" +# include "periph/rtc.h" #elif defined MODULE_VTIMER # include "vtimer.h" #endif diff --git a/cpu/arm7_common/syscalls.c b/cpu/arm7_common/syscalls.c index 6d5ca835cd..383975754a 100644 --- a/cpu/arm7_common/syscalls.c +++ b/cpu/arm7_common/syscalls.c @@ -31,7 +31,7 @@ #include "kernel.h" #include "irq.h" #if defined MODULE_RTC -#include "rtc.h" +#include "periph/rtc.h" #elif defined MODULE_VTIMER #include "vtimer.h" #endif diff --git a/cpu/cc430/cc430-rtc.c b/cpu/cc430/cc430-rtc.c index ec98840603..3a235de696 100644 --- a/cpu/cc430/cc430-rtc.c +++ b/cpu/cc430/cc430-rtc.c @@ -20,51 +20,45 @@ #include "cpu.h" #include "cc430-rtc.h" -//static volatile time_t epoch; +/* Alarm callback */ +static rtc_alarm_cb_t _cb; + +/* Argument to alarm callback */ +static void *_cb_arg; + static struct tm time_to_set; static int set_time = 0; kernel_pid_t rtc_second_pid = KERNEL_PID_UNDEF; -/*---------------------------------------------------------------------------*/ void rtc_init(void) { /* Set to calendar mode */ RTCCTL1 |= RTCMODE_H; } -/*---------------------------------------------------------------------------*/ -void rtc_enable(void) +void rtc_poweron(void) { /* Set RTC operational */ RTCCTL1 &= ~RTCHOLD_H; } -/*---------------------------------------------------------------------------*/ -void rtc_disable(void) +void rtc_poweroff(void) { /* Stop RTC */ RTCCTL1 |= RTCHOLD_H; } -/*---------------------------------------------------------------------------*/ -void rtc_set_localtime(struct tm *localt) + +int rtc_set_time(struct tm *localt) { if (localt == NULL) { - return; + return -1; } /* copy time to be set */ memcpy(&time_to_set, localt, sizeof(struct tm)); set_time = 1; + return 0; } -/*--------------------------------------------------------------------------- -void rtc_set(time_t time) { - 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; -} -*/ - /*--------------------------------------------------------------------------- time_t rtc_time(void) { time_t sec; @@ -74,15 +68,15 @@ time_t rtc_time(void) { return sec; } */ -/*---------------------------------------------------------------------------*/ -void rtc_get_localtime(struct tm *localt) + +int rtc_get_time(struct tm *localt) { uint8_t success = 0; uint8_t i; uint16_t tmpyear; if (localt == NULL) { - return; + return -1; } while (!success) { @@ -129,36 +123,53 @@ void rtc_get_localtime(struct tm *localt) } } } + + return 0; } -/*---------------------------------------------------------------------------*/ -void rtc_set_alarm(struct tm *localt, rtc_alarm_mask_t mask) +int rtc_set_alarm(struct tm *localt, rtc_alarm_cb_t cb, void *arg) { - if (mask & RTC_ALARM_MIN) { + if (localt != NULL) { RTCAMIN = localt->tm_min; RTCAMIN |= BIT7; - } - - if (mask & RTC_ALARM_HOUR) { RTCAHOUR = localt->tm_hour; RTCAHOUR |= BIT7; - } - - if (mask & RTC_ALARM_DOW) { RTCADOW = localt->tm_wday; RTCADOW |= BIT7; - } - - if (mask & RTC_ALARM_DOM) { RTCADAY = localt->tm_mday; RTCADAY |= BIT7; + + RTCCTL0 |= RTCAIE; + return 0; } - RTCCTL0 |= RTCAIE; + else if (cb == NULL) { + return -1; + } + + return -2; } -/*---------------------------------------------------------------------------*/ -void rtc_remove_alarm(void) +int rtc_get_alarm(struct tm *localt) +{ + if (localt != NULL) { + localt->tm_sec = -1; + localt->tm_min = RTCAMIN; + localt->tm_hour = RTCAHOUR; + localt->tm_mday = -1; + localt->tm_wday = RTCADOW; + localt->tm_yday = -1; + localt->tm_mon = - 1; + localt->tm_year = -1; + localt->tm_isdst = -1; /* not available */ + + return 0; + } + + return -1; +} + +void rtc_clear_alarm(void) { /* reset all AE bits */ RTCAHOUR &= ~BIT7; @@ -169,7 +180,7 @@ void rtc_remove_alarm(void) /* reset alarm interrupt enable */ RTCCTL0 &= ~RTCAIE; } -/*---------------------------------------------------------------------------*/ + interrupt(RTC_VECTOR) __attribute__((naked)) rtc_isr(void) { __enter_isr(); @@ -194,12 +205,15 @@ interrupt(RTC_VECTOR) __attribute__((naked)) rtc_isr(void) if (rtc_second_pid != KERNEL_PID_UNDEF) { static msg_t m; - m.type = RTC_SECOND; + m.type = RTCSEC; msg_send_int(&m, rtc_second_pid); } } /* RTC alarm */ else if (RTCIV == RTC_RTCAIFG) { + if (_cb) { + _cb(_cb_arg); + } } __exit_isr(); diff --git a/cpu/cc430/include/cc430-rtc.h b/cpu/cc430/include/cc430-rtc.h index ead80ecefb..2d95dfca6f 100644 --- a/cpu/cc430/include/cc430-rtc.h +++ b/cpu/cc430/include/cc430-rtc.h @@ -8,7 +8,7 @@ #ifndef CC430_RTC_H #define CC430_RTC_H -#include "rtc.h" +#include "periph/rtc.h" #include "time.h" #ifdef __cplusplus @@ -39,18 +39,6 @@ typedef enum { RTC_ALARM_DOM = 0x08 ///< Alarm mask for Day of Month } rtc_alarm_mask_t; -/** - * @brief Sets the alarm - * @internal - * @param[in] localt Alarm time - * @param[in] mask Sets the registers to poll for the alarm - * - * To disable the alarm set mask to RTC_ALARM_DISABLED. - * - * @see ::rtc_alarm_mask - */ -void rtc_set_alarm(struct tm *localti, rtc_alarm_mask_t mask); - /** * @brief Resets any set alarm */ diff --git a/cpu/lpc2387/include/lpc2387-rtc.h b/cpu/lpc2387/include/lpc2387-rtc.h index d0ca6f8d51..214084bff6 100644 --- a/cpu/lpc2387/include/lpc2387-rtc.h +++ b/cpu/lpc2387/include/lpc2387-rtc.h @@ -32,7 +32,7 @@ #include #include -#include "rtc.h" +#include "periph/rtc.h" #include "lpc2387.h" #ifdef __cplusplus @@ -95,7 +95,6 @@ void rtc_set(time_t time); * * @see ::rtc_alarm_mask */ -void rtc_set_alarm(struct tm *localt, enum rtc_alarm_mask mask); /** * @brief Gets the current alarm setting diff --git a/cpu/lpc2387/rtc/lpc2387-rtc.c b/cpu/lpc2387/rtc/lpc2387-rtc.c index f5dbb463ad..f43ccfd20e 100644 --- a/cpu/lpc2387/rtc/lpc2387-rtc.c +++ b/cpu/lpc2387/rtc/lpc2387-rtc.c @@ -24,6 +24,7 @@ #include "kernel_types.h" /* cpu */ +#include "periph/rtc.h" #include "VIC.h" #include "lpc2387.h" #include "lpc2387-rtc.h" @@ -35,21 +36,25 @@ #define ENABLE_DEBUG (0) #include "debug.h" +/* Alarm callback */ +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; -/*---------------------------------------------------------------------------*/ /** * @brief Sets the current time in broken down format directly from to RTC * @param[in] localt Pointer to structure with time to set */ -void -rtc_set_localtime(struct tm *localt) +int rtc_set_time(struct tm *localt) { if (localt == NULL) { - return; + return -1; } /* set clock */ @@ -61,25 +66,26 @@ rtc_set_localtime(struct tm *localt) RTC_DOY = localt->tm_yday; RTC_MONTH = localt->tm_mon + 1; RTC_YEAR = localt->tm_year; + + return 0; } -/*---------------------------------------------------------------------------*/ + void rtc_set(time_t time) { struct tm *localt; localt = localtime(&time); /* convert seconds to broken-down time */ - rtc_set_localtime(localt); + rtc_set_time(localt); epoch = time - localt->tm_sec - localt->tm_min * 60; } -/*---------------------------------------------------------------------------*/ -//* set clock to start of unix epoch */ + +/* set clock to start of unix epoch */ void rtc_reset(void) { rtc_set(0); epoch = 0; } -/*---------------------------------------------------------------------------*/ -void -rtc_set_alarm(struct tm *localt, enum rtc_alarm_mask mask) + +int rtc_set_alarm(struct tm *localt, rtc_alarm_cb_t cb, void *arg) { if (localt != NULL) { RTC_ALSEC = localt->tm_sec; @@ -90,17 +96,22 @@ rtc_set_alarm(struct tm *localt, enum rtc_alarm_mask mask) RTC_ALDOY = localt->tm_yday; RTC_ALMON = localt->tm_mon + 1; RTC_ALYEAR = localt->tm_year; - RTC_AMR = ~mask; /* set wich alarm fields to check */ + RTC_AMR = 0; /* set wich alarm fields to check */ DEBUG("alarm set %2lu.%2lu.%4lu %2lu:%2lu:%2lu\n", RTC_ALDOM, RTC_ALMON, RTC_ALYEAR, RTC_ALHOUR, RTC_ALMIN, RTC_ALSEC); + + _cb = cb; + return 0; } - else { - RTC_AMR = 0xff; + else if (cb == NULL) { + return -1; } + + RTC_AMR = 0xff; + return -2; } -/*---------------------------------------------------------------------------*/ -enum rtc_alarm_mask -rtc_get_alarm(struct tm *localt) + +int rtc_get_alarm(struct tm *localt) { if (localt != NULL) { localt->tm_sec = RTC_ALSEC; @@ -112,12 +123,20 @@ rtc_get_alarm(struct tm *localt) localt->tm_mon = RTC_ALMON - 1; localt->tm_year = RTC_ALYEAR; localt->tm_isdst = -1; /* not available */ + + return 0; } - return (~RTC_AMR) & 0xff; /* return which alarm fields are checked */ + return -1; } -/*---------------------------------------------------------------------------*/ + +void rtc_clear_alarm(void) +{ + RTC_AMR = 0xff; +} + void RTC_IRQHandler(void) __attribute__((interrupt("IRQ"))); + void RTC_IRQHandler(void) { lpm_begin_awake(); @@ -135,6 +154,9 @@ void RTC_IRQHandler(void) else if (RTC_ILR & ILR_RTCALF) { RTC_ILR |= ILR_RTCALF; RTC_AMR = 0xff; /* disable alarm irq */ + if (_cb) { + _cb(_cb_arg); + } DEBUG("Ring\n"); lpm_end_awake(); } @@ -142,9 +164,9 @@ void RTC_IRQHandler(void) VICVectAddr = 0; /* Acknowledge Interrupt */ } -/*---------------------------------------------------------------------------*/ void rtc_enable(void) { + PCONP |= BIT9; 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 */ @@ -152,7 +174,7 @@ void rtc_enable(void) time_t now = rtc_time(NULL); epoch = now - (now % 3600); } -/*---------------------------------------------------------------------------*/ + void rtc_init(void) { PCONP |= BIT9; @@ -174,7 +196,7 @@ void rtc_init(void) RTC_DOM, RTC_MONTH, RTC_YEAR, RTC_HOUR, RTC_MIN, RTC_SEC, epoch); } -/*---------------------------------------------------------------------------*/ + time_t rtc_time(struct timeval *time) { uint32_t sec; @@ -191,8 +213,8 @@ time_t rtc_time(struct timeval *time) min = RTC_MIN; } - sec += min * 60; /* add number of minutes */ - sec += epoch; /* add precalculated epoch in hour granularity */ + sec += min * 60; /* add number of minutes */ + sec += epoch; /* add precalculated epoch in hour granularity */ if (time != NULL) { usec = usec * 15625; @@ -203,16 +225,16 @@ time_t rtc_time(struct timeval *time) return sec; } -/*---------------------------------------------------------------------------*/ -void rtc_disable(void) + +void rtc_poweroff(void) { RTC_CCR &= ~CCR_CLKEN; /* disable clock */ install_irq(RTC_INT, NULL, 0); RTC_ILR = 0; + PCONP &= ~BIT9; } -/*---------------------------------------------------------------------------*/ -void -rtc_get_localtime(struct tm *localt) + +int rtc_get_time(struct tm *localt) { if (localt != NULL) { localt->tm_sec = RTC_SEC; @@ -224,9 +246,11 @@ rtc_get_localtime(struct tm *localt) localt->tm_mon = RTC_MONTH - 1; localt->tm_year = RTC_YEAR; localt->tm_isdst = -1; /* not available */ + return 0; } + return -1; } -/*---------------------------------------------------------------------------*/ + void gettimeofday_r(struct _reent *r, struct timeval *ptimeval, struct timezone *ptimezone) { (void) ptimezone; /* unused */ diff --git a/drivers/include/rtc.h b/drivers/include/rtc.h deleted file mode 100644 index 81bd6bd96d..0000000000 --- a/drivers/include/rtc.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2010, Freie Universitaet Berlin (FUB). All rights reserved. - * - * 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. - */ - -/** - * @defgroup rtc Realtime Clock - * @ingroup drivers - * @brief Generic real time clock driver interface - * @{ - */ - -#ifndef RTC_H -#define RTC_H - -#define RTC_SECOND 10001U - -#include -#include -#include "kernel_types.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * @brief Initializes the RTC for calendar mode - */ -void rtc_init(void); - -/** - * @brief Starts the RTC - */ -void rtc_enable(void); - -/** - * @brief Stops the RTC - */ -void rtc_disable(void); - -/** - * @brief Sets the current time in broken down format directly from to RTC - * @param[in] localt Pointer to structure with time to set - */ -void rtc_set_localtime(struct tm *localt); - -/** - * @brief Returns the current time in broken down format directly from the RTC - * @param[out] localt Pointer to structure to receive time - */ -void rtc_get_localtime(struct tm *localt); - -/** - * @brief Get the current time as a struct timeval - * @param[out] time Pointer to structure to receive time - */ -time_t rtc_time(struct timeval *time); - -extern kernel_pid_t rtc_second_pid; - -#ifdef __cplusplus -} -#endif - -/** @} */ -#endif diff --git a/examples/ccn-lite-relay/main.c b/examples/ccn-lite-relay/main.c index 4d12a9c121..6684116026 100644 --- a/examples/ccn-lite-relay/main.c +++ b/examples/ccn-lite-relay/main.c @@ -26,7 +26,7 @@ // riot #include "thread.h" -#include "rtc.h" +#include "periph/rtc.h" // ccn #include "ccn_lite/ccnl-riot.h" diff --git a/sys/auto_init/auto_init.c b/sys/auto_init/auto_init.c index a570959551..86cbaf7acd 100644 --- a/sys/auto_init/auto_init.c +++ b/sys/auto_init/auto_init.c @@ -52,7 +52,7 @@ #endif #ifdef MODULE_RTC -#include "rtc.h" +#include "periph/rtc.h" #endif #ifdef MODULE_SIXLOWPAN @@ -214,7 +214,6 @@ void auto_init(void) #ifdef MODULE_RTC DEBUG("Auto init rtc module.\n"); rtc_init(); - rtc_enable(); #endif #ifdef MODULE_SHT11 DEBUG("Auto init SHT11 module.\n");