diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index d774bb48c..a647207fc 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -2126,3 +2126,6 @@ time when the MCU is off and also for a low-resolution (1 sec/tick) RTC that can run until 2106 with no overflow. But in this latter case, higher resolution time must come from the system timer. + * CONFIG_SYSTEM_UTC - Removed support for the UTC system timer. It just + doesn't do enough to be worth the CPU cycles or the complexity. + * CONFIG_SYSTEM_TIME16 - Added support for an optional 64-bit system timer. \ No newline at end of file diff --git a/nuttx/Documentation/NuttxPortingGuide.html b/nuttx/Documentation/NuttxPortingGuide.html index d05eea239..e17a9c62e 100644 --- a/nuttx/Documentation/NuttxPortingGuide.html +++ b/nuttx/Documentation/NuttxPortingGuide.html @@ -1887,6 +1887,9 @@ The system can be re-made subsequently by just typing make. That timer interrupt runs at rate determined by CONFIG_MSEC_PER_TICKS (default 10 or 100Hz). The timer generates an interrupt each CONFIG_MSEC_PER_TICKS milliseconds and increments a counter called g_system_timer. g_system_timer then provides a time-base for calculating up-time and elapsed time intervals in units of CONFIG_MSEC_PER_TICKS. + The range of g_system_timer is, by default, 32-bits. + However, if the MCU supports type long long and CONFIG_SYSTEM_TIME16 is selected, + a 64-bit system timer will be supported instead.

System Timer Accuracy On many system, the exact timer interval specified by CONFIG_MSEC_PER_TICKS cannot be achieved due to limitations in frequencies or in dividers. @@ -1960,34 +1963,7 @@ else In this way, the timer interval is controlled from interrupt-to-interrupt to produce an average frequency of exactly 100Hz.

-

4.1.20.2 UTC Time Representation

-

- To enable UTC time representation use option: -

- -

- which adds the following variables: -

- -

- and replaces: -

- -

- Otherwise internal time is computed from 32-bit running tick timer - g_systemtimer the start date g_basetime and time offset the g_tickbias. -

- -

4.1.20.2 Hardware

+

4.1.20.1 Hardware

To enable hardware module use the following configuration options:

