diff options
| author | Rogan Morrow <[email protected]> | 2025-09-22 13:36:22 +1000 |
|---|---|---|
| committer | Rogan Morrow <[email protected]> | 2025-09-22 13:36:22 +1000 |
| commit | a0a204f586301ee4e014c3673c2ecd3ad90c504c (patch) | |
| tree | 520ba0ab08f0512c958d9342e4388d37f26b68c6 | |
| parent | 539837a7485381f83ef078595a4e248a0ea11436 (diff) | |
add as_nanos and from_nanos where missing
| -rw-r--r-- | embassy-time/src/duration.rs | 5 | ||||
| -rw-r--r-- | embassy-time/src/instant.rs | 14 |
2 files changed, 18 insertions, 1 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index 5b140eeff..b3ea0468d 100644 --- a/embassy-time/src/duration.rs +++ b/embassy-time/src/duration.rs | |||
| @@ -37,6 +37,11 @@ impl Duration { | |||
| 37 | self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) | 37 | self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | /// Convert the `Duration` to nanoseconds, rounding down. | ||
| 41 | pub const fn as_nanos(&self) -> u64 { | ||
| 42 | self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G) | ||
| 43 | } | ||
| 44 | |||
| 40 | /// Creates a duration from the specified number of clock ticks | 45 | /// Creates a duration from the specified number of clock ticks |
| 41 | pub const fn from_ticks(ticks: u64) -> Duration { | 46 | pub const fn from_ticks(ticks: u64) -> Duration { |
| 42 | Duration { ticks } | 47 | Duration { ticks } |
diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs index 6571bea62..a311b365c 100644 --- a/embassy-time/src/instant.rs +++ b/embassy-time/src/instant.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | use core::fmt; | 1 | use core::fmt; |
| 2 | use core::ops::{Add, AddAssign, Sub, SubAssign}; | 2 | use core::ops::{Add, AddAssign, Sub, SubAssign}; |
| 3 | 3 | ||
| 4 | use super::{Duration, GCD_1K, GCD_1M, TICK_HZ}; | 4 | use super::{Duration, GCD_1K, GCD_1M, GCD_1G, TICK_HZ}; |
| 5 | 5 | ||
| 6 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] | 6 | #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] |
| 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | 7 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| @@ -29,6 +29,13 @@ impl Instant { | |||
| 29 | Self { ticks } | 29 | Self { ticks } |
| 30 | } | 30 | } |
| 31 | 31 | ||
| 32 | /// Create an Instant from a nanosecond count since system boot. | ||
| 33 | pub const fn from_nanos(nanos: u64) -> Self { | ||
| 34 | Self { | ||
| 35 | ticks: nanos * (TICK_HZ / GCD_1G) / (1_000_000_000 / GCD_1G), | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 32 | /// Create an Instant from a microsecond count since system boot. | 39 | /// Create an Instant from a microsecond count since system boot. |
| 33 | pub const fn from_micros(micros: u64) -> Self { | 40 | pub const fn from_micros(micros: u64) -> Self { |
| 34 | Self { | 41 | Self { |
| @@ -101,6 +108,11 @@ impl Instant { | |||
| 101 | self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) | 108 | self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) |
| 102 | } | 109 | } |
| 103 | 110 | ||
| 111 | /// Nanoseconds since system boot. | ||
| 112 | pub const fn as_nanos(&self) -> u64 { | ||
| 113 | self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G) | ||
| 114 | } | ||
| 115 | |||
| 104 | /// Duration between this Instant and another Instant | 116 | /// Duration between this Instant and another Instant |
| 105 | /// Panics on over/underflow. | 117 | /// Panics on over/underflow. |
| 106 | pub fn duration_since(&self, earlier: Instant) -> Duration { | 118 | pub fn duration_since(&self, earlier: Instant) -> Duration { |
