diff --git a/embassy/src/time.rs b/embassy/src/time.rs index d42d0e5e..9a53c4da 100644 --- a/embassy/src/time.rs +++ b/embassy/src/time.rs @@ -25,6 +25,9 @@ pub struct Instant { ticks: u64, } +// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. +pub const TICKS_PER_SECOND: u32 = 32768; + impl Instant { pub fn now() -> Instant { Instant { ticks: now() } @@ -128,6 +131,18 @@ impl Duration { Duration { ticks } } + pub const fn from_secs(secs: u32) -> Duration { + Duration { + ticks: secs * TICKS_PER_SECOND, + } + } + + pub const fn from_millis(millis: u32) -> Duration { + Duration { + ticks: millis * TICKS_PER_SECOND / 1000, + } + } + pub fn checked_add(self, rhs: Duration) -> Option { self.ticks .checked_add(rhs.ticks)