2021-02-20 00:27:24 +01:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use core::marker::{PhantomData, PhantomPinned};
|
2021-01-05 01:57:05 +01:00
|
|
|
use core::pin::Pin;
|
2021-01-03 01:40:40 +01:00
|
|
|
|
2021-07-29 07:19:57 +02:00
|
|
|
use cortex_m::peripheral::scb::VectActive;
|
2021-07-27 09:28:52 +02:00
|
|
|
use cortex_m::peripheral::{NVIC, SCB};
|
2021-03-08 00:15:40 +01:00
|
|
|
use embassy::interrupt::{Interrupt, InterruptExt};
|
2021-03-01 00:44:38 +01:00
|
|
|
|
2021-07-24 04:53:57 +02:00
|
|
|
/// A type which can be used as state with `PeripheralMutex`.
|
|
|
|
///
|
|
|
|
/// It needs to be `Send` because `&mut` references are sent back and forth between the 'thread' which owns the `PeripheralMutex` and the interrupt,
|
2021-07-28 13:31:31 +02:00
|
|
|
/// and `&mut T` is only `Send` where `T: Send`.
|
2021-07-24 04:53:57 +02:00
|
|
|
///
|
2021-07-29 07:11:26 +02:00
|
|
|
/// It also requires `'static` to be used safely with `PeripheralMutex::register_interrupt`,
|
|
|
|
/// because although `Pin` guarantees that the memory of the state won't be invalidated,
|
2021-07-24 04:53:57 +02:00
|
|
|
/// it doesn't guarantee that the lifetime will last.
|
2021-07-29 07:11:26 +02:00
|
|
|
pub trait PeripheralState: Send {
|
2021-07-05 09:42:43 +02:00
|
|
|
type Interrupt: Interrupt;
|
|
|
|
fn on_interrupt(&mut self);
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:11:26 +02:00
|
|
|
pub struct PeripheralMutex<S: PeripheralState> {
|
2021-03-18 02:01:29 +01:00
|
|
|
state: UnsafeCell<S>,
|
2021-02-28 22:03:45 +01:00
|
|
|
|
2021-03-18 02:01:29 +01:00
|
|
|
irq_setup_done: bool,
|
|
|
|
irq: S::Interrupt,
|
2021-02-28 22:03:45 +01:00
|
|
|
|
2021-02-20 00:27:24 +01:00
|
|
|
_not_send: PhantomData<*mut ()>,
|
|
|
|
_pinned: PhantomPinned,
|
2021-01-03 01:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-27 09:28:52 +02:00
|
|
|
/// Whether `irq` can be preempted by the current interrupt.
|
|
|
|
pub(crate) fn can_be_preempted(irq: &impl Interrupt) -> bool {
|
|
|
|
match SCB::vect_active() {
|
2021-07-29 07:19:57 +02:00
|
|
|
// Thread mode can't preempt anything.
|
2021-07-27 09:28:52 +02:00
|
|
|
VectActive::ThreadMode => false,
|
2021-07-29 07:19:57 +02:00
|
|
|
// Exceptions don't always preempt interrupts,
|
|
|
|
// but there isn't much of a good reason to be keeping a `PeripheralMutex` in an exception anyway.
|
|
|
|
VectActive::Exception(_) => true,
|
2021-07-27 09:28:52 +02:00
|
|
|
VectActive::Interrupt { irqn } => {
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
struct NrWrap(u16);
|
|
|
|
unsafe impl cortex_m::interrupt::InterruptNumber for NrWrap {
|
|
|
|
fn number(self) -> u16 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
2021-07-28 13:31:31 +02:00
|
|
|
NVIC::get_priority(NrWrap(irqn.into())) < irq.get_priority().into()
|
2021-07-27 09:28:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:11:26 +02:00
|
|
|
impl<S: PeripheralState + 'static> PeripheralMutex<S> {
|
|
|
|
/// Registers `on_interrupt` as the wrapped interrupt's interrupt handler and enables it.
|
|
|
|
///
|
|
|
|
/// This requires this `PeripheralMutex`'s `PeripheralState` to live for `'static`,
|
|
|
|
/// because `Pin` only guarantees that it's memory won't be repurposed,
|
|
|
|
/// not that it's lifetime will last.
|
|
|
|
///
|
|
|
|
/// To use non-`'static` `PeripheralState`, use the unsafe `register_interrupt_unchecked`.
|
|
|
|
///
|
|
|
|
/// Note: `'static` doesn't mean it _has_ to live for the entire program, like an `&'static T`;
|
|
|
|
/// it just means it _can_ live for the entire program - for example, `u8` lives for `'static`.
|
|
|
|
pub fn register_interrupt(self: Pin<&mut Self>) {
|
|
|
|
// SAFETY: `S: 'static`, so there's no way it's lifetime can expire.
|
|
|
|
unsafe { self.register_interrupt_unchecked() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S: PeripheralState> PeripheralMutex<S> {
|
|
|
|
/// Create a new `PeripheralMutex` wrapping `irq`, with the initial state `state`.
|
2021-01-06 22:48:54 +01:00
|
|
|
pub fn new(state: S, irq: S::Interrupt) -> Self {
|
2021-07-27 09:28:52 +02:00
|
|
|
if can_be_preempted(&irq) {
|
|
|
|
panic!("`PeripheralMutex` cannot be created in an interrupt with higher priority than the interrupt it wraps");
|
|
|
|
}
|
|
|
|
|
2021-01-03 01:40:40 +01:00
|
|
|
Self {
|
2021-03-18 02:01:29 +01:00
|
|
|
irq,
|
|
|
|
irq_setup_done: false,
|
|
|
|
|
|
|
|
state: UnsafeCell::new(state),
|
2021-02-20 00:27:24 +01:00
|
|
|
_not_send: PhantomData,
|
|
|
|
_pinned: PhantomPinned,
|
2021-01-03 01:40:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 07:11:26 +02:00
|
|
|
/// Registers `on_interrupt` as the wrapped interrupt's interrupt handler and enables it.
|
|
|
|
///
|
|
|
|
/// # Safety
|
|
|
|
/// The lifetime of any data in `PeripheralState` that is accessed by the interrupt handler
|
|
|
|
/// must not end without `Drop` being called on this `PeripheralMutex`.
|
|
|
|
///
|
|
|
|
/// This can be accomplished by either not accessing any data with a lifetime in `on_interrupt`,
|
|
|
|
/// or making sure that nothing like `mem::forget` is used on the `PeripheralMutex`.
|
|
|
|
|
|
|
|
// TODO: this name isn't the best.
|
|
|
|
pub unsafe fn register_interrupt_unchecked(self: Pin<&mut Self>) {
|
|
|
|
let this = self.get_unchecked_mut();
|
2021-03-18 02:29:03 +01:00
|
|
|
if this.irq_setup_done {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.irq.disable();
|
|
|
|
this.irq.set_handler(|p| {
|
2021-07-27 09:28:52 +02:00
|
|
|
// Safety: it's OK to get a &mut to the state, since
|
|
|
|
// - We checked that the thread owning the `PeripheralMutex` can't preempt us in `new`.
|
|
|
|
// Interrupts' priorities can only be changed with raw embassy `Interrupts`,
|
|
|
|
// which can't safely store a `PeripheralMutex` across invocations.
|
|
|
|
// - We can't have preempted a with() call because the irq is disabled during it.
|
|
|
|
let state = unsafe { &mut *(p as *mut S) };
|
|
|
|
state.on_interrupt();
|
2021-02-26 02:04:48 +01:00
|
|
|
});
|
2021-03-18 02:29:03 +01:00
|
|
|
this.irq
|
|
|
|
.set_handler_context((&mut this.state) as *mut _ as *mut ());
|
|
|
|
this.irq.enable();
|
2021-02-28 22:03:45 +01:00
|
|
|
|
2021-03-18 02:29:03 +01:00
|
|
|
this.irq_setup_done = true;
|
2021-02-28 22:03:45 +01:00
|
|
|
}
|
|
|
|
|
2021-07-27 09:28:52 +02:00
|
|
|
pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S) -> R) -> R {
|
2021-02-28 22:03:45 +01:00
|
|
|
let this = unsafe { self.get_unchecked_mut() };
|
|
|
|
|
2021-03-18 02:01:29 +01:00
|
|
|
this.irq.disable();
|
2021-01-05 01:57:05 +01:00
|
|
|
|
|
|
|
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
|
2021-03-18 02:01:29 +01:00
|
|
|
let state = unsafe { &mut *this.state.get() };
|
2021-07-27 09:28:52 +02:00
|
|
|
let r = f(state);
|
2021-01-03 01:40:40 +01:00
|
|
|
|
2021-07-28 13:39:31 +02:00
|
|
|
this.irq.enable();
|
2021-01-03 01:40:40 +01:00
|
|
|
|
|
|
|
r
|
|
|
|
}
|
2021-07-27 09:28:52 +02:00
|
|
|
|
|
|
|
/// Returns whether the wrapped interrupt is currently in a pending state.
|
|
|
|
pub fn is_pending(&self) -> bool {
|
|
|
|
self.irq.is_pending()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forces the wrapped interrupt into a pending state.
|
|
|
|
pub fn pend(&self) {
|
|
|
|
self.irq.pend()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Forces the wrapped interrupt out of a pending state.
|
|
|
|
pub fn unpend(&self) {
|
|
|
|
self.irq.unpend()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the priority of the wrapped interrupt.
|
|
|
|
pub fn priority(&self) -> <S::Interrupt as Interrupt>::Priority {
|
|
|
|
self.irq.get_priority()
|
|
|
|
}
|
2021-01-03 01:40:40 +01:00
|
|
|
}
|
|
|
|
|
2021-07-29 07:11:26 +02:00
|
|
|
impl<S: PeripheralState> Drop for PeripheralMutex<S> {
|
2021-01-03 01:40:40 +01:00
|
|
|
fn drop(&mut self) {
|
2021-03-18 02:01:29 +01:00
|
|
|
self.irq.disable();
|
|
|
|
self.irq.remove_handler();
|
2021-01-03 01:40:40 +01:00
|
|
|
}
|
|
|
|
}
|