mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 00:14:22 +08:00
Add time and uptime
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3506 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user