@@ -13,8 +13,8 @@ use crate::pac::{DCB, DWT};
13
13
use enumset:: { EnumSet , EnumSetType } ;
14
14
use void:: Void ;
15
15
16
- use crate :: hal:: timer:: { CountDown , Periodic } ;
17
- use crate :: pac:: { Interrupt , RCC } ;
16
+ use crate :: hal:: timer:: { Cancel , CountDown , Periodic } ;
17
+ use crate :: pac:: RCC ;
18
18
use crate :: rcc:: { Clocks , APB1 , APB2 } ;
19
19
use crate :: time:: { duration, fixed_point:: FixedPoint , rate:: Hertz } ;
20
20
@@ -275,6 +275,26 @@ where
275
275
}
276
276
}
277
277
278
+ /// Error if a [`Cancel`]-ble [`Timer`] was cancled already or never been started.
279
+ #[ derive( Debug , Copy , Clone , PartialEq , Eq ) ]
280
+ #[ cfg_attr( feature = "defmt" , derive( defmt:: Format ) ) ]
281
+ pub struct AlreadyCancled ;
282
+
283
+ impl < TIM > Cancel for Timer < TIM >
284
+ where
285
+ TIM : Instance ,
286
+ {
287
+ type Error = AlreadyCancled ;
288
+ fn cancel ( & mut self ) -> Result < ( ) , Self :: Error > {
289
+ // If timer is already stopped.
290
+ if !self . tim . is_cr1_cen_set ( ) {
291
+ return Err ( AlreadyCancled ) ;
292
+ }
293
+ self . stop ( ) ;
294
+ Ok ( ( ) )
295
+ }
296
+ }
297
+
278
298
/// Common functionalities of all timer `RegisterBlock` types
279
299
/// based on [`crate::pac::tim6::RegisterBlock`].
280
300
///
@@ -283,6 +303,8 @@ pub trait CommonRegisterBlock: crate::private::Sealed {
283
303
#[ doc( hidden) ]
284
304
fn set_cr1_cen ( & mut self , enable : bool ) ;
285
305
#[ doc( hidden) ]
306
+ fn is_cr1_cen_set ( & mut self ) -> bool ;
307
+ #[ doc( hidden) ]
286
308
fn set_dier_uie ( & mut self , enable : bool ) ;
287
309
#[ doc( hidden) ]
288
310
fn is_dier_uie_set ( & self ) -> bool ;
@@ -334,6 +356,11 @@ macro_rules! timer {
334
356
self . cr1. modify( |_, w| w. cen( ) . bit( enable) ) ;
335
357
}
336
358
359
+ #[ inline]
360
+ fn is_cr1_cen_set( & mut self ) -> bool {
361
+ self . cr1. read( ) . cen( ) . bit( )
362
+ }
363
+
337
364
#[ inline]
338
365
fn set_dier_uie( & mut self , enable: bool ) {
339
366
self . dier. modify( |_, w| w. uie( ) . bit( enable) ) ;
0 commit comments