This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit b65f29da9e552c17b56334fe05d1927e0323b779
Author: ouyangxiangzhen <[email protected]>
AuthorDate: Mon Jun 29 15:52:31 2026 +0800

    sched/hrtimer: Add CONFIG_TIMER_ADJUST_NSEC for interrupt latency 
compensation
    
    Change CONFIG_TIMER_ADJUST_USEC to CONFIG_TIMER_ADJUST_NSEC with
    nanosecond granularity. In hrtimer_reprogram, subtract the configured
    value from the expiration time to fire timers earlier, compensating
    for interrupt latency in real-time systems.
    
    Assisted-By: mimo-v2.6-pro
    Signed-off-by: ouyangxiangzhen <[email protected]>
---
 sched/Kconfig           | 11 ++++++-----
 sched/hrtimer/hrtimer.h | 12 ++++++++++--
 sched/wdog/wdog.h       |  4 ++--
 3 files changed, 18 insertions(+), 9 deletions(-)

diff --git a/sched/Kconfig b/sched/Kconfig
index 770c30e6542..615cf065636 100644
--- a/sched/Kconfig
+++ b/sched/Kconfig
@@ -130,15 +130,16 @@ config USEC_PER_TICK
                This value should never be less than the underlying resolution 
of
                the timer.  Error may ensue.
 
-config TIMER_ADJUST_USEC
-       int "System timer tick adjustment (microseconds)"
+config TIMER_ADJUST_NSEC
+       int "System timer tick adjustment (nanoseconds)"
        default 0
        ---help---
-               This value is added to the absolute tick set to the system 
timer.
-               This is used to compensate for interrupt latency in real-time
+               This value is subtracted from the hrtimer expiration time in
+               nanoseconds to compensate for interrupt latency in real-time
                systems where the event required to be triggered on exact time.
                The default value of 0 means that no adjustment is made. E.g.
-               5 means for each timer being set will be fired 5 microseconds 
earlier.
+               5000 means for each timer being set will be fired 5 microseconds
+               earlier.
 
 if !SCHED_TICKLESS
 
diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h
index 2522a184dde..7f4a2aa168d 100644
--- a/sched/hrtimer/hrtimer.h
+++ b/sched/hrtimer/hrtimer.h
@@ -191,10 +191,12 @@ static inline_function uint64_t hrtimer_last_expired(void)
  * Description:
  *   Reprogram the hardware timer to expire at a specified nanosecond time.
  *   Converts the nanosecond time to timespec and calls the platform-specific
- *   timer start function.
+ *   timer start function. If CONFIG_TIMER_ADJUST_NSEC is set, the expiration
+ *   time is adjusted earlier by that amount to compensate for interrupt
+ *   latency.
  *
  * Input Parameters:
- *   ns - Expiration time in nanoseconds.
+ *   next_expired - Expiration time in nanoseconds.
  *
  * Returned Value:
  *   None.
@@ -235,6 +237,12 @@ static inline_function void hrtimer_reprogram(uint64_t 
next_expired)
           delay : TICK2NSEC((uint64_t)g_oneshot_maxticks);
 #  endif
 
+  /* Even if overflow occurs here, the functional correctness
+   * should still be fine.
+   */
+
+  delay -= CONFIG_TIMER_ADJUST_NSEC;
+
   clock_nsec2time(&ts, expired + delay);
 
 #  ifdef CONFIG_SCHED_TICKLESS_ALARM
diff --git a/sched/wdog/wdog.h b/sched/wdog/wdog.h
index b01e1a17ad2..c7139105c9e 100644
--- a/sched/wdog/wdog.h
+++ b/sched/wdog/wdog.h
@@ -148,7 +148,7 @@ static inline_function clock_t wd_adjust_next_tick(clock_t 
tick)
   next_tick         = g_wdexpired + interval;
 #endif
 
-#if CONFIG_TIMER_ADJUST_USEC > 0
+#if CONFIG_TIMER_ADJUST_NSEC > 0
   /* Normally, timer event cannot triggered on exact time due to the
    * existence of interrupt latency.
    * Assuming that the interrupt latency is distributed within
@@ -161,7 +161,7 @@ static inline_function clock_t wd_adjust_next_tick(clock_t 
tick)
    * interrupt, which may result in a much larger timer interrupt latency.
    */
 
-  next_tick -= CONFIG_TIMER_ADJUST_USEC / USEC_PER_TICK;
+  next_tick -= CONFIG_TIMER_ADJUST_NSEC / NSEC_PER_TICK;
 #endif
 
   return next_tick;

Reply via email to