aboutsummaryrefslogtreecommitdiff
path: root/src/delay.rs
diff options
context:
space:
mode:
authorGravatar Adam Greig <adam@adamgreig.com> 2021-12-31 17:44:33 +0000
committerGravatar GitHub <noreply@github.com> 2021-12-31 17:44:33 +0000
commit819f8be6c64c233ce65b12f27e0efd82833a6897 (patch)
tree1f3cf84dbe58ac8efbc1619f292c0dc485c08ece /src/delay.rs
parent255faf75de84f72389a45e3d99b824e0a4599bed (diff)
parent08452a9de25e4bc66fb80015e0c78c08eb967237 (diff)
downloadcortex-m-819f8be6c64c233ce65b12f27e0efd82833a6897.tar.gz
cortex-m-819f8be6c64c233ce65b12f27e0efd82833a6897.tar.zst
cortex-m-819f8be6c64c233ce65b12f27e0efd82833a6897.zip
Merge branch 'master' into patch-1
Diffstat (limited to 'src/delay.rs')
-rw-r--r--src/delay.rs22
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 {