diff options
author | 2017-03-04 20:46:19 -0500 | |
---|---|---|
committer | 2017-03-04 20:46:19 -0500 | |
commit | 251d1aa11244d5356659ccf969e29b0e7da82c7a (patch) | |
tree | c586f652b45b6981c3aa15ea870643763d7dbc35 /src/interrupt.rs | |
parent | b4f105cde28d89f3c8e42e4fe341390a7dc2dccf (diff) | |
download | cortex-m-251d1aa11244d5356659ccf969e29b0e7da82c7a.tar.gz cortex-m-251d1aa11244d5356659ccf969e29b0e7da82c7a.tar.zst cortex-m-251d1aa11244d5356659ccf969e29b0e7da82c7a.zip |
review safety of the existing API, make the register API type safe
Diffstat (limited to 'src/interrupt.rs')
-rw-r--r-- | src/interrupt.rs | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/interrupt.rs b/src/interrupt.rs index edc3dfb..2552892 100644 --- a/src/interrupt.rs +++ b/src/interrupt.rs @@ -37,12 +37,16 @@ unsafe impl<T> Sync for Mutex<T> {} /// Disable interrupts, globally #[inline(always)] -pub unsafe fn disable() { +pub fn disable() { match () { #[cfg(target_arch = "arm")] - () => { - asm!("cpsid i" :::: "volatile"); - } + () => unsafe { + asm!("cpsid i" + : + : + : + : "volatile"); + }, #[cfg(not(target_arch = "arm"))] () => {} } @@ -50,12 +54,16 @@ pub unsafe fn disable() { /// Enable interrupts, globally #[inline(always)] -pub unsafe fn enable() { +pub fn enable() { match () { #[cfg(target_arch = "arm")] - () => { - asm!("cpsie i" :::: "volatile"); - } + () => unsafe { + asm!("cpsie i" + : + : + : + : "volatile"); + }, #[cfg(not(target_arch = "arm"))] () => {} } @@ -70,20 +78,19 @@ pub struct CsToken { /// Execute closure `f` in an interrupt-free context. /// This as also known as a "critical section". -pub unsafe fn free<F, R>(f: F) -> R +pub fn free<F, R>(f: F) -> R where F: FnOnce(&CsToken) -> R { let primask = ::register::primask::read(); + // disable interrupts disable(); let r = f(&CsToken { _private: () }); - // If the interrupts were enabled before our `disable` call, then re-enable + // If the interrupts were active before our `disable` call, then re-enable // them. Otherwise, keep them disabled - // PRIMASK & 1 = 1 indicates that the interrupts were disabled - // PRIMASK & 1 = 0 indicates that they were enabled - if primask & 1 == 0 { + if primask.is_active() { enable(); } |