Skip to content

Commit 08452a9

Browse files
bors[bot]mciantyre
andauthored
Merge #374
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]>
2 parents bc00af3 + 7df5de5 commit 08452a9

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Added `DWT.set_cycle_count` (#347).
1717
- Added support for the Cortex-M7 TCM and cache access control registers.
1818
There is a feature `cm7` to enable access to these.
19+
- Added `delay::Delay::with_source`, a constructor that lets you specify
20+
the SysTick clock source (#374).
1921

2022
### Deprecated
2123

src/delay.rs

+14-8
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,27 @@ use embedded_hal::blocking::delay::{DelayMs, DelayUs};
66
/// System timer (SysTick) as a delay provider.
77
pub struct Delay {
88
syst: SYST,
9-
ahb_frequency: u32,
9+
frequency: u32,
1010
}
1111

1212
impl Delay {
1313
/// Configures the system timer (SysTick) as a delay provider.
1414
///
1515
/// `ahb_frequency` is a frequency of the AHB bus in Hz.
1616
#[inline]
17-
pub fn new(mut syst: SYST, ahb_frequency: u32) -> Self {
18-
syst.set_clock_source(SystClkSource::Core);
17+
pub fn new(syst: SYST, ahb_frequency: u32) -> Self {
18+
Self::with_source(syst, ahb_frequency, SystClkSource::Core)
19+
}
1920

20-
Delay {
21-
syst,
22-
ahb_frequency,
23-
}
21+
/// Configures the system timer (SysTick) as a delay provider
22+
/// with a clock source.
23+
///
24+
/// `frequency` is the frequency of your `clock_source` in Hz.
25+
#[inline]
26+
pub fn with_source(mut syst: SYST, frequency: u32, clock_source: SystClkSource) -> Self {
27+
syst.set_clock_source(clock_source);
28+
29+
Delay { syst, frequency }
2430
}
2531

2632
/// Releases the system timer (SysTick) resource.
@@ -32,7 +38,7 @@ impl Delay {
3238
/// Delay using the Cortex-M systick for a certain duration, in µs.
3339
#[allow(clippy::missing_inline_in_public_items)]
3440
pub fn delay_us(&mut self, us: u32) {
35-
let ticks = (u64::from(us)) * (u64::from(self.ahb_frequency)) / 1_000_000;
41+
let ticks = (u64::from(us)) * (u64::from(self.frequency)) / 1_000_000;
3642

3743
let full_cycles = ticks >> 24;
3844
if full_cycles > 0 {

0 commit comments

Comments
 (0)