aboutsummaryrefslogtreecommitdiff
path: root/embassy-nrf/src
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2021-05-11 01:20:04 +0200
committerGitHub <[email protected]>2021-05-11 01:20:04 +0200
commit65b78119dc61952dc7eb9dbea2b9d2fd4abf3b4d (patch)
treee01a595beed3bfd1cad4e256936990038b2cc51e /embassy-nrf/src
parentf817f374b6f787338aac17ee5f655a5f2a722a80 (diff)
parent7fa0e571724a15fd73de65d869cb1b755e12802a (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.rs30
-rw-r--r--embassy-nrf/src/rtc.rs17
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};
8use crate::pac::NVIC_PRIO_BITS; 8use crate::pac::NVIC_PRIO_BITS;
9 9
10// Re-exports 10// Re-exports
11pub use cortex_m::interrupt::{CriticalSection, Mutex};
12pub use embassy::interrupt::{declare, take, Interrupt}; 11pub 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]
51pub fn free<F, R>(f: F) -> R
52where
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")]
80mod irqs { 50mod 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 @@
1use core::cell::Cell; 1use core::cell::Cell;
2use core::sync::atomic::{compiler_fence, AtomicU32, Ordering}; 2use core::sync::atomic::{compiler_fence, AtomicU32, Ordering};
3 3use critical_section::CriticalSection;
4use embassy::interrupt::InterruptExt; 4use embassy::interrupt::InterruptExt;
5use embassy::time::Clock; 5use embassy::time::Clock;
6use embassy::util::CriticalSectionMutex as Mutex;
6 7
7use crate::interrupt::{CriticalSection, Interrupt, Mutex}; 8use crate::interrupt::Interrupt;
8use crate::pac; 9use crate::pac;
9use crate::{interrupt, peripherals}; 10use 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
290impl_instance!(RTC0, RTC0); 291impl_instance!(RTC0, RTC0);
291impl_instance!(RTC1, RTC1); 292impl_instance!(RTC1, RTC1);
292#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))] 293#[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))]
293impl_instance!(RTC2, RTC2); 294impl_instance!(RTC2, RTC2);