From bbbdceb9d13441f4b96257718ed090b8998759f7 Mon Sep 17 00:00:00 2001 From: patacongo Date: Fri, 6 May 2011 21:10:00 +0000 Subject: More timer changes from Uros git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3572 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/sched/Makefile | 4 -- nuttx/sched/clock_gettime.c | 51 ++++++++++++++++++----- nuttx/sched/clock_getutc.c | 92 ------------------------------------------ nuttx/sched/clock_initialize.c | 45 +++++++++++++-------- nuttx/sched/clock_settime.c | 49 +++++++++++++++++----- nuttx/sched/clock_systimer.c | 10 +++-- 6 files changed, 117 insertions(+), 134 deletions(-) delete mode 100644 nuttx/sched/clock_getutc.c (limited to 'nuttx/sched') diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile index 2ad17f826..859bd57b4 100644 --- a/nuttx/sched/Makefile +++ b/nuttx/sched/Makefile @@ -80,10 +80,6 @@ CLOCK_SRCS = clock_initialize.c clock_settime.c clock_gettime.c clock_getres.c \ clock_time2ticks.c clock_abstime2ticks.c clock_ticks2time.c \ clock_gettimeofday.c clock_systimer.c -ifeq ($(CONFIG_SYSTEM_UTC),y) -CLOCK_SRCS += clock_getutc.c -endif - SIGNAL_SRCS = sig_initialize.c \ sig_action.c sig_procmask.c sig_pending.c sig_suspend.c \ sig_kill.c sig_queue.c sig_waitinfo.c sig_timedwait.c \ diff --git a/nuttx/sched/clock_gettime.c b/nuttx/sched/clock_gettime.c index f3ffe2a6c..59e5b604c 100644 --- a/nuttx/sched/clock_gettime.c +++ b/nuttx/sched/clock_gettime.c @@ -38,6 +38,7 @@ ************************************************************************/ #include +#include #include #include @@ -95,17 +96,13 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp) sdbg("clock_id=%d\n", clock_id); - /* Only CLOCK_REALTIME is supported */ + /* CLOCK_REALTIME - POSIX demands this to be present. This is the wall + * time clock. + */ - if (clock_id != CLOCK_REALTIME) - { - sdbg("Returning ERROR\n"); - - *get_errno_ptr() = EINVAL; - ret = ERROR; - } - else + if (clock_id == CLOCK_REALTIME && tp) { +#ifndef CONFIG_SYSTEM_UTC /* Get the elapsed time since power up (in milliseconds) biased * as appropriate. */ @@ -142,10 +139,46 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp) tp->tv_sec = (time_t)secs; tp->tv_nsec = (long)nsecs; + +#else /* if CONFIG_SYSTEM_UTC=y */ + +#ifdef CONFIG_RTC + if (g_rtc_enabled) + { + tp->tv_sec = up_rtc_gettime(); + tp->tv_nsec = (up_rtc_getclock() & (RTC_CLOCKS_PER_SEC-1) ) * (1000000000/TICK_PER_SEC); + } + else +#endif + { + tp->tv_sec = g_system_utc; + tp->tv_nsec = g_tickcount * (1000000000/TICK_PER_SEC); + } +#endif sdbg("Returning tp=(%d,%d)\n", (int)tp->tv_sec, (int)tp->tv_nsec); } + /* CLOCK_ACTIVETIME is non-standard. Returns active UTC time, which is + * disabled during power down modes. Unit is 1 second. + */ + +#ifdef CONFIG_RTC + else if (clock_id == CLOCK_ACTIVETIME && g_rtc_enabled && tp) + { + tp->tv_sec = g_system_utc; + tp->tv_nsec = g_tickcount * (1000000000/TICK_PER_SEC); + } +#endif + + else + { + sdbg("Returning ERROR\n"); + + errno = EINVAL; + ret = ERROR; + } + return ret; } diff --git a/nuttx/sched/clock_getutc.c b/nuttx/sched/clock_getutc.c deleted file mode 100644 index 95c097019..000000000 --- a/nuttx/sched/clock_getutc.c +++ /dev/null @@ -1,92 +0,0 @@ -/**************************************************************************** - * sched/clock_getutc.c - * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name NuttX nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include - -#include -#include -#include -#include - -#if !defined(CONFIG_DISABLE_CLOCK) && defined(CONFIG_SYSTEM_UTC) && !defined(clock_getutc) - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Function: clock_getutc - * - * Description: - * Return the current value of the system timer counter, which is only - * enabled when system is in active mode. - * - * Parameters: - * None - * - * Return Value: - * The current value of the system time counter - * - * Assumptions: - * - ****************************************************************************/ - -time_t clock_getutc(void) -{ -#ifdef CONFIG_RTC - if (g_rtc_enabled) - { - return up_rtc_gettime(); - } - else -#endif - { - return g_system_utc; - } -} - -#endif /* CONFIG_DISABLE_CLOCK && CONFIG_SYSTEM_UTC */ diff --git a/nuttx/sched/clock_initialize.c b/nuttx/sched/clock_initialize.c index 91220c19e..8919b6005 100644 --- a/nuttx/sched/clock_initialize.c +++ b/nuttx/sched/clock_initialize.c @@ -60,9 +60,13 @@ #define SEC_PER_HOUR ((time_t)60 * SEC_PER_MIN) #define SEC_PER_DAY ((time_t)24 * SEC_PER_HOUR) -/* Defined just so the UTC counter and system counter/timer look similar */ +/* Macro to increment the system timer -- or not */ -#define incr_systimer() g_system_timer++ +#ifndef CONFIG_SYSTEM_UTC +# define incr_systimer() g_system_timer++ +#else +# define incr_systimer() +#endif /**************************************************************************** * Private Type Declarations @@ -80,14 +84,13 @@ * Public Variables ****************************************************************************/ -volatile clock_t g_system_timer = 0; - #if CONFIG_SYSTEM_UTC volatile time_t g_system_utc = 0; -#endif - +#else +volatile clock_t g_system_timer = 0; struct timespec g_basetime = {0,0}; uint32_t g_tickbias = 0; +#endif /************************************************************************** * Private Variables @@ -99,11 +102,11 @@ uint32_t g_tickbias = 0; #if CONFIG_SYSTEM_UTC #if TICK_PER_SEC > 32767 -static uint32_t g_tickcount = 0; +volatile uint32_t g_tickcount = 0; #elif TICK_PER_SEC > 255 -static uint16_t g_tickcount = 0; +volatile uint16_t g_tickcount = 0; #else -static uint8_t g_tickcount = 0; +volatile uint8_t g_tickcount = 0; #endif #endif /* CONFIG_SYSTEM_UTC */ @@ -153,32 +156,39 @@ static inline void incr_utc(void) void clock_initialize(void) { +#ifndef CONFIG_SYSTEM_UTC time_t jdn = 0; +#endif /* Initialize the real time close (this should be un-nesssary except on a * restart). */ - g_system_timer = 0; #ifdef CONFIG_SYSTEM_UTC g_system_utc = 0; +#else + g_system_timer = 0; #endif - /* Do we have hardware periodic timer support? */ + /* Do we have hardware RTC support? */ #ifdef CONFIG_RTC + +#ifndef CONFIG_SYSTEM_UTC +# error In order to support hardware RTC system must have set the CONFIG_SYSTEM_UTC=y +#endif + up_rtcinitialize(); +#endif + +#ifndef CONFIG_SYSTEM_UTC /* Get the EPOCH-relative julian date from the calendar year, * month, and date */ - if (g_rtc_enabled==false) -#endif - { - jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH, - CONFIG_START_DAY); - } + jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH, + CONFIG_START_DAY); /* Set the base time as seconds into this julian day. */ @@ -188,6 +198,7 @@ void clock_initialize(void) /* These is no time bias from this time. */ g_tickbias = 0; +#endif } /**************************************************************************** diff --git a/nuttx/sched/clock_settime.c b/nuttx/sched/clock_settime.c index 2af27acf1..72fb8276e 100644 --- a/nuttx/sched/clock_settime.c +++ b/nuttx/sched/clock_settime.c @@ -38,6 +38,7 @@ ************************************************************************/ #include +#include #include #include @@ -90,16 +91,13 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp) sdbg("clock_id=%d\n", clock_id); - /* Only CLOCK_REALTIME is supported */ + /* CLOCK_REALTIME - POSIX demands this to be present. This is the wall + * time clock. + */ - if (clock_id != CLOCK_REALTIME || !tp) - { - sdbg("Returning ERROR\n"); - *get_errno_ptr() = EINVAL; - ret = ERROR; - } - else + if (clock_id == CLOCK_REALTIME && tp) { +#ifndef CONFIG_SYSTEM_UTC /* Save the new base time. */ g_basetime.tv_sec = tp->tv_sec; @@ -110,11 +108,44 @@ int clock_settime(clockid_t clock_id, const struct timespec *tp) */ g_tickbias = clock_systimer(); + +#else /* if CONFIG_SYSTEM_UTC=y */ + + /* We ignore everything below one second in time configuration */ + +#ifdef CONFIG_RTC + if (g_rtc_enabled) + { + up_rtc_settime( tp->tv_sec ); + } + else +#endif + g_system_utc = tp->tv_sec; + +#endif sdbg("basetime=(%d,%d) tickbias=%d\n", (int)g_basetime.tv_sec, (int)g_basetime.tv_nsec, (int)g_tickbias); - } + } + + /* CLOCK_ACTIVETIME is non-standard. Returns active UTC time, which is + * disabled during power down modes. Unit is 1 second. + */ + +#ifdef CONFIG_RTC + else if (clock_id == CLOCK_ACTIVETIME && g_rtc_enabled && tp) + { + g_system_utc = tp->tv_sec; + } +#endif + + else + { + sdbg("Returning ERROR\n"); + *get_errno_ptr() = EINVAL; + ret = ERROR; + } return ret; } diff --git a/nuttx/sched/clock_systimer.c b/nuttx/sched/clock_systimer.c index c4560a8a1..a1219a681 100644 --- a/nuttx/sched/clock_systimer.c +++ b/nuttx/sched/clock_systimer.c @@ -83,17 +83,21 @@ uint32_t clock_systimer(void) /* Check if the periodic timer is initialized * - * Note that the unit of the g_system_timer and and up_rtc_getclock() must - * be the same in order (must have same [unit]) to allow smooth transitions. + * Note that the unit of the g_system_timer and and up_rtc_getclock() do + * not have the same unit. */ if (g_rtc_enabled) { -// return up_rtc_getclock(); + return up_rtc_getclock(); } #endif +#ifndef CONFIG_SYSTEM_UTC return g_system_timer; +#else + return g_system_utc * TICK_PER_SEC + g_tickcount; +#endif } #endif /* !clock_systtimer */ -- cgit v1.2.3