Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct a SysTick delay with a clock source #374

Merged
merged 1 commit into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `DWT.set_cycle_count` (#347).
- Added support for the Cortex-M7 TCM and cache access control registers.
There is a feature `cm7` to enable access to these.
- Added `delay::Delay::with_source`, a constructor that lets you specify
the SysTick clock source (#374).

### Deprecated

Expand Down
22 changes: 14 additions & 8 deletions src/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ 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 {
/// 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);
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.
Expand All @@ -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 {
Expand Down