esp_wifi/
time.rs

1// Time keeping
2pub const TICKS_PER_SECOND: u64 = 1_000_000;
3
4/// Current systimer count value
5/// A tick is 1 / 1_000_000 seconds
6/// This function must not be called in a critical section. Doing so may return
7/// an incorrect value.
8pub(crate) fn systimer_count() -> u64 {
9    esp_hal::time::Instant::now()
10        .duration_since_epoch()
11        .as_micros()
12}
13
14// TODO: use an Instance type instead...
15#[cfg(target_arch = "riscv32")]
16pub(crate) fn time_diff(start: u64, end: u64) -> u64 {
17    // 52-bit wrapping sub
18    end.wrapping_sub(start) & 0x000f_ffff_ffff_ffff
19}
20
21// TODO: use an Instance type instead...
22#[cfg(target_arch = "xtensa")]
23pub(crate) fn time_diff(start: u64, end: u64) -> u64 {
24    end.wrapping_sub(start)
25}
26
27#[allow(unused)]
28pub(crate) fn micros_to_ticks(us: u64) -> u64 {
29    us * (TICKS_PER_SECOND / 1_000_000)
30}
31
32#[allow(unused)]
33pub(crate) fn millis_to_ticks(ms: u64) -> u64 {
34    ms * (TICKS_PER_SECOND / 1_000)
35}
36
37#[allow(unused)]
38pub(crate) fn ticks_to_micros(ticks: u64) -> u64 {
39    ticks / (TICKS_PER_SECOND / 1_000_000)
40}
41
42#[allow(unused)]
43pub(crate) fn ticks_to_millis(ticks: u64) -> u64 {
44    ticks / (TICKS_PER_SECOND / 1_000)
45}
46
47/// Do not call this in a critical section!
48pub(crate) fn elapsed_time_since(start: u64) -> u64 {
49    let now = systimer_count();
50    time_diff(start, now)
51}