diff --git a/drivers/timers/arch_alarm.c b/drivers/timers/arch_alarm.c index 72e6f35e5f6c7..295369d919996 100644 --- a/drivers/timers/arch_alarm.c +++ b/drivers/timers/arch_alarm.c @@ -118,7 +118,7 @@ static void ndelay_accurate(unsigned long nanoseconds) static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower, FAR void *arg) { -#ifdef CONFIG_SCHED_TICKLESS +#if defined(CONFIG_HRTIMER) || defined(CONFIG_SCHED_TICKLESS) nxsched_process_timer(); #else clock_t now; @@ -380,7 +380,7 @@ int weak_function up_alarm_tick_cancel(FAR clock_t *ticks) * ****************************************************************************/ -#ifdef CONFIG_SCHED_TICKLESS +#if defined(CONFIG_HRTIMER) || defined(CONFIG_SCHED_TICKLESS) int weak_function up_alarm_start(FAR const struct timespec *ts) { int ret = -EAGAIN; diff --git a/include/nuttx/arch.h b/include/nuttx/arch.h index f975ecfe39580..5f6bbf9beaad1 100644 --- a/include/nuttx/arch.h +++ b/include/nuttx/arch.h @@ -2031,7 +2031,8 @@ int up_alarm_tick_cancel(FAR clock_t *ticks); * ****************************************************************************/ -#if defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM) +#if defined(CONFIG_HRTIMER) || \ + (defined(CONFIG_SCHED_TICKLESS) && defined(CONFIG_SCHED_TICKLESS_ALARM)) int up_alarm_start(FAR const struct timespec *ts); int up_alarm_tick_start(clock_t ticks); #endif diff --git a/include/nuttx/hrtimer.h b/include/nuttx/hrtimer.h index e6c617b6b2937..05145389181db 100644 --- a/include/nuttx/hrtimer.h +++ b/include/nuttx/hrtimer.h @@ -119,7 +119,7 @@ extern "C" ****************************************************************************/ static inline_function -void hrtimer_init(FAR hrtimer_t *hrtimer, hrtimer_cb func) +void hrtimer_init(FAR hrtimer_t *hrtimer, hrtimer_entry_t func) { memset(hrtimer, 0, sizeof(hrtimer_t)); hrtimer->func = func; diff --git a/sched/sched/sched_processtimer.c b/sched/sched/sched_processtimer.c index 9dc7a076129ed..2861729394754 100644 --- a/sched/sched/sched_processtimer.c +++ b/sched/sched/sched_processtimer.c @@ -44,11 +44,116 @@ #include "sched/sched.h" #include "wdog/wdog.h" #include "clock/clock.h" +#include "hrtimer/hrtimer.h" + +#ifdef CONFIG_HRTIMER +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static uint64_t +nxsched_hrtimer_callback(FAR const hrtimer_t *hrtimer, + uint64_t expired); + +static void nxsched_process_tick(void); + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Scheduler-owned high-resolution timer instance. + * + * This timer acts as the time source for scheduler-related events: + * + * - Periodic scheduler ticks in non-tickless mode + * - Dynamic expiration points in tickless mode + * + * The timer is initialized lazily to avoid unnecessary setup when + * CONFIG_HRTIMER is enabled but not used immediately. + */ + +static hrtimer_t g_nxsched_hrtimer = +{ + .func = nxsched_hrtimer_callback, +}; /**************************************************************************** * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: nxsched_hrtimer_callback + * + * Description: + * Callback invoked by the high-resolution timer framework when the + * scheduler timer expires. + * + * Behavior depends on scheduler configuration: + * + * CONFIG_SCHED_TICKLESS: + * - Query current high-resolution time + * - Convert time to scheduler ticks + * - Notify scheduler via nxsched_tick_expiration() + * + * !CONFIG_SCHED_TICKLESS: + * - Re-arm the next periodic tick + * - Process a single scheduler tick + * + * Input Parameters: + * hrtimer - Pointer to the expired high-resolution timer + * + * Returned Value: + * In non-tickless mode, returns the interval until the next expiration. + * In tickless mode, the return value is ignored. + * + ****************************************************************************/ + +static uint64_t +nxsched_hrtimer_callback(FAR hrtimer_t *hrtimer, uint64_t expired) +{ + UNUSED(hrtimer); + UNUSED(expired); + +#ifdef CONFIG_SCHED_TICKLESS + nxsched_tick_expiration(); +#else + nxsched_process_tick(); + return NSEC_PER_TICK; +#endif +} + +/**************************************************************************** + * Name: nxsched_process_hrtimer + * + * Description: + * Entry point for scheduler-related high-resolution timer processing. + * + * Responsibilities: + * - Process expired hrtimer events + * - Perform one-time initialization of the scheduler hrtimer + * - Arm the initial scheduler timer event + * + * This function is expected to be called from architecture-specific + * timer interrupt handling code. + * + * Input Parameters: + * None + * + * Returned Value: + * None + * + ****************************************************************************/ + +static void nxsched_process_hrtimer(void) +{ + uint64_t now = hrtimer_gettime(); + + /* Process any expired high-resolution timers */ + + hrtimer_process(now); +} +#endif /* CONFIG_HRTIMER */ + #ifndef CONFIG_SCHED_TICKLESS /**************************************************************************** * Name: nxsched_cpu_scheduler @@ -205,7 +310,12 @@ static void nxsched_process_tick(void) void nxsched_process_timer(void) { -#ifdef CONFIG_SCHED_TICKLESS +#if defined(CONFIG_HRTIMER) + /* High-resolution timer-based scheduling */ + + nxsched_process_hrtimer(); + +#elif defined(CONFIG_SCHED_TICKLESS) /* Tickless scheduling */ nxsched_tick_expiration();