diff --git a/sched/clock/clock.h b/sched/clock/clock.h index 9c4728f7b7..4a05bca02f 100644 --- a/sched/clock/clock.h +++ b/sched/clock/clock.h @@ -78,12 +78,6 @@ extern volatile uint32_t g_system_timer; # endif #endif -#ifdef CONFIG_SYSTEM_TIME64 -extern uint64_t g_tickbias; -#else -extern uint32_t g_tickbias; -#endif - extern struct timespec g_basetime; /******************************************************************************** diff --git a/sched/clock/clock_initialize.c b/sched/clock/clock_initialize.c index 62a41cd696..eab7e2d23f 100644 --- a/sched/clock/clock_initialize.c +++ b/sched/clock/clock_initialize.c @@ -88,12 +88,6 @@ volatile uint32_t g_system_timer; #endif #endif -#ifdef CONFIG_SYSTEM_TIME64 -uint64_t g_tickbias; -#else -uint32_t g_tickbias; -#endif - struct timespec g_basetime; /************************************************************************** @@ -191,7 +185,6 @@ static void clock_inittime(void) #ifndef CONFIG_SCHED_TICKLESS g_system_timer = 0; #endif - g_tickbias = 0; } /**************************************************************************** diff --git a/sched/clock/clock_settime.c b/sched/clock/clock_settime.c index 78e9e961f7..2649243cad 100644 --- a/sched/clock/clock_settime.c +++ b/sched/clock/clock_settime.c @@ -90,6 +90,7 @@ int clock_settime(clockid_t clock_id, FAR const struct timespec *tp) { + struct timespec bias; irqstate_t flags; int ret = OK; @@ -114,11 +115,27 @@ int clock_settime(clockid_t clock_id, FAR const struct timespec *tp) g_basetime.tv_sec = tp->tv_sec; g_basetime.tv_nsec = tp->tv_nsec; - /* Get the elapsed time since power up (in milliseconds) biased - * as appropriate. + /* Get the elapsed time since power up (in milliseconds). This is a + * bias value that we need to use to correct the base time. */ - g_tickbias = clock_systimer(); + (void)clock_systimespec(&bias); + + /* 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; /* Setup the RTC (lo- or high-res) */ @@ -130,9 +147,9 @@ int clock_settime(clockid_t clock_id, FAR const struct timespec *tp) #endif irqrestore(flags); - sdbg("basetime=(%d,%d) tickbias=%d\n", - (int)g_basetime.tv_sec, (int)g_basetime.tv_nsec, - (int)g_tickbias); + sdbg("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); } else {