diff options
| author | Thales Fragoso <[email protected]> | 2021-06-28 18:30:04 -0300 |
|---|---|---|
| committer | Thales Fragoso <[email protected]> | 2021-06-28 18:52:27 -0300 |
| commit | 51583afc1e1e5d6eafefbb994c153d1d923a502f (patch) | |
| tree | fb29dbf1cbef12744840c6d612dc0211ae6373b9 | |
| parent | 54197d1663b8579b9f569e60f1e4aa010bed2cfe (diff) | |
Add docs for BlockingTimer and rename tick features
| -rw-r--r-- | embassy/Cargo.toml | 8 | ||||
| -rw-r--r-- | embassy/src/time/mod.rs | 35 |
2 files changed, 25 insertions, 18 deletions
diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml index f3cb46550..87f54409a 100644 --- a/embassy/Cargo.toml +++ b/embassy/Cargo.toml | |||
| @@ -5,11 +5,11 @@ authors = ["Dario Nieuwenhuis <[email protected]>"] | |||
| 5 | edition = "2018" | 5 | edition = "2018" |
| 6 | 6 | ||
| 7 | [features] | 7 | [features] |
| 8 | default = ["tick-hz-32768"] | 8 | default = ["tick-32768hz"] |
| 9 | std = ["futures/std", "embassy-traits/std"] | 9 | std = ["futures/std", "embassy-traits/std"] |
| 10 | tick-hz-32768 = [] | 10 | tick-32768hz = [] |
| 11 | tick-hz-1000 = [] | 11 | tick-1000hz = [] |
| 12 | tick-mhz-1 = [] | 12 | tick-1mhz = [] |
| 13 | 13 | ||
| 14 | defmt-trace = [] | 14 | defmt-trace = [] |
| 15 | defmt-debug = [] | 15 | defmt-debug = [] |
diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index 2ebe19fbe..d50d9ef0d 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs | |||
| @@ -11,20 +11,20 @@ pub use instant::Instant; | |||
| 11 | pub use traits::*; | 11 | pub use traits::*; |
| 12 | 12 | ||
| 13 | #[cfg(any( | 13 | #[cfg(any( |
| 14 | all(feature = "tick-hz-32768", feature = "tick-hz-1000"), | 14 | all(feature = "tick-32768hz", feature = "tick-1000hz"), |
| 15 | all(feature = "tick-hz-32768", feature = "tick-mhz-1"), | 15 | all(feature = "tick-32768hz", feature = "tick-1mhz"), |
| 16 | ))] | 16 | ))] |
| 17 | compile_error!( | 17 | compile_error!( |
| 18 | "Disable default-features to be able to use a tick rate other than the default (32768 Hz)" | 18 | "Disable default-features to be able to use a tick rate other than the default (32768 Hz)" |
| 19 | ); | 19 | ); |
| 20 | 20 | ||
| 21 | #[cfg(feature = "tick-hz-1000")] | 21 | #[cfg(feature = "tick-1000hz")] |
| 22 | pub const TICKS_PER_SECOND: u64 = 1_000; | 22 | pub const TICKS_PER_SECOND: u64 = 1_000; |
| 23 | 23 | ||
| 24 | #[cfg(feature = "tick-hz-32768")] | 24 | #[cfg(feature = "tick-32768hz")] |
| 25 | pub const TICKS_PER_SECOND: u64 = 32_768; | 25 | pub const TICKS_PER_SECOND: u64 = 32_768; |
| 26 | 26 | ||
| 27 | #[cfg(feature = "tick-mhz-1")] | 27 | #[cfg(feature = "tick-1mhz")] |
| 28 | pub const TICKS_PER_SECOND: u64 = 1_000_000; | 28 | pub const TICKS_PER_SECOND: u64 = 1_000_000; |
| 29 | 29 | ||
| 30 | static mut CLOCK: Option<&'static dyn Clock> = None; | 30 | static mut CLOCK: Option<&'static dyn Clock> = None; |
| @@ -43,47 +43,54 @@ pub(crate) fn now() -> u64 { | |||
| 43 | unsafe { unwrap!(CLOCK, "No clock set").now() } | 43 | unsafe { unwrap!(CLOCK, "No clock set").now() } |
| 44 | } | 44 | } |
| 45 | 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. | ||
| 46 | pub struct BlockingTimer; | 53 | pub struct BlockingTimer; |
| 47 | 54 | ||
| 48 | impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer { | 55 | impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer { |
| 49 | fn delay_ms(&mut self, ms: u8) { | 56 | fn delay_ms(&mut self, ms: u8) { |
| 50 | block_for(Duration::from_millis(u64::from(ms))) | 57 | block_for(Duration::from_millis(ms as u64)) |
| 51 | } | 58 | } |
| 52 | } | 59 | } |
| 53 | 60 | ||
| 54 | impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { | 61 | impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { |
| 55 | fn delay_ms(&mut self, ms: u16) { | 62 | fn delay_ms(&mut self, ms: u16) { |
| 56 | block_for(Duration::from_millis(u64::from(ms))) | 63 | block_for(Duration::from_millis(ms as u64)) |
| 57 | } | 64 | } |
| 58 | } | 65 | } |
| 59 | 66 | ||
| 60 | impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { | 67 | impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { |
| 61 | fn delay_ms(&mut self, ms: u32) { | 68 | fn delay_ms(&mut self, ms: u32) { |
| 62 | block_for(Duration::from_millis(u64::from(ms))) | 69 | block_for(Duration::from_millis(ms as u64)) |
| 63 | } | 70 | } |
| 64 | } | 71 | } |
| 65 | 72 | ||
| 66 | #[cfg(feature = "tick-mhz-1")] | ||
| 67 | impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { | 73 | impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { |
| 68 | fn delay_us(&mut self, us: u8) { | 74 | fn delay_us(&mut self, us: u8) { |
| 69 | block_for(Duration::from_micros(u64::from(us))) | 75 | block_for(Duration::from_micros(us as u64)) |
| 70 | } | 76 | } |
| 71 | } | 77 | } |
| 72 | 78 | ||
| 73 | #[cfg(feature = "tick-mhz-1")] | ||
| 74 | impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { | 79 | impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { |
| 75 | fn delay_us(&mut self, us: u16) { | 80 | fn delay_us(&mut self, us: u16) { |
| 76 | block_for(Duration::from_micros(u64::from(us))) | 81 | block_for(Duration::from_micros(us as u64)) |
| 77 | } | 82 | } |
| 78 | } | 83 | } |
| 79 | 84 | ||
| 80 | #[cfg(feature = "tick-mhz-1")] | ||
| 81 | impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { | 85 | impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { |
| 82 | fn delay_us(&mut self, us: u32) { | 86 | fn delay_us(&mut self, us: u32) { |
| 83 | block_for(Duration::from_micros(u64::from(us))) | 87 | block_for(Duration::from_micros(us as u64)) |
| 84 | } | 88 | } |
| 85 | } | 89 | } |
| 86 | 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. | ||
| 87 | pub fn block_for(duration: Duration) { | 94 | pub fn block_for(duration: Duration) { |
| 88 | let expires_at = Instant::now() + duration; | 95 | let expires_at = Instant::now() + duration; |
| 89 | while Instant::now() < expires_at {} | 96 | while Instant::now() < expires_at {} |
