aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src/delay.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-time/src/delay.rs')
-rw-r--r--embassy-time/src/delay.rs98
1 files changed, 98 insertions, 0 deletions
diff --git a/embassy-time/src/delay.rs b/embassy-time/src/delay.rs
new file mode 100644
index 000000000..d010fff98
--- /dev/null
+++ b/embassy-time/src/delay.rs
@@ -0,0 +1,98 @@
1use super::{Duration, Instant};
2
3/// Blocks for at least `duration`.
4pub fn block_for(duration: Duration) {
5 let expires_at = Instant::now() + duration;
6 while Instant::now() < expires_at {}
7}
8
9/// Type implementing async delays and blocking `embedded-hal` delays.
10///
11/// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least
12/// the amount provided, but accuracy can be affected by many factors, including interrupt usage.
13/// Make sure to use a suitable tick rate for your use case. The tick rate is defined by the currently
14/// active driver.
15pub struct Delay;
16
17#[cfg(feature = "unstable-traits")]
18mod eh1 {
19 use super::*;
20
21 impl embedded_hal_1::delay::blocking::DelayUs for Delay {
22 type Error = core::convert::Infallible;
23
24 fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
25 Ok(block_for(Duration::from_micros(us as u64)))
26 }
27
28 fn delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
29 Ok(block_for(Duration::from_millis(ms as u64)))
30 }
31 }
32}
33
34cfg_if::cfg_if! {
35 if #[cfg(all(feature = "unstable-traits", feature = "nightly"))] {
36 use crate::Timer;
37 use core::future::Future;
38 use futures_util::FutureExt;
39
40 impl embedded_hal_async::delay::DelayUs for Delay {
41 type Error = core::convert::Infallible;
42
43 type DelayUsFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
44
45 fn delay_us(&mut self, micros: u32) -> Self::DelayUsFuture<'_> {
46 Timer::after(Duration::from_micros(micros as _)).map(Ok)
47 }
48
49 type DelayMsFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a;
50
51 fn delay_ms(&mut self, millis: u32) -> Self::DelayMsFuture<'_> {
52 Timer::after(Duration::from_millis(millis as _)).map(Ok)
53 }
54 }
55 }
56}
57
58mod eh02 {
59 use embedded_hal_02::blocking::delay::{DelayMs, DelayUs};
60
61 use super::*;
62
63 impl DelayMs<u8> for Delay {
64 fn delay_ms(&mut self, ms: u8) {
65 block_for(Duration::from_millis(ms as u64))
66 }
67 }
68
69 impl DelayMs<u16> for Delay {
70 fn delay_ms(&mut self, ms: u16) {
71 block_for(Duration::from_millis(ms as u64))
72 }
73 }
74
75 impl DelayMs<u32> for Delay {
76 fn delay_ms(&mut self, ms: u32) {
77 block_for(Duration::from_millis(ms as u64))
78 }
79 }
80
81 impl DelayUs<u8> for Delay {
82 fn delay_us(&mut self, us: u8) {
83 block_for(Duration::from_micros(us as u64))
84 }
85 }
86
87 impl DelayUs<u16> for Delay {
88 fn delay_us(&mut self, us: u16) {
89 block_for(Duration::from_micros(us as u64))
90 }
91 }
92
93 impl DelayUs<u32> for Delay {
94 fn delay_us(&mut self, us: u32) {
95 block_for(Duration::from_micros(us as u64))
96 }
97 }
98}