diff options
Diffstat (limited to 'src/interrupt.rs')
-rw-r--r-- | src/interrupt.rs | 19 |
1 files changed, 17 insertions, 2 deletions
diff --git a/src/interrupt.rs b/src/interrupt.rs index 68719ec..06b91f1 100644 --- a/src/interrupt.rs +++ b/src/interrupt.rs @@ -1,6 +1,10 @@ //! Interrupts pub use bare_metal::{CriticalSection, Mutex}; +#[cfg(cortex_m)] +use core::arch::asm; +#[cfg(cortex_m)] +use core::sync::atomic::{compiler_fence, Ordering}; /// Trait for enums of external interrupt numbers. /// @@ -24,9 +28,15 @@ pub unsafe trait InterruptNumber: Copy { } /// Disables all interrupts +#[cfg(cortex_m)] #[inline] pub fn disable() { - call_asm!(__cpsid()); + unsafe { + asm!("cpsid i", options(nomem, nostack, preserves_flags)); + } + + // Ensure no subsequent memory accesses are reordered to before interrupts are disabled. + compiler_fence(Ordering::SeqCst); } /// Enables all the interrupts @@ -34,14 +44,19 @@ pub fn disable() { /// # Safety /// /// - Do not call this function inside an `interrupt::free` critical section +#[cfg(cortex_m)] #[inline] pub unsafe fn enable() { - call_asm!(__cpsie()); + // Ensure no preceeding memory accesses are reordered to after interrupts are enabled. + compiler_fence(Ordering::SeqCst); + + asm!("cpsie i", options(nomem, nostack, preserves_flags)); } /// Execute closure `f` in an interrupt-free context. /// /// This as also known as a "critical section". +#[cfg(cortex_m)] #[inline] pub fn free<F, R>(f: F) -> R where |