diff options
Diffstat (limited to 'embassy-time/src/duration.rs')
| -rw-r--r-- | embassy-time/src/duration.rs | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index 8366455be..647d208e3 100644 --- a/embassy-time/src/duration.rs +++ b/embassy-time/src/duration.rs | |||
| @@ -2,6 +2,7 @@ use core::fmt; | |||
| 2 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; | 2 | use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign}; |
| 3 | 3 | ||
| 4 | use super::{GCD_1K, GCD_1M, TICK_HZ}; | 4 | use super::{GCD_1K, GCD_1M, TICK_HZ}; |
| 5 | use crate::GCD_1G; | ||
| 5 | 6 | ||
| 6 | #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | 7 | #[derive(Debug, Default, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 8 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -61,6 +62,14 @@ impl Duration { | |||
| 61 | } | 62 | } |
| 62 | } | 63 | } |
| 63 | 64 | ||
| 65 | /// Creates a duration from the specified number of nanoseconds, rounding up. | ||
| 66 | /// NOTE: Delays this small may be inaccurate. | ||
| 67 | pub const fn from_nanos(micros: u64) -> Duration { | ||
| 68 | Duration { | ||
| 69 | ticks: div_ceil(micros * (TICK_HZ / GCD_1G), 1_000_000_000 / GCD_1G), | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 64 | /// Creates a duration from the specified number of seconds, rounding down. | 73 | /// Creates a duration from the specified number of seconds, rounding down. |
| 65 | pub const fn from_secs_floor(secs: u64) -> Duration { | 74 | pub const fn from_secs_floor(secs: u64) -> Duration { |
| 66 | Duration { ticks: secs * TICK_HZ } | 75 | Duration { ticks: secs * TICK_HZ } |
