use crate::{common::unsafe_peripheral, plic::PriorityNumber};
unsafe_peripheral!(THRESHOLD, u32, RW);
impl THRESHOLD {
#[inline]
pub fn get_threshold<P: PriorityNumber>(self) -> P {
P::from_number(self.register.read() as _).unwrap()
}
#[inline]
pub unsafe fn set_threshold<P: PriorityNumber>(self, threshold: P) {
self.register.write(threshold.number() as _)
}
#[inline]
pub fn reset(self) {
self.register.write(0)
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::test::Priority;
#[test]
fn test_threshold() {
let mut raw_reg = 0u32;
let threshold = unsafe { THRESHOLD::new(&mut raw_reg as *mut _ as _) };
for i in 0..=Priority::MAX_PRIORITY_NUMBER {
let priority = Priority::from_number(i).unwrap();
unsafe { threshold.set_threshold(priority) };
assert_eq!(threshold.get_threshold::<Priority>(), priority);
}
threshold.reset();
assert_eq!(threshold.get_threshold::<Priority>(), Priority::P0);
}
}