2021-02-19 23:27:24 +00:00
|
|
|
use core::cell::UnsafeCell;
|
|
|
|
use core::marker::{PhantomData, PhantomPinned};
|
2021-02-28 21:03:45 +00:00
|
|
|
use core::mem::MaybeUninit;
|
2021-01-05 00:57:05 +00:00
|
|
|
use core::pin::Pin;
|
2021-01-03 00:40:40 +00:00
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
|
2021-02-28 21:03:45 +00:00
|
|
|
use crate::fmt::{assert, *};
|
2021-02-26 00:55:27 +00:00
|
|
|
use crate::interrupt::Interrupt;
|
2021-01-03 00:40:40 +00:00
|
|
|
|
2021-01-05 00:57:05 +00:00
|
|
|
pub trait PeripheralState {
|
2021-02-26 00:55:27 +00:00
|
|
|
type Interrupt: Interrupt;
|
2021-01-03 00:40:40 +00:00
|
|
|
fn on_interrupt(&mut self);
|
|
|
|
}
|
|
|
|
|
2021-02-28 21:03:45 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
|
|
|
enum Life {
|
|
|
|
Ready,
|
|
|
|
Created,
|
|
|
|
Freed,
|
|
|
|
}
|
|
|
|
|
2021-01-06 21:48:54 +00:00
|
|
|
pub struct PeripheralMutex<S: PeripheralState> {
|
2021-02-28 21:03:45 +00:00
|
|
|
life: Life,
|
|
|
|
|
|
|
|
state: MaybeUninit<UnsafeCell<S>>, // Init if life != Freed
|
|
|
|
irq: MaybeUninit<S::Interrupt>, // Init if life != Freed
|
|
|
|
|
2021-02-19 23:27:24 +00:00
|
|
|
_not_send: PhantomData<*mut ()>,
|
|
|
|
_pinned: PhantomPinned,
|
2021-01-03 00:40:40 +00:00
|
|
|
}
|
|
|
|
|
2021-01-06 21:48:54 +00:00
|
|
|
impl<S: PeripheralState> PeripheralMutex<S> {
|
|
|
|
pub fn new(state: S, irq: S::Interrupt) -> Self {
|
2021-01-03 00:40:40 +00:00
|
|
|
Self {
|
2021-02-28 21:03:45 +00:00
|
|
|
life: Life::Created,
|
|
|
|
state: MaybeUninit::new(UnsafeCell::new(state)),
|
|
|
|
irq: MaybeUninit::new(irq),
|
2021-02-19 23:27:24 +00:00
|
|
|
_not_send: PhantomData,
|
|
|
|
_pinned: PhantomPinned,
|
2021-01-03 00:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-28 21:03:45 +00:00
|
|
|
/// safety: self must be pinned.
|
|
|
|
unsafe fn setup(&mut self) {
|
|
|
|
assert!(self.life == Life::Created);
|
2021-01-05 00:57:05 +00:00
|
|
|
|
2021-02-28 21:03:45 +00:00
|
|
|
let irq = &mut *self.irq.as_mut_ptr();
|
2021-01-05 00:57:05 +00:00
|
|
|
irq.disable();
|
2021-01-03 00:40:40 +00:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-02-26 01:04:48 +00:00
|
|
|
irq.set_handler(|p| {
|
|
|
|
// 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-02-28 21:03:45 +00:00
|
|
|
let state = &mut *(p as *mut S);
|
2021-02-26 01:04:48 +00:00
|
|
|
state.on_interrupt();
|
|
|
|
});
|
2021-02-28 21:03:45 +00:00
|
|
|
irq.set_handler_context(self.state.as_mut_ptr() as *mut ());
|
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
irq.enable();
|
|
|
|
|
|
|
|
self.life = Life::Ready;
|
|
|
|
}
|
|
|
|
|
|
|
|
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() };
|
|
|
|
if this.life != Life::Ready {
|
|
|
|
unsafe { this.setup() }
|
|
|
|
}
|
|
|
|
|
|
|
|
let irq = unsafe { &mut *this.irq.as_mut_ptr() };
|
|
|
|
|
|
|
|
irq.disable();
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-01-05 00:57:05 +00:00
|
|
|
|
|
|
|
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
|
2021-02-28 21:03:45 +00:00
|
|
|
let state = unsafe { &mut *(*this.state.as_ptr()).get() };
|
2021-01-05 00:57:05 +00:00
|
|
|
|
2021-01-06 21:48:54 +00:00
|
|
|
let r = f(state, irq);
|
2021-01-03 00:40:40 +00:00
|
|
|
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-01-05 00:57:05 +00:00
|
|
|
irq.enable();
|
2021-01-03 00:40:40 +00:00
|
|
|
|
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2021-01-11 09:39:59 +00:00
|
|
|
pub fn try_free(self: Pin<&mut Self>) -> Option<(S, S::Interrupt)> {
|
2021-01-05 00:57:05 +00:00
|
|
|
let this = unsafe { self.get_unchecked_mut() };
|
2021-02-28 21:03:45 +00:00
|
|
|
|
|
|
|
if this.life != Life::Freed {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { &mut *this.irq.as_mut_ptr() }.disable();
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
this.life = Life::Freed;
|
|
|
|
|
|
|
|
let state = unsafe { this.state.as_ptr().read().into_inner() };
|
|
|
|
let irq = unsafe { this.irq.as_ptr().read() };
|
|
|
|
Some((state, irq))
|
2021-01-11 09:39:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) {
|
|
|
|
unwrap!(self.try_free())
|
2021-01-03 00:40:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 21:48:54 +00:00
|
|
|
impl<S: PeripheralState> Drop for PeripheralMutex<S> {
|
2021-01-03 00:40:40 +00:00
|
|
|
fn drop(&mut self) {
|
2021-02-28 21:03:45 +00:00
|
|
|
if self.life != Life::Freed {
|
|
|
|
let irq = unsafe { &mut *self.irq.as_mut_ptr() };
|
2021-01-05 00:57:05 +00:00
|
|
|
irq.disable();
|
|
|
|
irq.remove_handler();
|
|
|
|
}
|
2021-01-03 00:40:40 +00:00
|
|
|
}
|
|
|
|
}
|