Skip to content

Commit f48a760

Browse files
committed
generic Delay
1 parent 40a1f76 commit f48a760

File tree

4 files changed

+44
-112
lines changed

4 files changed

+44
-112
lines changed

examples/delay-timer-blinky.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ fn main() -> ! {
2828
let clocks = rcc.cfgr.sysclk(48.mhz()).freeze();
2929

3030
// Create a delay abstraction based on general-pupose 32-bit timer TIM5
31-
let mut delay = hal::delay::Tim5Delay::new(dp.TIM5, clocks);
31+
let mut delay = hal::delay::Delay::tim5(dp.TIM5, clocks);
3232

3333
loop {
3434
// On for 1s, off for 1s.
35-
led.set_high().unwrap();
35+
led.set_high();
3636
delay.delay_ms(1_000_u32);
37-
led.set_low().unwrap();
37+
led.set_low();
3838
delay.delay_us(1_000_000_u32);
3939
}
4040
}

src/delay/mod.rs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,14 @@
22
33
mod syst;
44

5-
pub use syst::SystickDelay as Delay;
5+
use cortex_m::peripheral::SYST;
66

7-
mod timer;
7+
use crate::rcc::Clocks;
88

9-
#[cfg(any(
10-
feature = "stm32f401",
11-
feature = "stm32f405",
12-
feature = "stm32f407",
13-
feature = "stm32f411",
14-
feature = "stm32f412",
15-
feature = "stm32f413",
16-
feature = "stm32f415",
17-
feature = "stm32f417",
18-
feature = "stm32f423",
19-
feature = "stm32f427",
20-
feature = "stm32f429",
21-
feature = "stm32f437",
22-
feature = "stm32f439",
23-
feature = "stm32f446",
24-
feature = "stm32f469",
25-
feature = "stm32f479"
26-
))]
27-
pub use timer::Tim2Delay;
9+
/// Timer as a delay provider (SysTick by default)
10+
pub struct Delay<T = SYST> {
11+
tim: T,
12+
clocks: Clocks,
13+
}
2814

29-
#[cfg(any(
30-
feature = "stm32f401",
31-
feature = "stm32f405",
32-
feature = "stm32f407",
33-
feature = "stm32f410",
34-
feature = "stm32f411",
35-
feature = "stm32f412",
36-
feature = "stm32f413",
37-
feature = "stm32f415",
38-
feature = "stm32f417",
39-
feature = "stm32f423",
40-
feature = "stm32f427",
41-
feature = "stm32f429",
42-
feature = "stm32f437",
43-
feature = "stm32f439",
44-
feature = "stm32f446",
45-
feature = "stm32f469",
46-
feature = "stm32f479"
47-
))]
48-
pub use timer::Tim5Delay;
15+
mod timer;

src/delay/syst.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@ use cortex_m::peripheral::SYST;
77
use crate::rcc::Clocks;
88
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
99

10-
/// System timer (SysTick) as a delay provider
11-
pub struct SystickDelay {
12-
clocks: Clocks,
13-
syst: SYST,
14-
}
10+
use super::Delay;
1511

