2019-06-13 23:56:59 +02:00
|
|
|
use core::{
|
|
|
|
cell::Cell,
|
|
|
|
sync::atomic::{AtomicBool, Ordering},
|
|
|
|
};
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2020-12-12 23:24:54 +01:00
|
|
|
pub use crate::tq::{NotReady, TimerQueue};
|
2020-10-01 20:01:25 +02:00
|
|
|
pub use bare_metal::CriticalSection;
|
2018-11-03 17:02:41 +01:00
|
|
|
#[cfg(armv7m)]
|
2019-06-13 23:56:59 +02:00
|
|
|
pub use cortex_m::register::basepri;
|
2018-11-03 17:02:41 +01:00
|
|
|
pub use cortex_m::{
|
2019-06-13 23:56:59 +02:00
|
|
|
asm::wfi,
|
|
|
|
interrupt,
|
2019-09-15 18:36:00 +02:00
|
|
|
peripheral::{scb::SystemHandler, syst::SystClkSource, DWT, NVIC},
|
2019-06-13 23:56:59 +02:00
|
|
|
Peripherals,
|
2018-11-03 17:02:41 +01:00
|
|
|
};
|
2021-04-03 19:30:34 +02:00
|
|
|
pub use heapless::spsc::Queue;
|
|
|
|
pub use heapless::BinaryHeap;
|
2021-02-18 19:30:59 +01:00
|
|
|
pub use rtic_monotonic as monotonic;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
2021-04-03 19:30:34 +02:00
|
|
|
pub type SCFQ<const N: usize> = Queue<u8, N>;
|
|
|
|
pub type SCRQ<T, const N: usize> = Queue<(T, u8), N>;
|
2018-11-03 17:02:41 +01:00
|
|
|
|
|
|
|
#[cfg(armv7m)]
|
|
|
|
#[inline(always)]
|
2019-04-21 20:02:59 +02:00
|
|
|
pub fn run<F>(priority: u8, f: F)
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
|
|
|
F: FnOnce(),
|
|
|
|
{
|
2019-04-21 20:02:59 +02:00
|
|
|
if priority == 1 {
|
2020-09-01 19:04:55 +02:00
|
|
|
// If the priority of this interrupt is `1` then BASEPRI can only be `0`
|
2019-04-21 20:02:59 +02:00
|
|
|
f();
|
|
|
|
unsafe { basepri::write(0) }
|
|
|
|
} else {
|
|
|
|
let initial = basepri::read();
|
|
|
|
f();
|
|
|
|
unsafe { basepri::write(initial) }
|
|
|
|
}
|
2018-11-03 17:02:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(armv7m))]
|
|
|
|
#[inline(always)]
|
2019-04-21 20:02:59 +02:00
|
|
|
pub fn run<F>(_priority: u8, f: F)
|
2018-11-03 17:02:41 +01:00
|
|
|
where
|
|
|
|
F: FnOnce(),
|
|
|
|
{
|
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:56:59 +02:00
|
|
|
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) {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-16 00:22:00 +01:00
|
|
|
// Newtype over `Cell` that forbids mutation through a shared reference
|
|
|
|
pub struct Priority {
|
|
|
|
inner: Cell<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Priority {
|
2020-10-13 16:16:33 +02:00
|
|
|
/// Create a new Priority
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Will overwrite the current Priority
|
2019-02-16 00:22:00 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub unsafe fn new(value: u8) -> Self {
|
2019-02-16 00:26:07 +01:00
|
|
|
Priority {
|
|
|
|
inner: Cell::new(value),
|
|
|
|
}
|
2019-02-16 00:22:00 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 16:16:33 +02:00
|
|
|
/// Change the current priority to `value`
|
2020-09-01 19:04:55 +02:00
|
|
|
// These two methods are used by `lock` (see below) but can't be used from the RTIC application
|
2019-02-16 00:22:00 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn set(&self, value: u8) {
|
|
|
|
self.inner.set(value)
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:16:33 +02:00
|
|
|
/// Get the current priority
|
2019-02-16 00:22:00 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn get(&self) -> u8 {
|
|
|
|
self.inner.get()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 17:02:41 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn assert_send<T>()
|
|
|
|
where
|
|
|
|
T: Send,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn assert_sync<T>()
|
|
|
|
where
|
|
|
|
T: Sync,
|
|
|
|
{
|
|
|
|
}
|
2018-11-04 18:50:42 +01:00
|
|
|
|
2020-12-10 20:33:13 +01:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn assert_monotonic<T>()
|
|
|
|
where
|
2021-02-18 19:30:59 +01:00
|
|
|
T: monotonic::Monotonic,
|
2020-12-10 20:33:13 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:16:33 +02:00
|
|
|
/// Lock the resource proxy by setting the BASEPRI
|
|
|
|
/// and running the closure with interrupt::free
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Writing to the BASEPRI
|
|
|
|
/// Dereferencing a raw pointer
|
2018-11-04 18:50:42 +01:00
|
|
|
#[cfg(armv7m)]
|
|
|
|
#[inline(always)]
|
2019-04-21 20:02:59 +02:00
|
|
|
pub unsafe fn lock<T, R>(
|
2018-11-04 18:50:42 +01:00
|
|
|
ptr: *mut T,
|
2019-02-16 00:22:00 +01:00
|
|
|
priority: &Priority,
|
2018-11-04 18:50:42 +01:00
|
|
|
ceiling: u8,
|
|
|
|
nvic_prio_bits: u8,
|
2019-04-21 20:02:59 +02:00
|
|
|
f: impl FnOnce(&mut T) -> R,
|
|
|
|
) -> R {
|
2018-11-04 18:50:42 +01:00
|
|
|
let current = priority.get();
|
|
|
|
|
2019-04-21 20:02:59 +02:00
|
|
|
if current < ceiling {
|
2018-11-04 18:50:42 +01:00
|
|
|
if ceiling == (1 << nvic_prio_bits) {
|
2019-06-13 23:56:59 +02:00
|
|
|
priority.set(u8::max_value());
|
2018-11-04 18:50:42 +01:00
|
|
|
let r = interrupt::free(|_| f(&mut *ptr));
|
|
|
|
priority.set(current);
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
priority.set(ceiling);
|
|
|
|
basepri::write(logical2hw(ceiling, nvic_prio_bits));
|
|
|
|
let r = f(&mut *ptr);
|
|
|
|
basepri::write(logical2hw(current, nvic_prio_bits));
|
|
|
|
priority.set(current);
|
|
|
|
r
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
f(&mut *ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 16:16:33 +02:00
|
|
|
/// Lock the resource proxy by setting the PRIMASK
|
|
|
|
/// and running the closure with interrupt::free
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
/// Writing to the PRIMASK
|
|
|
|
/// Dereferencing a raw pointer
|
2018-11-04 18:50:42 +01:00
|
|
|
#[cfg(not(armv7m))]
|
|
|
|
#[inline(always)]
|
2019-04-21 20:02:59 +02:00
|
|
|
pub unsafe fn lock<T, R>(
|
2018-11-04 18:50:42 +01:00
|
|
|
ptr: *mut T,
|
2019-02-16 00:22:00 +01:00
|
|
|
priority: &Priority,
|
2018-11-04 18:50:42 +01:00
|
|
|
ceiling: u8,
|
|
|
|
_nvic_prio_bits: u8,
|
2019-04-21 20:02:59 +02:00
|
|
|
f: impl FnOnce(&mut T) -> R,
|
|
|
|
) -> R {
|
2018-11-04 18:50:42 +01:00
|
|
|
let current = priority.get();
|
|
|
|
|
2019-04-21 20:02:59 +02:00
|
|
|
if current < ceiling {
|
2019-06-13 23:56:59 +02:00
|
|
|
priority.set(u8::max_value());
|
2018-11-04 18:50:42 +01:00
|
|
|
let r = interrupt::free(|_| f(&mut *ptr));
|
|
|
|
priority.set(current);
|
|
|
|
r
|
|
|
|
} else {
|
|
|
|
f(&mut *ptr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-04-21 20:02:59 +02:00
|
|
|
pub fn logical2hw(logical: u8, nvic_prio_bits: u8) -> u8 {
|
2018-11-04 18:50:42 +01:00
|
|
|
((1 << nvic_prio_bits) - logical) << (8 - nvic_prio_bits)
|
|
|
|
}
|