| 1 | //! Delays. |
| 2 | |
| 3 | /// Delay with up to nanosecond precision. |
| 4 | pub trait DelayNs { |
| 5 | /// Pauses execution for at minimum `ns` nanoseconds. Pause can be longer |
| 6 | /// if the implementation requires it due to precision/timing issues. |
| 7 | async fn delay_ns(&mut self, ns: u32); |
| 8 | |
| 9 | /// Pauses execution for at minimum `us` microseconds. Pause can be longer |
| 10 | /// if the implementation requires it due to precision/timing issues. |
| 11 | async fn delay_us(&mut self, mut us: u32) { |
| 12 | while us > 4_294_967 { |
| 13 | us -= 4_294_967; |
| 14 | self.delay_ns(4_294_967_000).await; |
| 15 | } |
| 16 | self.delay_ns(us * 1_000).await; |
| 17 | } |
| 18 | |
| 19 | /// Pauses execution for at minimum `ms` milliseconds. Pause can be longer |
| 20 | /// if the implementation requires it due to precision/timing issues. |
| 21 | #[inline ] |
| 22 | async fn delay_ms(&mut self, mut ms: u32) { |
| 23 | while ms > 4294 { |
| 24 | ms -= 4294; |
| 25 | self.delay_ns(4_294_000_000).await; |
| 26 | } |
| 27 | self.delay_ns(ms * 1_000_000).await; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl<T> DelayNs for &mut T |
| 32 | where |
| 33 | T: DelayNs + ?Sized, |
| 34 | { |
| 35 | #[inline ] |
| 36 | async fn delay_ns(&mut self, ns: u32) { |
| 37 | T::delay_ns(self, ns).await; |
| 38 | } |
| 39 | |
| 40 | #[inline ] |
| 41 | async fn delay_us(&mut self, us: u32) { |
| 42 | T::delay_us(self, us).await; |
| 43 | } |
| 44 | |
| 45 | #[inline ] |
| 46 | async fn delay_ms(&mut self, ms: u32) { |
| 47 | T::delay_ms(self, ms).await; |
| 48 | } |
| 49 | } |
| 50 | |