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 1aa8f8d11f9bfc6f81e6ce1c5bbd40c2da5d901a Author: ouyangxiangzhen <[email protected]> AuthorDate: Mon Jun 29 15:52:39 2026 +0800 sched/hrtimer: Add precision mask to reduce hrtimer interrupt frequency Add HRTIMER_PRECISION_SHIFT config and HRTIMER_PRECISION_MASK to align programmed expiration to a coarser precision boundary, reducing hrtimer interrupt frequency with acceptable accuracy loss. Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- sched/Kconfig | 19 +++++++++++++++++++ sched/hrtimer/hrtimer.h | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/sched/Kconfig b/sched/Kconfig index d5488299ebd..770c30e6542 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -2175,4 +2175,23 @@ config HRTIMER_TREE endchoice +config HRTIMER_PRECISION_SHIFT + int "HRTimer precision shift (0 = full precision)" + default 0 + range 0 20 + ---help--- + Reduce hrtimer precision to decrease interrupt frequency. + The value N defines the left-shift bits of the precision unit: + precision = 1 << N nanoseconds, and the mask = precision - 1. + + The programmed expiration time is ceiling-aligned to the + precision boundary via (expired + mask) & ~mask in + hrtimer_reprogram(). + + Examples: + 0 - full nanosecond precision (default, no alignment) + 10 - 1024 ns (~1 us) precision + 15 - 32768 ns (~32 us) precision + 20 - 1048576 ns (~1 ms) precision + endif # HRTIMER diff --git a/sched/hrtimer/hrtimer.h b/sched/hrtimer/hrtimer.h index b2fdadac91c..2522a184dde 100644 --- a/sched/hrtimer/hrtimer.h +++ b/sched/hrtimer/hrtimer.h @@ -61,6 +61,14 @@ #define HRTIMER_TIME_BEFORE(t1, t2) ((int64_t)((t2) - (t1)) > 0) #define HRTIMER_TIME_BEFORE_EQ(t1, t2) ((int64_t)((t2) - (t1)) >= 0) +/* Precision mask for reducing hrtimer resolution. + * When CONFIG_HRTIMER_PRECISION_SHIFT > 0, the programmed expiration + * time is ceiling-aligned to (1 << SHIFT) nanosecond boundaries. + */ + +#define HRTIMER_PRECISION_MASK \ + ((1ull << CONFIG_HRTIMER_PRECISION_SHIFT) - 1u) + /**************************************************************************** * Public Types ****************************************************************************/ @@ -209,6 +217,16 @@ static inline_function void hrtimer_reprogram(uint64_t next_expired) uint64_t delay; uint64_t expired = hrtimer_last_expired(); +#if CONFIG_HRTIMER_PRECISION_SHIFT > 0 + /* Ceiling-align the target expiration time to the precision boundary. + * This coarsens the timer resolution to (1 << SHIFT) nanoseconds, + * reducing the frequency of hardware timer reprogramming. + */ + + next_expired = (next_expired + HRTIMER_PRECISION_MASK) & + ~HRTIMER_PRECISION_MASK; +#endif + delay = HRTIMER_TIME_BEFORE_EQ(expired, next_expired) ? next_expired - expired : 0u;
