wangchdo opened a new pull request, #17489:
URL: https://github.com/apache/nuttx/pull/17489
## Summary
This PR is a continuation and optimized implementation, replacing the draft
PR #17065 proposed two months ago.
**Summary if this PR**
- Introduce a high-resolution timer (hrtimer) module to NuttX, providing
timers with nanosecond-level resolution.
- Enable coexistence with the existing NuttX timer facility (wdog):
wdog timers are now driven by an hrtimer instance (g_nxsched_hrtimer).
- Provide three new hrtimer APIs:
1. hrtimer_init() – Initialize a high-resolution timer instance.
2. hrtimer_start() – Start a high-resolution timer in absolute or relative
mode.
3. hrtimer_cancel() – Cancel a pending high-resolution timer.
**Motivation**
The main limitation of wdog is its tick-level resolution, typically in
milliseconds. While the tick duration can be configured, setting it to
microsecond or nanosecond resolution is impractical and may cause an interrupt
storm, where the CPU is constantly occupied handling tick interrupts.
In hard real-time applications, nanosecond-level control of task activation
is critical for certain scenarios, such as motor control, where tick-level
precision is insufficient.
Additionally, the hrtimer is implemented using RT_TREE (red-black tree),
which is more efficient than the list-based structure used by wdog. This allows
better performance when managing a large number of task activation points.
**Why not use wdog for high-resolution timing **
wdog is highly coupled with the scheduler and has inherent limitations:
1. It cannot achieve nanosecond-level resolution due to its tick-based
design.
2. While wdog is lightweight, efficient, and stable for all current NuttX
functionality, replacing its list-based data structure with a tree would
complicate the implementation and potentially reduce reliability。
## Impact
Add a new high-resolution timer (hrtimer) module to NuttX.
- When disabled, it has **no impact** on any existing NuttX functionality.
- When enabled, it **coexists with `wdog`** and provides three new APIs to
support **nanosecond-level timers**.
## Testing
**ostest passed on board a2g-tc397-5v-tft when hrtimer is enabled**
```
NuttShell (NSH)
nsh>
nsh> uname -a
NuttX 0.0.0 fa487accea Dec 12 2025 20:23:48 tricore a2g-tc397-5v-tft
nsh>
nsh> ostest
(...)
End of test memory usage:
VARIABLE BEFORE AFTER
======== ======== ========
arena 28dfc 28dfc
ordblks 7 6
mxordblk 1f8a8 1f8a8
uordblks 555c 555c
fordblks 238a0 238a0
user_main: nxevent test
End of test memory usage:
VARIABLE BEFORE AFTER
======== ======== ========
arena 28dfc 28dfc
ordblks 6 6
mxordblk 1f8a8 1f8a8
uordblks 555c 555c
fordblks 238a0 238a0
Final memory usage:
VARIABLE BEFORE AFTER
======== ======== ========
arena 28dfc 28dfc
ordblks 1 6
mxordblk 24220 1f8a8
uordblks 4bdc 555c
fordblks 24220 238a0
user_main: Exiting
ostest_main: Exiting with status 0
nsh>
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]