aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThales Fragoso <[email protected]>2021-06-28 18:30:04 -0300
committerThales Fragoso <[email protected]>2021-06-28 18:52:27 -0300
commit51583afc1e1e5d6eafefbb994c153d1d923a502f (patch)
treefb29dbf1cbef12744840c6d612dc0211ae6373b9
parent54197d1663b8579b9f569e60f1e4aa010bed2cfe (diff)
Add docs for BlockingTimer and rename tick features
-rw-r--r--embassy/Cargo.toml8
-rw-r--r--embassy/src/time/mod.rs35
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]>"]
5edition = "2018" 5edition = "2018"
6 6
7[features] 7[features]
8default = ["tick-hz-32768"] 8default = ["tick-32768hz"]
9std = ["futures/std", "embassy-traits/std"] 9std = ["futures/std", "embassy-traits/std"]
10tick-hz-32768 = [] 10tick-32768hz = []
11tick-hz-1000 = [] 11tick-1000hz = []
12tick-mhz-1 = [] 12tick-1mhz = []
13 13
14defmt-trace = [] 14defmt-trace = []
15defmt-debug = [] 15defmt-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;
11pub use traits::*; 11pub 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))]
17compile_error!( 17compile_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")]
22pub const TICKS_PER_SECOND: u64 = 1_000; 22pub const TICKS_PER_SECOND: u64 = 1_000;
23 23
24#[cfg(feature = "tick-hz-32768")] 24#[cfg(feature = "tick-32768hz")]
25pub const TICKS_PER_SECOND: u64 = 32_768; 25pub const TICKS_PER_SECOND: u64 = 32_768;
26 26
27#[cfg(feature = "tick-mhz-1")] 27#[cfg(feature = "tick-1mhz")]
28pub const TICKS_PER_SECOND: u64 = 1_000_000; 28pub const TICKS_PER_SECOND: u64 = 1_000_000;
29 29
30static mut CLOCK: Option<&'static dyn Clock> = None; 30static 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.
46pub struct BlockingTimer; 53pub struct BlockingTimer;
47 54
48impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer { 55impl 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
54impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer { 61impl 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
60impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer { 67impl 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")]
67impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer { 73impl 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")]
74impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer { 79impl 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")]
81impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer { 85impl 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.
87pub fn block_for(duration: Duration) { 94pub 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 {}