Skip to main content

embassy_time/
delay.rs

1use core::future::Future;
2
3use super::{Duration, Instant};
4use crate::Timer;
5
6/// Blocks for at least `duration`.
7///
8/// This function may require interrupts to be enabled to work correctly.
9///
10/// ## Panics
11///
12/// Panics if the computed instant overflows.
13/// Avoid panics with [`try_block_for()`].
14pub fn block_for(duration: Duration) {
15    let expires_at = Instant::now() + duration;
16    while Instant::now() < expires_at {}
17}
18
19/// Tries to block for at least `duration`.
20///
21/// This function may require interrupts to be enabled to work correctly.
22///
23/// This is a panic-free [`block_for()`].
24pub fn try_block_for(duration: Duration) -> Option<()> {
25    let expires_at = Instant::now().checked_add(duration)?;
26    while Instant::now() < expires_at {}
27    Some(())
28}
29
30/// Type implementing async delays and blocking `embedded-hal` delays.
31///
32/// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least
33/// the amount provided, but accuracy can be affected by many factors, including interrupt usage.
34/// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently
35/// active driver.
36#[derive(Clone, Debug)]
37#[cfg_attr(feature = "defmt", derive(defmt::Format))]
38pub struct Delay;
39
40impl embedded_hal_1::delay::DelayNs for Delay {
41    /// Pauses execution for at minimum `ns` nanoseconds.
42    ///
43    /// ## Panics
44    ///
45    /// Panics if the computed instant overflows.
46    fn delay_ns(&mut self, ns: u32) {
47        block_for(Duration::from_nanos(ns as u64))
48    }
49
50    /// Pauses execution for at minimum `us` microseconds.
51    ///
52    /// ## Panics
53    ///
54    /// Panics if the computed instant overflows.
55    fn delay_us(&mut self, us: u32) {
56        block_for(Duration::from_micros(us as u64))
57    }
58
59    /// Pauses execution for at minimum `ms` milliseconds.
60    ///
61    /// ## Panics
62    ///
63    /// Panics if the computed instant overflows.
64    fn delay_ms(&mut self, ms: u32) {
65        block_for(Duration::from_millis(ms as u64))
66    }
67}
68
69impl embedded_hal_async::delay::DelayNs for Delay {
70    /// Pauses execution for at minimum `ns` nanoseconds.
71    ///
72    /// ## Panics
73    ///
74    /// Panics if the computed instant overflows.
75    fn delay_ns(&mut self, ns: u32) -> impl Future<Output = ()> {
76        Timer::after_nanos(ns as _)
77    }
78
79    /// Pauses execution for at minimum `us` microseconds.
80    ///
81    /// ## Panics
82    ///
83    /// Panics if the computed instant overflows.
84    fn delay_us(&mut self, us: u32) -> impl Future<Output = ()> {
85        Timer::after_micros(us as _)
86    }
87
88    /// Pauses execution for at minimum `ms` milliseconds.
89    ///
90    /// ## Panics
91    ///
92    /// Panics if the computed instant overflows.
93    fn delay_ms(&mut self, ms: u32) -> impl Future<Output = ()> {
94        Timer::after_millis(ms as _)
95    }
96}
97
98impl embedded_hal_02::blocking::delay::DelayMs<u8> for Delay {
99    /// Pauses execution for at minimum `ms` milliseconds.
100    ///
101    /// ## Panics
102    ///
103    /// Panics if the computed instant overflows.
104    fn delay_ms(&mut self, ms: u8) {
105        block_for(Duration::from_millis(ms as u64))
106    }
107}
108
109impl embedded_hal_02::blocking::delay::DelayMs<u16> for Delay {
110    /// Pauses execution for at minimum `ms` milliseconds.
111    ///
112    /// ## Panics
113    ///
114    /// Panics if the computed instant overflows.
115    fn delay_ms(&mut self, ms: u16) {
116        block_for(Duration::from_millis(ms as u64))
117    }
118}
119
120impl embedded_hal_02::blocking::delay::DelayMs<u32> for Delay {
121    /// Pauses execution for at minimum `ms` milliseconds.
122    ///
123    /// ## Panics
124    ///
125    /// Panics if the computed instant overflows.
126    fn delay_ms(&mut self, ms: u32) {
127        block_for(Duration::from_millis(ms as u64))
128    }
129}
130
131impl embedded_hal_02::blocking::delay::DelayUs<u8> for Delay {
132    /// Pauses execution for at minimum `us` microseconds.
133    ///
134    /// ## Panics
135    ///
136    /// Panics if the computed instant overflows.
137    fn delay_us(&mut self, us: u8) {
138        block_for(Duration::from_micros(us as u64))
139    }
140}
141
142impl embedded_hal_02::blocking::delay::DelayUs<u16> for Delay {
143    /// Pauses execution for at minimum `us` microseconds.
144    ///
145    /// ## Panics
146    ///
147    /// Panics if the computed instant overflows.
148    fn delay_us(&mut self, us: u16) {
149        block_for(Duration::from_micros(us as u64))
150    }
151}
152
153impl embedded_hal_02::blocking::delay::DelayUs<u32> for Delay {
154    /// Pauses execution for at minimum `us` microseconds.
155    ///
156    /// ## Panics
157    ///
158    /// Panics if the computed instant overflows.
159    fn delay_us(&mut self, us: u32) {
160        block_for(Duration::from_micros(us as u64))
161    }
162}
163
164#[cfg(all(test, feature = "std"))]
165mod test {
166    use super::*;
167
168    #[test]
169    #[should_panic(expected = "overflow")]
170    #[cfg(panic = "unwind")]
171    fn block_for_panics() {
172        while Instant::now() == Instant::MIN {} // with non-0 tick
173        block_for(Duration::MAX); // PANIC
174    }
175
176    #[test]
177    fn try_block_for_none() {
178        while Instant::now() == Instant::MIN {} // with non-0 tick
179        assert!(try_block_for(Duration::MAX).is_none());
180    }
181}