diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-09-22 11:01:27 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-22 11:01:27 +0000 |
| commit | de5dd10a5832b330465d93399b3a9cb761e24029 (patch) | |
| tree | f434feced9d05ca3dd170d7517647e1455a12494 | |
| parent | a25c5c4b2ac04bc7289953e4d7eb68111323df4d (diff) | |
| parent | 4d9563805cee8a14f7c59a5b28227b99a6ffbf75 (diff) | |
Merge pull request #4695 from meowcakes/time-as-nanos
Add `as_nanos` and `from_nanos` where missing in embassy-time
| -rw-r--r-- | embassy-time/CHANGELOG.md | 2 | ||||
| -rw-r--r-- | embassy-time/src/duration.rs | 5 | ||||
| -rw-r--r-- | embassy-time/src/instant.rs | 25 |
3 files changed, 31 insertions, 1 deletions
diff --git a/embassy-time/CHANGELOG.md b/embassy-time/CHANGELOG.md index 572571215..4a50da8ef 100644 --- a/embassy-time/CHANGELOG.md +++ b/embassy-time/CHANGELOG.md | |||
| @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
| 8 | <!-- next-header --> | 8 | <!-- next-header --> |
| 9 | ## Unreleased - ReleaseDate | 9 | ## Unreleased - ReleaseDate |
| 10 | 10 | ||
| 11 | - Add as_nanos and from_nanos where missing | ||
| 12 | |||
| 11 | ## 0.5.0 - 2025-08-26 | 13 | ## 0.5.0 - 2025-08-26 |
| 12 | 14 | ||
| 13 | - Allow inlining on time driver boundary | 15 | - Allow inlining on time driver boundary |
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..de5ebebf8 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_1G, GCD_1K, GCD_1M, 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 { |
| @@ -50,6 +57,17 @@ impl Instant { | |||
| 50 | } | 57 | } |
| 51 | } | 58 | } |
| 52 | 59 | ||
| 60 | /// Try to create an Instant from a nanosecond count since system boot. | ||
| 61 | /// Fails if the number of nanoseconds is too large. | ||
| 62 | pub const fn try_from_nanos(nanos: u64) -> Option<Self> { | ||
| 63 | let Some(value) = nanos.checked_mul(TICK_HZ / GCD_1G) else { | ||
| 64 | return None; | ||
| 65 | }; | ||
| 66 | Some(Self { | ||
| 67 | ticks: value / (1_000_000_000 / GCD_1G), | ||
| 68 | }) | ||
| 69 | } | ||
| 70 | |||
| 53 | /// Try to create an Instant from a microsecond count since system boot. | 71 | /// Try to create an Instant from a microsecond count since system boot. |
| 54 | /// Fails if the number of microseconds is too large. | 72 | /// Fails if the number of microseconds is too large. |
| 55 | pub const fn try_from_micros(micros: u64) -> Option<Self> { | 73 | pub const fn try_from_micros(micros: u64) -> Option<Self> { |
| @@ -101,6 +119,11 @@ impl Instant { | |||
| 101 | self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) | 119 | self.ticks * (1_000_000 / GCD_1M) / (TICK_HZ / GCD_1M) |
| 102 | } | 120 | } |
| 103 | 121 | ||
| 122 | /// Nanoseconds since system boot. | ||
| 123 | pub const fn as_nanos(&self) -> u64 { | ||
| 124 | self.ticks * (1_000_000_000 / GCD_1G) / (TICK_HZ / GCD_1G) | ||
| 125 | } | ||
| 126 | |||
| 104 | /// Duration between this Instant and another Instant | 127 | /// Duration between this Instant and another Instant |
| 105 | /// Panics on over/underflow. | 128 | /// Panics on over/underflow. |
| 106 | pub fn duration_since(&self, earlier: Instant) -> Duration { | 129 | pub fn duration_since(&self, earlier: Instant) -> Duration { |
