aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src/lib.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2022-08-17 23:40:16 +0200
committerDario Nieuwenhuis <[email protected]>2022-08-18 01:22:30 +0200
commit5daa173ce4b153a532b4daa9e94c7a248231f25b (patch)
tree2ef0b4d6f9b1c02dac2589e7b57982c20cbc0e66 /embassy-time/src/lib.rs
parent1c5b54a4823d596db730eb476c3ab78110557214 (diff)
Split embassy-time from embassy-executor.
Diffstat (limited to 'embassy-time/src/lib.rs')
-rw-r--r--embassy-time/src/lib.rs99
1 files changed, 99 insertions, 0 deletions
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs
new file mode 100644
index 000000000..a6454d55e
--- /dev/null
+++ b/embassy-time/src/lib.rs
@@ -0,0 +1,99 @@
1#![cfg_attr(not(any(feature = "std", feature = "wasm")), no_std)]
2#![cfg_attr(feature = "nightly", feature(generic_associated_types, type_alias_impl_trait))]
3#![allow(clippy::new_without_default)]
4#![warn(missing_docs)]
5
6//! Timekeeping, delays and timeouts.
7//!
8//! Timekeeping is done with elapsed time since system boot. Time is represented in
9//! ticks, where the tick rate is defined by the current driver, usually to match
10//! the tick rate of the hardware.
11//!
12//! Tick counts are 64 bits. At the highest supported tick rate of 1Mhz this supports
13//! representing time spans of up to ~584558 years, which is big enough for all practical
14//! purposes and allows not having to worry about overflows.
15//!
16//! [`Instant`] represents a given instant of time (relative to system boot), and [`Duration`]
17//! represents the duration of a span of time. They implement the math operations you'd expect,
18//! like addition and substraction.
19//!
20//! # Delays and timeouts
21//!
22//! [`Timer`] allows performing async delays. [`Ticker`] allows periodic delays without drifting over time.
23//!
24//! An implementation of the `embedded-hal` delay traits is provided by [`Delay`], for compatibility
25//! with libraries from the ecosystem.
26//!
27//! # Wall-clock time
28//!
29//! The `time` module deals exclusively with a monotonically increasing tick count.
30//! Therefore it has no direct support for wall-clock time ("real life" datetimes
31//! like `2021-08-24 13:33:21`).
32//!
33//! If persistence across reboots is not needed, support can be built on top of
34//! `embassy_time` by storing the offset between "seconds elapsed since boot"
35//! and "seconds since unix epoch".
36//!
37//! # Time driver
38//!
39//! The `time` module is backed by a global "time driver" specified at build time.
40//! Only one driver can be active in a program.
41//!
42//! All methods and structs transparently call into the active driver. This makes it
43//! possible for libraries to use `embassy_time` in a driver-agnostic way without
44//! requiring generic parameters.
45//!
46//! For more details, check the [`driver`] module.
47
48// This mod MUST go first, so that the others see its macros.
49pub(crate) mod fmt;
50
51mod delay;
52pub mod driver;
53mod duration;
54mod instant;
55mod timer;
56
57#[cfg(feature = "std")]
58mod driver_std;
59#[cfg(feature = "wasm")]
60mod driver_wasm;
61
62pub use delay::{block_for, Delay};
63pub use duration::Duration;
64pub use instant::Instant;
65pub use timer::{with_timeout, Ticker, TimeoutError, Timer};
66
67#[cfg(feature = "tick-1000hz")]
68const TPS: u64 = 1_000;
69
70#[cfg(feature = "tick-32768hz")]
71const TPS: u64 = 32_768;
72
73#[cfg(feature = "tick-1mhz")]
74const TPS: u64 = 1_000_000;
75
76#[cfg(feature = "tick-16mhz")]
77const TPS: u64 = 16_000_000;
78
79/// Ticks per second of the global timebase.
80///
81/// This value is specified by the `tick-*` Cargo features, which
82/// should be set by the time driver. Some drivers support a fixed tick rate, others
83/// allow you to choose a tick rate with Cargo features of their own. You should not
84/// set the `tick-*` features for embassy yourself as an end user.
85pub const TICKS_PER_SECOND: u64 = TPS;
86
87const fn gcd(a: u64, b: u64) -> u64 {
88 if b == 0 {
89 a
90 } else {
91 gcd(b, a % b)
92 }
93}
94
95pub(crate) const GCD_1K: u64 = gcd(TICKS_PER_SECOND, 1_000);
96pub(crate) const GCD_1M: u64 = gcd(TICKS_PER_SECOND, 1_000_000);
97
98#[cfg(feature = "defmt-timestamp-uptime")]
99defmt::timestamp! {"{=u64:us}", Instant::now().as_micros() }