Skip to content

Commit cb6a61a

Browse files
hug-devadamgreig
authored andcommitted
Add a function to get SHCSR enable bit positions
This removes the duplication of the look-up table and enforces some safety checks with the match statement. Signed-off-by: Hugues de Valon <[email protected]>
1 parent b607b35 commit cb6a61a

File tree

1 file changed

+28
-47
lines changed

1 file changed

+28
-47
lines changed

src/peripheral/scb.rs

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
//! System Control Block
22
33
use core::ptr;
4-
#[cfg(not(any(armv6m, armv8m_base)))]
5-
use crate::interrupt;
64

75
use volatile_register::RW;
86

@@ -852,6 +850,20 @@ impl SCB {
852850
}
853851
}
854852

853+
/// Return the bit position of the exception enable bit in the SHCSR register
854+
#[inline]
855+
#[cfg(not(any(armv6m, armv8m_base)))]
856+
fn shcsr_enable_shift(exception: Exception) -> Option<u32> {
857+
match exception {
858+
Exception::MemoryManagement => Some(16),
859+
Exception::BusFault => Some(17),
860+
Exception::UsageFault => Some(18),
861+
#[cfg(armv8m_main)]
862+
Exception::SecureFault => Some(19),
863+
_ => None,
864+
}
865+
}
866+
855867
/// Enable the exception
856868
///
857869
/// If the exception is enabled, when the exception is triggered, the exception handler will be executed instead of the
@@ -866,24 +878,11 @@ impl SCB {
866878
#[inline]
867879
#[cfg(not(any(armv6m, armv8m_base)))]
868880
pub fn enable(&mut self, exception: Exception) {
869-
if self.is_enabled(exception) {
870-
return;
871-
}
872-
873-
// Make sure that the read-modify-write sequence happens during a critical section to avoid
874-
// modifying pending and active interrupts.
875-
interrupt::free(|_| {
876-
let shift = match exception {
877-
Exception::MemoryManagement => 16,
878-
Exception::BusFault => 17,
879-
Exception::UsageFault => 18,
880-
#[cfg(armv8m_main)]
881-
Exception::SecureFault => 19,
882-
_ => return,
883-
};
884-
881+
if let Some(shift) = SCB::shcsr_enable_shift(exception) {
882+
// The mutable reference to SCB makes sure that only this code is currently modifying
883+
// the register.
885884
unsafe { self.shcsr.modify(|value| value | (1 << shift)) }
886-
})
885+
}
887886
}
888887

889888
/// Disable the exception
@@ -900,24 +899,11 @@ impl SCB {
900899
#[inline]
901900
#[cfg(not(any(armv6m, armv8m_base)))]
902901
pub fn disable(&mut self, exception: Exception) {
903-
if !self.is_enabled(exception) {
904-
return;
905-
}
906-
907-
// Make sure that the read-modify-write sequence happens during a critical section to avoid
908-
// modifying pending and active interrupts.
909-
interrupt::free(|_| {
910-
let shift = match exception {
911-
Exception::MemoryManagement => 16,
912-
Exception::BusFault => 17,
913-
Exception::UsageFault => 18,
914-
#[cfg(armv8m_main)]
915-
Exception::SecureFault => 19,
916-
_ => return,
917-
};
918-
902+
if let Some(shift) = SCB::shcsr_enable_shift(exception) {
903+
// The mutable reference to SCB makes sure that only this code is currently modifying
904+
// the register.
919905
unsafe { self.shcsr.modify(|value| value & !(1 << shift)) }
920-
})
906+
}
921907
}
922908

923909
/// Check if an exception is enabled
@@ -931,16 +917,11 @@ impl SCB {
931917
/// Calling this function with any other exception will read `false`.
932918
#[inline]
933919
#[cfg(not(any(armv6m, armv8m_base)))]
934-
pub fn is_enabled(&mut self, exception: Exception) -> bool {
935-
let shift = match exception {
936-
Exception::MemoryManagement => 16,
937-
Exception::BusFault => 17,
938-
Exception::UsageFault => 18,
939-
#[cfg(armv8m_main)]
940-
Exception::SecureFault => 19,
941-
_ => return false,
942-
};
943-
944-
(self.shcsr.read() & (1 << shift)) > 0
920+
pub fn is_enabled(&self, exception: Exception) -> bool {
921+
if let Some(shift) = SCB::shcsr_enable_shift(exception) {
922+
(self.shcsr.read() & (1 << shift)) > 0
923+
} else {
924+
false
925+
}
945926
}
946927
}

0 commit comments

Comments
 (0)