summaryrefslogtreecommitdiff
path: root/nuttx/sched
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-04-15 14:57:53 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2011-04-15 14:57:53 +0000
commitf1893cbaf513c7f0fbca77240fc59707ad039734 (patch)
tree4a5128533aa866afa19719584a764f0f83b9d165 /nuttx/sched
parentc548df2a3788bfe7527f7fef439d1365cb095e7c (diff)
downloadpx4-nuttx-f1893cbaf513c7f0fbca77240fc59707ad039734.tar.gz
px4-nuttx-f1893cbaf513c7f0fbca77240fc59707ad039734.tar.bz2
px4-nuttx-f1893cbaf513c7f0fbca77240fc59707ad039734.zip
Add time and uptime
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3506 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/sched')
-rw-r--r--nuttx/sched/Makefile3
-rw-r--r--nuttx/sched/clock_initialize.c77
-rw-r--r--nuttx/sched/clock_systimer.c8
-rw-r--r--nuttx/sched/clock_uptime.c84
4 files changed, 163 insertions, 9 deletions
diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile
index 3c89a5c32..20e3522d0 100644
--- a/nuttx/sched/Makefile
+++ b/nuttx/sched/Makefile
@@ -82,6 +82,9 @@ CLOCK_SRCS = clock_initialize.c clock_settime.c clock_gettime.c clock_getres.c \
ifeq ($(CONFIG_NUTTX_KERNEL),y)
CLOCK_SRCS += clock_systimer.c
+ifeq ($(CONFIG_UPTIME),y)
+CLOCK_SRCS += clock_uptime.c
+endif
endif
SIGNAL_SRCS = sig_initialize.c \
diff --git a/nuttx/sched/clock_initialize.c b/nuttx/sched/clock_initialize.c
index baddc3136..e18b819fa 100644
--- a/nuttx/sched/clock_initialize.c
+++ b/nuttx/sched/clock_initialize.c
@@ -43,6 +43,8 @@
#include <time.h>
#include <errno.h>
#include <debug.h>
+
+#include <nuttx/clock.h>
#include <nuttx/time.h>
#include "clock_internal.h"
@@ -55,6 +57,12 @@
#define SEC_PER_HOUR ((time_t)60 * SEC_PER_MIN)
#define SEC_PER_DAY ((time_t)24 * SEC_PER_HOUR)
+#if __HAVE_SYSTEM_COUNTER
+# define incr_systimer() g_system_timer++
+#else
+# define incr_systimer()
+#endif
+
/****************************************************************************
* Private Type Declarations
****************************************************************************/
@@ -71,14 +79,63 @@
* Public Variables
****************************************************************************/
-volatile uint32_t g_system_timer = 0;
-struct timespec g_basetime = {0,0};
-uint32_t g_tickbias = 0;
+#if __HAVE_SYSTEM_COUNTER
+volatile clock_t g_system_timer = 0;
+#endif
+
+#if CONFIG_UPTIME
+volatile time_t g_uptime = 0;
+#endif
+
+struct timespec g_basetime = {0,0};
+uint32_t g_tickbias = 0;
/**************************************************************************
* Private Variables
**************************************************************************/
+/* This variable is used to count ticks and to increment the one-second
+ * uptime variable.
+ */
+
+#if CONFIG_UPTIME
+#if TICK_PER_SEC > 32767
+static uint32_t g_tickcount = 0;
+#elif TICK_PER_SEC > 255
+static uint16_t g_tickcount = 0;
+#else
+static uint8_t g_tickcount = 0;
+#endif
+#endif /* CONFIG_UPTIME */
+
+/**************************************************************************
+ * Private Functions
+ **************************************************************************/
+/****************************************************************************
+ * Function: 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 MSEC_PER_TICK
+ *
+ ****************************************************************************/
+
+#if CONFIG_UPTIME
+static inline void incr_uptime(void)
+{
+ g_tickcount++;
+
+ if (g_tickcount >= TICK_PER_SEC)
+ {
+ g_uptime++;
+ g_tickcount -= TICK_PER_SEC;
+ }
+}
+#else
+# define incr_uptime()
+#endif
+
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -101,14 +158,20 @@ void clock_initialize(void)
/* Initialize the real time close */
+#if __HAVE_SYSTEM_COUNTER
g_system_timer = 0;
+#endif
/* Get the EPOCH-relative julian date from the calendar year,
* month, and date
*/
+#ifndef CONFIG_PTIMER
jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH,
CONFIG_START_DAY);
+#else /* use UTC as starting date */
+ jdn = clock_calendar2utc(1970, 1, 1);
+#endif
/* Set the base time as seconds into this julian day. */
@@ -132,5 +195,11 @@ void clock_initialize(void)
void clock_timer(void)
{
- g_system_timer++;
+ /* Increment the per-tick system counter */
+
+ incr_systimer();
+
+ /* Increment the per-second uptime counter */
+
+ incr_uptime();
}
diff --git a/nuttx/sched/clock_systimer.c b/nuttx/sched/clock_systimer.c
index 921f9ebc5..5e18f53fd 100644
--- a/nuttx/sched/clock_systimer.c
+++ b/nuttx/sched/clock_systimer.c
@@ -42,15 +42,12 @@
#include <stdint.h>
#include <nuttx/clock.h>
-#if !defined(CONFIG_DISABLE_CLOCK) && \
- defined(CONFIG_NUTTX_KERNEL) && !defined(__KERNEL__)
+#if __HAVE_SYSTEM_COUNTER && !defined(clock_systimer) /* See nuttx/clock.h */
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
-#undef clock_systimer
-
/****************************************************************************
* Private Data
****************************************************************************/
@@ -79,5 +76,6 @@ uint32_t clock_systimer(void)
{
return g_system_timer;
}
-#endif
+
+#endif /* __HAVE_SYSTEM_COUNTER */
diff --git a/nuttx/sched/clock_uptime.c b/nuttx/sched/clock_uptime.c
new file mode 100644
index 000000000..ae580ed17
--- /dev/null
+++ b/nuttx/sched/clock_uptime.c
@@ -0,0 +1,84 @@
+/****************************************************************************
+ * sched/clock_uptime.c
+ *
+ * Copyright (C) 2011 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
+ *
+ * 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 <nuttx/time.h>
+
+#if !defined(CONFIG_DISABLE_CLOCK) && defined(CONFIG_UPTIME)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#undef clock_uptime
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Function: clock_uptime
+ *
+ * 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_uptime(void)
+{
+ return g_uptime;
+}
+
+#endif /* CONFIG_DISABLE_CLOCK && CONFIG_UPTIME */