Skip to content

Commit 1d9ec16

Browse files
authored
Implement timer::Cancel trait for CountDownTimer (#261)
1 parent 2f9f5da commit 1d9ec16

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1515
### Added
1616

1717
- Add 16 bit dataframe size for `SPI`
18+
- Implement `timer::Cancel` trait for `CountDownTimer`
1819

1920
### Added
2021

src/timer.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
| CH4 | PB9 | PD15 |
4848
*/
4949

50-
use crate::hal::timer::{CountDown, Periodic};
50+
use crate::hal::timer::{Cancel, CountDown, Periodic};
5151
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity",))]
5252
use crate::pac::TIM1;
5353
#[cfg(feature = "medium")]
@@ -83,6 +83,12 @@ pub enum Event {
8383
Update,
8484
}
8585

86+
#[derive(Debug, PartialEq)]
87+
pub enum Error {
88+
/// Timer is canceled
89+
Canceled,
90+
}
91+
8692
pub struct Timer<TIM> {
8793
pub(crate) tim: TIM,
8894
pub(crate) clk: Hertz,
@@ -255,6 +261,19 @@ impl CountDown for CountDownTimer<SYST> {
255261
}
256262
}
257263

264+
impl Cancel for CountDownTimer<SYST> {
265+
type Error = Error;
266+
267+
fn cancel(&mut self) -> Result<(), Self::Error> {
268+
if !self.tim.is_counter_enabled() {
269+
return Err(Self::Error::Canceled);
270+
}
271+
272+
self.tim.disable_counter();
273+
Ok(())
274+
}
275+
}
276+
258277
impl Periodic for CountDownTimer<SYST> {}
259278

260279
macro_rules! hal {
@@ -407,6 +426,22 @@ macro_rules! hal {
407426
}
408427
}
409428

429+
impl Cancel for CountDownTimer<$TIMX>
430+
{
431+
type Error = Error;
432+
433+
fn cancel(&mut self) -> Result<(), Self::Error> {
434+
let is_counter_enabled = self.tim.cr1.read().cen().is_enabled();
435+
if !is_counter_enabled {
436+
return Err(Self::Error::Canceled);
437+
}
438+
439+
// disable counter
440+
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
441+
Ok(())
442+
}
443+
}
444+
410445
impl Periodic for CountDownTimer<$TIMX> {}
411446
)+
412447
}

0 commit comments

Comments
 (0)