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
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
|
2021-03-08 00:15:40 +01:00
|
|
|
use embassy::interrupt::{Interrupt, InterruptExt};
|
2021-03-01 00:44:38 +01:00
|
|
|
|
2021-01-05 01:57:05 +01:00
|
|
|
pub trait PeripheralState {
|
2021-02-26 01:55:27 +01:00
|
|
|
type Interrupt: Interrupt;
|
2021-01-03 01:40:40 +01:00
|
|
|
fn on_interrupt(&mut self);
|
|
|
|
}
|
|
|
|
|
2021-01-06 22:48:54 +01: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-01-06 22:48:54 +01:00
|
|
|
impl<S: PeripheralState> PeripheralMutex<S> {
|
|
|
|
pub fn new(state: S, irq: S::Interrupt) -> Self {
|
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-03-18 02:29:03 +01:00
|
|
|
pub fn register_interrupt(self: Pin<&mut Self>) {
|
|
|
|
let this = unsafe { self.get_unchecked_mut() };
|
|
|
|
if this.irq_setup_done {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.irq.disable();
|
2021-01-03 01:40:40 +01:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-03-18 02:29:03 +01:00
|
|
|
this.irq.set_handler(|p| {
|
2021-02-26 02:04:48 +01:00
|
|
|
// Safety: it's OK to get a &mut to the state, since
|
|
|
|
// - We're in the IRQ, no one else can't preempt us
|
|
|
|
// - We can't have preempted a with() call because the irq is disabled during it.
|
2021-03-18 02:29:03 +01:00
|
|
|
let state = unsafe { &mut *(p as *mut S) };
|
2021-02-26 02:04:48 +01:00
|
|
|
state.on_interrupt();
|
|
|
|
});
|
2021-03-18 02:29:03 +01:00
|
|
|
this.irq
|
|
|
|
.set_handler_context((&mut this.state) as *mut _ as *mut ());
|
2021-02-28 22:03:45 +01:00
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-03-18 02:29:03 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R {
|
|
|
|
let this = unsafe { self.get_unchecked_mut() };
|
|
|
|
|
2021-03-18 02:01:29 +01:00
|
|
|
this.irq.disable();
|
2021-02-28 22:03:45 +01:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
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-01-05 01:57:05 +01:00
|
|
|
|
2021-03-18 02:01:29 +01:00
|
|
|
let r = f(state, &mut this.irq);
|
2021-01-03 01:40:40 +01:00
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-03-18 02:01:29 +01:00
|
|
|
this.irq.enable();
|
2021-01-03 01:40:40 +01:00
|
|
|
|
|
|
|
r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 22:48:54 +01: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
|
|
|
}
|
|
|
|
}
|