3939
4040#include "sched/sched.h"
4141#include "wdog/wdog.h"
42+ #include "hrtimer/hrtimer.h"
4243#include "clock/clock.h"
4344
4445#ifdef CONFIG_CLOCK_TIMEKEEPING
@@ -80,7 +81,8 @@ static clock_t nxsched_process_scheduler(clock_t ticks, clock_t elapsed,
8081 bool noswitches );
8182static clock_t nxsched_timer_process (clock_t ticks , clock_t elapsed ,
8283 bool noswitches );
83- static clock_t nxsched_timer_start (clock_t ticks , clock_t interval );
84+ static inline_function
85+ clock_t nxsched_timer_update (clock_t ticks , bool noswitches );
8486
8587/****************************************************************************
8688 * Private Data
@@ -104,6 +106,10 @@ static atomic_t g_timer_interval;
104106static unsigned int g_timernested ;
105107#endif
106108
109+ #ifdef CONFIG_HRTIMER
110+ static hrtimer_t g_hrtimer_sched ;
111+ #endif
112+
107113/****************************************************************************
108114 * Private Functions
109115 ****************************************************************************/
@@ -164,7 +170,51 @@ int up_timer_gettick(FAR clock_t *ticks)
164170}
165171#endif
166172
167- #ifdef CONFIG_SCHED_TICKLESS_ALARM
173+ #ifdef CONFIG_HRTIMER
174+ static uint64_t nxsched_hrtimer_expiration (void * args , uint64_t expired )
175+ {
176+ uint64_t next_delay = 0u ;
177+ clock_t ticks = div_const (expired , NSEC_PER_TICK );
178+ irqstate_t flags ;
179+ clock_t interval ;
180+
181+ /* Process the wdog and scheduler. */
182+
183+ flags = enter_critical_section ();
184+ g_timernested ++ ;
185+
186+ interval = nxsched_timer_update (ticks , false);
187+
188+ g_timernested -- ;
189+ leave_critical_section (flags );
190+
191+ /* Calculate the next delay. */
192+
193+ if (interval != CLOCK_MAX )
194+ {
195+ interval = adjust_next_interval (interval );
196+ next_delay = interval == 0u ? 1u : interval * NSEC_PER_TICK ;
197+ }
198+
199+ return next_delay ;
200+ }
201+
202+ static inline_function
203+ int nxsched_timer_tick_start (clock_t ticks , clock_t delay )
204+ {
205+ return hrtimer_restart_absolute (& g_hrtimer_sched ,
206+ nxsched_hrtimer_expiration , NULL ,
207+ (ticks + delay ) * NSEC_PER_TICK );
208+ }
209+
210+ static inline_function
211+ int nxsched_timer_tick_cancel (clock_t * ticks )
212+ {
213+ hrtimer_cancel (& g_hrtimer_sched );
214+ return up_timer_gettick (ticks );
215+ }
216+
217+ #elif defined(CONFIG_SCHED_TICKLESS_ALARM )
168218static inline_function
169219int nxsched_timer_tick_start (clock_t ticks , clock_t delay )
170220{
@@ -474,6 +524,7 @@ clock_t nxsched_timer_update(clock_t ticks, bool noswitches)
474524 *
475525 ****************************************************************************/
476526
527+ #ifndef CONFIG_HRTIMER
477528void nxsched_timer_expiration (void )
478529{
479530 irqstate_t flags ;
@@ -495,6 +546,12 @@ void nxsched_timer_expiration(void)
495546
496547 leave_critical_section (flags );
497548}
549+ #else
550+ void nxsched_timer_expiration (void )
551+ {
552+ hrtimer_expiry (clock_systime_nsec (), false);
553+ }
554+ #endif
498555
499556/****************************************************************************
500557 * Name: nxsched_reassess_timer
0 commit comments