diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-05-11 01:20:04 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-05-11 01:20:04 +0200 |
| commit | 65b78119dc61952dc7eb9dbea2b9d2fd4abf3b4d (patch) | |
| tree | e01a595beed3bfd1cad4e256936990038b2cc51e /embassy-nrf/src | |
| parent | f817f374b6f787338aac17ee5f655a5f2a722a80 (diff) | |
| parent | 7fa0e571724a15fd73de65d869cb1b755e12802a (diff) | |
Merge pull request #172 from embassy-rs/critical-section
Use `critical_section` crate
Diffstat (limited to 'embassy-nrf/src')
| -rw-r--r-- | embassy-nrf/src/interrupt.rs | 30 | ||||
| -rw-r--r-- | embassy-nrf/src/rtc.rs | 17 |
2 files changed, 9 insertions, 38 deletions
diff --git a/embassy-nrf/src/interrupt.rs b/embassy-nrf/src/interrupt.rs index 741dbaee2..8d069e329 100644 --- a/embassy-nrf/src/interrupt.rs +++ b/embassy-nrf/src/interrupt.rs | |||
| @@ -8,7 +8,6 @@ use core::sync::atomic::{compiler_fence, Ordering}; | |||
| 8 | use crate::pac::NVIC_PRIO_BITS; | 8 | use crate::pac::NVIC_PRIO_BITS; |
| 9 | 9 | ||
| 10 | // Re-exports | 10 | // Re-exports |
| 11 | pub use cortex_m::interrupt::{CriticalSection, Mutex}; | ||
| 12 | pub use embassy::interrupt::{declare, take, Interrupt}; | 11 | pub use embassy::interrupt::{declare, take, Interrupt}; |
| 13 | 12 | ||
| 14 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] | 13 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)] |
| @@ -47,35 +46,6 @@ impl From<Priority> for u8 { | |||
| 47 | } | 46 | } |
| 48 | } | 47 | } |
| 49 | 48 | ||
| 50 | #[inline] | ||
| 51 | pub fn free<F, R>(f: F) -> R | ||
| 52 | where | ||
| 53 | F: FnOnce(&CriticalSection) -> R, | ||
| 54 | { | ||
| 55 | unsafe { | ||
| 56 | // TODO: assert that we're in privileged level | ||
| 57 | // Needed because disabling irqs in non-privileged level is a noop, which would break safety. | ||
| 58 | |||
| 59 | let primask: u32; | ||
| 60 | asm!("mrs {}, PRIMASK", out(reg) primask); | ||
| 61 | |||
| 62 | asm!("cpsid i"); | ||
| 63 | |||
| 64 | // Prevent compiler from reordering operations inside/outside the critical section. | ||
| 65 | compiler_fence(Ordering::SeqCst); | ||
| 66 | |||
| 67 | let r = f(&CriticalSection::new()); | ||
| 68 | |||
| 69 | compiler_fence(Ordering::SeqCst); | ||
| 70 | |||
| 71 | if primask & 1 == 0 { | ||
| 72 | asm!("cpsie i"); | ||
| 73 | } | ||
| 74 | |||
| 75 | r | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | #[cfg(feature = "52810")] | 49 | #[cfg(feature = "52810")] |
| 80 | mod irqs { | 50 | mod irqs { |
| 81 | use super::*; | 51 | use super::*; |
diff --git a/embassy-nrf/src/rtc.rs b/embassy-nrf/src/rtc.rs index cbb5a87ef..dc0e3ceb6 100644 --- a/embassy-nrf/src/rtc.rs +++ b/embassy-nrf/src/rtc.rs | |||
| @@ -1,10 +1,11 @@ | |||
| 1 | use core::cell::Cell; | 1 | use core::cell::Cell; |
| 2 | use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; | 2 | use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; |
| 3 | 3 | use critical_section::CriticalSection; | |
| 4 | use embassy::interrupt::InterruptExt; | 4 | use embassy::interrupt::InterruptExt; |
| 5 | use embassy::time::Clock; | 5 | use embassy::time::Clock; |
| 6 | use embassy::util::CriticalSectionMutex as Mutex; | ||
| 6 | 7 | ||
| 7 | use crate::interrupt::{CriticalSection, Interrupt, Mutex}; | 8 | use crate::interrupt::Interrupt; |
| 8 | use crate::pac; | 9 | use crate::pac; |
| 9 | use crate::{interrupt, peripherals}; | 10 | use crate::{interrupt, peripherals}; |
| 10 | 11 | ||
| @@ -134,7 +135,7 @@ impl<T: Instance> RTC<T> { | |||
| 134 | for n in 0..ALARM_COUNT { | 135 | for n in 0..ALARM_COUNT { |
| 135 | if r.events_compare[n].read().bits() == 1 { | 136 | if r.events_compare[n].read().bits() == 1 { |
| 136 | r.events_compare[n].write(|w| w); | 137 | r.events_compare[n].write(|w| w); |
| 137 | interrupt::free(|cs| { | 138 | critical_section::with(|cs| { |
| 138 | self.trigger_alarm(n, cs); | 139 | self.trigger_alarm(n, cs); |
| 139 | }) | 140 | }) |
| 140 | } | 141 | } |
| @@ -142,7 +143,7 @@ impl<T: Instance> RTC<T> { | |||
| 142 | } | 143 | } |
| 143 | 144 | ||
| 144 | fn next_period(&self) { | 145 | fn next_period(&self) { |
| 145 | interrupt::free(|cs| { | 146 | critical_section::with(|cs| { |
| 146 | let r = self.rtc.regs(); | 147 | let r = self.rtc.regs(); |
| 147 | let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; | 148 | let period = self.period.fetch_add(1, Ordering::Relaxed) + 1; |
| 148 | let t = (period as u64) << 23; | 149 | let t = (period as u64) << 23; |
| @@ -160,7 +161,7 @@ impl<T: Instance> RTC<T> { | |||
| 160 | }) | 161 | }) |
| 161 | } | 162 | } |
| 162 | 163 | ||
| 163 | fn trigger_alarm(&self, n: usize, cs: &CriticalSection) { | 164 | fn trigger_alarm(&self, n: usize, cs: CriticalSection) { |
| 164 | let r = self.rtc.regs(); | 165 | let r = self.rtc.regs(); |
| 165 | r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); | 166 | r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) }); |
| 166 | 167 | ||
| @@ -174,14 +175,14 @@ impl<T: Instance> RTC<T> { | |||
| 174 | } | 175 | } |
| 175 | 176 | ||
| 176 | fn set_alarm_callback(&self, n: usize, callback: fn(*mut ()), ctx: *mut ()) { | 177 | fn set_alarm_callback(&self, n: usize, callback: fn(*mut ()), ctx: *mut ()) { |
| 177 | interrupt::free(|cs| { | 178 | critical_section::with(|cs| { |
| 178 | let alarm = &self.alarms.borrow(cs)[n]; | 179 | let alarm = &self.alarms.borrow(cs)[n]; |
| 179 | alarm.callback.set(Some((callback, ctx))); | 180 | alarm.callback.set(Some((callback, ctx))); |
| 180 | }) | 181 | }) |
| 181 | } | 182 | } |
| 182 | 183 | ||
| 183 | fn set_alarm(&self, n: usize, timestamp: u64) { | 184 | fn set_alarm(&self, n: usize, timestamp: u64) { |
| 184 | interrupt::free(|cs| { | 185 | critical_section::with(|cs| { |
| 185 | let alarm = &self.alarms.borrow(cs)[n]; | 186 | let alarm = &self.alarms.borrow(cs)[n]; |
| 186 | alarm.timestamp.set(timestamp); | 187 | alarm.timestamp.set(timestamp); |
| 187 | 188 | ||
| @@ -289,5 +290,5 @@ pub trait Instance: sealed::Instance + 'static { | |||
| 289 | 290 | ||
| 290 | impl_instance!(RTC0, RTC0); | 291 | impl_instance!(RTC0, RTC0); |
| 291 | impl_instance!(RTC1, RTC1); | 292 | impl_instance!(RTC1, RTC1); |
| 292 | #[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] | 293 | #[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))] |
| 293 | impl_instance!(RTC2, RTC2); | 294 | impl_instance!(RTC2, RTC2); |