16-
impl SystickDelay {
12+
impl Delay<SYST> {
1713
/// Configures the system timer (SysTick) as a delay provider
18-
pub fn new(mut syst: SYST, clocks: Clocks) -> Self {
19-
syst.set_clock_source(SystClkSource::External);
20-
Self { syst, clocks }
14+
pub fn new(mut tim: SYST, clocks: Clocks) -> Self {
15+
tim.set_clock_source(SystClkSource::External);
16+
Self { tim, clocks }
2117
}
2218

2319
#[deprecated(since = "0.10.0", note = "Please use release instead")]
@@ -27,29 +23,29 @@ impl SystickDelay {
2723

2824
/// Releases the system timer (SysTick) resource
2925
pub fn release(self) -> SYST {
30-
self.syst
26+
self.tim
3127
}
3228
}
3329

34-
impl DelayMs<u32> for SystickDelay {
30+
impl DelayMs<u32> for Delay<SYST> {
3531
fn delay_ms(&mut self, ms: u32) {
3632
self.delay_us(ms * 1_000);
3733
}
3834
}
3935

40-
impl DelayMs<u16> for SystickDelay {
36+
impl DelayMs<u16> for Delay<SYST> {
4137
fn delay_ms(&mut self, ms: u16) {
4238
self.delay_ms(u32(ms));
4339
}
4440
}
4541

46-
impl DelayMs<u8> for SystickDelay {
42+
impl DelayMs<u8> for Delay<SYST> {
4743
fn delay_ms(&mut self, ms: u8) {
4844
self.delay_ms(u32(ms));
4945
}
5046
}
5147

52-
impl DelayUs<u32> for SystickDelay {
48+
impl DelayUs<u32> for Delay<SYST> {
5349
fn delay_us(&mut self, us: u32) {
5450
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
5551
const MAX_RVR: u32 = 0x00FF_FFFF;
@@ -63,27 +59,27 @@ impl DelayUs<u32> for SystickDelay {
6359
MAX_RVR
6460
};
6561

66-
self.syst.set_reload(current_rvr);
67-
self.syst.clear_current();
68-
self.syst.enable_counter();
62+
self.tim.set_reload(current_rvr);
63+
self.tim.clear_current();
64+
self.tim.enable_counter();
6965

7066
// Update the tracking variable while we are waiting...
7167
total_rvr -= current_rvr;
7268

73-
while !self.syst.has_wrapped() {}
69+
while !self.tim.has_wrapped() {}
7470

75-
self.syst.disable_counter();
71+
self.tim.disable_counter();
7672
}
7773
}
7874
}
7975

80-
impl DelayUs<u16> for SystickDelay {
76+
impl DelayUs<u16> for Delay<SYST> {
8177
fn delay_us(&mut self, us: u16) {
8278
self.delay_us(u32(us))
8379
}
8480
}
8581

86-
impl DelayUs<u8> for SystickDelay {
82+
impl DelayUs<u8> for Delay<SYST> {
8783
fn delay_us(&mut self, us: u8) {
8884
self.delay_us(u32(us))
8985
}

src/delay/timer.rs

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,16 @@ use cast::{u16, u32};
99
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
1010

1111
use crate::{
12-
bb,
1312
pac::{self, RCC},
14-
rcc::Clocks,
13+
rcc::{Clocks, Enable, Reset},
1514
};
1615

16+
use super::Delay;
17+
1718
macro_rules! hal {
18-
($($TIM:ident: ($struct:ident, $waitfn:ident, $en_bit:expr, $apbenr:ident, $apbrstr:ident, $pclk:ident, $ppre:ident),)+) => {
19+
($($TIM:ty: ($tim:ident, $waitfn:ident),)+) => {
1920
$(
20-
/// General purpose timer as delay provider
21-
pub struct $struct {
22-
clocks: Clocks,
23-
tim: pac::$TIM,
24-
}
25-
26-
fn $waitfn(tim: &mut pac::$TIM, prescaler: u16, auto_reload_register: u32) {
21+
fn $waitfn(tim: &mut $TIM, prescaler: u16, auto_reload_register: u32) {
2722
// Write Prescaler (PSC)
2823
tim.psc.write(|w| w.psc().bits(prescaler));
2924

@@ -46,22 +41,15 @@ macro_rules! hal {
4641
while tim.cr1.read().cen().is_enabled() { /* wait */ }
4742
}
4843

49-
impl $struct {
44+
impl Delay<$TIM> {
5045
/// Configures the timer as a delay provider
51-
pub fn new(tim: pac::$TIM, clocks: Clocks) -> Self {
46+
pub fn $tim(tim: $TIM, clocks: Clocks) -> Self {
5247
unsafe {
5348
//NOTE(unsafe) this reference will only be used for atomic writes with no side effects
5449
let rcc = &(*RCC::ptr());
5550

56-
// Enable timer peripheral in RCC
57-
bb::set(&rcc.$apbenr, $en_bit);
58-
59-
// Stall the pipeline to work around erratum 2.1.13 (DM00037591)
60-
cortex_m::asm::dsb();
61-
62-
// Reset timer
63-
bb::set(&rcc.$apbrstr, $en_bit);
64-
bb::clear(&rcc.$apbrstr, $en_bit);
51+
<$TIM>::enable(rcc);
52+
<$TIM>::reset(rcc);
6553
}
6654

6755
// Enable one-pulse mode (counter stops counting at the next update
@@ -72,12 +60,12 @@ macro_rules! hal {
7260
}
7361

7462
/// Releases the timer resource
75-
pub fn free(self) -> pac::$TIM {
63+
pub fn free(self) -> $TIM {
7664
self.tim
7765
}
7866
}
7967

80-
impl DelayUs<u32> for $struct {
68+
impl DelayUs<u32> for Delay<$TIM> {
8169
/// Sleep for up to 2^32-1 microseconds (~71 minutes).
8270
fn delay_us(&mut self, us: u32) {
8371
// Set up prescaler so that a tick takes exactly 1 µs.
@@ -92,7 +80,7 @@ macro_rules! hal {
9280
}
9381
}
9482

95-
impl DelayUs<u16> for $struct {
83+
impl DelayUs<u16> for Delay<$TIM> {
9684
/// Sleep for up to 2^16-1 microseconds (~65 milliseconds).
9785
fn delay_us(&mut self, us: u16) {
9886
// See DelayUs<u32> for explanations.
@@ -103,7 +91,7 @@ macro_rules! hal {
10391
}
10492
}
10593

106-
impl DelayMs<u32> for $struct {
94+
impl DelayMs<u32> for Delay<$TIM> {
10795
/// Sleep for up to (2^32)/2-1 milliseconds (~24 days).
10896
/// If the `ms` value is larger than 2147483647, the code will panic.
10997
fn delay_ms(&mut self, ms: u32) {
@@ -132,7 +120,7 @@ macro_rules! hal {
132120
}
133121
}
134122

135-
impl DelayMs<u16> for $struct {
123+
impl DelayMs<u16> for Delay<$TIM> {
136124
/// Sleep for up to (2^16)-1 milliseconds (~65 seconds).
137125
fn delay_ms(&mut self, ms: u16) {
138126
// See DelayMs<u32> for explanations. Since the value range is only 16 bit,
@@ -147,27 +135,8 @@ macro_rules! hal {
147135
}
148136
}
149137

150-
#[cfg(any(
151-
feature = "stm32f401",
152-
feature = "stm32f405",
153-
feature = "stm32f407",
154-
feature = "stm32f410",
155-
feature = "stm32f411",
156-
feature = "stm32f412",
157-
feature = "stm32f413",
158-
feature = "stm32f415",
159-
feature = "stm32f417",
160-
feature = "stm32f423",
161-
feature = "stm32f427",
162-
feature = "stm32f429",
163-
feature = "stm32f437",
164-
feature = "stm32f439",
165-
feature = "stm32f446",
166-
feature = "stm32f469",
167-
feature = "stm32f479"
168-
))]
169138
hal! {
170-
TIM5: (Tim5Delay, wait_tim5, 3, apb1enr, apb1rstr, pclk1, ppre1),
139+
pac::TIM5: (tim5, wait_tim5),
171140
}
172141

173142
#[cfg(any(
@@ -189,5 +158,5 @@ hal! {
189158
feature = "stm32f479"
190159
))]
191160
hal! {
192-
TIM2: (Tim2Delay, wait_tim2, 0, apb1enr, apb1rstr, pclk1, ppre1),
161+
pac::TIM2: (tim2, wait_tim2),
193162
}

0 commit comments

Comments
 (0)