diff options
| -rw-r--r-- | embassy/src/time/duration.rs | 29 | ||||
| -rw-r--r-- | embassy/src/time/instant.rs | 36 | ||||
| -rw-r--r-- | embassy/src/time/mod.rs | 2 | ||||
| -rw-r--r-- | embassy/src/time/timer.rs | 2 |
4 files changed, 54 insertions, 15 deletions
diff --git a/embassy/src/time/duration.rs b/embassy/src/time/duration.rs index 4bc515c0b..4ff8d40e9 100644 --- a/embassy/src/time/duration.rs +++ b/embassy/src/time/duration.rs | |||
| @@ -1,28 +1,37 @@ | |||
| 1 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; | 1 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; |
| 2 | use core::fmt; | ||
| 2 | 3 | ||
| 3 | use super::TICKS_PER_SECOND; | 4 | use super::TICKS_PER_SECOND; |
| 4 | 5 | ||
| 5 | #[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | 6 | #[derive(defmt::Format, Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 6 | pub struct Duration { | 7 | pub struct Duration { |
| 7 | pub(crate) ticks: u32, | 8 | pub(crate) ticks: u64, |
| 8 | } | 9 | } |
| 9 | 10 | ||
| 10 | impl Duration { | 11 | impl Duration { |
| 11 | pub const fn into_ticks(&self) -> u32 { | 12 | pub const fn as_ticks(&self) -> u64 { |
| 12 | self.ticks | 13 | self.ticks |
| 13 | } | 14 | } |
| 14 | 15 | ||
| 15 | pub const fn from_ticks(ticks: u32) -> Duration { | 16 | pub const fn as_secs(&self) -> u64 { |
| 17 | self.ticks / TICKS_PER_SECOND | ||
| 18 | } | ||
| 19 | |||
| 20 | pub const fn as_millis(&self) -> u64 { | ||
| 21 | self.ticks * 1000 / TICKS_PER_SECOND | ||
| 22 | } | ||
| 23 | |||
| 24 | pub const fn from_ticks(ticks: u64) -> Duration { | ||
| 16 | Duration { ticks } | 25 | Duration { ticks } |
| 17 | } | 26 | } |
| 18 | 27 | ||
| 19 | pub const fn from_secs(secs: u32) -> Duration { | 28 | pub const fn from_secs(secs: u64) -> Duration { |
| 20 | Duration { | 29 | Duration { |
| 21 | ticks: secs * TICKS_PER_SECOND, | 30 | ticks: secs * TICKS_PER_SECOND, |
| 22 | } | 31 | } |
| 23 | } | 32 | } |
| 24 | 33 | ||
| 25 | pub const fn from_millis(millis: u32) -> Duration { | 34 | pub const fn from_millis(millis: u64) -> Duration { |
| 26 | Duration { | 35 | Duration { |
| 27 | ticks: millis * TICKS_PER_SECOND / 1000, | 36 | ticks: millis * TICKS_PER_SECOND / 1000, |
| 28 | } | 37 | } |
| @@ -41,11 +50,11 @@ impl Duration { | |||
| 41 | } | 50 | } |
| 42 | 51 | ||
| 43 | pub fn checked_mul(self, rhs: u32) -> Option<Duration> { | 52 | pub fn checked_mul(self, rhs: u32) -> Option<Duration> { |
| 44 | self.ticks.checked_mul(rhs).map(|ticks| Duration { ticks }) | 53 | self.ticks.checked_mul(rhs as _).map(|ticks| Duration { ticks }) |
| 45 | } | 54 | } |
| 46 | 55 | ||
| 47 | pub fn checked_div(self, rhs: u32) -> Option<Duration> { | 56 | pub fn checked_div(self, rhs: u32) -> Option<Duration> { |
| 48 | self.ticks.checked_div(rhs).map(|ticks| Duration { ticks }) | 57 | self.ticks.checked_div(rhs as _).map(|ticks| Duration { ticks }) |
| 49 | } | 58 | } |
| 50 | } | 59 | } |
| 51 | 60 | ||
| @@ -116,3 +125,9 @@ impl DivAssign<u32> for Duration { | |||
| 116 | *self = *self / rhs; | 125 | *self = *self / rhs; |
| 117 | } | 126 | } |
| 118 | } | 127 | } |
| 128 | |||
| 129 | impl<'a> fmt::Display for Duration { | ||
| 130 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| 131 | write!(f, "{} ticks", self.ticks) | ||
| 132 | } | ||
| 133 | } | ||
diff --git a/embassy/src/time/instant.rs b/embassy/src/time/instant.rs index 853fb745f..0eb0d9517 100644 --- a/embassy/src/time/instant.rs +++ b/embassy/src/time/instant.rs | |||
| @@ -1,10 +1,8 @@ | |||
| 1 | use core::convert::TryInto; | 1 | use core::convert::TryInto; |
| 2 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; | 2 | use core::ops::{Add, AddAssign, Sub, SubAssign}; |
| 3 | use core::pin::Pin; | 3 | use core::fmt; |
| 4 | use core::ptr; | ||
| 5 | use core::sync::atomic::{AtomicPtr, Ordering}; | ||
| 6 | use core::task::{Context, Poll}; | ||
| 7 | 4 | ||
| 5 | use super::TICKS_PER_SECOND; | ||
| 8 | use super::{now, Duration}; | 6 | use super::{now, Duration}; |
| 9 | 7 | ||
| 10 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)] | 8 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, defmt::Format)] |
| @@ -21,10 +19,30 @@ impl Instant { | |||
| 21 | Self { ticks } | 19 | Self { ticks } |
| 22 | } | 20 | } |
| 23 | 21 | ||
| 24 | pub const fn into_ticks(&self) -> u64 { | 22 | pub const fn from_millis(millis: u64) -> Self { |
| 23 | Self { | ||
| 24 | ticks: millis * TICKS_PER_SECOND as u64 / 1000, | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | pub const fn from_secs(seconds: u64) -> Self { | ||
| 29 | Self { | ||
| 30 | ticks: seconds * TICKS_PER_SECOND as u64, | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | pub const fn as_ticks(&self) -> u64 { | ||
| 25 | self.ticks | 35 | self.ticks |
| 26 | } | 36 | } |
| 27 | 37 | ||
| 38 | pub const fn as_secs(&self) -> u64 { | ||
| 39 | self.ticks / TICKS_PER_SECOND as u64 | ||
| 40 | } | ||
| 41 | |||
| 42 | pub const fn as_millis(&self) -> u64 { | ||
| 43 | self.ticks * 1000 / TICKS_PER_SECOND as u64 | ||
| 44 | } | ||
| 45 | |||
| 28 | pub fn duration_since(&self, earlier: Instant) -> Duration { | 46 | pub fn duration_since(&self, earlier: Instant) -> Duration { |
| 29 | Duration { | 47 | Duration { |
| 30 | ticks: (self.ticks - earlier.ticks).try_into().unwrap(), | 48 | ticks: (self.ticks - earlier.ticks).try_into().unwrap(), |
| @@ -104,3 +122,9 @@ impl Sub<Instant> for Instant { | |||
| 104 | self.duration_since(other) | 122 | self.duration_since(other) |
| 105 | } | 123 | } |
| 106 | } | 124 | } |
| 125 | |||
| 126 | impl<'a> fmt::Display for Instant { | ||
| 127 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
| 128 | write!(f, "{} ticks", self.ticks) | ||
| 129 | } | ||
| 130 | } | ||
diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index b3ae10e72..da2e1c21d 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs | |||
| @@ -11,7 +11,7 @@ pub use traits::*; | |||
| 11 | use crate::util::Dewrap; | 11 | use crate::util::Dewrap; |
| 12 | 12 | ||
| 13 | // TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. | 13 | // TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. |
| 14 | pub const TICKS_PER_SECOND: u32 = 32768; | 14 | pub const TICKS_PER_SECOND: u64 = 32768; |
| 15 | 15 | ||
| 16 | static mut CLOCK: Option<&'static dyn Clock> = None; | 16 | static mut CLOCK: Option<&'static dyn Clock> = None; |
| 17 | 17 | ||
diff --git a/embassy/src/time/timer.rs b/embassy/src/time/timer.rs index 0315d9fba..109563008 100644 --- a/embassy/src/time/timer.rs +++ b/embassy/src/time/timer.rs | |||
| @@ -13,7 +13,7 @@ pub struct Timer { | |||
| 13 | impl Timer { | 13 | impl Timer { |
| 14 | pub fn at(when: Instant) -> Self { | 14 | pub fn at(when: Instant) -> Self { |
| 15 | Self { | 15 | Self { |
| 16 | inner: current_timer_queue().deadline(when.into_ticks()), | 16 | inner: current_timer_queue().deadline(when.as_ticks()), |
| 17 | } | 17 | } |
| 18 | } | 18 | } |
| 19 | 19 | ||
