2009-07-11 19:24:14 +02:00
|
|
|
/****************************************************************************
|
2014-08-08 22:43:02 +02:00
|
|
|
* sched/clock/clock_initialize.c
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2018-03-12 20:36:06 +01:00
|
|
|
* Copyright (C) 2007, 2009, 2011-2012, 2017-2018 Gregory Nutt. All rights
|
|
|
|
* reserved.
|
2012-03-22 15:52:46 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-07-11 19:24:14 +02:00
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
2007-02-18 00:21:28 +01:00
|
|
|
* 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.
|
|
|
|
*
|
2009-07-11 19:24:14 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-07-11 19:24:14 +02:00
|
|
|
/****************************************************************************
|
2007-02-18 00:21:28 +01:00
|
|
|
* Included Files
|
2009-07-11 19:24:14 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-07-11 19:24:14 +02:00
|
|
|
#include <nuttx/config.h>
|
2011-10-02 16:16:30 +02:00
|
|
|
#include <nuttx/compiler.h>
|
2009-07-11 19:24:14 +02:00
|
|
|
|
2009-12-14 19:39:29 +01:00
|
|
|
#include <stdint.h>
|
2007-02-18 00:21:28 +01:00
|
|
|
#include <time.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
2011-04-15 16:57:53 +02:00
|
|
|
|
2012-03-22 15:52:46 +01:00
|
|
|
#ifdef CONFIG_RTC
|
2016-02-14 15:17:46 +01:00
|
|
|
# include <nuttx/irq.h>
|
2012-03-22 15:52:46 +01:00
|
|
|
#endif
|
|
|
|
|
2015-02-13 15:41:34 +01:00
|
|
|
#include <nuttx/arch.h>
|
2011-04-15 16:57:53 +02:00
|
|
|
#include <nuttx/clock.h>
|
2009-07-11 19:24:14 +02:00
|
|
|
#include <nuttx/time.h>
|
|
|
|
|
2014-08-08 22:43:02 +02:00
|
|
|
#include "clock/clock.h"
|
2016-07-11 14:54:02 +02:00
|
|
|
#ifdef CONFIG_CLOCK_TIMEKEEPING
|
2016-07-11 00:14:25 +02:00
|
|
|
# include "clock/clock_timekeeping.h"
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2009-07-11 19:24:14 +02:00
|
|
|
/****************************************************************************
|
2015-10-03 00:30:35 +02:00
|
|
|
* Public Data
|
2009-07-11 19:24:14 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 02:26:16 +02:00
|
|
|
#ifndef CONFIG_SCHED_TICKLESS
|
2011-10-02 16:16:30 +02:00
|
|
|
#ifdef CONFIG_SYSTEM_TIME64
|
|
|
|
volatile uint64_t g_system_timer;
|
2011-05-06 23:10:00 +02:00
|
|
|
#else
|
2011-10-02 16:16:30 +02:00
|
|
|
volatile uint32_t g_system_timer;
|
2014-08-07 02:26:16 +02:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2017-04-21 16:45:57 +02:00
|
|
|
#ifndef CONFIG_CLOCK_TIMEKEEPING
|
2011-10-02 16:16:30 +02:00
|
|
|
struct timespec g_basetime;
|
|
|
|
|
2017-04-21 16:45:57 +02:00
|
|
|
#ifdef CONFIG_CLOCK_MONOTONIC
|
|
|
|
struct timespec g_monotonic_basetime;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2015-10-03 01:43:18 +02:00
|
|
|
/****************************************************************************
|
2011-04-15 16:57:53 +02:00
|
|
|
* Private Functions
|
2015-10-03 01:43:18 +02:00
|
|
|
****************************************************************************/
|
2011-04-15 16:57:53 +02:00
|
|
|
|
2009-07-11 19:24:14 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: clock_basetime
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2011-10-02 00:09:00 +02:00
|
|
|
* Get the initial time value from the best source available.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-07-11 19:24:14 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-10-02 00:09:00 +02:00
|
|
|
#ifdef CONFIG_RTC
|
2011-12-14 15:47:42 +01:00
|
|
|
#if defined(CONFIG_RTC_DATETIME)
|
|
|
|
/* Initialize the system time using a broken out date/time structure */
|
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
static inline int clock_basetime(FAR struct timespec *tp)
|
2011-12-14 15:47:42 +01:00
|
|
|
{
|
|
|
|
struct tm rtctime;
|
2017-04-21 16:45:57 +02:00
|
|
|
long nsecs = 0;
|
2015-12-21 21:39:40 +01:00
|
|
|
int ret;
|
2011-12-14 15:47:42 +01:00
|
|
|
|
|
|
|
/* Get the broken-out time from the date/time RTC. */
|
|
|
|
|
2017-04-21 16:45:57 +02:00
|
|
|
#ifdef CONFIG_ARCH_HAVE_RTC_SUBSECONDS
|
|
|
|
ret = up_rtc_getdatetime_with_subseconds(&rtctime, &nsecs);
|
|
|
|
#else
|
2015-12-21 21:39:40 +01:00
|
|
|
ret = up_rtc_getdatetime(&rtctime);
|
2017-04-21 16:45:57 +02:00
|
|
|
#endif
|
2015-12-21 21:39:40 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
/* And use the broken-out time to initialize the system time */
|
2011-12-14 15:47:42 +01:00
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
tp->tv_sec = mktime(&rtctime);
|
2017-04-21 16:45:57 +02:00
|
|
|
tp->tv_nsec = nsecs;
|
2015-12-21 21:39:40 +01:00
|
|
|
}
|
2011-12-14 15:47:42 +01:00
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
return ret;
|
2011-12-14 15:47:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(CONFIG_RTC_HIRES)
|
|
|
|
|
|
|
|
/* Initialize the system time using a high-resolution structure */
|
2011-10-02 00:09:00 +02:00
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
static inline int clock_basetime(FAR struct timespec *tp)
|
2007-02-18 00:21:28 +01:00
|
|
|
{
|
2011-10-02 00:09:00 +02:00
|
|
|
/* Get the complete time from the hi-res RTC. */
|
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
return up_rtc_gettime(tp);
|
2011-10-02 00:09:00 +02:00
|
|
|
}
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2011-05-06 23:10:00 +02:00
|
|
|
#else
|
2011-04-15 18:20:25 +02:00
|
|
|
|
2011-12-14 15:47:42 +01:00
|
|
|
/* Initialize the system time using seconds only */
|
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
static inline int clock_basetime(FAR struct timespec *tp)
|
2011-10-02 00:09:00 +02:00
|
|
|
{
|
2011-12-14 15:47:42 +01:00
|
|
|
/* Get the seconds (only) from the lo-resolution RTC */
|
2011-04-15 18:20:25 +02:00
|
|
|
|
2011-10-02 00:09:00 +02:00
|
|
|
tp->tv_sec = up_rtc_time();
|
|
|
|
tp->tv_nsec = 0;
|
2015-12-21 21:39:40 +01:00
|
|
|
return OK;
|
2011-10-02 00:09:00 +02:00
|
|
|
}
|
2011-05-06 23:10:00 +02:00
|
|
|
|
2011-10-02 00:09:00 +02:00
|
|
|
#endif /* CONFIG_RTC_HIRES */
|
|
|
|
#else /* CONFIG_RTC */
|
2011-05-06 23:10:00 +02:00
|
|
|
|
2015-12-21 21:39:40 +01:00
|
|
|
static inline int clock_basetime(FAR struct timespec *tp)
|
2011-10-02 00:09:00 +02:00
|
|
|
{
|
|
|
|
time_t jdn = 0;
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Get the EPOCH-relative julian date from the calendar year,
|
|
|
|
* month, and date
|
|
|
|
*/
|
|
|
|
|
2016-10-27 16:36:24 +02:00
|
|
|
jdn = clock_calendar2utc(CONFIG_START_YEAR, CONFIG_START_MONTH - 1,
|
2011-05-06 23:10:00 +02:00
|
|
|
CONFIG_START_DAY);
|
2007-02-18 00:21:28 +01:00
|
|
|
|
|
|
|
/* Set the base time as seconds into this julian day. */
|
|
|
|
|
2011-10-02 00:09:00 +02:00
|
|
|
tp->tv_sec = jdn * SEC_PER_DAY;
|
|
|
|
tp->tv_nsec = 0;
|
2015-12-21 21:39:40 +01:00
|
|
|
return OK;
|
2011-10-02 00:09:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_RTC */
|
|
|
|
|
2012-03-22 15:52:46 +01:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: clock_inittime
|
2012-03-22 15:52:46 +01:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Get the initial time value from the best source available.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static void clock_inittime(void)
|
|
|
|
{
|
2013-02-04 23:38:59 +01:00
|
|
|
/* (Re-)initialize the time value to match the RTC */
|
2012-03-22 15:52:46 +01:00
|
|
|
|
2016-07-11 14:54:02 +02:00
|
|
|
#ifndef CONFIG_CLOCK_TIMEKEEPING
|
2016-07-11 00:14:25 +02:00
|
|
|
#ifndef CONFIG_RTC_HIRES
|
|
|
|
clock_basetime(&g_basetime);
|
|
|
|
#endif
|
2014-08-07 02:26:16 +02:00
|
|
|
#ifndef CONFIG_SCHED_TICKLESS
|
2017-04-21 16:55:07 +02:00
|
|
|
g_system_timer = INITIAL_SYSTEM_TIMER_TICKS;
|
|
|
|
if (g_system_timer > 0)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
(void)clock_ticks2time((ssystime_t)g_system_timer, &ts);
|
|
|
|
|
|
|
|
/* Adjust base time to hide initial timer ticks. */
|
|
|
|
|
|
|
|
g_basetime.tv_sec -= ts.tv_sec;
|
|
|
|
g_basetime.tv_nsec -= ts.tv_nsec;
|
|
|
|
while (g_basetime.tv_nsec < 0)
|
|
|
|
{
|
|
|
|
g_basetime.tv_nsec += NSEC_PER_SEC;
|
|
|
|
g_basetime.tv_sec--;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CLOCK_MONOTONIC
|
|
|
|
/* Adjust monotonic clock offset to hide initial timer ticks. */
|
|
|
|
|
|
|
|
g_monotonic_basetime.tv_sec -= ts.tv_sec;
|
|
|
|
g_monotonic_basetime.tv_nsec -= ts.tv_nsec;
|
|
|
|
while (g_monotonic_basetime.tv_nsec < 0)
|
|
|
|
{
|
|
|
|
g_monotonic_basetime.tv_nsec += NSEC_PER_SEC;
|
|
|
|
g_monotonic_basetime.tv_sec--;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CLOCK_MONOTONIC */
|
|
|
|
}
|
|
|
|
#endif /* !CONFIG_SCHED_TICKLESS */
|
2016-07-11 00:14:25 +02:00
|
|
|
#else
|
|
|
|
clock_inittimekeeping();
|
|
|
|
#endif
|
2012-03-22 15:52:46 +01:00
|
|
|
}
|
2011-10-02 00:09:00 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: clock_initialize
|
2011-10-02 00:09:00 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Perform one-time initialization of the timing facilities.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void clock_initialize(void)
|
|
|
|
{
|
2015-11-19 22:12:40 +01:00
|
|
|
#if defined(CONFIG_RTC) && !defined(CONFIG_RTC_EXTERNAL)
|
2015-11-19 21:54:30 +01:00
|
|
|
/* Initialize the internal RTC hardware. Initialization of external RTC
|
|
|
|
* must be deferred until the system has booted.
|
|
|
|
*/
|
2011-10-02 00:09:00 +02:00
|
|
|
|
2015-11-20 15:27:52 +01:00
|
|
|
up_rtc_initialize();
|
2011-10-02 00:09:00 +02:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-03-22 15:52:46 +01:00
|
|
|
/* Initialize the time value to match the RTC */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2012-03-22 15:52:46 +01:00
|
|
|
clock_inittime();
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: clock_synchronize
|
2012-03-22 15:52:46 +01:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-03-22 15:52:46 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RTC
|
|
|
|
void clock_synchronize(void)
|
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Re-initialize the time value to match the RTC */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2012-03-22 15:52:46 +01:00
|
|
|
clock_inittime();
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2012-03-22 15:52:46 +01:00
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2017-04-21 16:45:57 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: clock_resynchronize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Resynchronize the system timer to a hardware RTC. The user can
|
|
|
|
* 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. Function also keeps monotonic clock at rate of RTC.
|
|
|
|
*
|
|
|
|
* Calling this function will not result in system time going "backward" in
|
|
|
|
* time. If setting system time with RTC would result time going "backward"
|
|
|
|
* then resynchronization is not performed.
|
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* rtc_diff: amount of time system-time is adjusted forward with RTC
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2017-04-21 16:45:57 +02:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS) && \
|
|
|
|
!defined(CONFIG_CLOCK_TIMEKEEPING)
|
|
|
|
void clock_resynchronize(FAR struct timespec *rtc_diff)
|
|
|
|
{
|
|
|
|
struct timespec rtc_time, bias, curr_ts;
|
|
|
|
struct timespec rtc_diff_tmp;
|
|
|
|
irqstate_t flags;
|
|
|
|
int32_t carry;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (rtc_diff == NULL)
|
|
|
|
{
|
|
|
|
rtc_diff = &rtc_diff_tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the time value to match the RTC */
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
|
|
|
/* Get RTC time */
|
|
|
|
|
|
|
|
ret = clock_basetime(&rtc_time);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
/* Error reading RTC, skip resynchronization. */
|
|
|
|
|
|
|
|
sinfo("rtc error %d, skip resync\n", ret);
|
|
|
|
|
|
|
|
rtc_diff->tv_sec = 0;
|
|
|
|
rtc_diff->tv_nsec = 0;
|
|
|
|
goto skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the elapsed time since power up (in milliseconds). This is a
|
|
|
|
* bias value that we need to use to correct the base time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
(void)clock_systimespec(&bias);
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
curr_ts.tv_sec = bias.tv_sec + g_basetime.tv_sec;
|
|
|
|
curr_ts.tv_nsec = bias.tv_nsec + g_basetime.tv_nsec;
|
|
|
|
|
|
|
|
/* Handle carry to seconds. */
|
|
|
|
|
|
|
|
if (curr_ts.tv_nsec >= NSEC_PER_SEC)
|
|
|
|
{
|
|
|
|
carry = curr_ts.tv_nsec / NSEC_PER_SEC;
|
|
|
|
curr_ts.tv_sec += carry;
|
|
|
|
curr_ts.tv_nsec -= (carry * NSEC_PER_SEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if RTC has advanced past system time. */
|
|
|
|
|
|
|
|
if (curr_ts.tv_sec > rtc_time.tv_sec ||
|
|
|
|
(curr_ts.tv_sec == rtc_time.tv_sec && curr_ts.tv_nsec >= rtc_time.tv_nsec))
|
|
|
|
{
|
|
|
|
/* Setting system time with RTC now would result time going
|
|
|
|
* backwards. Skip resynchronization.
|
|
|
|
*/
|
|
|
|
|
|
|
|
sinfo("skip resync\n");
|
|
|
|
|
|
|
|
rtc_diff->tv_sec = 0;
|
|
|
|
rtc_diff->tv_nsec = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Save RTC time as the new base time. */
|
|
|
|
|
|
|
|
g_basetime.tv_sec = rtc_time.tv_sec;
|
|
|
|
g_basetime.tv_nsec = rtc_time.tv_nsec;
|
|
|
|
|
|
|
|
/* Subtract that bias from the basetime so that when the system
|
|
|
|
* timer is again added to the base time, the result is the current
|
|
|
|
* time relative to basetime.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (g_basetime.tv_nsec < bias.tv_nsec)
|
|
|
|
{
|
|
|
|
g_basetime.tv_nsec += NSEC_PER_SEC;
|
|
|
|
g_basetime.tv_sec--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Result could be negative seconds */
|
|
|
|
|
|
|
|
g_basetime.tv_nsec -= bias.tv_nsec;
|
|
|
|
g_basetime.tv_sec -= bias.tv_sec;
|
|
|
|
|
|
|
|
sinfo("basetime=(%ld,%lu) bias=(%ld,%lu)\n",
|
|
|
|
(long)g_basetime.tv_sec, (unsigned long)g_basetime.tv_nsec,
|
|
|
|
(long)bias.tv_sec, (unsigned long)bias.tv_nsec);
|
|
|
|
|
|
|
|
/* Output difference between time at entry and new current time. */
|
|
|
|
|
|
|
|
rtc_diff->tv_sec = (bias.tv_sec + g_basetime.tv_sec) - curr_ts.tv_sec;
|
|
|
|
rtc_diff->tv_nsec = (bias.tv_nsec + g_basetime.tv_nsec) - curr_ts.tv_nsec;
|
|
|
|
|
|
|
|
/* Handle carry to seconds. */
|
|
|
|
|
|
|
|
if (rtc_diff->tv_nsec < 0)
|
|
|
|
{
|
|
|
|
carry = -((-(rtc_diff->tv_nsec + 1)) / NSEC_PER_SEC + 1);
|
|
|
|
}
|
|
|
|
else if (rtc_diff->tv_nsec >= NSEC_PER_SEC)
|
|
|
|
{
|
|
|
|
carry = rtc_diff->tv_nsec / NSEC_PER_SEC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
carry = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (carry)
|
|
|
|
{
|
|
|
|
rtc_diff->tv_sec += carry;
|
|
|
|
rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_CLOCK_MONOTONIC
|
|
|
|
/* Monotonic clock follows wall time since system start-up. Adjust
|
|
|
|
* CLOCK_MONOTONIC same amount as CLOCK_REALTIME.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_monotonic_basetime.tv_sec += (uint32_t)rtc_diff->tv_sec;
|
|
|
|
g_monotonic_basetime.tv_nsec += (uint32_t)rtc_diff->tv_nsec;
|
|
|
|
|
|
|
|
/* Handle carry to seconds. */
|
|
|
|
|
|
|
|
if (g_monotonic_basetime.tv_nsec >= NSEC_PER_SEC)
|
|
|
|
{
|
|
|
|
carry = g_monotonic_basetime.tv_nsec / NSEC_PER_SEC;
|
|
|
|
g_monotonic_basetime.tv_sec += carry;
|
|
|
|
g_monotonic_basetime.tv_nsec -= (carry * NSEC_PER_SEC);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
skip:
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-07-11 19:24:14 +02:00
|
|
|
/****************************************************************************
|
2012-07-14 21:30:31 +02:00
|
|
|
* Name: clock_timer
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2012-07-14 21:30:31 +02:00
|
|
|
* This function must be called once every time the real time clock
|
|
|
|
* interrupt occurs. The interval of this clock interrupt must be
|
2014-08-08 02:00:38 +02:00
|
|
|
* USEC_PER_TICK
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2009-07-11 19:24:14 +02:00
|
|
|
****************************************************************************/
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2014-08-07 02:26:16 +02:00
|
|
|
#ifndef CONFIG_SCHED_TICKLESS
|
2007-02-18 00:21:28 +01:00
|
|
|
void clock_timer(void)
|
|
|
|
{
|
2011-04-15 16:57:53 +02:00
|
|
|
/* Increment the per-tick system counter */
|
|
|
|
|
2011-10-02 16:16:30 +02:00
|
|
|
g_system_timer++;
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2014-08-08 02:00:38 +02:00
|
|
|
#endif
|