Skip to content

Commit 9ccb935

Browse files
committed
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 809876e commit 9ccb935

File tree

1 file changed

+29
-38
lines changed

1 file changed

+29
-38
lines changed

src/peripheral/scb.rs

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,20 @@ impl SCB {
10141014
}
10151015
}
10161016

1017+
/// Return the bit position of the exception enable bit in the SHCSR register
1018+
#[inline]
1019+
#[cfg(not(any(armv6m, armv8m_base)))]
1020+
fn shcsr_enable_shift(exception: Exception) -> Option<u32> {
1021+
match exception {
1022+
Exception::MemoryManagement => Some(16),
1023+
Exception::BusFault => Some(17),
1024+
Exception::UsageFault => Some(18),
1025+
#[cfg(armv8m_main)]
1026+
Exception::SecureFault => Some(19),
1027+
_ => None,
1028+
}
1029+
}
1030+
10171031
/// Enable the exception
10181032
///
10191033
/// If the exception is enabled, when the exception is triggered, the exception handler will be executed instead of the
@@ -1032,20 +1046,11 @@ impl SCB {
10321046
return;
10331047
}
10341048

1035-
// Make sure that the read-modify-write sequence happens during a critical section to avoid
1036-
// modifying pending and active interrupts.
1037-
interrupt::free(|_| {
1038-
let shift = match exception {
1039-
Exception::MemoryManagement => 16,
1040-
Exception::BusFault => 17,
1041-
Exception::UsageFault => 18,
1042-
#[cfg(armv8m_main)]
1043-
Exception::SecureFault => 19,
1044-
_ => return,
1045-
};
1046-
1047-
unsafe { self.shcsr.modify(|value| value | (1 << shift)) }
1048-
})
1049+
if let Some(shift) = SCB::shcsr_enable_shift(exception) {
1050+
// Make sure that the read-modify-write sequence happens during a critical section to
1051+
// avoid modifying pending and active interrupts.
1052+
interrupt::free(|_| unsafe { self.shcsr.modify(|value| value | (1 << shift)) })
1053+
}
10491054
}
10501055

10511056
/// Disable the exception
@@ -1066,20 +1071,11 @@ impl SCB {
10661071
return;
10671072
}
10681073

1069-
// Make sure that the read-modify-write sequence happens during a critical section to avoid
1070-
// modifying pending and active interrupts.
1071-
interrupt::free(|_| {
1072-
let shift = match exception {
1073-
Exception::MemoryManagement => 16,
1074-
Exception::BusFault => 17,
1075-
Exception::UsageFault => 18,
1076-
#[cfg(armv8m_main)]
1077-
Exception::SecureFault => 19,
1078-
_ => return,
1079-
};
1080-
1081-
unsafe { self.shcsr.modify(|value| value & !(1 << shift)) }
1082-
})
1074+
if let Some(shift) = SCB::shcsr_enable_shift(exception) {
1075+
// Make sure that the read-modify-write sequence happens during a critical section to
1076+
// avoid modifying pending and active interrupts.
1077+
interrupt::free(|_| unsafe { self.shcsr.modify(|value| value & !(1 << shift)) })
1078+
}
10831079
}
10841080

10851081
/// Check if an exception is enabled
@@ -1094,15 +1090,10 @@ impl SCB {
10941090
#[inline]
10951091
#[cfg(not(any(armv6m, armv8m_base)))]
10961092
pub fn is_enabled(&mut self, exception: Exception) -> bool {
1097-
let shift = match exception {
1098-
Exception::MemoryManagement => 16,
1099-
Exception::BusFault => 17,
1100-
Exception::UsageFault => 18,
1101-
#[cfg(armv8m_main)]
1102-
Exception::SecureFault => 19,
1103-
_ => return false,
1104-
};
1105-
1106-
(self.shcsr.read() & (1 << shift)) > 0
1093+
if let Some(shift) = SCB::shcsr_enable_shift(exception) {
1094+
(self.shcsr.read() & (1 << shift)) > 0
1095+
} else {
1096+
false
1097+
}
11071098
}
11081099
}

0 commit comments

Comments
 (0)