rtic_time/monotonic/
embedded_hal_macros.rs

1//! Macros that implement embedded-hal traits for Monotonics
2
3/// Implements [`embedded_hal::delay::DelayNs`] for a given monotonic.
4#[macro_export]
5macro_rules! impl_embedded_hal_delay_fugit {
6    ($t:ty) => {
7        impl $crate::embedded_hal::delay::DelayNs for $t {
8            fn delay_ns(&mut self, ns: u32) {
9                let now = <Self as $crate::Monotonic>::now();
10                let mut done =
11                    now + <Self as $crate::Monotonic>::Duration::nanos_at_least(ns.into());
12                if now != done {
13                    // Compensate for sub-tick uncertainty
14                    done += <Self as $crate::Monotonic>::Duration::from_ticks(1);
15                }
16
17                while <Self as $crate::Monotonic>::now() < done {}
18            }
19
20            fn delay_us(&mut self, us: u32) {
21                let now = <Self as $crate::Monotonic>::now();
22                let mut done =
23                    now + <Self as $crate::Monotonic>::Duration::micros_at_least(us.into());
24                if now != done {
25                    // Compensate for sub-tick uncertainty
26                    done += <Self as $crate::Monotonic>::Duration::from_ticks(1);
27                }
28
29                while <Self as $crate::Monotonic>::now() < done {}
30            }
31
32            fn delay_ms(&mut self, ms: u32) {
33                let now = <Self as $crate::Monotonic>::now();
34                let mut done =
35                    now + <Self as $crate::Monotonic>::Duration::millis_at_least(ms.into());
36                if now != done {
37                    // Compensate for sub-tick uncertainty
38                    done += <Self as $crate::Monotonic>::Duration::from_ticks(1);
39                }
40
41                while <Self as $crate::Monotonic>::now() < done {}
42            }
43        }
44    };
45}
46
47/// Implements [`embedded_hal_async::delay::DelayNs`] for a given monotonic.
48#[macro_export]
49macro_rules! impl_embedded_hal_async_delay_fugit {
50    ($t:ty) => {
51        impl $crate::embedded_hal_async::delay::DelayNs for $t {
52            #[inline]
53            async fn delay_ns(&mut self, ns: u32) {
54                <Self as $crate::Monotonic>::delay(
55                    <Self as $crate::Monotonic>::Duration::nanos_at_least(ns.into()),
56                )
57                .await;
58            }
59
60            #[inline]
61            async fn delay_us(&mut self, us: u32) {
62                <Self as $crate::Monotonic>::delay(
63                    <Self as $crate::Monotonic>::Duration::micros_at_least(us.into()),
64                )
65                .await;
66            }
67
68            #[inline]
69            async fn delay_ms(&mut self, ms: u32) {
70                <Self as $crate::Monotonic>::delay(
71                    <Self as $crate::Monotonic>::Duration::millis_at_least(ms.into()),
72                )
73                .await;
74            }
75        }
76    };
77}