Skip to content

Commit

Permalink
Implement timer::Cancel trait for CountDownTimer (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusknaust authored Sep 7, 2020
1 parent 2f9f5da commit 1d9ec16
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Add 16 bit dataframe size for `SPI`
- Implement `timer::Cancel` trait for `CountDownTimer`

### Added

Expand Down
37 changes: 36 additions & 1 deletion src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
| CH4 | PB9 | PD15 |
*/

use crate::hal::timer::{CountDown, Periodic};
use crate::hal::timer::{Cancel, CountDown, Periodic};
#[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity",))]
use crate::pac::TIM1;
#[cfg(feature = "medium")]
Expand Down Expand Up @@ -83,6 +83,12 @@ pub enum Event {
Update,
}

#[derive(Debug, PartialEq)]
pub enum Error {
/// Timer is canceled
Canceled,
}

pub struct Timer<TIM> {
pub(crate) tim: TIM,
pub(crate) clk: Hertz,
Expand Down Expand Up @@ -255,6 +261,19 @@ impl CountDown for CountDownTimer<SYST> {
}
}

impl Cancel for CountDownTimer<SYST> {
type Error = Error;

fn cancel(&mut self) -> Result<(), Self::Error> {
if !self.tim.is_counter_enabled() {
return Err(Self::Error::Canceled);
}

self.tim.disable_counter();
Ok(())
}
}

impl Periodic for CountDownTimer<SYST> {}

macro_rules! hal {
Expand Down Expand Up @@ -407,6 +426,22 @@ macro_rules! hal {
}
}

impl Cancel for CountDownTimer<$TIMX>
{
type Error = Error;

fn cancel(&mut self) -> Result<(), Self::Error> {
let is_counter_enabled = self.tim.cr1.read().cen().is_enabled();
if !is_counter_enabled {
return Err(Self::Error::Canceled);
}

// disable counter
self.tim.cr1.modify(|_, w| w.cen().clear_bit());
Ok(())
}
}

impl Periodic for CountDownTimer<$TIMX> {}
)+
}
Expand Down

0 comments on commit 1d9ec16

Please sign in to comment.