summaryrefslogtreecommitdiff
path: root/nuttx/sched/clock_initialize.c
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/clock_initialize.c
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/clock_initialize.c')
-rw-r--r--nuttx/sched/clock_initialize.c77
1 files changed, 73 insertions, 4 deletions
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();
}