Add BlockingTimer and features to choose tick rate
This commit is contained in:
parent
cdb0c72849
commit
54197d1663
2 changed files with 67 additions and 2 deletions
|
@ -5,7 +5,11 @@ authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
|
|||
edition = "2018"
|
||||
|
||||
[features]
|
||||
default = ["tick-hz-32768"]
|
||||
std = ["futures/std", "embassy-traits/std"]
|
||||
tick-hz-32768 = []
|
||||
tick-hz-1000 = []
|
||||
tick-mhz-1 = []
|
||||
|
||||
defmt-trace = []
|
||||
defmt-debug = []
|
||||
|
@ -26,6 +30,7 @@ embassy-macros = { version = "0.1.0", path = "../embassy-macros"}
|
|||
embassy-traits = { version = "0.1.0", path = "../embassy-traits"}
|
||||
atomic-polyfill = { version = "0.1.1" }
|
||||
critical-section = "0.2.1"
|
||||
embedded-hal = "0.2.5"
|
||||
|
||||
# Workaround https://github.com/japaric/cast.rs/pull/27
|
||||
cast = { version = "=0.2.3", default-features = false }
|
||||
|
|
|
@ -10,8 +10,22 @@ pub use duration::Duration;
|
|||
pub use instant::Instant;
|
||||
pub use traits::*;
|
||||
|
||||
// TODO allow customizing, probably via Cargo features `tick-hz-32768` or something.
|
||||
pub const TICKS_PER_SECOND: u64 = 32768;
|
||||
#[cfg(any(
|
||||
all(feature = "tick-hz-32768", feature = "tick-hz-1000"),
|
||||
all(feature = "tick-hz-32768", feature = "tick-mhz-1"),
|
||||
))]
|
||||
compile_error!(
|
||||
"Disable default-features to be able to use a tick rate other than the default (32768 Hz)"
|
||||
);
|
||||
|
||||
#[cfg(feature = "tick-hz-1000")]
|
||||
pub const TICKS_PER_SECOND: u64 = 1_000;
|
||||
|
||||
#[cfg(feature = "tick-hz-32768")]
|
||||
pub const TICKS_PER_SECOND: u64 = 32_768;
|
||||
|
||||
#[cfg(feature = "tick-mhz-1")]
|
||||
pub const TICKS_PER_SECOND: u64 = 1_000_000;
|
||||
|
||||
static mut CLOCK: Option<&'static dyn Clock> = None;
|
||||
|
||||
|
@ -28,3 +42,49 @@ pub unsafe fn set_clock(clock: &'static dyn Clock) {
|
|||
pub(crate) fn now() -> u64 {
|
||||
unsafe { unwrap!(CLOCK, "No clock set").now() }
|
||||
}
|
||||
|
||||
pub struct BlockingTimer;
|
||||
|
||||
impl embedded_hal::blocking::delay::DelayMs<u8> for BlockingTimer {
|
||||
fn delay_ms(&mut self, ms: u8) {
|
||||
block_for(Duration::from_millis(u64::from(ms)))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal::blocking::delay::DelayMs<u16> for BlockingTimer {
|
||||
fn delay_ms(&mut self, ms: u16) {
|
||||
block_for(Duration::from_millis(u64::from(ms)))
|
||||
}
|
||||
}
|
||||
|
||||
impl embedded_hal::blocking::delay::DelayMs<u32> for BlockingTimer {
|
||||
fn delay_ms(&mut self, ms: u32) {
|
||||
block_for(Duration::from_millis(u64::from(ms)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tick-mhz-1")]
|
||||
impl embedded_hal::blocking::delay::DelayUs<u8> for BlockingTimer {
|
||||
fn delay_us(&mut self, us: u8) {
|
||||
block_for(Duration::from_micros(u64::from(us)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tick-mhz-1")]
|
||||
impl embedded_hal::blocking::delay::DelayUs<u16> for BlockingTimer {
|
||||
fn delay_us(&mut self, us: u16) {
|
||||
block_for(Duration::from_micros(u64::from(us)))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tick-mhz-1")]
|
||||
impl embedded_hal::blocking::delay::DelayUs<u32> for BlockingTimer {
|
||||
fn delay_us(&mut self, us: u32) {
|
||||
block_for(Duration::from_micros(u64::from(us)))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn block_for(duration: Duration) {
|
||||
let expires_at = Instant::now() + duration;
|
||||
while Instant::now() < expires_at {}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue