diff options
| author | Thales Fragoso <[email protected]> | 2021-06-28 18:01:40 -0300 |
|---|---|---|
| committer | Thales Fragoso <[email protected]> | 2021-06-28 18:01:40 -0300 |
| commit | 54197d1663b8579b9f569e60f1e4aa010bed2cfe (patch) | |
| tree | dbd3db67c208c8b155cddc5dbad6d01a62fdb0ae | |
| parent | cdb0c72849558db2b210301f5d13a922308e6bf1 (diff) | |
Add BlockingTimer and features to choose tick rate
| -rw-r--r-- | embassy/Cargo.toml | 5 | ||||
| -rw-r--r-- | embassy/src/time/mod.rs | 64 |
2 files changed, 67 insertions, 2 deletions
diff --git a/embassy/Cargo.toml b/embassy/Cargo.toml index 6364f9902..f3cb46550 100644 --- a/embassy/Cargo.toml +++ b/embassy/Cargo.toml | |||
| @@ -5,7 +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 | std = ["futures/std", "embassy-traits/std"] | 9 | std = ["futures/std", "embassy-traits/std"] |
| 10 | tick-hz-32768 = [] | ||
| 11 | tick-hz-1000 = [] | ||
| 12 | tick-mhz-1 = [] | ||
| 9 | 13 | ||
| 10 | defmt-trace = [] | 14 | defmt-trace = [] |
| 11 | defmt-debug = [] | 15 | defmt-debug = [] |
| @@ -26,6 +30,7 @@ embassy-macros = { version = "0.1.0", path = "../embassy-macros"} | |||
| 26 | embassy-traits = { version = "0.1.0", path = "../embassy-traits"} | 30 | embassy-traits = { version = "0.1.0", path = "../embassy-traits"} |
| 27 | atomic-polyfill = { version = "0.1.1" } | 31 | atomic-polyfill = { version = "0.1.1" } |
| 28 | critical-section = "0.2.1" | 32 | critical-section = "0.2.1" |
| 33 | embedded-hal = "0.2.5" | ||
| 29 | 34 | ||
| 30 | # Workaround https://github.com/japaric/cast.rs/pull/27 | 35 | # Workaround https://github.com/japaric/cast.rs/pull/27 |
| 31 | cast = { version = "=0.2.3", default-features = false } | 36 | cast = { version = "=0.2.3", default-features = false } |
diff --git a/embassy/src/time/mod.rs b/embassy/src/time/mod.rs index 21b93d384..2ebe19fbe 100644 --- a/embassy/src/time/mod.rs +++ b/embassy/src/time/mod.rs | |||
| @@ -10,8 +10,22 @@ pub use duration::Duration; | |||
| 10 | pub use instant::Instant; | 10 | pub use instant::Instant; |
| 11 | pub use traits::*; | 11 | pub use traits::*; |
| 12 | 12 | ||
| 13 | // TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. | 13 | #[cfg(any( |
| 14 | pub const TICKS_PER_SECOND: u64 = 32768; | 14 | all(feature = "tick-hz-32768", feature = "tick-hz-1000"), |
| 15 | all(feature = "tick-hz-32768", feature = "tick-mhz-1"), | ||
| 16 | ))] | ||
| 17 | compile_error!( | ||
| 18 | "Disable default-features to be able to use a tick rate other than the default (32768 Hz)" | ||
| 19 | ); | ||
| 20 | |||
| 21 | #[cfg(feature = "tick-hz-1000")] | ||
| 22 | pub const TICKS_PER_SECOND: u64 = 1_000; | ||
| 23 | |||
| 24 | #[cfg(feature = "tick-hz-32768")] | ||
| 25 | pub const TICKS_PER_SECOND: u64 = 32_768; | ||
| 26 | |||
| 27 | #[cfg(feature = "tick-mhz-1")] | ||
| 28 | pub const TICKS_PER_SECOND: u64 = 1_000_000; | ||
| 15 | 29 | ||
| 16 | static mut CLOCK: Option<&'static dyn Clock> = None; | 30 | static mut CLOCK: Option<&'static dyn Clock> = None; |
| 17 | 31 | ||
| @@ -28,3 +42,49 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) { | |||
| 28 | pub(crate) fn now() -> u64 { | 42 | pub(crate) fn now() -> u64 { |
| 29 | unsafe { unwrap!(CLOCK, "No clock set").now() } | 43 | unsafe { unwrap!(CLOCK, "No clock set").now() } |
| 30 | } | 44 | } |
| 45 | |||
| 46 | pub struct BlockingTimer; | ||
| 47 | |||
| 48 | impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer { | ||
| 49 | fn delay_ms(&mut self, ms: u8) { | ||
| 50 | block_for(Duration::from_millis(u64::from(ms))) | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { | ||
| 55 | fn delay_ms(&mut self, ms: u16) { | ||
| 56 | block_for(Duration::from_millis(u64::from(ms))) | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { | ||
| 61 | fn delay_ms(&mut self, ms: u32) { | ||
| 62 | block_for(Duration::from_millis(u64::from(ms))) | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | #[cfg(feature = "tick-mhz-1")] | ||
| 67 | impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { | ||
| 68 | fn delay_us(&mut self, us: u8) { | ||
| 69 | block_for(Duration::from_micros(u64::from(us))) | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | #[cfg(feature = "tick-mhz-1")] | ||
| 74 | impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { | ||
| 75 | fn delay_us(&mut self, us: u16) { | ||
| 76 | block_for(Duration::from_micros(u64::from(us))) | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | #[cfg(feature = "tick-mhz-1")] | ||
| 81 | impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { | ||
| 82 | fn delay_us(&mut self, us: u32) { | ||
| 83 | block_for(Duration::from_micros(u64::from(us))) | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | pub fn block_for(duration: Duration) { | ||
| 88 | let expires_at = Instant::now() + duration; | ||
| 89 | while Instant::now() < expires_at {} | ||
| 90 | } | ||
