Skip to content

Commit 164647b

Browse files
committed
Allow mapping interrupt in Vector.
1 parent 924293d commit 164647b

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/peripheral/scb.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,18 @@ impl TryFrom<i8> for Exception {
287287
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
288288
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
289289
#[cfg_attr(feature = "std", derive(PartialOrd, Hash))]
290-
pub enum Vector {
290+
pub enum Vector<INT = u16> {
291291
/// Thread mode
292292
ThreadMode,
293293

294294
/// Processor core exception (internal interrupts)
295295
Exception(Exception),
296296

297297
/// Device specific exception (external interrupts)
298-
Interrupt {
298+
Interrupt(
299299
/// Interrupt number. This number is always in range `[0, 495]` (9-bit integer - 16)
300-
irqn: u16,
301-
},
300+
INT,
301+
),
302302
}
303303

304304
impl Vector {
@@ -311,10 +311,35 @@ impl Vector {
311311
match isrn {
312312
0 => Self::ThreadMode,
313313
2..=15 => Self::Exception(Exception::new_unchecked(isrn as i8 - 16)),
314-
16..=511 => Self::Interrupt { irqn: isrn - 16 },
314+
16..=511 => Self::Interrupt(isrn - 16),
315315
_ => core::hint::unreachable_unchecked(),
316316
}
317317
}
318+
319+
/// Map the interrupt number to a different type.
320+
///
321+
/// ### Example
322+
///
323+
/// ```
324+
/// #[exception]
325+
/// unsafe fn DefaultHandler(vect_active: Vector) -> ! {
326+
/// let interrupt = vect_active.map_interrupt(|i| {
327+
/// core::mem::transmute::<_, stm32l4xx_hal::pac::interrupt>(i)
328+
/// });
329+
///
330+
/// log::error!("Unexpected interrupt: ({:?})", interrupt);
331+
///
332+
/// loop {}
333+
/// }
334+
/// ```
335+
#[inline]
336+
pub fn map_interrupt<INT>(&self, f: impl FnOnce(u16) -> INT) -> Vector<INT> {
337+
match self {
338+
Self::ThreadMode => Vector::ThreadMode,
339+
Self::Exception(ex) => Vector::Exception(*ex),
340+
Self::Interrupt(irqn) => Vector::Interrupt(f(*irqn)),
341+
}
342+
}
318343
}
319344

320345
impl TryFrom<u16> for Vector {
@@ -326,7 +351,7 @@ impl TryFrom<u16> for Vector {
326351
Ok(match isrn {
327352
0 => Self::ThreadMode,
328353
2..=15 => Self::Exception(Exception::try_from(isrn as i8 - 16).or(Err(isrn))?),
329-
16..=511 => Self::Interrupt { irqn: isrn - 16 },
354+
16..=511 => Self::Interrupt(isrn - 16),
330355
_ => return Err(isrn),
331356
})
332357
}

xtask/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ pub fn check_host_side() {
2424
{
2525
let v = Vector::try_from(22).unwrap();
2626
let json = serde_json::to_string(&v).expect("Failed to serialize Vector");
27-
let deser_v: Vector =
28-
serde_json::from_str(&json).expect("Failed to deserialize Vector");
27+
let deser_v: Vector = serde_json::from_str(&json).expect("Failed to deserialize Vector");
2928
assert_eq!(deser_v, v);
3029

3130
let lts = LocalTimestampOptions::EnabledDiv4;

0 commit comments

Comments
 (0)