diff options
author | 2021-12-28 22:20:53 -0500 | |
---|---|---|
committer | 2021-12-29 07:11:45 -0500 | |
commit | 7df5de5347a245cad601aea86fca93c8581ee4d3 (patch) | |
tree | e6c7265f40a735d386c1d38b53d73d3b745e99d5 /src/delay.rs | |
parent | 441cb87e4095eafbd8b170d8f0848b99fa1ec72e (diff) | |
download | cortex-m-7df5de5347a245cad601aea86fca93c8581ee4d3.tar.gz cortex-m-7df5de5347a245cad601aea86fca93c8581ee4d3.tar.zst cortex-m-7df5de5347a245cad601aea86fca93c8581ee4d3.zip |
Construct a SysTick delay with a clock source
Helpful for users who prefer an external clock source for their system
timer. Proposing this as a 0.7 backwards-compatible change.
The constructor name tries to follow the "general constructors"
recommendation in the Rust API naming guidelines.
https://rust-lang.github.io/api-guidelines/naming.html#casing-conforms-to-rfc-430-c-case
Diffstat (limited to 'src/delay.rs')
-rw-r--r-- | src/delay.rs | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/delay.rs b/src/delay.rs index 8ed1fea..66a63bf 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -6,7 +6,7 @@ use embedded_hal::blocking::delay::{DelayMs, DelayUs}; /// System timer (SysTick) as a delay provider. pub struct Delay { syst: SYST, - ahb_frequency: u32, + frequency: u32, } impl Delay { @@ -14,13 +14,19 @@ impl Delay { /// /// `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); + pub fn new(syst: SYST, ahb_frequency: u32) -> Self { + Self::with_source(syst, ahb_frequency, SystClkSource::Core) + } - Delay { - syst, - ahb_frequency, - } + /// Configures the system timer (SysTick) as a delay provider + /// with a clock source. + /// + /// `frequency` is the frequency of your `clock_source` in Hz. + #[inline] + pub fn with_source(mut syst: SYST, frequency: u32, clock_source: SystClkSource) -> Self { + syst.set_clock_source(clock_source); + + Delay { syst, frequency } } /// Releases the system timer (SysTick) resource. @@ -32,7 +38,7 @@ impl Delay { /// Delay using the Cortex-M systick for a certain duration, in µs. #[allow(clippy::missing_inline_in_public_items)] pub fn delay_us(&mut self, us: u32) { - let ticks = (u64::from(us)) * (u64::from(self.ahb_frequency)) / 1_000_000; + let ticks = (u64::from(us)) * (u64::from(self.frequency)) / 1_000_000; let full_cycles = ticks >> 24; if full_cycles > 0 { |