aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-07-12 03:10:01 +0200
committerDario Nieuwenhuis <[email protected]>2021-07-12 03:45:48 +0200
commit94bd4eb7d5713e4c6e679617a3e6e94671c8ff77 (patch)
treef911e57633d6d6b217fe0670b0ddec01468f5212
parent40bc67bee04256a13267bf19e4ba8b93509fa8cb (diff)
embassy/time: refactor module structure
-rw-r--r--embassy/src/executor/mod.rs1
-rw-r--r--embassy/src/time/delay.rs70
-rw-r--r--embassy/src/time/mod.rs58
-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
5pub mod raw; 5pub mod raw;
6mod run_queue; 6mod run_queue;
7pub(crate) mod timer;
8mod timer_queue; 7mod timer_queue;
9mod util; 8mod util;
10mod waker; 9mod 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 @@
1use core::future::Future;
2
3use super::{Duration, Instant, Timer};
4
5/// Async or blocking delay.
6pub struct Delay;
7
8impl 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.
26pub struct BlockingTimer;
27
28impl 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
34impl 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
40impl 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
46impl 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
52impl 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
58impl 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.
67pub 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//!
4mod delay;
4mod duration; 5mod duration;
5mod instant; 6mod instant;
7mod timer;
6mod traits; 8mod traits;
7 9
8pub use crate::executor::timer::{with_timeout, Delay, Ticker, TimeoutError, Timer}; 10pub use delay::{block_for, Delay};
9pub use duration::Duration; 11pub use duration::Duration;
10pub use instant::Instant; 12pub use instant::Instant;
13pub use timer::{with_timeout, Ticker, TimeoutError, Timer};
11pub use traits::*; 14pub use traits::*;
12 15
13#[cfg(any( 16#[cfg(any(
@@ -42,56 +45,3 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) {
42pub(crate) fn now() -> u64 { 45pub(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.
53pub struct BlockingTimer;
54
55impl 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
61impl 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
67impl 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
73impl 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
79impl 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
85impl 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.
94pub 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 @@
1use core::future::Future; 1use core::future::Future;
2use core::marker::PhantomData;
3use core::pin::Pin; 2use core::pin::Pin;
4use core::task::{Context, Poll}; 3use core::task::{Context, Poll};
5use futures::{future::select, future::Either, pin_mut, Stream}; 4use futures::{future::select, future::Either, pin_mut, Stream};
6 5
7use super::raw; 6use crate::executor::raw;
8use crate::time::{Duration, Instant}; 7use crate::time::{Duration, Instant};
9 8
10/// Delay abstraction using embassy's clock.
11pub struct Delay {
12 _data: PhantomData<bool>,
13}
14
15impl Delay {
16 pub fn new() -> Self {
17 Delay {
18 _data: PhantomData {},
19 }
20 }
21}
22
23impl 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
34pub struct TimeoutError; 9pub struct TimeoutError;
35pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> { 10pub 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);