diff --git a/embassy/src/time/duration.rs b/embassy/src/time/duration.rs index 4bc515c0..4ff8d40e 100644 --- a/embassy/src/time/duration.rs +++ b/embassy/src/time/duration.rs @@ -1,28 +1,37 @@ use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; +use core::fmt; use super::TICKS_PER_SECOND; #[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] pub struct Duration { - pub(crate) ticks: u32, + pub(crate) ticks: u64, } impl Duration { - pub const fn into_ticks(&self) -> u32 { + pub const fn as_ticks(&self) -> u64 { self.ticks } - pub const fn from_ticks(ticks: u32) -> Duration { + pub const fn as_secs(&self) -> u64 { + self.ticks / TICKS_PER_SECOND + } + + pub const fn as_millis(&self) -> u64 { + self.ticks * 1000 / TICKS_PER_SECOND + } + + pub const fn from_ticks(ticks: u64) -> Duration { Duration { ticks } } - pub const fn from_secs(secs: u32) -> Duration { + pub const fn from_secs(secs: u64) -> Duration { Duration { ticks: secs * TICKS_PER_SECOND, } } - pub const fn from_millis(millis: u32) -> Duration { + pub const fn from_millis(millis: u64) -> Duration { Duration { ticks: millis * TICKS_PER_SECOND / 1000, } @@ -41,11 +50,11 @@ impl Duration { } pub fn checked_mul(self, rhs: u32) -> Option { - self.ticks.checked_mul(rhs).map(|ticks| Duration { ticks }) + self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks }) } pub fn checked_div(self, rhs: u32) -> Option { - self.ticks.checked_div(rhs).map(|ticks| Duration { ticks }) + self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks }) } } @@ -116,3 +125,9 @@ impl DivAssign for Duration { *self = *self / rhs; } } + +impl<'a> fmt::Display for Duration { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} ticks", self.ticks) + } +} diff --git a/embassy/src/time/instant.rs b/embassy/src/time/instant.rs index 853fb745..0eb0d951 100644 --- a/embassy/src/time/instant.rs +++ b/embassy/src/time/instant.rs @@ -1,10 +1,8 @@ use core::convert::TryInto; -use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; -use core::pin::Pin; -use core::ptr; -use core::sync::atomic::{AtomicPtr, Ordering}; -use core::task::{Context, Poll}; +use core::ops::{Add, AddAssign, Sub, SubAssign}; +use core::fmt; +use super::TICKS_PER_SECOND; use super::{now, Duration}; #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)] @@ -21,10 +19,30 @@ impl Instant { Self { ticks } } - pub const fn into_ticks(&self) -> u64 { + pub const fn from_millis(millis: u64) -> Self { + Self { + ticks: millis * TICKS_PER_SECOND as u64 / 1000, + } + } + + pub const fn from_secs(seconds: u64) -> Self { + Self { + ticks: seconds * TICKS_PER_SECOND as u64, + } + } + + pub const fn as_ticks(&self) -> u64 { self.ticks } + pub const fn as_secs(&self) -> u64 { + self.ticks / TICKS_PER_SECOND as u64 + } + + pub const fn as_millis(&self) -> u64 { + self.ticks * 1000 / TICKS_PER_SECOND as u64 + } + pub fn duration_since(&self, earlier: Instant) -> Duration { Duration { ticks: (self.ticks - earlier.ticks).try_into().unwrap(), @@ -104,3 +122,9 @@ impl Sub for Instant { self.duration_since(other) } } + +impl<'a> fmt::Display for Instant { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{} ticks", self.ticks) + } +} diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index b3ae10e7..da2e1c21 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs @@ -11,7 +11,7 @@ pub use traits::*; use crate::util::Dewrap; // TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. -pub const TICKS_PER_SECOND: u32 = 32768; +pub const TICKS_PER_SECOND: u64 = 32768; static mut CLOCK: Option<&'static dyn Clock> = None; diff --git a/embassy/src/time/timer.rs b/embassy/src/time/timer.rs index 0315d9fb..10956300 100644 --- a/embassy/src/time/timer.rs +++ b/embassy/src/time/timer.rs @@ -13,7 +13,7 @@ pub struct Timer { impl Timer { pub fn at(when: Instant) -> Self { Self { - inner: current_timer_queue().deadline(when.into_ticks()), + inner: current_timer_queue().deadline(when.as_ticks()), } }