@@ -374,18 +374,18 @@ impl TryFrom<i8> for Exception {
374
374
#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
375
375
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
376
376
#[ cfg_attr( feature = "std" , derive( PartialOrd , Hash ) ) ]
377
- pub enum ActiveVector {
377
+ pub enum ActiveVector < INT = u16 > {
378
378
/// Thread mode
379
379
ThreadMode ,
380
380
381
381
/// Processor core exception (internal interrupts)
382
382
Exception ( Exception ) ,
383
383
384
384
/// Device specific exception (external interrupts)
385
- Interrupt {
385
+ Interrupt (
386
386
/// Interrupt number. This number is always in range `[0, 495]` (9-bit integer - 16)
387
- irqn : u16 ,
388
- } ,
387
+ INT ,
388
+ ) ,
389
389
}
390
390
391
391
impl ActiveVector {
@@ -398,10 +398,35 @@ impl ActiveVector {
398
398
match isrn {
399
399
0 => Self :: ThreadMode ,
400
400
2 ..=15 => Self :: Exception ( Exception :: new_unchecked ( isrn as i8 - 16 ) ) ,
401
- 16 ..=511 => Self :: Interrupt { irqn : isrn - 16 } ,
401
+ 16 ..=511 => Self :: Interrupt ( isrn - 16 ) ,
402
402
_ => core:: hint:: unreachable_unchecked ( ) ,
403
403
}
404
404
}
405
+
406
+ /// Map the interrupt number to a different type.
407
+ ///
408
+ /// ### Example
409
+ ///
410
+ /// ```
411
+ /// #[exception]
412
+ /// unsafe fn DefaultHandler(vect_active: ActiveVector) -> ! {
413
+ /// let interrupt = vect_active.map_interrupt(|i| {
414
+ /// core::mem::transmute::<_, stm32l4xx_hal::pac::interrupt>(i)
415
+ /// });
416
+ ///
417
+ /// log::error!("Unexpected interrupt: ({:?})", interrupt);
418
+ ///
419
+ /// loop {}
420
+ /// }
421
+ /// ```
422
+ #[ inline]
423
+ pub fn map_interrupt < INT > ( & self , f : impl FnOnce ( u16 ) -> INT ) -> ActiveVector < INT > {
424
+ match self {
425
+ Self :: ThreadMode => ActiveVector :: ThreadMode ,
426
+ Self :: Exception ( ex) => ActiveVector :: Exception ( * ex) ,
427
+ Self :: Interrupt ( irqn) => ActiveVector :: Interrupt ( f ( * irqn) ) ,
428
+ }
429
+ }
405
430
}
406
431
407
432
impl TryFrom < u16 > for ActiveVector {
@@ -413,7 +438,7 @@ impl TryFrom<u16> for ActiveVector {
413
438
Ok ( match isrn {
414
439
0 => Self :: ThreadMode ,
415
440
2 ..=15 => Self :: Exception ( Exception :: try_from ( isrn as i8 - 16 ) . or ( Err ( isrn) ) ?) ,
416
- 16 ..=511 => Self :: Interrupt { irqn : isrn - 16 } ,
441
+ 16 ..=511 => Self :: Interrupt ( isrn - 16 ) ,
417
442
_ => return Err ( isrn) ,
418
443
} )
419
444
}
0 commit comments