Skip to content

Commit 55d1572

Browse files
authored
Implement Cancel trait for Timer<TIM> (#148)
* Implement Cancel trait for Timer<TIM>
1 parent afc60c1 commit 55d1572

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Implement `timer::Cancel` trait for `Timer<TIM>`.
13+
1014
## [v0.8.0] - 2020-04-30
1115

1216
### Changed

examples/timer-periph.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//! Start and stop a periodic peripheral timer.
2+
//!
3+
//! This example should run on all stm32f4xx boards but it was tested with
4+
//! stm32f4-discovery board (model STM32F407G-DISC1).
5+
//!
6+
//! ```bash
7+
//! cargo run --release --features stm32f407,rt --example timer-periph
8+
//! ```
9+
10+
#![no_std]
11+
#![no_main]
12+
13+
extern crate panic_halt;
14+
15+
use cortex_m_rt::entry;
16+
use cortex_m_semihosting::hprintln;
17+
18+
use embedded_hal::timer::Cancel;
19+
use hal::timer;
20+
use hal::timer::Timer;
21+
use nb;
22+
use stm32f4xx_hal as hal;
23+
24+
use crate::hal::{prelude::*, stm32};
25+
26+
#[entry]
27+
fn main() -> ! {
28+
let dp = stm32::Peripherals::take().unwrap();
29+
let rcc = dp.RCC.constrain();
30+
let clocks = rcc.cfgr.sysclk(24.mhz()).freeze();
31+
32+
// Create a timer based on SysTick
33+
let mut timer = Timer::tim1(dp.TIM1, 1.hz(), clocks);
34+
35+
hprintln!("hello!").unwrap();
36+
// wait until timer expires
37+
nb::block!(timer.wait()).unwrap();
38+
hprintln!("timer expired 1").unwrap();
39+
40+
// the function syst() creates a periodic timer, so it is automatically
41+
// restarted
42+
nb::block!(timer.wait()).unwrap();
43+
hprintln!("timer expired 2").unwrap();
44+
45+
// cancel current timer
46+
timer.cancel().unwrap();
47+
48+
// start it again
49+
timer.start(1.hz());
50+
nb::block!(timer.wait()).unwrap();
51+
hprintln!("timer expired 3").unwrap();
52+
53+
timer.cancel().unwrap();
54+
let cancel_outcome = timer.cancel();
55+
assert_eq!(cancel_outcome, Err(timer::Error::Disabled));
56+
hprintln!("ehy, you cannot cancel a timer two times!").unwrap();
57+
// this time the timer was not restarted, therefore this function should
58+
// wait forever
59+
nb::block!(timer.wait()).unwrap();
60+
// you should never see this print
61+
hprintln!("if you see this there is something wrong").unwrap();
62+
panic!();
63+
}

src/timer.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ macro_rules! hal {
274274
}
275275
}
276276

277+
impl Cancel for Timer<$TIM>
278+
{
279+
type Error = Error;
280+
281+
fn cancel(&mut self) -> Result<(), Self::Error> {
282+
let is_counter_enabled = self.tim.cr1.read().cen().is_enabled();
283+
if !is_counter_enabled {
284+
return Err(Self::Error::Disabled);
285+
}
286+
287+
// disable counter
288+
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
289+
Ok(())
290+
}
291+
}
292+
277293
impl Periodic for Timer<$TIM> {}
278294
)+
279295
}

0 commit comments

Comments
 (0)