diff options
| author | everdrone <[email protected]> | 2025-11-11 15:48:56 +0100 |
|---|---|---|
| committer | everdrone <[email protected]> | 2025-11-11 15:48:56 +0100 |
| commit | cede7216861a82b0db55f5a88afb3acf2ace6c4b (patch) | |
| tree | d92fb578897c77f51317318c5b180931b7b25c63 /embassy-time/src | |
| parent | cf55b39f9a54cf3ed01f52c0565a36a444174235 (diff) | |
| parent | 3d1f09597335d3681699ba09a77da4b39ed984fd (diff) | |
Merge branch main into n6
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/duration.rs | 13 | ||||
| -rw-r--r-- | embassy-time/src/instant.rs | 25 | ||||
| -rw-r--r-- | embassy-time/src/lib.rs | 11 | ||||
| -rw-r--r-- | embassy-time/src/timer.rs | 4 |
4 files changed, 36 insertions, 17 deletions
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index 5b140eeff..b2bfd6de9 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 } |
| @@ -170,13 +175,7 @@ impl Duration { | |||
| 170 | /// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1 | 175 | /// NOTE: Giving this function a hz >= the TICK_HZ of your platform will clamp the Duration to 1 |
| 171 | /// tick. Doing so will not deadlock, but will certainly not produce the desired output. | 176 | /// tick. Doing so will not deadlock, but will certainly not produce the desired output. |
| 172 | pub const fn from_hz(hz: u64) -> Duration { | 177 | pub const fn from_hz(hz: u64) -> Duration { |
| 173 | let ticks = { | 178 | let ticks = { if hz >= TICK_HZ { 1 } else { (TICK_HZ + hz / 2) / hz } }; |
| 174 | if hz >= TICK_HZ { | ||
| 175 | 1 | ||
| 176 | } else { | ||
| 177 | (TICK_HZ + hz / 2) / hz | ||
| 178 | } | ||
| 179 | }; | ||
| 180 | Duration { ticks } | 179 | Duration { ticks } |
| 181 | } | 180 | } |
| 182 | 181 | ||
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 { |
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs index 77f4b344d..e375fe93e 100644 --- a/embassy-time/src/lib.rs +++ b/embassy-time/src/lib.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | #![cfg_attr(not(any(feature = "std", feature = "wasm", test)), no_std)] | 1 | #![cfg_attr(not(any(feature = "std", feature = "wasm", test)), no_std)] |
| 2 | #![allow(async_fn_in_trait)] | 2 | #![allow(async_fn_in_trait)] |
| 3 | #![allow(unsafe_op_in_unsafe_fn)] | ||
| 3 | #![doc = include_str!("../README.md")] | 4 | #![doc = include_str!("../README.md")] |
| 4 | #![allow(clippy::new_without_default)] | 5 | #![allow(clippy::new_without_default)] |
| 5 | #![warn(missing_docs)] | 6 | #![warn(missing_docs)] |
| @@ -27,18 +28,14 @@ mod driver_std; | |||
| 27 | #[cfg(feature = "wasm")] | 28 | #[cfg(feature = "wasm")] |
| 28 | mod driver_wasm; | 29 | mod driver_wasm; |
| 29 | 30 | ||
| 30 | pub use delay::{block_for, Delay}; | 31 | pub use delay::{Delay, block_for}; |
| 31 | pub use duration::Duration; | 32 | pub use duration::Duration; |
| 32 | pub use embassy_time_driver::TICK_HZ; | 33 | pub use embassy_time_driver::TICK_HZ; |
| 33 | pub use instant::Instant; | 34 | pub use instant::Instant; |
| 34 | pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer, WithTimeout}; | 35 | pub use timer::{Ticker, TimeoutError, Timer, WithTimeout, with_deadline, with_timeout}; |
| 35 | 36 | ||
| 36 | const fn gcd(a: u64, b: u64) -> u64 { | 37 | const fn gcd(a: u64, b: u64) -> u64 { |
| 37 | if b == 0 { | 38 | if b == 0 { a } else { gcd(b, a % b) } |
| 38 | a | ||
| 39 | } else { | ||
| 40 | gcd(b, a % b) | ||
| 41 | } | ||
| 42 | } | 39 | } |
| 43 | 40 | ||
| 44 | pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000); | 41 | pub(crate) const GCD_1K: u64 = gcd(TICK_HZ, 1_000); |
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index fcf79f58e..2f5967c63 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | use core::future::{poll_fn, Future}; | 1 | use core::future::{Future, poll_fn}; |
| 2 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll}; | 3 | use core::task::{Context, Poll}; |
| 4 | 4 | ||
| 5 | use futures_core::stream::FusedStream; | ||
| 6 | use futures_core::Stream; | 5 | use futures_core::Stream; |
| 6 | use futures_core::stream::FusedStream; | ||
| 7 | 7 | ||
| 8 | use crate::{Duration, Instant}; | 8 | use crate::{Duration, Instant}; |
| 9 | 9 | ||
