axhal/
time.rs

1//! Time-related operations.
2
3pub use core::time::Duration;
4
5/// A measurement of the system clock.
6///
7/// Currently, it reuses the [`core::time::Duration`] type. But it does not
8/// represent a duration, but a clock time.
9pub type TimeValue = Duration;
10
11#[cfg(feature = "irq")]
12pub use crate::platform::irq::TIMER_IRQ_NUM;
13#[cfg(feature = "irq")]
14pub use crate::platform::time::set_oneshot_timer;
15pub use crate::platform::time::{current_ticks, epochoffset_nanos, nanos_to_ticks, ticks_to_nanos};
16
17/// Number of milliseconds in a second.
18pub const MILLIS_PER_SEC: u64 = 1_000;
19/// Number of microseconds in a second.
20pub const MICROS_PER_SEC: u64 = 1_000_000;
21/// Number of nanoseconds in a second.
22pub const NANOS_PER_SEC: u64 = 1_000_000_000;
23/// Number of nanoseconds in a millisecond.
24pub const NANOS_PER_MILLIS: u64 = 1_000_000;
25/// Number of nanoseconds in a microsecond.
26pub const NANOS_PER_MICROS: u64 = 1_000;
27
28/// Returns nanoseconds elapsed since system boot.
29pub fn monotonic_time_nanos() -> u64 {
30    ticks_to_nanos(current_ticks())
31}
32
33/// Returns the time elapsed since system boot in [`TimeValue`].
34pub fn monotonic_time() -> TimeValue {
35    TimeValue::from_nanos(monotonic_time_nanos())
36}
37
38/// Returns nanoseconds elapsed since epoch (also known as realtime).
39pub fn wall_time_nanos() -> u64 {
40    monotonic_time_nanos() + epochoffset_nanos()
41}
42
43/// Returns the time elapsed since epoch (also known as realtime) in [`TimeValue`].
44pub fn wall_time() -> TimeValue {
45    TimeValue::from_nanos(monotonic_time_nanos() + epochoffset_nanos())
46}
47
48/// Busy waiting for the given duration.
49pub fn busy_wait(dur: Duration) {
50    busy_wait_until(wall_time() + dur);
51}
52
53/// Busy waiting until reaching the given deadline.
54pub fn busy_wait_until(deadline: TimeValue) {
55    while wall_time() < deadline {
56        core::hint::spin_loop();
57    }
58}