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
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
2021-02-08 16:33:58 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2007-02-18 00:21:28 +01:00
|
|
|
*
|
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
|
2022-09-27 06:03:48 +02:00
|
|
|
volatile uint64_t g_system_ticks = INITIAL_SYSTEM_TIMER_TICKS;
|
2011-05-06 23:10:00 +02:00
|
|
|
#else
|
2022-09-27 06:03:48 +02:00
|
|
|
volatile uint32_t g_system_ticks = INITIAL_SYSTEM_TIMER_TICKS;
|
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
|
|
|
#endif
|
|
|
|
|
2023-04-24 13:45:56 +02:00
|
|
|
#ifdef CONFIG_CLOCK_ADJTIME
|
|
|
|
long long g_clk_adj_usec;
|
|
|
|
long long g_clk_adj_count;
|
|
|
|
#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 */
|
|
|
|
|
2018-11-12 14:00:41 +01:00
|
|
|
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
|
|
|
|
2021-06-21 18:44:48 +02:00
|
|
|
tp->tv_sec = timegm(&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
|
|
|
|
2018-11-12 14:00:41 +01:00
|
|
|
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 */
|
|
|
|
|
2018-11-12 14:00:41 +01:00
|
|
|
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
|
|
|
|
2018-11-12 14:00:41 +01:00
|
|
|
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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-02-07 08:14:06 +01:00
|
|
|
#ifdef CONFIG_RTC
|
2022-01-13 15:01:06 +01:00
|
|
|
static void clock_inittime(FAR const struct timespec *tp)
|
2012-03-22 15:52:46 +01:00
|
|
|
{
|
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
|
2020-02-07 08:14:06 +01:00
|
|
|
struct timespec ts;
|
2019-10-24 18:49:28 +02:00
|
|
|
|
2022-01-13 15:01:06 +01:00
|
|
|
if (tp)
|
|
|
|
{
|
|
|
|
memcpy(&g_basetime, tp, sizeof(struct timespec));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clock_basetime(&g_basetime);
|
|
|
|
}
|
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
clock_systime_timespec(&ts);
|
2017-04-21 16:55:07 +02:00
|
|
|
|
2020-02-07 08:14:06 +01:00
|
|
|
/* Adjust base time to hide initial timer ticks. */
|
2019-04-26 15:24:57 +02:00
|
|
|
|
2020-02-07 08:14:06 +01:00
|
|
|
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--;
|
2017-04-21 16:55:07 +02:00
|
|
|
}
|
2016-07-11 00:14:25 +02:00
|
|
|
#else
|
2022-01-13 15:01:06 +01:00
|
|
|
clock_inittimekeeping(tp);
|
2016-07-11 00:14:25 +02:00
|
|
|
#endif
|
2012-03-22 15:52:46 +01:00
|
|
|
}
|
2020-02-07 08:14:06 +01:00
|
|
|
#endif
|
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)
|
|
|
|
{
|
2020-02-07 08:14:06 +01:00
|
|
|
#if !defined(CONFIG_SUPPRESS_INTERRUPTS) && \
|
|
|
|
!defined(CONFIG_SUPPRESS_TIMER_INTS) && \
|
|
|
|
!defined(CONFIG_SYSTEMTICK_EXTCLK)
|
|
|
|
/* Initialize the system timer interrupt */
|
|
|
|
|
|
|
|
up_timer_initialize();
|
|
|
|
#endif
|
|
|
|
|
2022-01-17 04:49:48 +01:00
|
|
|
#if defined(CONFIG_RTC)
|
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();
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-01-17 04:49:48 +01:00
|
|
|
#if !defined(CONFIG_RTC_EXTERNAL)
|
2012-03-22 15:52:46 +01:00
|
|
|
/* Initialize the time value to match the RTC */
|
2007-02-18 00:21:28 +01:00
|
|
|
|
2022-01-13 15:01:06 +01:00
|
|
|
clock_inittime(NULL);
|
2020-02-07 08:14:06 +01:00
|
|
|
#endif
|
2023-04-24 13:45:56 +02:00
|
|
|
|
|
|
|
#if defined(CONFIG_CLOCK_ADJTIME)
|
|
|
|
g_clk_adj_count = 0;
|
|
|
|
g_clk_adj_usec = 0;
|
|
|
|
#endif
|
|
|
|
|
2022-01-17 04:49:48 +01:00
|
|
|
#endif
|
2012-03-22 15:52:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
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.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2022-01-13 15:01:06 +01:00
|
|
|
* tp: rtc time should be synced, set NULL to re-get time
|
2012-03-22 15:52:46 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2012-03-22 15:52:46 +01:00
|
|
|
* None
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RTC
|
2022-01-13 15:01:06 +01:00
|
|
|
void clock_synchronize(FAR const struct timespec *tp)
|
2012-03-22 15:52:46 +01:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
|
|
|
|
/* Re-initialize the time value to match the RTC */
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2022-01-13 15:01:06 +01:00
|
|
|
clock_inittime(tp);
|
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.
|
|
|
|
*
|
2018-03-13 16:52:27 +01:00
|
|
|
* Input Parameters:
|
2017-04-21 16:45:57 +02:00
|
|
|
* 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:
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-04-24 13:45:56 +02:00
|
|
|
#if defined(CONFIG_RTC) && !defined(CONFIG_SCHED_TICKLESS) && !defined(CONFIG_CLOCK_TIMEKEEPING)
|
2017-04-21 16:45:57 +02:00
|
|
|
void clock_resynchronize(FAR struct timespec *rtc_diff)
|
|
|
|
{
|
2019-03-01 17:50:02 +01:00
|
|
|
struct timespec rtc_time;
|
|
|
|
struct timespec bias;
|
|
|
|
struct timespec curr_ts;
|
2017-04-21 16:45:57 +02:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-05-04 16:15:10 +02:00
|
|
|
clock_systime_timespec(&bias);
|
2017-04-21 16:45:57 +02:00
|
|
|
|
|
|
|
/* 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 ||
|
2020-05-04 16:15:10 +02:00
|
|
|
(curr_ts.tv_sec == rtc_time.tv_sec &&
|
|
|
|
curr_ts.tv_nsec >= rtc_time.tv_nsec))
|
2017-04-21 16:45:57 +02:00
|
|
|
{
|
|
|
|
/* 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
|
|
|
|
{
|
|
|
|
/* Output difference between time at entry and new current time. */
|
|
|
|
|
2018-11-12 13:50:37 +01:00
|
|
|
rtc_diff->tv_sec = rtc_time.tv_sec - curr_ts.tv_sec;
|
|
|
|
rtc_diff->tv_nsec = rtc_time.tv_nsec - curr_ts.tv_nsec;
|
2017-04-21 16:45:57 +02:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2018-11-12 13:50:37 +01:00
|
|
|
if (carry != 0)
|
2017-04-21 16:45:57 +02:00
|
|
|
{
|
|
|
|
rtc_diff->tv_sec += carry;
|
|
|
|
rtc_diff->tv_nsec -= (carry * NSEC_PER_SEC);
|
|
|
|
}
|
|
|
|
|
2018-11-12 13:50:37 +01:00
|
|
|
/* Add the sleep time to correct system timer */
|
2017-04-21 16:45:57 +02:00
|
|
|
|
2022-09-27 06:03:48 +02:00
|
|
|
g_system_ticks += SEC2TICK(rtc_diff->tv_sec);
|
|
|
|
g_system_ticks += NSEC2TICK(rtc_diff->tv_nsec);
|
2017-04-21 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
skip:
|
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-04-24 13:45:56 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: clock_set_adjust
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* This function is called from adjtime() defined in clock_adjtime.c if
|
|
|
|
* CONFIG_CLOCK_ADJTIME is enabled. It sets global variables g_clk_adj_usec
|
|
|
|
* and g_clk_adj_count which are used for clock adjustment.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* adj_usec - period adjustment in usec
|
|
|
|
* adj_count - number of clock ticks over which we apply adj_usec
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_CLOCK_ADJTIME
|
|
|
|
void clock_set_adjust(long long adj_usec, long long adj_count,
|
|
|
|
long long *adj_usec_old, long long *adj_count_old)
|
|
|
|
{
|
|
|
|
/* Get old adjust values. */
|
|
|
|
|
|
|
|
*adj_usec_old = g_clk_adj_usec;
|
|
|
|
*adj_count_old = g_clk_adj_count;
|
|
|
|
|
|
|
|
/* Set new adjust values. */
|
|
|
|
|
|
|
|
g_clk_adj_usec = adj_usec;
|
|
|
|
g_clk_adj_count = adj_count;
|
|
|
|
|
|
|
|
/* And change timer period. */
|
|
|
|
|
|
|
|
up_adj_timer_period(g_clk_adj_usec);
|
|
|
|
}
|
|
|
|
#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 */
|
|
|
|
|
2022-09-27 06:03:48 +02:00
|
|
|
g_system_ticks++;
|
2023-04-24 13:45:56 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_CLOCK_ADJTIME
|
|
|
|
/* Do we apply timer adjustment? */
|
|
|
|
|
|
|
|
if (g_clk_adj_count > 0)
|
|
|
|
{
|
|
|
|
/* Yes, decrement the count each tick. */
|
|
|
|
|
|
|
|
g_clk_adj_count--;
|
|
|
|
|
|
|
|
/* Check if clock adjusment is finished. */
|
|
|
|
|
|
|
|
if (g_clk_adj_count == 0)
|
|
|
|
{
|
|
|
|
/* Yes... reset timer period. */
|
|
|
|
|
|
|
|
up_adj_timer_period(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2007-02-18 00:21:28 +01:00
|
|
|
}
|
2014-08-08 02:00:38 +02:00
|
|
|
#endif
|