mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-23 20:22:51 +01:00
a452700628
This commit: - Implements RFC 147: "all functions must be safe" - Implements RFC 155: "explicit Context parameter" - Implements the pending breaking change #141: reject assign syntax in `init` (which was used to initialize late resources) - Refactors code generation to make it more readable -- there are no more random identifiers in the output -- and align it with the book description of RTFM internals. - Makes the framework hard depend on `core::mem::MaybeUninit` and thus will require nightly until that API is stabilized. - Fixes a ceiling analysis bug where the priority of the system timer was not considered in the analysis. - Shrinks the size of all the internal queues by turning `AtomicUsize` indices into `AtomicU8`s. - Removes the integration with `owned_singleton`.
169 lines
3.6 KiB
Rust
169 lines
3.6 KiB
Rust
//! IMPLEMENTATION DETAILS. DO NOT USE ANYTHING IN THIS MODULE
|
|
|
|
use core::{cell::Cell, u8};
|
|
|
|
#[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};
|
|
|
|
#[cfg(feature = "timer-queue")]
|
|
pub use crate::tq::{NotReady, TimerQueue};
|
|
|
|
pub type FreeQueue<N> = Queue<u8, N, u8, SingleCore>;
|
|
pub type ReadyQueue<T, N> = Queue<(T, u8), N, u8, SingleCore>;
|
|
|
|
#[cfg(armv7m)]
|
|
#[inline(always)]
|
|
pub fn run<F>(priority: u8, f: F)
|
|
where
|
|
F: FnOnce(),
|
|
{
|
|
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>(_priority: u8, f: F)
|
|
where
|
|
F: FnOnce(),
|
|
{
|
|
f();
|
|
}
|
|
|
|
// 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 {
|
|
Priority {
|
|
inner: Cell::new(value),
|
|
}
|
|
}
|
|
|
|
// these two methods are used by `lock` (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()
|
|
}
|
|
}
|
|
|
|
// We newtype `core::mem::MaybeUninit` so the end-user doesn't need `#![feature(maybe_uninit)]` in
|
|
// their code
|
|
pub struct MaybeUninit<T> {
|
|
inner: core::mem::MaybeUninit<T>,
|
|
}
|
|
|
|
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 unsafe fn read(&self) -> T {
|
|
self.inner.read()
|
|
}
|
|
|
|
pub fn write(&mut self, value: T) -> &mut T {
|
|
self.inner.write(value)
|
|
}
|
|
}
|
|
|
|
#[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 lock<T, R>(
|
|
ptr: *mut T,
|
|
priority: &Priority,
|
|
ceiling: u8,
|
|
nvic_prio_bits: u8,
|
|
f: impl FnOnce(&mut T) -> R,
|
|
) -> R {
|
|
let current = priority.get();
|
|
|
|
if current < 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 lock<T, R>(
|
|
ptr: *mut T,
|
|
priority: &Priority,
|
|
ceiling: u8,
|
|
_nvic_prio_bits: u8,
|
|
f: impl FnOnce(&mut T) -> R,
|
|
) -> R {
|
|
let current = priority.get();
|
|
|
|
if current < ceiling {
|
|
priority.set(u8::MAX);
|
|
let r = interrupt::free(|_| f(&mut *ptr));
|
|
priority.set(current);
|
|
r
|
|
} else {
|
|
f(&mut *ptr)
|
|
}
|
|
}
|
|
|
|
#[inline]
|
|
pub fn logical2hw(logical: u8, nvic_prio_bits: u8) -> u8 {
|
|
((1 << nvic_prio_bits) - logical) << (8 - nvic_prio_bits)
|
|
}
|