diff options
Diffstat (limited to 'src/export.rs')
| -rw-r--r-- | src/export.rs | 178 |
1 files changed, 59 insertions, 119 deletions
diff --git a/src/export.rs b/src/export.rs index cf7293b..27f7f5f 100644 --- a/src/export.rs +++ b/src/export.rs @@ -1,44 +1,71 @@ -//! IMPLEMENTATION DETAILS. DO NOT USE ANYTHING IN THIS MODULE - -#[cfg(not(feature = "nightly"))] -use core::ptr; -use core::{cell::Cell, u8}; +use core::{ + cell::Cell, + sync::atomic::{AtomicBool, Ordering}, +}; +pub use crate::tq::{NotReady, TimerQueue}; +pub use bare_metal::CriticalSection; #[cfg(armv7m)] -use cortex_m::register::basepri; +pub use cortex_m::register::basepri; pub use cortex_m::{ - asm::wfi, interrupt, peripheral::scb::SystemHandler, peripheral::syst::SystClkSource, - peripheral::Peripherals, + asm::wfi, + interrupt, + peripheral::{scb::SystemHandler, syst::SystClkSource, DWT, NVIC}, + Peripherals, }; -pub use heapless::consts; -use heapless::spsc::{Queue, SingleCore}; +use heapless::spsc::SingleCore; +pub use heapless::{consts, i::Queue as iQueue, spsc::Queue}; +pub use heapless::{i::BinaryHeap as iBinaryHeap, BinaryHeap}; -#[cfg(feature = "timer-queue")] -pub use crate::tq::{isr as sys_tick, NotReady, TimerQueue}; - -pub type FreeQueue<N> = Queue<u8, N, usize, SingleCore>; -pub type ReadyQueue<T, N> = Queue<(T, u8), N, usize, SingleCore>; +pub type SCFQ<N> = Queue<u8, N, u8, SingleCore>; +pub type SCRQ<T, N> = Queue<(T, u8), N, u8, SingleCore>; #[cfg(armv7m)] #[inline(always)] -pub fn run<F>(f: F) +pub fn run<F>(priority: u8, f: F) where F: FnOnce(), { - let initial = basepri::read(); - f(); - unsafe { basepri::write(initial) } + if priority == 1 { + // If the priority of this interrupt is `1` then BASEPRI can only be `0` + f(); + unsafe { basepri::write(0) } + } else { + let initial = basepri::read(); + f(); + unsafe { basepri::write(initial) } + } } #[cfg(not(armv7m))] #[inline(always)] -pub fn run<F>(f: F) +pub fn run<F>(_priority: u8, f: F) where F: FnOnce(), { f(); } +pub struct Barrier { + inner: AtomicBool, +} + +impl Barrier { + pub const fn new() -> Self { + Barrier { + inner: AtomicBool::new(false), + } + } + + pub fn release(&self) { + self.inner.store(true, Ordering::Release) + } + + pub fn wait(&self) { + while !self.inner.load(Ordering::Acquire) {} + } +} + // Newtype over `Cell` that forbids mutation through a shared reference pub struct Priority { inner: Cell<u8>, @@ -52,7 +79,7 @@ impl Priority { } } - // these two methods are used by claim (see below) but can't be used from the RTFM application + // These two methods are used by `lock` (see below) but can't be used from the RTIC application #[inline(always)] fn set(&self, value: u8) { self.inner.set(value) @@ -64,86 +91,6 @@ impl Priority { } } -#[cfg(feature = "nightly")] -pub struct MaybeUninit<T> { - // we newtype so the end-user doesn't need `#![feature(maybe_uninit)]` in their code - inner: core::mem::MaybeUninit<T>, -} - -#[cfg(feature = "nightly")] -impl<T> MaybeUninit<T> { - pub const fn uninit() -> Self { - MaybeUninit { - inner: core::mem::MaybeUninit::uninit(), - } - } - - pub fn as_ptr(&self) -> *const T { - self.inner.as_ptr() - } - - pub fn as_mut_ptr(&mut self) -> *mut T { - self.inner.as_mut_ptr() - } - - pub fn write(&mut self, value: T) -> &mut T { - self.inner.write(value) - } -} - -#[cfg(not(feature = "nightly"))] -pub struct MaybeUninit<T> { - value: Option<T>, -} - -#[cfg(not(feature = "nightly"))] -const MSG: &str = - "you have hit a bug (UB) in RTFM implementation; try enabling this crate 'nightly' feature"; - -#[cfg(not(feature = "nightly"))] -impl<T> MaybeUninit<T> { - pub const fn uninit() -> Self { - MaybeUninit { value: None } - } - - pub fn as_ptr(&self) -> *const T { - if let Some(x) = self.value.as_ref() { - x - } else { - unreachable!(MSG) - } - } - - pub fn as_mut_ptr(&mut self) -> *mut T { - if let Some(x) = self.value.as_mut() { - x - } else { - unreachable!(MSG) - } - } - - pub unsafe fn get_ref(&self) -> &T { - if let Some(x) = self.value.as_ref() { - x - } else { - unreachable!(MSG) - } - } - - pub unsafe fn get_mut(&mut self) -> &mut T { - if let Some(x) = self.value.as_mut() { - x - } else { - unreachable!(MSG) - } - } - - pub fn write(&mut self, val: T) { - // NOTE(volatile) we have observed UB when this uses a plain `ptr::write` - unsafe { ptr::write_volatile(&mut self.value, Some(val)) } - } -} - #[inline(always)] pub fn assert_send<T>() where @@ -160,21 +107,18 @@ where #[cfg(armv7m)] #[inline(always)] -pub unsafe fn claim<T, R, F>( +pub unsafe fn lock<T, R>( ptr: *mut T, priority: &Priority, ceiling: u8, nvic_prio_bits: u8, - f: F, -) -> R -where - F: FnOnce(&mut T) -> R, -{ + f: impl FnOnce(&mut T) -> R, +) -> R { let current = priority.get(); - if priority.get() < ceiling { + if current < ceiling { if ceiling == (1 << nvic_prio_bits) { - priority.set(u8::MAX); + priority.set(u8::max_value()); let r = interrupt::free(|_| f(&mut *ptr)); priority.set(current); r @@ -193,20 +137,17 @@ where #[cfg(not(armv7m))] #[inline(always)] -pub unsafe fn claim<T, R, F>( +pub unsafe fn lock<T, R>( ptr: *mut T, priority: &Priority, ceiling: u8, _nvic_prio_bits: u8, - f: F, -) -> R -where - F: FnOnce(&mut T) -> R, -{ + f: impl FnOnce(&mut T) -> R, +) -> R { let current = priority.get(); - if priority.get() < ceiling { - priority.set(u8::MAX); + if current < ceiling { + priority.set(u8::max_value()); let r = interrupt::free(|_| f(&mut *ptr)); priority.set(current); r @@ -215,8 +156,7 @@ where } } -#[cfg(armv7m)] #[inline] -fn logical2hw(logical: u8, nvic_prio_bits: u8) -> u8 { +pub fn logical2hw(logical: u8, nvic_prio_bits: u8) -> u8 { ((1 << nvic_prio_bits) - logical) << (8 - nvic_prio_bits) } |
