diff options
| author | 1-rafael-1 <[email protected]> | 2025-09-15 20:07:18 +0200 |
|---|---|---|
| committer | 1-rafael-1 <[email protected]> | 2025-09-15 20:07:18 +0200 |
| commit | 6bb3d2c0720fa082f27d3cdb70f516058497ec87 (patch) | |
| tree | 5a1e255cff999b00800f203b91a759c720c973e5 /embassy-time/src | |
| parent | eb685574601d98c44faed9a3534d056199b46e20 (diff) | |
| parent | 92a6fd2946f2cbb15359290f68aa360953da2ff7 (diff) | |
Merge branch 'main' into rp2040-rtc-alarm
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/delay.rs | 17 | ||||
| -rw-r--r-- | embassy-time/src/driver_mock.rs | 2 | ||||
| -rw-r--r-- | embassy-time/src/driver_std.rs | 3 | ||||
| -rw-r--r-- | embassy-time/src/driver_wasm.rs | 6 | ||||
| -rw-r--r-- | embassy-time/src/duration.rs | 9 | ||||
| -rw-r--r-- | embassy-time/src/lib.rs | 1 | ||||
| -rw-r--r-- | embassy-time/src/timer.rs | 11 |
7 files changed, 40 insertions, 9 deletions
diff --git a/embassy-time/src/delay.rs b/embassy-time/src/delay.rs index f77859d4a..11b54b098 100644 --- a/embassy-time/src/delay.rs +++ b/embassy-time/src/delay.rs | |||
| @@ -1,3 +1,5 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | |||
| 1 | use super::{Duration, Instant}; | 3 | use super::{Duration, Instant}; |
| 2 | use crate::Timer; | 4 | use crate::Timer; |
| 3 | 5 | ||
| @@ -13,7 +15,8 @@ pub fn block_for(duration: Duration) { | |||
| 13 | /// the amount provided, but accuracy can be affected by many factors, including interrupt usage. | 15 | /// the amount provided, but accuracy can be affected by many factors, including interrupt usage. |
| 14 | /// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently | 16 | /// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently |
| 15 | /// active driver. | 17 | /// active driver. |
| 16 | #[derive(Clone)] | 18 | #[derive(Clone, Debug)] |
| 19 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 17 | pub struct Delay; | 20 | pub struct Delay; |
| 18 | 21 | ||
| 19 | impl embedded_hal_1::delay::DelayNs for Delay { | 22 | impl embedded_hal_1::delay::DelayNs for Delay { |
| @@ -31,16 +34,16 @@ impl embedded_hal_1::delay::DelayNs for Delay { | |||
| 31 | } | 34 | } |
| 32 | 35 | ||
| 33 | impl embedded_hal_async::delay::DelayNs for Delay { | 36 | impl embedded_hal_async::delay::DelayNs for Delay { |
| 34 | async fn delay_ns(&mut self, ns: u32) { | 37 | fn delay_ns(&mut self, ns: u32) -> impl Future<Output = ()> { |
| 35 | Timer::after_nanos(ns as _).await | 38 | Timer::after_nanos(ns as _) |
| 36 | } | 39 | } |
| 37 | 40 | ||
| 38 | async fn delay_us(&mut self, us: u32) { | 41 | fn delay_us(&mut self, us: u32) -> impl Future<Output = ()> { |
| 39 | Timer::after_micros(us as _).await | 42 | Timer::after_micros(us as _) |
| 40 | } | 43 | } |
| 41 | 44 | ||
| 42 | async fn delay_ms(&mut self, ms: u32) { | 45 | fn delay_ms(&mut self, ms: u32) -> impl Future<Output = ()> { |
| 43 | Timer::after_millis(ms as _).await | 46 | Timer::after_millis(ms as _) |
| 44 | } | 47 | } |
| 45 | } | 48 | } |
| 46 | 49 | ||
diff --git a/embassy-time/src/driver_mock.rs b/embassy-time/src/driver_mock.rs index bb1961bf2..bcde2a6c9 100644 --- a/embassy-time/src/driver_mock.rs +++ b/embassy-time/src/driver_mock.rs | |||
| @@ -28,6 +28,7 @@ use crate::{Duration, Instant}; | |||
| 28 | /// assert_eq!(true, has_a_second_passed(reference)); | 28 | /// assert_eq!(true, has_a_second_passed(reference)); |
| 29 | /// } | 29 | /// } |
| 30 | /// ``` | 30 | /// ``` |
| 31 | #[derive(Debug)] | ||
| 31 | pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>); | 32 | pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>); |
| 32 | 33 | ||
| 33 | embassy_time_driver::time_driver_impl!(static DRIVER: MockDriver = MockDriver::new()); | 34 | embassy_time_driver::time_driver_impl!(static DRIVER: MockDriver = MockDriver::new()); |
| @@ -80,6 +81,7 @@ impl Driver for MockDriver { | |||
| 80 | } | 81 | } |
| 81 | } | 82 | } |
| 82 | 83 | ||
| 84 | #[derive(Debug)] | ||
| 83 | struct InnerMockDriver { | 85 | struct InnerMockDriver { |
| 84 | now: Instant, | 86 | now: Instant, |
| 85 | queue: Queue, | 87 | queue: Queue, |
diff --git a/embassy-time/src/driver_std.rs b/embassy-time/src/driver_std.rs index 87d7ef7eb..0cdb8f4ac 100644 --- a/embassy-time/src/driver_std.rs +++ b/embassy-time/src/driver_std.rs | |||
| @@ -5,11 +5,13 @@ use std::time::{Duration as StdDuration, Instant as StdInstant}; | |||
| 5 | use embassy_time_driver::Driver; | 5 | use embassy_time_driver::Driver; |
| 6 | use embassy_time_queue_utils::Queue; | 6 | use embassy_time_queue_utils::Queue; |
| 7 | 7 | ||
| 8 | #[derive(Debug)] | ||
| 8 | struct TimeDriver { | 9 | struct TimeDriver { |
| 9 | signaler: Signaler, | 10 | signaler: Signaler, |
| 10 | inner: Mutex<Inner>, | 11 | inner: Mutex<Inner>, |
| 11 | } | 12 | } |
| 12 | 13 | ||
| 14 | #[derive(Debug)] | ||
| 13 | struct Inner { | 15 | struct Inner { |
| 14 | zero_instant: Option<StdInstant>, | 16 | zero_instant: Option<StdInstant>, |
| 15 | queue: Queue, | 17 | queue: Queue, |
| @@ -64,6 +66,7 @@ fn alarm_thread() { | |||
| 64 | } | 66 | } |
| 65 | } | 67 | } |
| 66 | 68 | ||
| 69 | #[derive(Debug)] | ||
| 67 | struct Signaler { | 70 | struct Signaler { |
| 68 | mutex: Mutex<bool>, | 71 | mutex: Mutex<bool>, |
| 69 | condvar: Condvar, | 72 | condvar: Condvar, |
diff --git a/embassy-time/src/driver_wasm.rs b/embassy-time/src/driver_wasm.rs index e3207691a..646ce170e 100644 --- a/embassy-time/src/driver_wasm.rs +++ b/embassy-time/src/driver_wasm.rs | |||
| @@ -5,6 +5,8 @@ use embassy_time_queue_utils::Queue; | |||
| 5 | use wasm_bindgen::prelude::*; | 5 | use wasm_bindgen::prelude::*; |
| 6 | use wasm_timer::Instant as StdInstant; | 6 | use wasm_timer::Instant as StdInstant; |
| 7 | 7 | ||
| 8 | #[derive(Debug)] | ||
| 9 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 8 | struct AlarmState { | 10 | struct AlarmState { |
| 9 | token: Option<f64>, | 11 | token: Option<f64>, |
| 10 | } | 12 | } |
| @@ -21,10 +23,14 @@ extern "C" { | |||
| 21 | fn clearTimeout(token: f64); | 23 | fn clearTimeout(token: f64); |
| 22 | } | 24 | } |
| 23 | 25 | ||
| 26 | #[derive(Debug)] | ||
| 27 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 24 | struct TimeDriver { | 28 | struct TimeDriver { |
| 25 | inner: Mutex<Inner>, | 29 | inner: Mutex<Inner>, |
| 26 | } | 30 | } |
| 27 | 31 | ||
| 32 | #[derive(Debug)] | ||
| 33 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 28 | struct Inner { | 34 | struct Inner { |
| 29 | alarm: AlarmState, | 35 | alarm: AlarmState, |
| 30 | zero_instant: Option<StdInstant>, | 36 | zero_instant: Option<StdInstant>, |
diff --git a/embassy-time/src/duration.rs b/embassy-time/src/duration.rs index dcda705d3..5b140eeff 100644 --- a/embassy-time/src/duration.rs +++ b/embassy-time/src/duration.rs | |||
| @@ -293,3 +293,12 @@ impl From<Duration> for core::time::Duration { | |||
| 293 | core::time::Duration::from_micros(value.as_micros()) | 293 | core::time::Duration::from_micros(value.as_micros()) |
| 294 | } | 294 | } |
| 295 | } | 295 | } |
| 296 | |||
| 297 | impl core::iter::Sum for Duration { | ||
| 298 | fn sum<I>(iter: I) -> Self | ||
| 299 | where | ||
| 300 | I: Iterator<Item = Duration>, | ||
| 301 | { | ||
| 302 | Duration::from_ticks(iter.map(|d| d.as_ticks()).sum()) | ||
| 303 | } | ||
| 304 | } | ||
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs index 80a359413..77f4b344d 100644 --- a/embassy-time/src/lib.rs +++ b/embassy-time/src/lib.rs | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | #![doc = include_str!("../README.md")] | 3 | #![doc = include_str!("../README.md")] |
| 4 | #![allow(clippy::new_without_default)] | 4 | #![allow(clippy::new_without_default)] |
| 5 | #![warn(missing_docs)] | 5 | #![warn(missing_docs)] |
| 6 | #![deny(missing_debug_implementations)] | ||
| 6 | 7 | ||
| 7 | //! ## Feature flags | 8 | //! ## Feature flags |
| 8 | #![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)] | 9 | #![doc = document_features::document_features!(feature_label = r#"<span class="stab portability"><code>{feature}</code></span>"#)] |
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index d1162eadd..fcf79f58e 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -2,8 +2,8 @@ use core::future::{poll_fn, Future}; | |||
| 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_util::stream::FusedStream; | 5 | use futures_core::stream::FusedStream; |
| 6 | use futures_util::Stream; | 6 | use futures_core::Stream; |
| 7 | 7 | ||
| 8 | use crate::{Duration, Instant}; | 8 | use crate::{Duration, Instant}; |
| 9 | 9 | ||
| @@ -66,6 +66,8 @@ impl<F: Future> WithTimeout for F { | |||
| 66 | 66 | ||
| 67 | /// Future for the [`with_timeout`] and [`with_deadline`] functions. | 67 | /// Future for the [`with_timeout`] and [`with_deadline`] functions. |
| 68 | #[must_use = "futures do nothing unless you `.await` or poll them"] | 68 | #[must_use = "futures do nothing unless you `.await` or poll them"] |
| 69 | #[derive(Debug)] | ||
| 70 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 69 | pub struct TimeoutFuture<F> { | 71 | pub struct TimeoutFuture<F> { |
| 70 | timer: Timer, | 72 | timer: Timer, |
| 71 | fut: F, | 73 | fut: F, |
| @@ -92,6 +94,8 @@ impl<F: Future> Future for TimeoutFuture<F> { | |||
| 92 | 94 | ||
| 93 | /// A future that completes at a specified [Instant](struct.Instant.html). | 95 | /// A future that completes at a specified [Instant](struct.Instant.html). |
| 94 | #[must_use = "futures do nothing unless you `.await` or poll them"] | 96 | #[must_use = "futures do nothing unless you `.await` or poll them"] |
| 97 | #[derive(Debug)] | ||
| 98 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 95 | pub struct Timer { | 99 | pub struct Timer { |
| 96 | expires_at: Instant, | 100 | expires_at: Instant, |
| 97 | yielded_once: bool, | 101 | yielded_once: bool, |
| @@ -99,6 +103,7 @@ pub struct Timer { | |||
| 99 | 103 | ||
| 100 | impl Timer { | 104 | impl Timer { |
| 101 | /// Expire at specified [Instant](struct.Instant.html) | 105 | /// Expire at specified [Instant](struct.Instant.html) |
| 106 | /// Will expire immediately if the Instant is in the past. | ||
| 102 | pub fn at(expires_at: Instant) -> Self { | 107 | pub fn at(expires_at: Instant) -> Self { |
| 103 | Self { | 108 | Self { |
| 104 | expires_at, | 109 | expires_at, |
| @@ -227,6 +232,8 @@ impl Future for Timer { | |||
| 227 | /// ## Cancel safety | 232 | /// ## Cancel safety |
| 228 | /// It is safe to cancel waiting for the next tick, | 233 | /// It is safe to cancel waiting for the next tick, |
| 229 | /// meaning no tick is lost if the Future is dropped. | 234 | /// meaning no tick is lost if the Future is dropped. |
| 235 | #[derive(Debug)] | ||
| 236 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 230 | pub struct Ticker { | 237 | pub struct Ticker { |
| 231 | expires_at: Instant, | 238 | expires_at: Instant, |
| 232 | duration: Duration, | 239 | duration: Duration, |
