1
1
//! System Control Block
2
2
3
3
use core:: ptr;
4
- #[ cfg( not( any( armv6m, armv8m_base) ) ) ]
5
- use crate :: interrupt;
6
4
7
5
use volatile_register:: RW ;
8
6
@@ -852,6 +850,20 @@ impl SCB {
852
850
}
853
851
}
854
852
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
+
855
867
/// Enable the exception
856
868
///
857
869
/// 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 {
866
878
#[ inline]
867
879
#[ cfg( not( any( armv6m, armv8m_base) ) ) ]
868
880
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.
885
884
unsafe { self . shcsr . modify ( |value| value | ( 1 << shift) ) }
886
- } )
885
+ }
887
886
}
888
887
889
888
/// Disable the exception
@@ -900,24 +899,11 @@ impl SCB {
900
899
#[ inline]
901
900
#[ cfg( not( any( armv6m, armv8m_base) ) ) ]
902
901
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.
919
905
unsafe { self . shcsr . modify ( |value| value & !( 1 << shift) ) }
920
- } )
906
+ }
921
907
}
922
908
923
909
/// Check if an exception is enabled
@@ -931,16 +917,11 @@ impl SCB {
931
917
/// Calling this function with any other exception will read `false`.
932
918
#[ inline]
933
919
#[ 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
+ }
945
926
}
946
927
}
0 commit comments