Skip to content

Commit

Permalink
Merge #374
Browse files Browse the repository at this point in the history
374: Construct a SysTick delay with a clock source r=adamgreig a=mciantyre

Helpful for users who prefer an external clock source for their system
timer. Proposing this as a 0.7 backwards-compatible change.

Co-authored-by: Ian McIntyre <[email protected]>
  • Loading branch information
2 people authored and adamgreig committed Dec 31, 2021
1 parent 2f0593e commit bbcdae6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
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

0 comments on commit bbcdae6

Please sign in to comment.