@@ -2014,47 +1990,29 @@ CONFIG_SYSTEM_UTC=y

  • up_rtc_time(). UTC time in seconds.
  • up_rtc_gettime(). Replacement for g_system_tick
  • -

    - This module depends on CONFIG_SYSTEM_UTC=y. -

    -

    4.1.20.3 System Tick and Time

    +

    4.1.20.2 System Tick and Time

    - The system tick is represented by, when CONFIG_SYSTEM_UTC=n: + The system tick is represented by::

    -

    - or, when CONFIG_SYSTEM_UTC=y -

    -

    Running at rate of system base timer, used for time-slicing, and so forth.

    - If hardware RTC is present (CONFIG_RTC) and and high-resolution timeing + If hardware RTC is present (CONFIG_RTC) and and high-resolution timing is enabled (CONFIG_RTC_HIRES), then after successful initiliazation variables are overriden by calls to up_rtc_gettime() which is running continously even in power-down modes.

    - In the case of CONFIG_RTC_HIRES is set the g_tickcount and - g_system_utc keep counting at rate of a system timer, which however, is - disabled in power-down mode. + In the case of CONFIG_RTC_HIRES is set the g_system_timer + keeps counting at rate of a system timer, which however, is disabled in power-down mode. By comparing this time and RTC (actual time) one may determine the actual system active time. To retrieve that variable use:

    - -

    - If the CLOCK_ACTIVETIME time is never set it will serve as power-up time - minus all deep sleeps. -

    4.2 APIs Exported by NuttX to Architecture-Specific Logic

    @@ -3765,6 +3723,12 @@ build task name to save in the TCB. Useful if scheduler instrumentation is selected. Set to zero to disable. +

  • + CONFIG_SYSTEM_TIME16: + The range of system time is, by default, 32-bits. + However, if the MCU supports type long long and CONFIG_SYSTEM_TIME16 is selected, + a 64-bit system timer will be supported instead. +
  • CONFIG_START_YEAR, CONFIG_START_MONTH, CONFIG_START_DAY - Used to initialize the internal time logic. diff --git a/nuttx/configs/vsn/nsh/defconfig b/nuttx/configs/vsn/nsh/defconfig index ddb8114ec..7c9a4ce68 100755 --- a/nuttx/configs/vsn/nsh/defconfig +++ b/nuttx/configs/vsn/nsh/defconfig @@ -269,7 +269,6 @@ CONFIG_WIRELESS=y CONFIG_RTC=y CONFIG_RTC_HIRES=y CONFIG_RTC_FREQUENCY=16384 -CONFIG_SYSTEM_UTC=y # # OS support for events diff --git a/nuttx/configs/vsn/src/sif.c b/nuttx/configs/vsn/src/sif.c index e185d8d0d..3c304a3b2 100644 --- a/nuttx/configs/vsn/src/sif.c +++ b/nuttx/configs/vsn/src/sif.c @@ -712,7 +712,7 @@ int sif_main(int argc, char *argv[]) fprintf(stderr, "%s:\tinit\n\tgpio\tA B\n\tpwr\tval\n", argv[0]); struct timespec t_active; - clock_gettime(CLOCK_ACTIVETIME, &t_active); + clock_gettime(CLOCK_REALTIME, &t_active); fprintf(stderr, "rtc time = %u, active = %u / %u, time / systick = %u / %u\n", up_rtc_time(), t_active.tv_sec, t_active.tv_nsec, diff --git a/nuttx/include/nuttx/clock.h b/nuttx/include/nuttx/clock.h index 4990c65b4..472ff3bfc 100644 --- a/nuttx/include/nuttx/clock.h +++ b/nuttx/include/nuttx/clock.h @@ -44,6 +44,7 @@ #include #include +#include /**************************************************************************** * Pro-processor Definitions @@ -63,6 +64,14 @@ # define __HAVE_KERNEL_GLOBALS 0 #endif +/* 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 + /* Timing constants *********************************************************/ #define NSEC_PER_SEC 1000000000 @@ -126,20 +135,18 @@ */ #if __HAVE_KERNEL_GLOBALS +# ifdef CONFIG_SYSTEM_TIME64 + +extern volatile uint64_t g_system_timer; +#define clock_systimer() (uint32_t)(g_system_timer & 0x00000000ffffffff) +#define clock_systimer64() g_system_timer + +# else + extern volatile uint32_t g_system_timer; -extern volatile uint32_t g_system_utc; - -#if TICK_PER_SEC > 32767 -extern volatile uint32_t g_tickcount; -#elif TICK_PER_SEC > 255 -extern volatile uint16_t g_tickcount; -#else -extern volatile uint8_t g_tickcount; -#endif -#endif /* __HAVE_KERNEL_GLOBALS */ - -#if !defined(CONFIG_SYSTEM_UTC) && __HAVE_KERNEL_GLOBALS #define clock_systimer() g_system_timer + +# endif #endif /**************************************************************************** @@ -157,10 +164,10 @@ extern "C" { * Function: clock_systimer * * Description: - * Return the current value of the system timer counter. Indirect access - * to the system timer counter is required through this function if (1) we - * are using a hardware periodic timer, OR (2) the execution environment - * does not have direct access to kernel global data + * Return the current value of the 32-bit system timer counter. Indirect + * access to the system timer counter is required through this function if + * the execution environment does not have direct access to kernel global + * data * * Parameters: * None @@ -172,8 +179,35 @@ extern "C" { * ****************************************************************************/ -#if defined(CONFIG_SYSTEM_UTC) || !__HAVE_KERNEL_GLOBALS +#if !__HAVE_KERNEL_GLOBALS +# ifdef CONFIG_SYSTEM_TIME64 +# define clock_systimer() (uint32_t)(clock_systimer64() & 0x00000000ffffffff) +# else EXTERN uint32_t clock_systimer(void); +# endif +#endif + +/**************************************************************************** + * Function: clock_systimer64 + * + * Description: + * Return the current value of the 64-bit system timer counter. Indirect + * access to the system timer counter is required through this function if + * the execution environment does not have direct access to kernel global + * data + * + * Parameters: + * None + * + * Return Value: + * The current value of the system timer counter + * + * Assumptions: + * + ****************************************************************************/ + +#if !__HAVE_KERNEL_GLOBALS && defined(CONFIG_SYSTEM_TIME64) +EXTERN uint64_t clock_systimer64(void); #endif #undef EXTERN diff --git a/nuttx/include/time.h b/nuttx/include/time.h index fae948402..7a76b7ef2 100644 --- a/nuttx/include/time.h +++ b/nuttx/include/time.h @@ -73,16 +73,6 @@ #define CLOCK_REALTIME 0 -/* Non-standard. Returns active UTC time, which is disabled during - * power down modes. Unit is 1 second. - */ - -#ifdef CONFIG_SYSTEM_UTC -# define CLOCK_ACTIVETIME 1 -#endif - -#define CLOCK_ABSTIME - /* This is a flag that may be passed to the timer_settime() function */ #define TIMER_ABSTIME 1 diff --git a/nuttx/sched/clock_gettime.c b/nuttx/sched/clock_gettime.c index fc3a1593a..b146949fb 100644 --- a/nuttx/sched/clock_gettime.c +++ b/nuttx/sched/clock_gettime.c @@ -91,127 +91,78 @@ int clock_gettime(clockid_t clock_id, struct timespec *tp) { -#ifndef CONFIG_SYSTEM_UTC +#ifdef CONFIG_SYSTEM_TIME64 + uint64_t msecs; + uint64_t secs; + uint64_t nsecs; +#else uint32_t msecs; uint32_t secs; uint32_t nsecs; -#else - uint32_t system_utc; - uint32_t tickcount; #endif -#if defined(CONFIG_RTC) || defined(CONFIG_SYSTEM_UTC) - irqstate_t flags; -#endif - 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. */ - if (clock_id == CLOCK_REALTIME && tp) + if (clock_id == CLOCK_REALTIME) { - /* If CONFIG_SYSTEM_UTC is not defined, then we have to get the time - * from g_system_timer. - */ - -#ifndef CONFIG_SYSTEM_UTC - - /* Get the elapsed time since power up (in milliseconds) biased - * as appropriate. - */ - - msecs = MSEC_PER_TICK * (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. */ - - secs += (uint32_t)g_basetime.tv_sec; - nsecs += (uint32_t)g_basetime.tv_nsec; - - /* Handle carry to seconds. */ - - if (nsecs > NSEC_PER_SEC) - { - uint32_t dwCarrySecs = nsecs / NSEC_PER_SEC; - secs += dwCarrySecs; - nsecs -= (dwCarrySecs * NSEC_PER_SEC); - } - - /* And return the result to the caller. */ - - tp->tv_sec = (time_t)secs; - tp->tv_nsec = (long)nsecs; - -#else /* CONFIG_SYSTEM_UTC */ - - /* CONFIG_SYSTEM_UTC is defined. But we might be able to get the time - * from the hardware if a high resolution RTC is available. - */ + /* Do we have a high-resolution RTC that can provie us with the time? */ #ifdef CONFIG_RTC_HIRES if (g_rtc_enabled) { - /* Get the hi-resolution time from the RTC */ + /* Yes.. Get the hi-resolution time from the RTC */ ret = up_rtc_gettime(tp); } else #endif { - /* Disable interrupts while g_system_utc and g_tickcount are sampled - * so that we can be assured that g_system_utc and g_tickcount are based - * at the same point in time. + /* Get the elapsed time since power up (in milliseconds) biased + * as appropriate. */ - flags = irqsave(); - system_utc = g_system_utc; - tickcount = g_tickcount; - irqrestore(flags); + msecs = MSEC_PER_TICK * (g_system_timer - g_tickbias); - tp->tv_sec = system_utc; - tp->tv_nsec = tickcount * (1000000000/TICK_PER_SEC); + 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. */ + + secs += (uint32_t)g_basetime.tv_sec; + nsecs += (uint32_t)g_basetime.tv_nsec; + + /* Handle carry to seconds. */ + + if (nsecs > NSEC_PER_SEC) + { + uint32_t dwCarrySecs = nsecs / NSEC_PER_SEC; + secs += dwCarrySecs; + nsecs -= (dwCarrySecs * NSEC_PER_SEC); + } + + /* And return the result to the caller. */ + + tp->tv_sec = (time_t)secs; + tp->tv_nsec = (long)nsecs; } -#endif /* CONFIG_SYSTEM_UTC */ 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_SYSTEM_UTC - else if (clock_id == CLOCK_ACTIVETIME && g_rtc_enabled && tp) - { - /* Disable interrupts while g_system_utc and g_tickcount are sampled - * so that we can be assured that g_system_utc and g_tickcount are based - * at the same point in time. - */ - - flags = irqsave(); - system_utc = g_system_utc; - tickcount = g_tickcount; - irqrestore(flags); - - tp->tv_sec = system_utc; - tp->tv_nsec = tickcount * (1000000000/TICK_PER_SEC); - } -#endif - else { sdbg("Returning ERROR\n"); diff --git a/nuttx/sched/clock_initialize.c b/nuttx/sched/clock_initialize.c index ed3a25828..2ffdd5819 100644 --- a/nuttx/sched/clock_initialize.c +++ b/nuttx/sched/clock_initialize.c @@ -38,6 +38,7 @@ ****************************************************************************/ #include +#include #include #include @@ -53,27 +54,12 @@ /**************************************************************************** * Definitions ****************************************************************************/ - -#ifdef CONFIG_RTC -# ifndef CONFIG_SYSTEM_UTC -# error "In order to support hardware RTC system must have set the CONFIG_SYSTEM_UTC=y" -# endif -#endif - /* 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) -/* Macro to increment the system timer -- or not */ - -#ifndef CONFIG_SYSTEM_UTC -# define incr_systimer() g_system_timer++ -#else -# define incr_systimer() -#endif - /**************************************************************************** * Private Type Declarations ****************************************************************************/ @@ -90,59 +76,23 @@ * Public Variables ****************************************************************************/ -#if CONFIG_SYSTEM_UTC -volatile time_t g_system_utc; +#ifdef CONFIG_SYSTEM_TIME64 +volatile uint64_t g_system_timer; +uint64_t g_tickbias; #else -volatile clock_t g_system_timer; -struct timespec g_basetime; -uint32_t g_tickbias; +volatile uint32_t g_system_timer; +uint32_t g_tickbias; #endif +struct timespec g_basetime; + /************************************************************************** * Private Variables **************************************************************************/ -/* This variable is used to count ticks and to increment the one-second - * UTC variable. - */ - -#if CONFIG_SYSTEM_UTC -#if TICK_PER_SEC > 32767 -volatile uint32_t g_tickcount; -#elif TICK_PER_SEC > 255 -volatile uint16_t g_tickcount; -#else -volatile uint8_t g_tickcount; -#endif -#endif /* CONFIG_SYSTEM_UTC */ - /************************************************************************** * Private Functions **************************************************************************/ -/**************************************************************************** - * Function: incr_utc - * - * 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_SYSTEM_UTC -static inline void incr_utc(void) -{ - g_tickcount++; - - if (g_tickcount >= TICK_PER_SEC) - { - g_system_utc++; - g_tickcount -= TICK_PER_SEC; - } -} -#else -# define incr_utc() -#endif /**************************************************************************** * Function: clock_inittime @@ -210,27 +160,17 @@ static inline void clock_inittime(FAR struct timespec *tp) void clock_initialize(void) { -#ifdef CONFIG_SYSTEM_UTC - struct timespec ts; -#endif - /* Initialize the RTC hardware */ #ifdef CONFIG_RTC up_rtcinitialize(); #endif - /* Initialize the time value */ + /* Initialize the time value to match */ -#ifdef CONFIG_SYSTEM_UTC - clock_inittime(&ts); - g_system_utc = ts.tv_sec; - g_tickcount = ((ts.tv_nsec > 10) * CLOCKS_PER_SEC) / (1000000000 >> 10); -#else clock_inittime(&g_basetime); g_system_timer = 0; g_tickbias = 0; -#endif } /**************************************************************************** @@ -247,9 +187,5 @@ void clock_timer(void) { /* Increment the per-tick system counter */ - incr_systimer(); - - /* Increment the per-second UTC counter */ - - incr_utc(); + g_system_timer++; } diff --git a/nuttx/sched/clock_internal.h b/nuttx/sched/clock_internal.h index 1752aae7b..dadf5d66a 100644 --- a/nuttx/sched/clock_internal.h +++ b/nuttx/sched/clock_internal.h @@ -43,12 +43,21 @@ #include #include + #include #include /******************************************************************************** - * Definitions + * 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 @@ -58,8 +67,13 @@ * Global Variables ********************************************************************************/ -extern struct timespec g_basetime; +#ifdef CONFIG_SYSTEM_TIME64 +extern uint64_t g_tickbias; +#else extern uint32_t g_tickbias; +#endif + +extern struct timespec g_basetime; /******************************************************************************** * Public Function Prototypes diff --git a/nuttx/sched/clock_settime.c b/nuttx/sched/clock_settime.c index 299e8c65b..f8bff78e3 100644 --- a/nuttx/sched/clock_settime.c +++ b/nuttx/sched/clock_settime.c @@ -89,17 +89,25 @@ 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. */ - if (clock_id == CLOCK_REALTIME && tp) + if (clock_id == CLOCK_REALTIME) { -#ifndef CONFIG_SYSTEM_UTC + /* 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; @@ -109,55 +117,22 @@ int clock_settime(clockid_t clock_id, FAR const struct timespec *tp) * as appropriate. */ - g_tickbias = clock_systimer(); - -#else /* if CONFIG_SYSTEM_UTC=y */ + g_tickbias = g_system_timer; - /* We ignore everything below one second in time configuration */ + /* Setup the RTC (lo- or high-res) */ #ifdef CONFIG_RTC if (g_rtc_enabled) { up_rtc_settime(tp); } - else -#endif - { - g_system_utc = tp->tv_sec; - } #endif + irqrestore(flags); 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_SYSTEM_UTC - else if (clock_id == CLOCK_ACTIVETIME && tp) - { - irqstate_t flags; - uint32_t tickcount; - - /* Calculate the number of ticks correspond to the nanosecond count... - * exercising care to avoid overflows. This could still overflow - * if CLOCKS_PER_SEC is very large (something like 4096). - */ - - tickcount = ((tp->tv_nsec >> 10) * CLOCKS_PER_SEC) / (1000000000 >> 10); - - /* Then set the UTC time (seconds) plus the tickcount (fractional seconds */ - - flags = irqsave(); - g_system_utc = tp->tv_sec; - g_tickcount = tickcount; - irqrestore(flags); - } -#endif - else { sdbg("Returning ERROR\n"); diff --git a/nuttx/sched/clock_systimer.c b/nuttx/sched/clock_systimer.c index 2061b1de6..6a2a9e24e 100644 --- a/nuttx/sched/clock_systimer.c +++ b/nuttx/sched/clock_systimer.c @@ -42,12 +42,8 @@ #include #include -#include -#include -#include - -#if !defined(clock_systimer) /* See nuttx/clock.h */ +#include "clock_internal.h" /**************************************************************************** * Pre-processor Definitions @@ -65,7 +61,7 @@ * Function: clock_systimer * * Description: - * Return the current value of the system timer counter + * Return the current value of the 32-bit system timer counter * * Parameters: * None @@ -77,38 +73,38 @@ * ****************************************************************************/ +#if !defined(clock_systimer) /* See nuttx/clock.h */ uint32_t clock_systimer(void) { -#ifdef CONFIG_SYSTEM_UTC - irqstate_t flags; - uint32_t system_utc; - uint32_t tickcount; -#endif - -#ifdef CONFIG_RTC_HIRES - /* Fetch the g_system_timer value from timer hardware, if available. - * - * Note that the unit of the g_system_timer and and up_rtc_gettime() do - * not have the same unit. - */ -#endif - -#ifndef CONFIG_SYSTEM_UTC - return g_system_timer; +#ifdef CONFIG_SYSTEM_TIME64 + return (uint32_t)(g_system_timer & 0x00000000ffffffff); #else - /* Disable interrupts while g_system_utc and g_tickcount are sampled - * so that we can be assured that g_system_utc and g_tickcount are based - * at the same point in time. - */ - - flags = irqsave(); - system_utc = g_system_utc; - tickcount = g_tickcount; - irqrestore(flags); - - return system_utc * TICK_PER_SEC + tickcount; + return g_system_timer; #endif } +#endif -#endif /* !clock_systtimer */ +/**************************************************************************** + * Function: 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) +{ + return g_system_timer; +} +#endif +#endif