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