Skip to content

Commit fd04623

Browse files
committed
Add SCB::vect_pending function.
1 parent 164647b commit fd04623

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/peripheral/scb.rs

+23-3
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,33 @@ impl SCB {
168168
}
169169

170170
impl SCB {
171+
#[inline]
172+
fn icsr() -> u32 {
173+
unsafe { ptr::read_volatile(&(*Self::PTR).icsr as *const _ as *const u32) }
174+
}
175+
176+
/// Return the `Vector` containing the pending exception number, if any.
177+
#[inline]
178+
pub fn vect_pending() -> Option<Vector> {
179+
let icsr = Self::icsr();
180+
let isrn = ((icsr >> 12) & 0x7F) as u16;
181+
182+
match isrn {
183+
// No pending exceptions.
184+
0 => return None,
185+
// SAFETY: `isrn` is in range [1, 127] and contains
186+
// a valid `Exception` if in range [2, 15].
187+
isrn => unsafe { Some(Vector::new_unchecked(isrn)) },
188+
}
189+
}
190+
171191
/// Returns the `Vector` containing the active exception number.
172192
#[inline]
173193
pub fn vect_active() -> Vector {
174-
let icsr = unsafe { ptr::read_volatile(&(*SCB::PTR).icsr as *const _ as *const u32) };
194+
let icsr = Self::icsr();
175195
let isrn = (icsr & 0x1FF) as u16;
176196

177-
// NOTE(unsafe): `isrn` is in range [0, 511] and contains
197+
// SAFETY: `isrn` is in range [0, 511] and contains
178198
// a valid `Exception` if in range [2, 15].
179199
unsafe { Vector::new_unchecked(isrn) }
180200
}
@@ -283,7 +303,7 @@ impl TryFrom<i8> for Exception {
283303
}
284304
}
285305

286-
/// Exception/Interrupt Vector
306+
/// Active exception number
287307
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
288308
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
289309
#[cfg_attr(feature = "std", derive(PartialOrd, Hash))]

0 commit comments

Comments
 (0)