diff options
| -rw-r--r-- | embassy/src/time/delay.rs | 29 |
1 files changed, 13 insertions, 16 deletions
diff --git a/embassy/src/time/delay.rs b/embassy/src/time/delay.rs index f8b7f8400..c97e8b5c2 100644 --- a/embassy/src/time/delay.rs +++ b/embassy/src/time/delay.rs | |||
| @@ -2,7 +2,13 @@ use core::future::Future; | |||
| 2 | 2 | ||
| 3 | use super::{Duration, Instant, Timer}; | 3 | use super::{Duration, Instant, Timer}; |
| 4 | 4 | ||
| 5 | /// Async or blocking delay. | 5 | /// Type implementing async delays and blocking `embedded-hal` delays. |
| 6 | /// | ||
| 7 | /// For this interface to work, the Executor's clock must be correctly initialized before using it. | ||
| 8 | /// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least | ||
| 9 | /// the amount provided, but accuracy can be affected by many factors, including interrupt usage. | ||
| 10 | /// Make sure to use a suitable tick rate for your use case. The tick rate can be chosen through | ||
| 11 | /// features flags of this crate. | ||
| 6 | pub struct Delay; | 12 | pub struct Delay; |
| 7 | 13 | ||
| 8 | impl crate::traits::delay::Delay for Delay { | 14 | impl crate::traits::delay::Delay for Delay { |
| @@ -16,46 +22,37 @@ impl crate::traits::delay::Delay for Delay { | |||
| 16 | } | 22 | } |
| 17 | } | 23 | } |
| 18 | 24 | ||
| 19 | /// Type used for blocking delays through embedded-hal traits. | 25 | impl embedded_hal::blocking::delay::DelayMs<u8> for Delay { |
| 20 | /// | ||
| 21 | /// For this interface to work, the Executor's clock must be correctly initialized before using it. | ||
| 22 | /// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least | ||
| 23 | /// the amount provided, but accuracy can be affected by many factors, including interrupt usage. | ||
| 24 | /// Make sure to use a suitable tick rate for your use case. The tick rate can be chosen through | ||
| 25 | /// features flags of this crate. | ||
| 26 | pub struct BlockingTimer; | ||
| 27 | |||
| 28 | impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer { | ||
| 29 | fn delay_ms(&mut self, ms: u8) { | 26 | fn delay_ms(&mut self, ms: u8) { |
| 30 | block_for(Duration::from_millis(ms as u64)) | 27 | block_for(Duration::from_millis(ms as u64)) |
| 31 | } | 28 | } |
| 32 | } | 29 | } |
| 33 | 30 | ||
| 34 | impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { | 31 | impl embedded_hal::blocking::delay::DelayMs<u16> for Delay { |
| 35 | fn delay_ms(&mut self, ms: u16) { | 32 | fn delay_ms(&mut self, ms: u16) { |
| 36 | block_for(Duration::from_millis(ms as u64)) | 33 | block_for(Duration::from_millis(ms as u64)) |
| 37 | } | 34 | } |
| 38 | } | 35 | } |
| 39 | 36 | ||
| 40 | impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { | 37 | impl embedded_hal::blocking::delay::DelayMs<u32> for Delay { |
| 41 | fn delay_ms(&mut self, ms: u32) { | 38 | fn delay_ms(&mut self, ms: u32) { |
| 42 | block_for(Duration::from_millis(ms as u64)) | 39 | block_for(Duration::from_millis(ms as u64)) |
| 43 | } | 40 | } |
| 44 | } | 41 | } |
| 45 | 42 | ||
| 46 | impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { | 43 | impl embedded_hal::blocking::delay::DelayUs<u8> for Delay { |
| 47 | fn delay_us(&mut self, us: u8) { | 44 | fn delay_us(&mut self, us: u8) { |
| 48 | block_for(Duration::from_micros(us as u64)) | 45 | block_for(Duration::from_micros(us as u64)) |
| 49 | } | 46 | } |
| 50 | } | 47 | } |
| 51 | 48 | ||
| 52 | impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { | 49 | impl embedded_hal::blocking::delay::DelayUs<u16> for Delay { |
| 53 | fn delay_us(&mut self, us: u16) { | 50 | fn delay_us(&mut self, us: u16) { |
| 54 | block_for(Duration::from_micros(us as u64)) | 51 | block_for(Duration::from_micros(us as u64)) |
| 55 | } | 52 | } |
| 56 | } | 53 | } |
| 57 | 54 | ||
| 58 | impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { | 55 | impl embedded_hal::blocking::delay::DelayUs<u32> for Delay { |
| 59 | fn delay_us(&mut self, us: u32) { | 56 | fn delay_us(&mut self, us: u32) { |
| 60 | block_for(Duration::from_micros(us as u64)) | 57 | block_for(Duration::from_micros(us as u64)) |
| 61 | } | 58 | } |
