embassy_time/
lib.rs

1#![cfg_attr(not(any(feature = "std", feature = "wasm", test)), no_std)]
2#![allow(async_fn_in_trait)]
3#![doc = include_str!("../README.md")]
4#![allow(clippy::new_without_default)]
5#![warn(missing_docs)]
6#![deny(missing_debug_implementations)]
7
8//! ## Feature flags
9#![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)]
10
11// This mod MUST go first, so that the others see its macros.
12pub(crate) mod fmt;
13
14mod delay;
15mod duration;
16mod instant;
17mod timer;
18
19#[cfg(feature = "mock-driver")]
20mod driver_mock;
21
22#[cfg(feature = "mock-driver")]
23pub use driver_mock::MockDriver;
24
25#[cfg(feature = "std")]
26mod driver_std;
27#[cfg(feature = "wasm")]
28mod driver_wasm;
29
30pub use delay::{block_for, Delay};
31pub use duration::Duration;
32pub use embassy_time_driver::TICK_HZ;
33pub use instant::Instant;
34pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer, WithTimeout};
35
36const fn gcd(a: u64, b: u64) -> u64 {
37    if b == 0 {
38        a
39    } else {
40        gcd(b, a % b)
41    }
42}
43
44pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000);
45pub(crate) const GCD_1M: u64 = gcd(TICK_HZ, 1_000_000);
46pub(crate) const GCD_1G: u64 = gcd(TICK_HZ, 1_000_000_000);
47
48#[cfg(feature = "defmt-timestamp-uptime-s")]
49defmt::timestamp! {"{=u64}", Instant::now().as_secs() }
50
51#[cfg(feature = "defmt-timestamp-uptime-ms")]
52defmt::timestamp! {"{=u64:ms}", Instant::now().as_millis() }
53
54#[cfg(any(feature = "defmt-timestamp-uptime", feature = "defmt-timestamp-uptime-us"))]
55defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() }
56
57#[cfg(feature = "defmt-timestamp-uptime-ts")]
58defmt::timestamp! {"{=u64:ts}", Instant::now().as_secs() }
59
60#[cfg(feature = "defmt-timestamp-uptime-tms")]
61defmt::timestamp! {"{=u64:tms}", Instant::now().as_millis() }
62
63#[cfg(feature = "defmt-timestamp-uptime-tus")]
64defmt::timestamp! {"{=u64:tus}", Instant::now().as_micros() }