Use critical_section crate

This commit is contained in:
Dario Nieuwenhuis
2021-05-11 00:57:52 +02:00
parent f817f374b6
commit 7fa0e57172
17 changed files with 41 additions and 157 deletions

View File

@ -36,3 +36,4 @@ nrf52811-pac = { version = "0.9.1", optional = true, features = [ "rt" ]}
nrf52832-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
nrf52833-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
nrf52840-pac = { version = "0.9.0", optional = true, features = [ "rt" ]}
critical-section = "0.2.0"

View File

@ -8,7 +8,6 @@ use core::sync::atomic::{compiler_fence, Ordering};
use crate::pac::NVIC_PRIO_BITS;
// Re-exports
pub use cortex_m::interrupt::{CriticalSection, Mutex};
pub use embassy::interrupt::{declare, take, Interrupt};
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
@ -47,35 +46,6 @@ impl From<Priority> for u8 {
}
}
#[inline]
pub fn free<F, R>(f: F) -> R
where
F: FnOnce(&CriticalSection) -> R,
{
unsafe {
// TODO: assert that we're in privileged level
// Needed because disabling irqs in non-privileged level is a noop, which would break safety.
let primask: u32;
asm!("mrs {}, PRIMASK", out(reg) primask);
asm!("cpsid i");
// Prevent compiler from reordering operations inside/outside the critical section.
compiler_fence(Ordering::SeqCst);
let r = f(&CriticalSection::new());
compiler_fence(Ordering::SeqCst);
if primask & 1 == 0 {
asm!("cpsie i");
}
r
}
}
#[cfg(feature = "52810")]
mod irqs {
use super::*;

View File

@ -1,10 +1,11 @@
use core::cell::Cell;
use core::sync::atomic::{compiler_fence, AtomicU32, Ordering};
use critical_section::CriticalSection;
use embassy::interrupt::InterruptExt;
use embassy::time::Clock;
use embassy::util::CriticalSectionMutex as Mutex;
use crate::interrupt::{CriticalSection, Interrupt, Mutex};
use crate::interrupt::Interrupt;
use crate::pac;
use crate::{interrupt, peripherals};
@ -134,7 +135,7 @@ impl<T: Instance> RTC<T> {
for n in 0..ALARM_COUNT {
if r.events_compare[n].read().bits() == 1 {
r.events_compare[n].write(|w| w);
interrupt::free(|cs| {
critical_section::with(|cs| {
self.trigger_alarm(n, cs);
})
}
@ -142,7 +143,7 @@ impl<T: Instance> RTC<T> {
}
fn next_period(&self) {
interrupt::free(|cs| {
critical_section::with(|cs| {
let r = self.rtc.regs();
let period = self.period.fetch_add(1, Ordering::Relaxed) + 1;
let t = (period as u64) << 23;
@ -160,7 +161,7 @@ impl<T: Instance> RTC<T> {
})
}
fn trigger_alarm(&self, n: usize, cs: &CriticalSection) {
fn trigger_alarm(&self, n: usize, cs: CriticalSection) {
let r = self.rtc.regs();
r.intenclr.write(|w| unsafe { w.bits(compare_n(n)) });
@ -174,14 +175,14 @@ impl<T: Instance> RTC<T> {
}
fn set_alarm_callback(&self, n: usize, callback: fn(*mut ()), ctx: *mut ()) {
interrupt::free(|cs| {
critical_section::with(|cs| {
let alarm = &self.alarms.borrow(cs)[n];
alarm.callback.set(Some((callback, ctx)));
})
}
fn set_alarm(&self, n: usize, timestamp: u64) {
interrupt::free(|cs| {
critical_section::with(|cs| {
let alarm = &self.alarms.borrow(cs)[n];
alarm.timestamp.set(timestamp);
@ -289,5 +290,5 @@ pub trait Instance: sealed::Instance + 'static {
impl_instance!(RTC0, RTC0);
impl_instance!(RTC1, RTC1);
#[cfg(any(feature = "52832", feature = "52833", feature = "52840"))]
#[cfg(any(feature = "nrf52832", feature = "nrf52833", feature = "nrf52840"))]
impl_instance!(RTC2, RTC2);