rtic/src/export.rs

223 lines
4.7 KiB
Rust
Raw Normal View History

//! IMPLEMENTATION DETAILS. DO NOT USE ANYTHING IN THIS MODULE
2019-02-15 19:52:25 +01:00
#[cfg(not(feature = "nightly"))]
use core::ptr;
use core::{cell::Cell, u8};
2018-11-03 17:02:41 +01:00
#[cfg(armv7m)]
use cortex_m::register::basepri;
pub use cortex_m::{
asm::wfi, interrupt, peripheral::scb::SystemHandler, peripheral::syst::SystClkSource,
peripheral::Peripherals,
};
pub use heapless::consts;
use heapless::spsc::{Queue, SingleCore};
2018-11-03 17:02:41 +01:00
#[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>;
2018-11-03 17:02:41 +01:00
#[cfg(armv7m)]
#[inline(always)]
pub fn run<F>(f: F)
where
F: FnOnce(),
{
let initial = basepri::read();
f();
unsafe { basepri::write(initial) }
}
#[cfg(not(armv7m))]
#[inline(always)]
pub fn run<F>(f: F)
where
F: FnOnce(),
{
f();
}
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 {
#[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
}
// these two methods are used by claim (see below) but can't be used from the RTFM application
#[inline(always)]
fn set(&self, value: u8) {
self.inner.set(value)
}
#[inline(always)]
fn get(&self) -> u8 {
self.inner.get()
}
}
2019-02-15 19:52:25 +01:00
#[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> {
2019-04-16 23:41:00 +02:00
pub const fn uninit() -> Self {
2019-02-15 19:52:25 +01:00
MaybeUninit {
2019-04-16 23:41:00 +02:00
inner: core::mem::MaybeUninit::uninit(),
2019-02-15 19:52:25 +01:00
}
}
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()
}
2019-04-16 23:17:28 +02:00
pub fn write(&mut self, value: T) -> &mut T {
self.inner.write(value)
2019-02-15 19:52:25 +01:00
}
}
#[cfg(not(feature = "nightly"))]
2018-11-03 17:02:41 +01:00
pub struct MaybeUninit<T> {
value: Option<T>,
}
2019-02-15 20:23:32 +01:00
#[cfg(not(feature = "nightly"))]
const MSG: &str =
"you have hit a bug (UB) in RTFM implementation; try enabling this crate 'nightly' feature";
2019-02-15 19:52:25 +01:00
#[cfg(not(feature = "nightly"))]
2018-11-03 17:02:41 +01:00
impl<T> MaybeUninit<T> {
2019-04-16 23:41:00 +02:00
pub const fn uninit() -> Self {
2018-11-03 17:02:41 +01:00
MaybeUninit { value: None }
}
2019-02-15 19:52:25 +01:00
pub fn as_ptr(&self) -> *const T {
if let Some(x) = self.value.as_ref() {
x
} else {
2019-02-15 20:23:32 +01:00
unreachable!(MSG)
2019-02-15 19:52:25 +01:00
}
}
pub fn as_mut_ptr(&mut self) -> *mut T {
if let Some(x) = self.value.as_mut() {
x
} else {
2019-02-15 20:23:32 +01:00
unreachable!(MSG)
2019-02-15 19:52:25 +01:00
}
}
2018-11-03 17:02:41 +01:00
pub unsafe fn get_ref(&self) -> &T {
if let Some(x) = self.value.as_ref() {
x
} else {
2019-02-15 20:23:32 +01:00
unreachable!(MSG)
2018-11-03 17:02:41 +01:00
}
}
pub unsafe fn get_mut(&mut self) -> &mut T {
if let Some(x) = self.value.as_mut() {
x
} else {
2019-02-15 20:23:32 +01:00
unreachable!(MSG)
2018-11-03 17:02:41 +01:00
}
}
2019-04-16 23:17:28 +02:00
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)) }
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,
{
}
#[cfg(armv7m)]
#[inline(always)]
pub unsafe fn claim<T, R, F>(
ptr: *mut T,
2019-02-16 00:22:00 +01:00
priority: &Priority,
ceiling: u8,
nvic_prio_bits: u8,
f: F,
) -> R
where
F: FnOnce(&mut T) -> R,
{
let current = priority.get();
if priority.get() < ceiling {
if ceiling == (1 << nvic_prio_bits) {
priority.set(u8::MAX);
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)
}
}
#[cfg(not(armv7m))]
#[inline(always)]
pub unsafe fn claim<T, R, F>(
ptr: *mut T,
2019-02-16 00:22:00 +01:00
priority: &Priority,
ceiling: u8,
_nvic_prio_bits: u8,
f: F,
) -> R
where
F: FnOnce(&mut T) -> R,
{
let current = priority.get();
if priority.get() < ceiling {
priority.set(u8::MAX);
let r = interrupt::free(|_| f(&mut *ptr));
priority.set(current);
r
} else {
f(&mut *ptr)
}
}
#[cfg(armv7m)]
#[inline]
fn logical2hw(logical: u8, nvic_prio_bits: u8) -> u8 {
((1 << nvic_prio_bits) - logical) << (8 - nvic_prio_bits)
}