aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src
diff options
context:
space:
mode:
author1-rafael-1 <[email protected]>2025-09-15 20:07:18 +0200
committer1-rafael-1 <[email protected]>2025-09-15 20:07:18 +0200
commit6bb3d2c0720fa082f27d3cdb70f516058497ec87 (patch)
tree5a1e255cff999b00800f203b91a759c720c973e5 /embassy-time/src
parenteb685574601d98c44faed9a3534d056199b46e20 (diff)
parent92a6fd2946f2cbb15359290f68aa360953da2ff7 (diff)
Merge branch 'main' into rp2040-rtc-alarm
Diffstat (limited to 'embassy-time/src')
-rw-r--r--embassy-time/src/delay.rs17
-rw-r--r--embassy-time/src/driver_mock.rs2
-rw-r--r--embassy-time/src/driver_std.rs3
-rw-r--r--embassy-time/src/driver_wasm.rs6
-rw-r--r--embassy-time/src/duration.rs9
-rw-r--r--embassy-time/src/lib.rs1
-rw-r--r--embassy-time/src/timer.rs11
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 @@
1use core::future::Future;
2
1use super::{Duration, Instant}; 3use super::{Duration, Instant};
2use crate::Timer; 4use 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))]
17pub struct Delay; 20pub struct Delay;
18 21
19impl embedded_hal_1::delay::DelayNs for Delay { 22impl embedded_hal_1::delay::DelayNs for Delay {
@@ -31,16 +34,16 @@ impl embedded_hal_1::delay::DelayNs for Delay {
31} 34}
32 35
33impl embedded_hal_async::delay::DelayNs for Delay { 36impl 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)]
31pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>); 32pub struct MockDriver(CsMutex<RefCell<InnerMockDriver>>);
32 33
33embassy_time_driver::time_driver_impl!(static DRIVER: MockDriver = MockDriver::new()); 34embassy_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)]
83struct InnerMockDriver { 85struct 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};
5use embassy_time_driver::Driver; 5use embassy_time_driver::Driver;
6use embassy_time_queue_utils::Queue; 6use embassy_time_queue_utils::Queue;
7 7
8#[derive(Debug)]
8struct TimeDriver { 9struct TimeDriver {
9 signaler: Signaler, 10 signaler: Signaler,
10 inner: Mutex<Inner>, 11 inner: Mutex<Inner>,
11} 12}
12 13
14#[derive(Debug)]
13struct Inner { 15struct 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)]
67struct Signaler { 70struct 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;
5use wasm_bindgen::prelude::*; 5use wasm_bindgen::prelude::*;
6use wasm_timer::Instant as StdInstant; 6use wasm_timer::Instant as StdInstant;
7 7
8#[derive(Debug)]
9#[cfg_attr(feature = "defmt", derive(defmt::Format))]
8struct AlarmState { 10struct 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))]
24struct TimeDriver { 28struct TimeDriver {
25 inner: Mutex<Inner>, 29 inner: Mutex<Inner>,
26} 30}
27 31
32#[derive(Debug)]
33#[cfg_attr(feature = "defmt", derive(defmt::Format))]
28struct Inner { 34struct 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
297impl 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};
2use core::pin::Pin; 2use core::pin::Pin;
3use core::task::{Context, Poll}; 3use core::task::{Context, Poll};
4 4
5use futures_util::stream::FusedStream; 5use futures_core::stream::FusedStream;
6use futures_util::Stream; 6use futures_core::Stream;
7 7
8use crate::{Duration, Instant}; 8use 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))]
69pub struct TimeoutFuture<F> { 71pub 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))]
95pub struct Timer { 99pub 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
100impl Timer { 104impl 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))]
230pub struct Ticker { 237pub struct Ticker {
231 expires_at: Instant, 238 expires_at: Instant,
232 duration: Duration, 239 duration: Duration,