diff options
author | 2022-08-12 00:34:12 +0000 | |
---|---|---|
committer | 2022-08-12 00:34:12 +0000 | |
commit | 0e530549de322684c50e858c6bb985afb5479dbe (patch) | |
tree | 21efc15cdadf74ac03a2e210f15eff1542f33e82 /src/interrupt.rs | |
parent | e46e2310adc86a5a09a1858a23ecdde2a2c6963f (diff) | |
parent | 3a15a6b4b320fa328e8ab99c31f81536960dd280 (diff) | |
download | cortex-m-0e530549de322684c50e858c6bb985afb5479dbe.tar.gz cortex-m-0e530549de322684c50e858c6bb985afb5479dbe.tar.zst cortex-m-0e530549de322684c50e858c6bb985afb5479dbe.zip |
Merge #447
447: Add implementation for critical-section 1.0 r=adamgreig a=Dirbaio
Picking up #433 since it seems stalled. Changes from #433 are:
- Update to `critical-section 1.0.0-alpha.2`
- Use `bool` restore token
- Name Cargo feature `critical-section-single-core`.
TODO before merging:
- [x] Wait for `critical-section 1.0` release https://github.com/rust-embedded/critical-section/pull/19
Co-Authored-By: Markus Reiter `@reitermarkus`
Co-authored-by: Dario Nieuwenhuis <dirbaio@dirbaio.net>
Diffstat (limited to 'src/interrupt.rs')
-rw-r--r-- | src/interrupt.rs | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/src/interrupt.rs b/src/interrupt.rs index 72450c4..f6ce990 100644 --- a/src/interrupt.rs +++ b/src/interrupt.rs @@ -1,6 +1,5 @@ //! Interrupts -pub use bare_metal::{CriticalSection, Mutex}; #[cfg(cortex_m)] use core::arch::asm; #[cfg(cortex_m)] @@ -27,7 +26,7 @@ pub unsafe trait InterruptNumber: Copy { fn number(self) -> u16; } -/// Disables all interrupts +/// Disables all interrupts in the current core. #[cfg(cortex_m)] #[inline] pub fn disable() { @@ -39,11 +38,11 @@ pub fn disable() { compiler_fence(Ordering::SeqCst); } -/// Enables all the interrupts +/// Enables all the interrupts in the current core. /// /// # Safety /// -/// - Do not call this function inside an `interrupt::free` critical section +/// - Do not call this function inside a critical section. #[cfg(cortex_m)] #[inline] pub unsafe fn enable() { @@ -53,21 +52,26 @@ pub unsafe fn enable() { asm!("cpsie i", options(nomem, nostack, preserves_flags)); } -/// Execute closure `f` in an interrupt-free context. +/// Execute closure `f` with interrupts disabled in the current core. /// -/// This as also known as a "critical section". +/// This method does not synchronise multiple cores and may disable required +/// interrupts on some platforms; see the `critical-section` crate for a cross-platform +/// way to enter a critical section which provides a `CriticalSection` token. +/// +/// This crate provides an implementation for `critical-section` suitable for single-core systems, +/// based on disabling all interrupts. It can be enabled with the `critical-section-single-core` feature. #[cfg(cortex_m)] #[inline] pub fn free<F, R>(f: F) -> R where - F: FnOnce(&CriticalSection) -> R, + F: FnOnce() -> R, { let primask = crate::register::primask::read(); // disable interrupts disable(); - let r = f(unsafe { &CriticalSection::new() }); + let r = f(); // If the interrupts were active before our `disable` call, then re-enable // them. Otherwise, keep them disabled @@ -85,7 +89,7 @@ where #[inline] pub fn free<F, R>(_: F) -> R where - F: FnOnce(&CriticalSection) -> R, + F: FnOnce() -> R, { panic!("cortex_m::interrupt::free() is only functional on cortex-m platforms"); } |