From a867d1c023e7b7c798877e6ad78d1e31d69b2c4f Mon Sep 17 00:00:00 2001 From: wangchengdong Date: Fri, 12 Dec 2025 17:14:35 +0800 Subject: [PATCH 1/3] sched/sched: Add high-resolution timer support to scheduler Add nxsched_process_hrtimer() to support scheduler operations using high-resolution timers (hrtimer). Signed-off-by: Chengdong Wang --- drivers/timers/arch_alarm.c | 4 + include/nuttx/arch.h | 4 + sched/hrtimer/hrtimer.h | 2 +- sched/sched/CMakeLists.txt | 4 + sched/sched/Make.defs | 4 + sched/sched/sched.h | 5 + sched/sched/sched_processhrtimer.c | 149 +++++++++++++++++++++++++++++ 7 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 sched/sched/sched_processhrtimer.c diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index 4377d37fbccc9..7ca56999affff 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -118,6 +118,9 @@ static void ndelay_accurate(unsigned long nanoseconds) static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower, FAR void *arg) { +#ifdef CONFIG_HRTIMER + nxsched_process_hrtimer(); +#else clock_t now; ONESHOT_TICK_CURRENT(g_oneshot_lower, &now); @@ -138,6 +141,7 @@ static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower, } ONESHOT_TICK_ABSOLUTE(g_oneshot_lower, now + 1); +# endif #endif } diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index 8657eb7a4577f..453d0cd0aade6 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -2442,6 +2442,10 @@ void up_ndelay(unsigned long nanoseconds); void nxsched_process_timer(void); #endif +#ifdef CONFIG_HRTIMER +void nxsched_process_hrtimer(void); +#endif + /**************************************************************************** * Name: nxsched_timer_expiration * diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index de206e25eb755..2a3eba0b22486 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -120,7 +120,7 @@ static inline_function int hrtimer_starttimer(uint64_t ns) { struct timespec ts; - int ret; + int ret = OK; /* Convert nanoseconds to timespec */ diff --git a/sched/sched/CMakeLists.txt b/sched/sched/CMakeLists.txt index 782234f846d20..358ddff5a3a23 100644 --- a/sched/sched/CMakeLists.txt +++ b/sched/sched/CMakeLists.txt @@ -118,4 +118,8 @@ if(CONFIG_SMP) list(APPEND SRCS sched_smp.c) endif() +if(CONFIG_HRTIMER) + list(APPEND SRCS sched_processhrtimer.c) +endif() + target_sources(sched PRIVATE ${SRCS}) diff --git a/sched/sched/Make.defs b/sched/sched/Make.defs index 71a7bc2ce6be7..ef58338e3eed2 100644 --- a/sched/sched/Make.defs +++ b/sched/sched/Make.defs @@ -100,6 +100,10 @@ ifeq ($(CONFIG_SMP),y) CSRCS += sched_smp.c endif +ifeq ($(CONFIG_HRTIMER),y) +CSRCS += sched_processhrtimer.c +endif + # Include sched build support DEPPATH += --dep-path sched diff --git a/sched/sched/sched.h b/sched/sched/sched.h index aa7e0c1c18649..e6bd8483088ff 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -37,6 +37,7 @@ #include #include #include +#include "hrtimer/hrtimer.h" /**************************************************************************** * Pre-processor Definitions @@ -307,6 +308,10 @@ extern volatile spinlock_t g_cpu_tasklistlock; * Public Function Prototypes ****************************************************************************/ +#if defined(CONFIG_HRTIMER) && defined(CONFIG_SCHED_TICKLESS) +void nxsched_hrtimer_start(clock_t ticks); +#endif + int nxthread_create(FAR const char *name, uint8_t ttype, int priority, FAR void *stack_addr, int stack_size, main_t entry, FAR char * const argv[], FAR char * const envp[]); diff --git a/sched/sched/sched_processhrtimer.c b/sched/sched/sched_processhrtimer.c new file mode 100644 index 0000000000000..bde31878faff2 --- /dev/null +++ b/sched/sched/sched_processhrtimer.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * sched/sched/sched_processhrtimer.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include "sched/sched.h" +#include "hrtimer/hrtimer.h" +#include "clock/clock.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Global high-resolution timer instance used by the scheduler */ + +static hrtimer_t g_nxsched_hrtimer; + +/* Whether the scheduler high-resolution timer has been initialized */ + +static bool g_sched_hrtimer_inited = false; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nxsched_hrtimer_callback + * + * Description: + * High-resolution timer callback used for scheduler tick processing. + * + * This callback updates the scheduler state and arms the next timer + * event. The behavior differs depending on whether tickless mode is + * enabled: + * + * - In tickless mode, nxsched_tick_expiration() determines the next + * expiration point. + * - In non-tickless mode, a tick is generated unconditionally at + * every timer interval. + * + * Input Parameters: + * hrtimer - Pointer to the high-resolution timer instance that expired. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +static uint32_t nxsched_hrtimer_callback(FAR hrtimer_t *hrtimer) +{ +#ifdef CONFIG_SCHED_TICKLESS + uint64_t now = hrtimer_gettime(); + nxsched_tick_expiration(NSEC2TICK(now)); +#else + + /* Process a single scheduler tick */ + + nxsched_process_timer(); + + /* Schedule the next tick relative to now */ + + return NSEC_PER_TICK; +#endif +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_SCHED_TICKLESS +void nxsched_hrtimer_start(clock_t ticks) +{ + hrtimer_start(&g_nxsched_hrtimer, + hrtimer->expired + ticks * NSEC_PER_TICK, + HRTIMER_MODE_ABS); +} +#endif + +/**************************************************************************** + * Name: nxsched_process_hrtimer + * + * Description: + * Process pending high-resolution timer events for the scheduler. + * + * This function performs lazy initialization of the scheduler's + * high-resolution timer and processes expired timer events. It is intended + * to be invoked periodically from architecture-specific high-resolution + * timer handling code. + * + * Input Parameters: + * None. + * + * Returned Value: + * None. + * + ****************************************************************************/ + +void nxsched_process_hrtimer(void) +{ + uint64_t now = hrtimer_gettime(); + + /* Perform one-time initialization */ + + if (g_sched_hrtimer_inited == false) + { + g_sched_hrtimer_inited = true; + + /* Initialize the scheduler hrtimer */ + + hrtimer_init(&g_nxsched_hrtimer, + nxsched_hrtimer_callback, + NULL); + + /* Start the first timer event */ + + hrtimer_start(&g_nxsched_hrtimer, + now + NSEC_PER_TICK, + HRTIMER_MODE_ABS); + } + + /* Process any expired high-resolution timer events */ + + hrtimer_process(now); +} From 2285cb8ee8fa7de99c771d4800ddcfe84c798e63 Mon Sep 17 00:00:00 2001 From: wangchengdong Date: Sun, 14 Dec 2025 23:52:17 +0800 Subject: [PATCH 2/3] sched/sched: Update tickless implementation to use hrtimer Update nxsched_tick_expiration and nxsched_reassess_timer to use hrtimer when hrtimer is enabled. Signed-off-by: Chengdong Wang --- sched/sched/sched.h | 2 +- sched/sched/sched_processhrtimer.c | 8 ++++---- sched/sched/sched_timerexpiration.c | 11 ++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/sched/sched/sched.h b/sched/sched/sched.h index e6bd8483088ff..a08b32bedd946 100644 --- a/sched/sched/sched.h +++ b/sched/sched/sched.h @@ -309,7 +309,7 @@ extern volatile spinlock_t g_cpu_tasklistlock; ****************************************************************************/ #if defined(CONFIG_HRTIMER) && defined(CONFIG_SCHED_TICKLESS) -void nxsched_hrtimer_start(clock_t ticks); +int nxsched_hrtimer_start(clock_t ticks); #endif int nxthread_create(FAR const char *name, uint8_t ttype, int priority, diff --git a/sched/sched/sched_processhrtimer.c b/sched/sched/sched_processhrtimer.c index bde31878faff2..2f04a8da9f946 100644 --- a/sched/sched/sched_processhrtimer.c +++ b/sched/sched/sched_processhrtimer.c @@ -93,11 +93,11 @@ static uint32_t nxsched_hrtimer_callback(FAR hrtimer_t *hrtimer) ****************************************************************************/ #ifdef CONFIG_SCHED_TICKLESS -void nxsched_hrtimer_start(clock_t ticks) +int nxsched_hrtimer_start(clock_t ticks) { - hrtimer_start(&g_nxsched_hrtimer, - hrtimer->expired + ticks * NSEC_PER_TICK, - HRTIMER_MODE_ABS); + return hrtimer_start(&g_nxsched_hrtimer, + hrtimer->expired + ticks * NSEC_PER_TICK, + HRTIMER_MODE_ABS); } #endif diff --git a/sched/sched/sched_timerexpiration.c b/sched/sched/sched_timerexpiration.c index 5565b0211b681..bb4a59fb7fd85 100644 --- a/sched/sched/sched_timerexpiration.c +++ b/sched/sched/sched_timerexpiration.c @@ -424,18 +424,21 @@ static clock_t nxsched_timer_start(clock_t ticks, clock_t interval) interval = interval <= (CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK) ? 0 : interval - (CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK); -#ifdef CONFIG_SCHED_TICKLESS_ALARM +#ifdef CONFIG_HRTIMER + ret = nxsched_hrtimer_start(interval); +#else +# ifdef CONFIG_SCHED_TICKLESS_ALARM /* Convert the delay to a time in the future (with respect * to the time when last stopped the timer). */ ret = up_alarm_tick_start(ticks + interval); -#else +# else /* [Re-]start the interval timer */ ret = up_timer_tick_start(interval); +# endif #endif - if (ret < 0) { serr("ERROR: up_timer_start/up_alarm_start failed: %d\n", ret); @@ -490,7 +493,6 @@ void nxsched_tick_expiration(clock_t ticks) /* Process the timer ticks and set up the next interval (or not) */ nexttime = nxsched_timer_process(ticks, elapsed, false); - elapsed = nxsched_timer_start(ticks, nexttime); atomic_set(&g_timer_interval, elapsed); } @@ -596,7 +598,6 @@ void nxsched_reassess_timer(void) /* Process the timer ticks and start next timer */ nexttime = nxsched_timer_process(ticks, elapsed, true); - elapsed = nxsched_timer_start(ticks, nexttime); atomic_set(&g_timer_interval, elapsed); } From 3663e78f82e26d6f42ee0c44f9b65f164449c6f0 Mon Sep 17 00:00:00 2001 From: wangchengdong Date: Fri, 12 Dec 2025 18:03:40 +0800 Subject: [PATCH 3/3] arch/tricore: Enable SYSTEM_TIME64 for Tricore architecture Enable SYSTEM_TIME64 in the Tricore architecture because the System Timer (STM) is a 64-bit timer. Signed-off-by: Chengdong Wang --- arch/tricore/Kconfig | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/tricore/Kconfig b/arch/tricore/Kconfig index 25885ded5d96e..bce3f5d69bf1a 100644 --- a/arch/tricore/Kconfig +++ b/arch/tricore/Kconfig @@ -42,6 +42,7 @@ config ARCH_CHIP_TC397 select ALARM_ARCH select ONESHOT select ONESHOT_COUNT + select SYSTEM_TIME64 ---help--- AURIX TC39x family: TC397