diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ad454a..558f07a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). various NVIC functions, replacing the previous use of `Nr` from bare-metal. - Associated const `PTR` is introduced to Core Peripherals to eventually replace the existing `ptr()` API. +- A delay driver based on SysTick. ## [v0.6.2] - 2020-01-12 diff --git a/Cargo.toml b/Cargo.toml index e995d82e..2e55f6ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ links = "cortex-m" # prevent multiple versions of this crate to be linked toget bare-metal = { version = "0.2.0", features = ["const-fn"] } volatile-register = "0.2.0" bitfield = "0.13.2" +embedded-hal = "0.2.4" [package.metadata.docs.rs] targets = [ diff --git a/src/delay.rs b/src/delay.rs new file mode 100644 index 00000000..051151fa --- /dev/null +++ b/src/delay.rs @@ -0,0 +1,119 @@ +//! A delay driver based on SysTick. + +use crate::peripheral::{syst::SystClkSource, SYST}; +use embedded_hal::blocking::delay::{DelayMs, DelayUs}; + +/// System timer (SysTick) as a delay provider. +pub struct Delay { + syst: SYST, + ahb_frequency: u32, +} + +impl Delay { + /// Configures the system timer (SysTick) as a delay provider. + /// + /// `ahb_frequency` is a frequency of the AHB bus in Hz. + #[inline] + pub fn new(mut syst: SYST, ahb_frequency: u32) -> Self { + syst.set_clock_source(SystClkSource::Core); + + Delay { syst, ahb_frequency } + } + + /// Releases the system timer (SysTick) resource. + #[inline] + pub fn free(self) -> SYST { + self.syst + } + + fn _delay_us(&mut self, us: u32) { + let ticks = (us as u64) * (self.ahb_frequency as u64) / 1_000_000; + + let full_cycles = ticks >> 24; + if full_cycles > 0 { + self.syst.set_reload(0xffffff); + self.syst.clear_current(); + self.syst.enable_counter(); + + for _ in 0..full_cycles { + while !self.syst.has_wrapped() {} + } + } + + let ticks = (ticks & 0xffffff) as u32; + if ticks > 1 { + self.syst.set_reload(ticks - 1); + self.syst.clear_current(); + self.syst.enable_counter(); + + while !self.syst.has_wrapped() {} + } + + self.syst.disable_counter(); + } +} + +impl DelayMs for Delay { + #[inline] + fn delay_ms(&mut self, mut ms: u32) { + // 4294967 is the highest u32 value which you can multiply by 1000 without overflow + while ms > 4294967 { + self.delay_us(4294967000u32); + ms -= 4294967; + } + self.delay_us(ms * 1_000); + } +} + +// This is a workaround to allow `delay_ms(42)` construction without specifying a type. +impl DelayMs for Delay { + #[inline(always)] + fn delay_ms(&mut self, ms: i32) { + assert!(ms >= 0); + self.delay_ms(ms as u32); + } +} + +impl DelayMs for Delay { + #[inline(always)] + fn delay_ms(&mut self, ms: u16) { + self.delay_ms(u32::from(ms)); + } +} + +impl DelayMs for Delay { + #[inline(always)] + fn delay_ms(&mut self, ms: u8) { + self.delay_ms(u32::from(ms)); + } +} + +impl DelayUs for Delay { + #[inline] + fn delay_us(&mut self, us: u32) { + self._delay_us(us); + } +} + +// This is a workaround to allow `delay_us(42)` construction without specifying a type. +impl DelayUs for Delay { + #[inline(always)] + fn delay_us(&mut self, us: i32) { + assert!(us >= 0); + self.delay_us(us as u32); + } +} + +impl DelayUs for Delay { + #[inline(always)] + fn delay_us(&mut self, us: u16) { + self.delay_us(u32::from(us)) + } +} + +impl DelayUs for Delay { + #[inline(always)] + fn delay_us(&mut self, us: u8) { + self.delay_us(u32::from(us)) + } +} diff --git a/src/lib.rs b/src/lib.rs index 89f420de..5dbb8f56 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,10 +61,12 @@ mod macros; pub mod asm; #[cfg(armv8m)] pub mod cmse; +pub mod delay; pub mod interrupt; #[cfg(all(not(armv6m), not(armv8m_base)))] pub mod itm; pub mod peripheral; +pub mod prelude; pub mod register; pub use crate::peripheral::Peripherals; diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 00000000..bc47cc02 --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,3 @@ +//! Prelude + +pub use embedded_hal::prelude::*;