diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-07-12 03:10:01 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-07-12 03:45:48 +0200 |
| commit | 94bd4eb7d5713e4c6e679617a3e6e94671c8ff77 (patch) | |
| tree | f911e57633d6d6b217fe0670b0ddec01468f5212 | |
| parent | 40bc67bee04256a13267bf19e4ba8b93509fa8cb (diff) | |
embassy/time: refactor module structure
| -rw-r--r-- | embassy/src/executor/mod.rs | 1 | ||||
| -rw-r--r-- | embassy/src/time/delay.rs | 70 | ||||
| -rw-r--r-- | embassy/src/time/mod.rs | 58 | ||||
| -rw-r--r-- | embassy/src/time/timer.rs (renamed from embassy/src/executor/timer.rs) | 27 |
4 files changed, 75 insertions, 81 deletions
diff --git a/embassy/src/executor/mod.rs b/embassy/src/executor/mod.rs index 598d4e558..d9740d56d 100644 --- a/embassy/src/executor/mod.rs +++ b/embassy/src/executor/mod.rs | |||
| @@ -4,7 +4,6 @@ use core::{mem, ptr}; | |||
| 4 | 4 | ||
| 5 | pub mod raw; | 5 | pub mod raw; |
| 6 | mod run_queue; | 6 | mod run_queue; |
| 7 | pub(crate) mod timer; | ||
| 8 | mod timer_queue; | 7 | mod timer_queue; |
| 9 | mod util; | 8 | mod util; |
| 10 | mod waker; | 9 | mod waker; |
diff --git a/embassy/src/time/delay.rs b/embassy/src/time/delay.rs new file mode 100644 index 000000000..f8b7f8400 --- /dev/null +++ b/embassy/src/time/delay.rs | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | |||
| 3 | use super::{Duration, Instant, Timer}; | ||
| 4 | |||
| 5 | /// Async or blocking delay. | ||
| 6 | pub struct Delay; | ||
| 7 | |||
| 8 | impl crate::traits::delay::Delay for Delay { | ||
| 9 | type DelayFuture<'a> = impl Future<Output = ()> + 'a; | ||
| 10 | |||
| 11 | fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a> { | ||
| 12 | Timer::after(Duration::from_millis(millis)) | ||
| 13 | } | ||
| 14 | fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a> { | ||
| 15 | Timer::after(Duration::from_micros(micros)) | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | /// Type used for blocking delays through embedded-hal traits. | ||
| 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) { | ||
| 30 | block_for(Duration::from_millis(ms as u64)) | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { | ||
| 35 | fn delay_ms(&mut self, ms: u16) { | ||
| 36 | block_for(Duration::from_millis(ms as u64)) | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { | ||
| 41 | fn delay_ms(&mut self, ms: u32) { | ||
| 42 | block_for(Duration::from_millis(ms as u64)) | ||
| 43 | } | ||
| 44 | } | ||
| 45 | |||
| 46 | impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { | ||
| 47 | fn delay_us(&mut self, us: u8) { | ||
| 48 | block_for(Duration::from_micros(us as u64)) | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { | ||
| 53 | fn delay_us(&mut self, us: u16) { | ||
| 54 | block_for(Duration::from_micros(us as u64)) | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { | ||
| 59 | fn delay_us(&mut self, us: u32) { | ||
| 60 | block_for(Duration::from_micros(us as u64)) | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | /// Blocks the cpu for at least `duration`. | ||
| 65 | /// | ||
| 66 | /// For this interface to work, the Executor's clock must be correctly initialized before using it. | ||
| 67 | pub fn block_for(duration: Duration) { | ||
| 68 | let expires_at = Instant::now() + duration; | ||
| 69 | while Instant::now() < expires_at {} | ||
| 70 | } | ||
diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index d50d9ef0d..9b7a4be18 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs | |||
| @@ -1,13 +1,16 @@ | |||
| 1 | //! Time abstractions | 1 | //! Time abstractions |
| 2 | //! To use these abstractions, first call `set_clock` with an instance of an [Clock](trait.Clock.html). | 2 | //! To use these abstractions, first call `set_clock` with an instance of an [Clock](trait.Clock.html). |
| 3 | //! | 3 | //! |
| 4 | mod delay; | ||
| 4 | mod duration; | 5 | mod duration; |
| 5 | mod instant; | 6 | mod instant; |
| 7 | mod timer; | ||
| 6 | mod traits; | 8 | mod traits; |
| 7 | 9 | ||
| 8 | pub use crate::executor::timer::{with_timeout, Delay, Ticker, TimeoutError, Timer}; | 10 | pub use delay::{block_for, Delay}; |
| 9 | pub use duration::Duration; | 11 | pub use duration::Duration; |
| 10 | pub use instant::Instant; | 12 | pub use instant::Instant; |
| 13 | pub use timer::{with_timeout, Ticker, TimeoutError, Timer}; | ||
| 11 | pub use traits::*; | 14 | pub use traits::*; |
| 12 | 15 | ||
| 13 | #[cfg(any( | 16 | #[cfg(any( |
| @@ -42,56 +45,3 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) { | |||
| 42 | pub(crate) fn now() -> u64 { | 45 | pub(crate) fn now() -> u64 { |
| 43 | unsafe { unwrap!(CLOCK, "No clock set").now() } | 46 | unsafe { unwrap!(CLOCK, "No clock set").now() } |
| 44 | } | 47 | } |
| 45 | |||
| 46 | /// Type used for blocking delays through embedded-hal traits. | ||
| 47 | /// | ||
| 48 | /// For this interface to work, the Executor's clock must be correctly initialized before using it. | ||
| 49 | /// The delays are implemented in a "best-effort" way, meaning that the cpu will block for at least | ||
| 50 | /// the amount provided, but accuracy can be affected by many factors, including interrupt usage. | ||
| 51 | /// Make sure to use a suitable tick rate for your use case. The tick rate can be chosen through | ||
| 52 | /// features flags of this crate. | ||
| 53 | pub struct BlockingTimer; | ||
| 54 | |||
| 55 | impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer { | ||
| 56 | fn delay_ms(&mut self, ms: u8) { | ||
| 57 | block_for(Duration::from_millis(ms as u64)) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { | ||
| 62 | fn delay_ms(&mut self, ms: u16) { | ||
| 63 | block_for(Duration::from_millis(ms as u64)) | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { | ||
| 68 | fn delay_ms(&mut self, ms: u32) { | ||
| 69 | block_for(Duration::from_millis(ms as u64)) | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { | ||
| 74 | fn delay_us(&mut self, us: u8) { | ||
| 75 | block_for(Duration::from_micros(us as u64)) | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { | ||
| 80 | fn delay_us(&mut self, us: u16) { | ||
| 81 | block_for(Duration::from_micros(us as u64)) | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { | ||
| 86 | fn delay_us(&mut self, us: u32) { | ||
| 87 | block_for(Duration::from_micros(us as u64)) | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | /// Blocks the cpu for at least `duration`. | ||
| 92 | /// | ||
| 93 | /// For this interface to work, the Executor's clock must be correctly initialized before using it. | ||
| 94 | pub fn block_for(duration: Duration) { | ||
| 95 | let expires_at = Instant::now() + duration; | ||
| 96 | while Instant::now() < expires_at {} | ||
| 97 | } | ||
diff --git a/embassy/src/executor/timer.rs b/embassy/src/time/timer.rs index 8ee336960..a2a37f706 100644 --- a/embassy/src/executor/timer.rs +++ b/embassy/src/time/timer.rs | |||
| @@ -1,36 +1,11 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::Future; |
| 2 | use core::marker::PhantomData; | ||
| 3 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 4 | use core::task::{Context, Poll}; | 3 | use core::task::{Context, Poll}; |
| 5 | use futures::{future::select, future::Either, pin_mut, Stream}; | 4 | use futures::{future::select, future::Either, pin_mut, Stream}; |
| 6 | 5 | ||
| 7 | use super::raw; | 6 | use crate::executor::raw; |
| 8 | use crate::time::{Duration, Instant}; | 7 | use crate::time::{Duration, Instant}; |
| 9 | 8 | ||
| 10 | /// Delay abstraction using embassy's clock. | ||
| 11 | pub struct Delay { | ||
| 12 | _data: PhantomData<bool>, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl Delay { | ||
| 16 | pub fn new() -> Self { | ||
| 17 | Delay { | ||
| 18 | _data: PhantomData {}, | ||
| 19 | } | ||
| 20 | } | ||
| 21 | } | ||
| 22 | |||
| 23 | impl crate::traits::delay::Delay for Delay { | ||
| 24 | type DelayFuture<'a> = impl Future<Output = ()> + 'a; | ||
| 25 | |||
| 26 | fn delay_ms<'a>(&'a mut self, millis: u64) -> Self::DelayFuture<'a> { | ||
| 27 | Timer::after(Duration::from_millis(millis)) | ||
| 28 | } | ||
| 29 | fn delay_us<'a>(&'a mut self, micros: u64) -> Self::DelayFuture<'a> { | ||
| 30 | Timer::after(Duration::from_micros(micros)) | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | pub struct TimeoutError; | 9 | pub struct TimeoutError; |
| 35 | pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> { | 10 | pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> { |
| 36 | let timeout_fut = Timer::after(timeout); | 11 | let timeout_fut = Timer::after(timeout); |
