diff options
| author | Matteo Meluzzi <[email protected]> | 2025-10-02 10:53:31 +0200 |
|---|---|---|
| committer | Matteo Meluzzi <[email protected]> | 2025-10-02 10:53:31 +0200 |
| commit | 828a8df18d04877df1f55f04354980b28ff2f2f8 (patch) | |
| tree | c4fa405f5eba7a14b6d435d6cc746c9e0dc52632 /embassy-time | |
| parent | 176649e71ad442ca9856af6c11989b0b2f228c4b (diff) | |
| parent | 194a721d0eab929a2af0a2a4e45ca8e70e0d3f0a (diff) | |
Merge branch 'main' into 17-add-support-for-boot-protocol
Diffstat (limited to 'embassy-time')
| -rw-r--r-- | embassy-time/CHANGELOG.md | 2 | ||||
| -rw-r--r-- | embassy-time/Cargo.toml | 5 | ||||
| -rw-r--r-- | embassy-time/src/duration.rs | 5 | ||||
| -rw-r--r-- | embassy-time/src/instant.rs | 25 |
4 files changed, 36 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/Cargo.toml b/embassy-time/Cargo.toml index 2d7c3c1fa..bad6ecf97 100644 --- a/embassy-time/Cargo.toml +++ b/embassy-time/Cargo.toml | |||
| @@ -31,6 +31,11 @@ target = "x86_64-unknown-linux-gnu" | |||
| 31 | features = ["defmt", "std"] | 31 | features = ["defmt", "std"] |
| 32 | 32 | ||
| 33 | [features] | 33 | [features] |
| 34 | ## Enable defmt | ||
| 35 | defmt = ["dep:defmt"] | ||
| 36 | ## Enable log | ||
| 37 | log = ["dep:log"] | ||
| 38 | |||
| 34 | ## Display the time since startup next to defmt log messages. | 39 | ## Display the time since startup next to defmt log messages. |
| 35 | ## At most 1 `defmt-timestamp-uptime-*` feature can be used. | 40 | ## At most 1 `defmt-timestamp-uptime-*` feature can be used. |
| 36 | ## `defmt-timestamp-uptime` is provided for backwards compatibility (provides the same format as `uptime-us`). | 41 | ## `defmt-timestamp-uptime` is provided for backwards compatibility (provides the same format as `uptime-us`). |
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 { |
