aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThales Fragoso <[email protected]>2021-06-28 18:01:40 -0300
committerThales Fragoso <[email protected]>2021-06-28 18:01:40 -0300
commit54197d1663b8579b9f569e60f1e4aa010bed2cfe (patch)
treedbd3db67c208c8b155cddc5dbad6d01a62fdb0ae
parentcdb0c72849558db2b210301f5d13a922308e6bf1 (diff)
Add BlockingTimer and features to choose tick rate
-rw-r--r--embassy/Cargo.toml5
-rw-r--r--embassy/src/time/mod.rs64
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]>"]
5edition = "2018" 5edition = "2018"
6 6
7[features] 7[features]
8default = ["tick-hz-32768"]
8std = ["futures/std", "embassy-traits/std"] 9std = ["futures/std", "embassy-traits/std"]
10tick-hz-32768 = []
11tick-hz-1000 = []
12tick-mhz-1 = []
9 13
10defmt-trace = [] 14defmt-trace = []
11defmt-debug = [] 15defmt-debug = []
@@ -26,6 +30,7 @@ embassy-macros = { version = "0.1.0", path = "../embassy-macros"}
26embassy-traits = { version = "0.1.0", path = "../embassy-traits"} 30embassy-traits = { version = "0.1.0", path = "../embassy-traits"}
27atomic-polyfill = { version = "0.1.1" } 31atomic-polyfill = { version = "0.1.1" }
28critical-section = "0.2.1" 32critical-section = "0.2.1"
33embedded-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
31cast = { version = "=0.2.3", default-features = false } 36cast = { 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;
10pub use instant::Instant; 10pub use instant::Instant;
11pub use traits::*; 11pub use traits::*;
12 12
13// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something. 13#[cfg(any(
14pub 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))]
17compile_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")]
22pub const TICKS_PER_SECOND: u64 = 1_000;
23
24#[cfg(feature = "tick-hz-32768")]
25pub const TICKS_PER_SECOND: u64 = 32_768;
26
27#[cfg(feature = "tick-mhz-1")]
28pub const TICKS_PER_SECOND: u64 = 1_000_000;
15 29
16static mut CLOCK: Option<&'static dyn Clock> = None; 30static mut CLOCK: Option<&'static dyn Clock> = None;
17 31
@@ -28,3 +42,49 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) {
28pub(crate) fn now() -> u64 { 42pub(crate) fn now() -> u64 {
29 unsafe { unwrap!(CLOCK, "No clock set").now() } 43 unsafe { unwrap!(CLOCK, "No clock set").now() }
30} 44}
45
46pub struct BlockingTimer;
47
48impl 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
54impl 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
60impl 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")]
67impl 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")]
74impl 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")]
81impl 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
87pub fn block_for(duration: Duration) {
88 let expires_at = Instant::now() + duration;
89 while Instant::now() < expires_at {}
90}