diff options
| author | Dario Nieuwenhuis <[email protected]> | 2022-07-29 21:58:35 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2022-07-29 23:40:36 +0200 |
| commit | a0f1b0ee01d461607660d2d56b5b1bdc57e0d3fb (patch) | |
| tree | e60fc8f8db8ec07e55d655c1a830b07f4db0b7d2 /embassy-executor/src/time/mod.rs | |
| parent | 8745d646f0976791b7098456aa61adb983fb1c18 (diff) | |
Split embassy crate into embassy-executor, embassy-util.
Diffstat (limited to 'embassy-executor/src/time/mod.rs')
| -rw-r--r-- | embassy-executor/src/time/mod.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/embassy-executor/src/time/mod.rs b/embassy-executor/src/time/mod.rs new file mode 100644 index 000000000..b787a5cf2 --- /dev/null +++ b/embassy-executor/src/time/mod.rs | |||
| @@ -0,0 +1,91 @@ | |||
| 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 | |||
| 45 | mod delay; | ||
| 46 | pub mod driver; | ||
| 47 | mod duration; | ||
| 48 | mod instant; | ||
| 49 | mod timer; | ||
| 50 | |||
| 51 | #[cfg(feature = "std")] | ||
| 52 | mod driver_std; | ||
| 53 | |||
| 54 | #[cfg(feature = "wasm")] | ||
| 55 | mod driver_wasm; | ||
| 56 | |||
| 57 | pub use delay::{block_for, Delay}; | ||
| 58 | pub use duration::Duration; | ||
| 59 | pub use instant::Instant; | ||
| 60 | pub use timer::{with_timeout, Ticker, TimeoutError, Timer}; | ||
| 61 | |||
| 62 | #[cfg(feature = "time-tick-1000hz")] | ||
| 63 | const TPS: u64 = 1_000; | ||
| 64 | |||
| 65 | #[cfg(feature = "time-tick-32768hz")] | ||
| 66 | const TPS: u64 = 32_768; | ||
| 67 | |||
| 68 | #[cfg(feature = "time-tick-1mhz")] | ||
| 69 | const TPS: u64 = 1_000_000; | ||
| 70 | |||
| 71 | #[cfg(feature = "time-tick-16mhz")] | ||
| 72 | const 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. | ||
| 80 | pub const TICKS_PER_SECOND: u64 = TPS; | ||
| 81 | |||
| 82 | const fn gcd(a: u64, b: u64) -> u64 { | ||
| 83 | if b == 0 { | ||
| 84 | a | ||
| 85 | } else { | ||
| 86 | gcd(b, a % b) | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | pub(crate) const GCD_1K: u64 = gcd(TICKS_PER_SECOND, 1_000); | ||
| 91 | pub(crate) const GCD_1M: u64 = gcd(TICKS_PER_SECOND, 1_000_000); | ||
