Skip to content

Commit d5b3579

Browse files
committed
Allow mapping interrupt in ActiveVector.
1 parent d93b114 commit d5b3579

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/peripheral/scb.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,18 @@ impl TryFrom<i8> for Exception {
374374
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
375375
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
376376
#[cfg_attr(feature = "std", derive(PartialOrd, Hash))]
377-
pub enum ActiveVector {
377+
pub enum ActiveVector<INT = u16> {
378378
/// Thread mode
379379
ThreadMode,
380380

381381
/// Processor core exception (internal interrupts)
382382
Exception(Exception),
383383

384384
/// Device specific exception (external interrupts)
385-
Interrupt {
385+
Interrupt(
386386
/// Interrupt number. This number is always in range `[0, 495]` (9-bit integer - 16)
387-
irqn: u16,
388-
},
387+
INT,
388+
),
389389
}
390390

391391
impl ActiveVector {
@@ -398,10 +398,35 @@ impl ActiveVector {
398398
match isrn {
399399
0 => Self::ThreadMode,
400400
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),
402402
_ => core::hint::unreachable_unchecked(),
403403
}
404404
}
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+
}
405430
}
406431

407432
impl TryFrom<u16> for ActiveVector {
@@ -413,7 +438,7 @@ impl TryFrom<u16> for ActiveVector {
413438
Ok(match isrn {
414439
0 => Self::ThreadMode,
415440
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),
417442
_ => return Err(isrn),
418443
})
419444
}

0 commit comments

Comments
 (0)