diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2014-08-08 14:43:02 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2014-08-08 14:43:02 -0600 |
commit | d390565d32af5e45e30b3b6b9eb91c3318be34a1 (patch) | |
tree | 2fde49b5dbc2cc114d1664f24ede38eb5c95ee62 /nuttx/sched/clock | |
parent | a78414202cf4dd56330dbeb10a2e355686215b97 (diff) | |
download | nuttx-d390565d32af5e45e30b3b6b9eb91c3318be34a1.tar.gz nuttx-d390565d32af5e45e30b3b6b9eb91c3318be34a1.tar.bz2 nuttx-d390565d32af5e45e30b3b6b9eb91c3318be34a1.zip |
Move clock functions from sched/ to sched/clock
Diffstat (limited to 'nuttx/sched/clock')
-rw-r--r-- | nuttx/sched/clock/Make.defs | 43 | ||||
-rw-r--r-- | nuttx/sched/clock/clock.h | 93 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_abstime2ticks.c | 126 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_dow.c | 88 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_getres.c | 114 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_gettime.c | 225 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_gettimeofday.c | 113 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_initialize.c | 280 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_settime.c | 149 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_systimer.c | 147 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_ticks2time.c | 94 | ||||
-rw-r--r-- | nuttx/sched/clock/clock_time2ticks.c | 136 |
12 files changed, 1608 insertions, 0 deletions
diff --git a/nuttx/sched/clock/Make.defs b/nuttx/sched/clock/Make.defs new file mode 100644 index 000000000..aa7dce762 --- /dev/null +++ b/nuttx/sched/clock/Make.defs @@ -0,0 +1,43 @@ +############################################################################ +# sched/clock/Make.defs +# +# Copyright (C) 2014 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt <gnutt@nuttx.org> +# +# 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. +# +############################################################################ + +CLOCK_SRCS = clock_initialize.c clock_settime.c clock_gettime.c clock_getres.c +CLOCK_SRCS += clock_time2ticks.c clock_abstime2ticks.c clock_ticks2time.c +CLOCK_SRCS += clock_gettimeofday.c clock_systimer.c + +# Include clock build support + +DEPPATH += --dep-path clock +VPATH += :clock diff --git a/nuttx/sched/clock/clock.h b/nuttx/sched/clock/clock.h new file mode 100644 index 000000000..976ec117f --- /dev/null +++ b/nuttx/sched/clock/clock.h @@ -0,0 +1,93 @@ +/******************************************************************************** + * sched/clock/clock.h + * + * Copyright (C) 2007-2009, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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. + * + ********************************************************************************/ + +#ifndef __SCHED_CLOCK_CLOCK_H +#define __SCHED_CLOCK_CLOCK_H + +/******************************************************************************** + * Included Files + ********************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +#include <nuttx/clock.h> +#include <nuttx/compiler.h> + +/******************************************************************************** + * Pre-processor Definitions + ********************************************************************************/ +/* Configuration ************************************************************/ +/* If CONFIG_SYSTEM_TIME64 is selected and the CPU supports long long types, + * then a 64-bit system time will be used. + */ + +#ifndef CONFIG_HAVE_LONG_LONG +# undef CONFIG_SYSTEM_TIME64 +#endif + +/******************************************************************************** + * Public Type Definitions + ********************************************************************************/ + +/******************************************************************************** + * Global Variables + ********************************************************************************/ + +#ifdef CONFIG_SYSTEM_TIME64 +extern uint64_t g_tickbias; +#else +extern uint32_t g_tickbias; +#endif + +extern struct timespec g_basetime; + +/******************************************************************************** + * Public Function Prototypes + ********************************************************************************/ + +void weak_function clock_initialize(void); +#ifndef CONFIG_SCHED_TICKLESS +void weak_function clock_timer(void); +#endif + +int clock_abstime2ticks(clockid_t clockid, + FAR const struct timespec *abstime, + FAR int *ticks); +int clock_time2ticks(FAR const struct timespec *reltime, FAR int *ticks); +int clock_ticks2time(int ticks, FAR struct timespec *reltime); + +#endif /* __SCHED_CLOCK_CLOCK_H */ diff --git a/nuttx/sched/clock/clock_abstime2ticks.c b/nuttx/sched/clock/clock_abstime2ticks.c new file mode 100644 index 000000000..1f8845d05 --- /dev/null +++ b/nuttx/sched/clock/clock_abstime2ticks.c @@ -0,0 +1,126 @@ +/******************************************************************************** + * sched/clock/clock_abstime2ticks.c + * + * Copyright (C) 2007, 2008, 2013 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <time.h> +#include <errno.h> +#include <debug.h> +#include "clock/clock.h" + +/******************************************************************************** + * Pre-processor Definitions + ********************************************************************************/ + +/******************************************************************************** + * Private Type Declarations + ********************************************************************************/ + +/******************************************************************************** + * Global Variables + ********************************************************************************/ + +/******************************************************************************** + * Private Variables + ********************************************************************************/ + +/******************************************************************************** + * Private Functions + ********************************************************************************/ + +/******************************************************************************** + * Public Functions + ********************************************************************************/ + +/******************************************************************************** + * Name: clock_abstime2ticks + * + * Description: + * Convert an absolute timespec delay to system timer ticks. + * + * Parameters: + * clockid - The timing source to use in the conversion + * reltime - Convert this absolue time to system clock ticks. + * ticks - Return the converted number of ticks here. + * + * Return Value: + * OK on success; A non-zero error number on failure; + * + * Assumptions: + * Interrupts should be disabled so that the time is not changing during the + * calculation + * + ********************************************************************************/ + +int clock_abstime2ticks(clockid_t clockid, FAR const struct timespec *abstime, + FAR int *ticks) +{ + struct timespec currtime; + struct timespec reltime; + int ret; + + /* Convert the timespec to clock ticks. NOTE: Here we use internal knowledge + * that CLOCK_REALTIME is defined to be zero! + */ + + ret = clock_gettime(clockid, &currtime); + if (ret) + { + return EINVAL; + } + + /* The relative time to wait is the absolute time minus the current time. */ + + reltime.tv_nsec = (abstime->tv_nsec - currtime.tv_nsec); + reltime.tv_sec = (abstime->tv_sec - currtime.tv_sec); + + /* Check if we were supposed to borrow from the seconds to borrow from the + * seconds + */ + + if (reltime.tv_nsec < 0) + { + reltime.tv_nsec += NSEC_PER_SEC; + reltime.tv_sec -= 1; + } + + /* Convert this relative time into clock ticks. */ + + return clock_time2ticks(&reltime, ticks); +} diff --git a/nuttx/sched/clock/clock_dow.c b/nuttx/sched/clock/clock_dow.c new file mode 100644 index 000000000..00e9703bc --- /dev/null +++ b/nuttx/sched/clock/clock_dow.c @@ -0,0 +1,88 @@ +/**************************************************************************** + * sched/clock/clock_dow.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <stdint.h> + +#include <nuttx/clock.h> + +#include "clock/clock.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* 23 * (month + 1) / 9, month = 0..11 */ + +static const uint8_t g_lookup[12] = {2, 5, 7, 10, 12, 15, 17, 20, 23, 25, 28, 30}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: clock_dow + * + * Description: + * Calculate the day of week (DOW) from they year month and day. Based on + * an algorithm pubished in 1990 by Michael Keith and Tom Craver with some + * tweaks to handle months in the range 0-11. + * + * Parameters: + * year - year (e.g., 1988) + * month - 0 through 11 + * day - 1 through 31 + * + * Return Value: + * The day of the week as days since Sunday: 0 = Sunday, 1 = Monday, etc. + * + * Assumptions: + * + ****************************************************************************/ + +int clock_dow(int year, int month, int day) +{ + day += month < 2 ? year-- : year - 2; + return ((int)g_lookup[month] + day + 4 + year/4 - year/100 + year/400) % 7; +} diff --git a/nuttx/sched/clock/clock_getres.c b/nuttx/sched/clock/clock_getres.c new file mode 100644 index 000000000..5f4a0b6e8 --- /dev/null +++ b/nuttx/sched/clock/clock_getres.c @@ -0,0 +1,114 @@ +/************************************************************************ + * sched/clock/clock_getres.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <stdint.h> +#include <time.h> +#include <errno.h> +#include <debug.h> + +#include "clock/clock.h" + +/************************************************************************ + * Definitions + ************************************************************************/ + +/************************************************************************ + * Private Type Declarations + ************************************************************************/ + +/************************************************************************ + * Private Function Prototypes + ************************************************************************/ + +/********************************************************************** + * Public Constant Data + **********************************************************************/ + +/************************************************************************ + * Public Variables + ************************************************************************/ + +/********************************************************************** + * Private Variables + **********************************************************************/ + +/************************************************************************ + * Private Functions + ************************************************************************/ + +/************************************************************************ + * Public Functions + ************************************************************************/ + +/************************************************************************ + * Name: clock_getres + * + * Description: + * Clock Functions based on POSIX APIs + * + ************************************************************************/ + +int clock_getres(clockid_t clock_id, struct timespec *res) +{ + int ret = OK; + + sdbg("clock_id=%d\n", clock_id); + + /* Only CLOCK_REALTIME is supported */ + + if (clock_id != CLOCK_REALTIME) + { + sdbg("Returning ERROR\n"); + set_errno(EINVAL); + ret = ERROR; + } + else + { + /* Form the timspec using clock resolution in nanoseconds */ + + res->tv_sec = 0; + res->tv_nsec = NSEC_PER_TICK; + + sdbg("Returning res=(%d,%d)\n", (int)res->tv_sec, (int)res->tv_nsec); + } + + return ret; +} diff --git a/nuttx/sched/clock/clock_gettime.c b/nuttx/sched/clock/clock_gettime.c new file mode 100644 index 000000000..d0cc48370 --- /dev/null +++ b/nuttx/sched/clock/clock_gettime.c @@ -0,0 +1,225 @@ +/************************************************************************ + * sched/clock/clock_gettime.c + * + * Copyright (C) 2007, 2009, 2011, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> +#include <nuttx/rtc.h> + +#include <stdint.h> +#include <time.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <arch/irq.h> + +#include "clock/clock.h" + +/************************************************************************ + * Pre-processor Definitions + ************************************************************************/ + +/************************************************************************ + * Private Type Declarations + ************************************************************************/ + +/************************************************************************ + * Private Function Prototypes + ************************************************************************/ + +/********************************************************************** + * Public Constant Data + **********************************************************************/ + +/************************************************************************ + * Public Variables + ************************************************************************/ + +/********************************************************************** + * Private Variables + **********************************************************************/ + +/************************************************************************ + * Private Functions + ************************************************************************/ + +/************************************************************************ + * Public Functions + ************************************************************************/ + +/************************************************************************ + * Name: clock_gettime + * + * Description: + * Clock Functions based on POSIX APIs + * + ************************************************************************/ + +int clock_gettime(clockid_t clock_id, struct timespec *tp) +{ +#ifdef CONFIG_SYSTEM_TIME64 + uint64_t msecs; + uint64_t secs; + uint64_t nsecs; +#else + uint32_t msecs; + uint32_t secs; + uint32_t nsecs; +#endif + uint32_t carry; + int ret = OK; + + sdbg("clock_id=%d\n", clock_id); + DEBUGASSERT(tp != NULL); + +#ifdef CONFIG_CLOCK_MONOTONIC + /* CLOCK_MONOTONIC is an optional under POSIX: "If the Monotonic Clock + * option is supported, all implementations shall support a clock_id + * of CLOCK_MONOTONIC defined in <time.h>. This clock represents the + * monotonic clock for the system. For this clock, the value returned + * by clock_gettime() represents the amount of time (in seconds and + * nanoseconds) since an unspecified point in the past (for example, + * system start-up time, or the Epoch). This point does not change + * after system start-up time. The value of the CLOCK_MONOTONIC clock + * cannot be set via clock_settime(). This function shall fail if it + * is invoked with a clock_id argument of CLOCK_MONOTONIC." + */ + + if (clock_id == CLOCK_MONOTONIC) + { + /* Get the time since power-on in seconds and milliseconds */ + + msecs = TICK2MSEC(clock_systimer()); + secs = msecs / MSEC_PER_SEC; + + /* Return the elapsed time in seconds and nanoseconds */ + + nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; + + tp->tv_sec = (time_t)secs; + tp->tv_nsec = (long)nsecs; + } + else +#endif + + /* CLOCK_REALTIME - POSIX demands this to be present. CLOCK_REALTIME + * represents the machine's best-guess as to the current wall-clock, + * time-of-day time. This means that CLOCK_REALTIME can jump forward and + * backward as the system time-of-day clock is changed. + * + * If an RTC is supported, then the non-standard CLOCK_ACTIVETIME is also + * supported to manage time based on the system timer interrupt separately + * from the RTC. This may be necessary, for example, in certain cases where + * the system timer interrupt has been stopped in low power modes. + */ + +#ifdef CONFIG_RTC + if (clock_id == CLOCK_REALTIME || clock_id == CLOCK_ACTIVETIME) +#else + if (clock_id == CLOCK_REALTIME) +#endif + { + /* Do we have a high-resolution RTC that can provide us with the time? */ + +#ifdef CONFIG_RTC_HIRES + if (g_rtc_enabled && clock_id != CLOCK_ACTIVETIME) + { + /* Yes.. Get the hi-resolution time from the RTC unless the caller + * has specifically asked for the system timer (CLOCK_ACTIVETIME) + */ + + ret = up_rtc_gettime(tp); + } + else +#endif + { + /* Get the elapsed time since the time-of-day was last set. + * clock_systimer() provides the number of clock times since + * power was applied; the bias value corresponds to the time + * when the time-of-day was last set. + */ + + msecs = TICK2MSEC((clock_systimer() - g_tickbias)); + + sdbg("msecs = %d g_tickbias=%d\n", + (int)msecs, (int)g_tickbias); + + /* Get the elapsed time in seconds and nanoseconds. */ + + secs = msecs / MSEC_PER_SEC; + nsecs = (msecs - (secs * MSEC_PER_SEC)) * NSEC_PER_MSEC; + + sdbg("secs = %d + %d nsecs = %d + %d\n", + (int)msecs, (int)g_basetime.tv_sec, + (int)nsecs, (int)g_basetime.tv_nsec); + + /* Add the base time to this. The base time is the time-of-day + * setting. When added to the elapsed time since the time-of-day + * was last set, this gives us the current time. + */ + + secs += (uint32_t)g_basetime.tv_sec; + nsecs += (uint32_t)g_basetime.tv_nsec; + + /* Handle carry to seconds. */ + + if (nsecs > NSEC_PER_SEC) + { + carry = nsecs / NSEC_PER_SEC; + secs += carry; + nsecs -= (carry * NSEC_PER_SEC); + } + + /* And return the result to the caller. */ + + tp->tv_sec = (time_t)secs; + tp->tv_nsec = (long)nsecs; + } + + sdbg("Returning tp=(%d,%d)\n", (int)tp->tv_sec, (int)tp->tv_nsec); + } + else + { + sdbg("Returning ERROR\n"); + + set_errno(EINVAL); + ret = ERROR; + } + + return ret; +} diff --git a/nuttx/sched/clock/clock_gettimeofday.c b/nuttx/sched/clock/clock_gettimeofday.c new file mode 100644 index 000000000..46db6e698 --- /dev/null +++ b/nuttx/sched/clock/clock_gettimeofday.c @@ -0,0 +1,113 @@ +/**************************************************************************** + * sched/clock/clock_gettimeofday.c + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <sys/time.h> +#include <errno.h> +#include <debug.h> + +#include "clock/clock.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/************************************************************************** + * Public Constant Data + **************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/************************************************************************** + * Private Variables + **************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: gettimeofday + * + * Description: + * Get the current time + * + ****************************************************************************/ + +int gettimeofday(struct timeval *tp, void *tzp) +{ + struct timespec ts; + int ret; + +#ifdef CONFIG_DEBUG + if (!tp) + { + errno = EINVAL; + return ERROR; + } +#endif + + /* Let clock_gettime do most of the work */ + + ret = clock_gettime(CLOCK_REALTIME, &ts); + if (ret == OK) + { + /* Convert the struct timespec to a struct timeval */ + + tp->tv_sec = ts.tv_sec; + tp->tv_usec = ts.tv_nsec / NSEC_PER_USEC; + } + + return ret; +} diff --git a/nuttx/sched/clock/clock_initialize.c b/nuttx/sched/clock/clock_initialize.c new file mode 100644 index 000000000..62a41cd69 --- /dev/null +++ b/nuttx/sched/clock/clock_initialize.c @@ -0,0 +1,280 @@ +/**************************************************************************** + * sched/clock/clock_initialize.c + * + * Copyright (C) 2007, 2009, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> +#include <nuttx/compiler.h> + +#include <stdint.h> +#include <time.h> +#include <errno.h> +#include <debug.h> + +#ifdef CONFIG_RTC +# include <arch/irq.h> +#endif + +#include <nuttx/clock.h> +#include <nuttx/time.h> +#include <nuttx/rtc.h> + +#include "clock/clock.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ +/* Standard time definitions (in units of seconds) */ + +#define SEC_PER_MIN ((time_t)60) +#define SEC_PER_HOUR ((time_t)60 * SEC_PER_MIN) +#define SEC_PER_DAY ((time_t)24 * SEC_PER_HOUR) + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/************************************************************************** + * Public Constant Data + **************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +#ifndef CONFIG_SCHED_TICKLESS +#ifdef CONFIG_SYSTEM_TIME64 +volatile uint64_t g_system_timer; +#else +volatile uint32_t g_system_timer; +#endif +#endif + +#ifdef CONFIG_SYSTEM_TIME64 +uint64_t g_tickbias; +#else +uint32_t g_tickbias; +#endif + +struct timespec g_basetime; + +/************************************************************************** + * Private Variables + **************************************************************************/ + +/************************************************************************** + * Private Functions + **************************************************************************/ + +/**************************************************************************** + * Name: clock_basetime + * + * Description: + * Get the initial time value from the best source available. + * + ****************************************************************************/ + +#ifdef CONFIG_RTC +#if defined(CONFIG_RTC_DATETIME) +/* Initialize the system time using a broken out date/time structure */ + +static inline void clock_basetime(FAR struct timespec *tp) +{ + struct tm rtctime; + + /* Get the broken-out time from the date/time RTC. */ + + (void)up_rtc_getdatetime(&rtctime); + + /* And use the broken-out time to initialize the system time */ + + tp->tv_sec = mktime(&rtctime); + tp->tv_nsec = 0; +} + +#elif defined(CONFIG_RTC_HIRES) + +/* Initialize the system time using a high-resolution structure */ + +static inline void clock_basetime(FAR struct timespec *tp) +{ + /* Get the complete time from the hi-res RTC. */ + + (void)up_rtc_gettime(tp); +} + +#else + +/* Initialize the system time using seconds only */ + +static inline void clock_basetime(FAR struct timespec *tp) +{ + /* Get the seconds (only) from the lo-resolution RTC */ + + tp->tv_sec = up_rtc_time(); + tp->tv_nsec = 0; +} + +#endif /* CONFIG_RTC_HIRES */ +#else /* CONFIG_RTC */ + +static inline void clock_basetime(FAR struct timespec *tp) +{ + time_t jdn = 0; + + /* Get the EPOCH-relative julian date from the calendar year, + * month, and date + */ + + jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH, + CONFIG_START_DAY); + + /* Set the base time as seconds into this julian day. */ + + tp->tv_sec = jdn * SEC_PER_DAY; + tp->tv_nsec = 0; +} + +#endif /* CONFIG_RTC */ + +/**************************************************************************** + * Name: clock_inittime + * + * Description: + * Get the initial time value from the best source available. + * + ****************************************************************************/ + +static void clock_inittime(void) +{ + /* (Re-)initialize the time value to match the RTC */ + + clock_basetime(&g_basetime); +#ifndef CONFIG_SCHED_TICKLESS + g_system_timer = 0; +#endif + g_tickbias = 0; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: clock_initialize + * + * Description: + * Perform one-time initialization of the timing facilities. + * + ****************************************************************************/ + +void clock_initialize(void) +{ + /* Initialize the RTC hardware */ + +#ifdef CONFIG_RTC + up_rtcinitialize(); +#endif + + /* Initialize the time value to match the RTC */ + + clock_inittime(); +} + +/**************************************************************************** + * Name: clock_synchronize + * + * Description: + * Synchronize the system timer to a hardware RTC. This operation is + * normally performed automatically by the system during clock + * initialization. However, the user may also need to explicitly re- + * synchronize the system timer to the RTC under certain conditions where + * the system timer is known to be in error. For example, in certain low- + * power states, the system timer may be stopped but the RTC will continue + * keep correct time. After recovering from such low-power state, this + * function should be called to restore the correct system time. + * + * Calling this function could result in system time going "backward" in + * time, especially with certain lower resolution RTC implementations. + * Time going backward could have bad consequences if there are ongoing + * timers and delays. So use this interface with care. + * + * Parameters: + * None + * + * Return Value: + * None + * + * Assumptions: + * + ****************************************************************************/ + +#ifdef CONFIG_RTC +void clock_synchronize(void) +{ + irqstate_t flags; + + /* Re-initialize the time value to match the RTC */ + + flags = irqsave(); + clock_inittime(); + irqrestore(flags); +} +#endif + +/**************************************************************************** + * Name: clock_timer + * + * Description: + * This function must be called once every time the real time clock + * interrupt occurs. The interval of this clock interrupt must be + * USEC_PER_TICK + * + ****************************************************************************/ + +#ifndef CONFIG_SCHED_TICKLESS +void clock_timer(void) +{ + /* Increment the per-tick system counter */ + + g_system_timer++; +} +#endif diff --git a/nuttx/sched/clock/clock_settime.c b/nuttx/sched/clock/clock_settime.c new file mode 100644 index 000000000..8d26da155 --- /dev/null +++ b/nuttx/sched/clock/clock_settime.c @@ -0,0 +1,149 @@ +/************************************************************************ + * sched/clock/clock_settime.c + * + * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> +#include <nuttx/rtc.h> + +#include <time.h> +#include <assert.h> +#include <errno.h> +#include <debug.h> + +#include <arch/irq.h> +#include "clock/clock.h" + +/************************************************************************ + * Definitions + ************************************************************************/ + +/************************************************************************ + * Private Type Declarations + ************************************************************************/ + +/************************************************************************ + * Private Function Prototypes + ************************************************************************/ + +/********************************************************************** + * Public Constant Data + **********************************************************************/ + +/************************************************************************ + * Public Variables + ************************************************************************/ + +/********************************************************************** + * Private Variables + **********************************************************************/ + +/************************************************************************ + * Private Functions + ************************************************************************/ + +/************************************************************************ + * Public Functions + ************************************************************************/ + +/************************************************************************ + * Name: clock_settime + * + * Description: + * Clock Functions based on POSIX APIs + * + ************************************************************************/ + +int clock_settime(clockid_t clock_id, FAR const struct timespec *tp) +{ + irqstate_t flags; + int ret = OK; + + sdbg("clock_id=%d\n", clock_id); + DEBUGASSERT(tp != NULL); + + /* CLOCK_REALTIME - POSIX demands this to be present. This is the wall + * time clock. + */ + +#ifdef CONFIG_RTC + if (clock_id == CLOCK_REALTIME || clock_id == CLOCK_ACTIVETIME) +#else + if (clock_id == CLOCK_REALTIME) +#endif + { + /* Interrupts are disabled here so that the in-memory time + * representation and the RTC setting will be as close as + * possible. + */ + + flags = irqsave(); + + /* Save the new base time. */ + + g_basetime.tv_sec = tp->tv_sec; + g_basetime.tv_nsec = tp->tv_nsec; + + /* Get the elapsed time since power up (in milliseconds) biased + * as appropriate. + */ + + g_tickbias = clock_systimer(); + + /* Setup the RTC (lo- or high-res) */ + +#ifdef CONFIG_RTC + if (g_rtc_enabled && clock_id != CLOCK_ACTIVETIME) + { + up_rtc_settime(tp); + } +#endif + irqrestore(flags); + + sdbg("basetime=(%d,%d) tickbias=%d\n", + (int)g_basetime.tv_sec, (int)g_basetime.tv_nsec, + (int)g_tickbias); + } + else + { + sdbg("Returning ERROR\n"); + set_errno(EINVAL); + ret = ERROR; + } + + return ret; +} diff --git a/nuttx/sched/clock/clock_systimer.c b/nuttx/sched/clock/clock_systimer.c new file mode 100644 index 000000000..1d24803ef --- /dev/null +++ b/nuttx/sched/clock/clock_systimer.c @@ -0,0 +1,147 @@ +/**************************************************************************** + * sched/clock/clock_systimer.c + * + * Copyright (C) 2011, 2014 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <stdint.h> + +#include <nuttx/arch.h> +#include <nuttx/clock.h> + +#include "clock/clock.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: clock_systimer + * + * Description: + * Return the current value of the 32-bit system timer counter + * + * Parameters: + * None + * + * Return Value: + * The current value of the system timer counter + * + * Assumptions: + * + ****************************************************************************/ + +#if !defined(clock_systimer) /* See nuttx/clock.h */ +uint32_t clock_systimer(void) +{ +#ifdef CONFIG_SCHED_TICKLESS + struct timespec ts; + uint64_t tmp; + + /* Get the time from the platform specific hardware */ + + (void)up_timer_gettime(&ts); + + /* Convert to a 64- then 32-bit value */ + + tmp = MSEC2TICK(1000 * (uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec / 1000000); + return (uint32_t)(tmp & 0x00000000ffffffff); + +#else + +#ifdef CONFIG_SYSTEM_TIME64 + /* Return the current system time truncated to 32-bits */ + + return (uint32_t)(g_system_timer & 0x00000000ffffffff); +#else + /* Return the current system time */ + + return g_system_timer; +#endif + +#endif +} +#endif + +/**************************************************************************** + * Name: clock_systimer64 + * + * Description: + * Return the current value of the 64-bit system timer counter + * + * Parameters: + * None + * + * Return Value: + * The current value of the system timer counter + * + * Assumptions: + * + ****************************************************************************/ + +#if !defined(clock_systimer) /* See nuttx/clock.h */ +#ifdef CONFIG_SYSTEM_TIME64 +uint64_t clock_systimer64(void) +{ +#ifdef CONFIG_SCHED_TICKLESS + struct timespec ts; + + /* Get the time from the platform specific hardware */ + + (void)up_timer_gettime(&ts); + + /* Convert to a 64- then 32-bit value */ + + return MSEC2TICK(1000 * (uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec / 1000000); + +#else + /* Return the current system time */ + + return g_system_timer; +#endif +} +#endif +#endif diff --git a/nuttx/sched/clock/clock_ticks2time.c b/nuttx/sched/clock/clock_ticks2time.c new file mode 100644 index 000000000..416487667 --- /dev/null +++ b/nuttx/sched/clock/clock_ticks2time.c @@ -0,0 +1,94 @@ +/******************************************************************************** + * sched/clock/clock_ticks2time.c + * + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <time.h> +#include "clock/clock.h" + +/******************************************************************************** + * Definitions + ********************************************************************************/ + +/******************************************************************************** + * Private Type Declarations + ********************************************************************************/ + +/******************************************************************************** + * Global Variables + ********************************************************************************/ + +/******************************************************************************** + * Private Variables + ********************************************************************************/ + +/******************************************************************************** + * Private Functions + ********************************************************************************/ + +/******************************************************************************** + * Public Functions + ********************************************************************************/ + +/******************************************************************************** + * Name: clock_ticks2time + * + * Description: + * Convert the system time tick value to a relative time. + * + * Parameters: + * ticks - The number of system time ticks to convert. + * reltime - Return the converted system time here. + * + * Return Value: + * Always returns OK + * + * Assumptions: + * + ********************************************************************************/ + +int clock_ticks2time(int ticks, FAR struct timespec *reltime) +{ + int remainder; + + reltime->tv_sec = ticks / TICK_PER_SEC; + remainder = ticks - TICK_PER_SEC * reltime->tv_sec; + reltime->tv_nsec = remainder * NSEC_PER_TICK; + return OK; +} diff --git a/nuttx/sched/clock/clock_time2ticks.c b/nuttx/sched/clock/clock_time2ticks.c new file mode 100644 index 000000000..c54cc949b --- /dev/null +++ b/nuttx/sched/clock/clock_time2ticks.c @@ -0,0 +1,136 @@ +/******************************************************************************** + * sched/clock/clock_time2ticks.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <gnutt@nuttx.org> + * + * 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 <nuttx/config.h> + +#include <stdint.h> +#include <time.h> + +#include "clock/clock.h" + +/******************************************************************************** + * Definitions + ********************************************************************************/ + +/******************************************************************************** + * Private Type Declarations + ********************************************************************************/ + +/******************************************************************************** + * Global Variables + ********************************************************************************/ + +/******************************************************************************** + * Private Variables + ********************************************************************************/ + +/******************************************************************************** + * Private Functions + ********************************************************************************/ + +/******************************************************************************** + * Public Functions + ********************************************************************************/ + +/******************************************************************************** + * Name: clock_time2ticks + * + * Description: + * Convert a timespec delay to system timer ticks. This function is suitable + * for calculating relative time delays and does not depend on the other + * clock_* logic. + * + * Parameters: + * reltime - Convert this relative time to system clock ticks. + * ticks - Return the converted number of ticks here. + * + * Return Value: + * Always returns OK + * + * Assumptions: + * + ********************************************************************************/ + +int clock_time2ticks(FAR const struct timespec *reltime, FAR int *ticks) +{ +#ifdef CONFIG_HAVE_LONG_LONG + int64_t relnsec; + + /* Convert the relative time into nanoseconds. The range of the int64_t is + * sufficiently large that there is no real need for range checking. + */ + + relnsec = (int64_t)reltime->tv_sec * NSEC_PER_SEC + + (int64_t)reltime->tv_nsec; + + /* Convert nanoseconds to clock ticks, rounding up to the smallest integer + * that is greater than or equal to the exact number of tick. + */ + + *ticks = (int)((relnsec + NSEC_PER_TICK - 1) / NSEC_PER_TICK); + return OK; +#else + int32_t relusec; + + /* This function uses an int32_t to only the relative time in microseconds. + * that means that the maximum supported relative time is 2,147,487.647 + * seconds + */ + +#if 0 // overkill + DEBUGASSERT(reltime->tv_sec < 2147487 || + reltime->tv_sec == 2147487 && + reltime->tv_nsec <= 647 * NSEC_PER_MSEC); +#endif + + /* Convert the relative time into microseconds, rounding up to the smallest + * value that is greater than or equal to the exact number of microseconds. + */ + + relusec = reltime->tv_sec * USEC_PER_SEC + + (reltime->tv_nsec + NSEC_PER_USEC - 1) / NSEC_PER_USEC; + + /* Convert microseconds to clock ticks, rounding up to the smallest integer + * that is greater than or equal to the exact number of tick. + */ + + *ticks = (int)((relusec + USEC_PER_TICK - 1) / USEC_PER_TICK); + return OK; +#endif +} |