This is an automated email from the ASF dual-hosted git repository. GUIDINGLI pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit a9d963bf851c1fbc9e7d26ed64cf188034c0debb Author: ouyangxiangzhen <[email protected]> AuthorDate: Mon Jul 6 16:40:58 2026 +0800 sched/clock: Convert adjtime period config to nanoseconds Convert CLOCK_ADJTIME_PERIOD_MS to CLOCK_ADJTIME_PERIOD_NS to enable nanosecond-granularity adjustment period configuration: - Rename CONFIG_CLOCK_ADJTIME_PERIOD_MS to CONFIG_CLOCK_ADJTIME_PERIOD_NS with default 970000000 and range 1000000-2000000000 - Change adjtime_start parameter from adjust_usec to adjust_ns - Rename adjtime_remaining_usec to adjtime_remaining_nsec, returning remaining adjustment in nanoseconds - Simplify ppb calculation to a single expression - Update rpmsg_boottime.c to use NSEC_PER_SEC with new config Signed-off-by: ouyangxiangzhen <[email protected]> --- drivers/timers/rpmsg_boottime.c | 8 ++++---- sched/Kconfig | 8 ++++---- sched/clock/clock_adjtime.c | 24 +++++++++++------------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/drivers/timers/rpmsg_boottime.c b/drivers/timers/rpmsg_boottime.c index 79a931113b0..e2bc4456531 100644 --- a/drivers/timers/rpmsg_boottime.c +++ b/drivers/timers/rpmsg_boottime.c @@ -206,8 +206,8 @@ void rpmsg_boottime_update_local(FAR struct rpmsg_boottime_client_s *client) /* Account for the adjustment previously made */ - drift_ppb += client->last_adjtime_ns * MSEC_PER_SEC / - CONFIG_CLOCK_ADJTIME_PERIOD_MS; + drift_ppb += client->last_adjtime_ns * NSEC_PER_SEC / + CONFIG_CLOCK_ADJTIME_PERIOD_NS; if (drift_ppb > CONFIG_CLOCK_ADJTIME_SLEWLIMIT_PPM * 1000 || drift_ppb < -CONFIG_CLOCK_ADJTIME_SLEWLIMIT_PPM * 1000) @@ -235,8 +235,8 @@ void rpmsg_boottime_update_local(FAR struct rpmsg_boottime_client_s *client) * drift rate. */ - adjustment_ns = client->drift_ppb * CONFIG_CLOCK_ADJTIME_PERIOD_MS / - MSEC_PER_SEC; + adjustment_ns = client->drift_ppb * CONFIG_CLOCK_ADJTIME_PERIOD_NS / + NSEC_PER_SEC; /* Drift estimation ensures local clock runs at same rate as remote. * diff --git a/sched/Kconfig b/sched/Kconfig index 820b6b811ac..92a6d061abd 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -223,12 +223,12 @@ config CLOCK_ADJTIME_SLEWLIMIT_PPM For example CLOCK_ADJTIME_SLEWLIMIT=1000 will slow or speed the timer tick period by at most 0.1 percent of the nominal value. -config CLOCK_ADJTIME_PERIOD_MS +config CLOCK_ADJTIME_PERIOD_NS int "Adjtime period" - default 970 - range 1 3600000 + default 970000000 + range 1000000 2000000000 ---help--- - Define system clock adjustment period in milliseconds. + Define system clock adjustment period in nanoseconds. The adjustment commanded by adjtime() call is applied over this time period. endif diff --git a/sched/clock/clock_adjtime.c b/sched/clock/clock_adjtime.c index c6241ee9594..d735e92eee1 100644 --- a/sched/clock/clock_adjtime.c +++ b/sched/clock/clock_adjtime.c @@ -90,29 +90,28 @@ static uint64_t adjtime_timer_callback(FAR const hrtimer_t *timer, static long long adjtime_remaining_usec(void) { - return (long long)g_adjtime_ppb - * (hrtimer_gettime(&g_adjtime_timer) / NSEC_PER_USEC); + return g_adjtime_ppb * hrtimer_gettime(&g_adjtime_timer) / + NSEC_PER_SEC / NSEC_PER_USEC; } /* Start new adjustment period */ -static int adjtime_start(long long adjust_usec) +static int adjtime_start(long long adjust_us) { + long long ppb_limit = CONFIG_CLOCK_ADJTIME_SLEWLIMIT_PPM * 1000ll; long long ppb; - long long ppb_limit; irqstate_t flags; int ret = OK; - /* Calculate rate adjustmend to get adjust_usec change over the - * CONFIG_CLOCK_ADJTIME_PERIOD_MS. + /* Calculate rate adjustment to apply adjust_us over the + * CONFIG_CLOCK_ADJTIME_PERIOD_NS. */ - ppb = adjust_usec * NSEC_PER_USEC; - ppb = ppb * MSEC_PER_SEC / CONFIG_CLOCK_ADJTIME_PERIOD_MS; + ppb = adjust_us * NSEC_PER_USEC * NSEC_PER_SEC / + CONFIG_CLOCK_ADJTIME_PERIOD_NS; /* Limit to maximum rate adjustment */ - ppb_limit = CONFIG_CLOCK_ADJTIME_SLEWLIMIT_PPM * 1000; if (ppb > ppb_limit) { ppb = ppb_limit; @@ -141,8 +140,7 @@ static int adjtime_start(long long adjust_usec) if (g_adjtime_ppb != 0) { hrtimer_start(&g_adjtime_timer, adjtime_timer_callback, - (uint64_t)CONFIG_CLOCK_ADJTIME_PERIOD_MS * - (uint64_t)NSEC_PER_MSEC, HRTIMER_MODE_REL); + CONFIG_CLOCK_ADJTIME_PERIOD_NS, HRTIMER_MODE_REL); } else { @@ -201,9 +199,9 @@ int adjtime(FAR const struct timeval *delta, FAR struct timeval *olddelta) if (olddelta) { - adjust_usec_old = adjtime_remaining_usec(); + adjust_usec_old = adjtime_remaining_usec(); olddelta->tv_sec = adjust_usec_old / USEC_PER_SEC; - olddelta->tv_usec = adjust_usec_old; + olddelta->tv_usec = adjust_usec_old % USEC_PER_SEC; } if (delta)
