|
47 | 47 | | CH4 | PB9 | PD15 | |
48 | 48 | */ |
49 | 49 |
|
50 | | -use crate::hal::timer::{CountDown, Periodic}; |
| 50 | +use crate::hal::timer::{Cancel, CountDown, Periodic}; |
51 | 51 | #[cfg(any(feature = "stm32f100", feature = "stm32f103", feature = "connectivity",))] |
52 | 52 | use crate::pac::TIM1; |
53 | 53 | #[cfg(feature = "medium")] |
@@ -83,6 +83,12 @@ pub enum Event { |
83 | 83 | Update, |
84 | 84 | } |
85 | 85 |
|
| 86 | +#[derive(Debug, PartialEq)] |
| 87 | +pub enum Error { |
| 88 | + /// Timer is canceled |
| 89 | + Canceled, |
| 90 | +} |
| 91 | + |
86 | 92 | pub struct Timer<TIM> { |
87 | 93 | pub(crate) tim: TIM, |
88 | 94 | pub(crate) clk: Hertz, |
@@ -255,6 +261,19 @@ impl CountDown for CountDownTimer<SYST> { |
255 | 261 | } |
256 | 262 | } |
257 | 263 |
|
| 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 | + |
258 | 277 | impl Periodic for CountDownTimer<SYST> {} |
259 | 278 |
|
260 | 279 | macro_rules! hal { |
@@ -407,6 +426,22 @@ macro_rules! hal { |
407 | 426 | } |
408 | 427 | } |
409 | 428 |
|
| 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 | + |
410 | 445 | impl Periodic for CountDownTimer<$TIMX> {} |
411 | 446 | )+ |
412 | 447 | } |
|
0 commit comments