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};
|
|
|
|
use core::{cell::UnsafeCell, marker::PhantomData};
|
|
|
|
|
2021-01-05 00:57:05 +00:00
|
|
|
use crate::fmt::*;
|
2021-01-03 00:40:40 +00:00
|
|
|
use crate::interrupt::OwnedInterrupt;
|
|
|
|
|
2021-01-05 00:57:05 +00:00
|
|
|
pub trait PeripheralState {
|
2021-01-06 21:48:54 +00:00
|
|
|
type Interrupt: OwnedInterrupt;
|
2021-01-03 00:40:40 +00:00
|
|
|
fn on_interrupt(&mut self);
|
|
|
|
}
|
|
|
|
|
2021-01-06 21:48:54 +00:00
|
|
|
pub struct PeripheralMutex<S: PeripheralState> {
|
|
|
|
inner: Option<(UnsafeCell<S>, S::Interrupt)>,
|
2021-01-05 00:57:05 +00:00
|
|
|
not_send: PhantomData<*mut ()>,
|
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-01-06 21:48:54 +00:00
|
|
|
inner: Some((UnsafeCell::new(state), irq)),
|
2021-01-03 00:40:40 +00:00
|
|
|
not_send: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-06 21:48:54 +00:00
|
|
|
pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut S, &mut S::Interrupt) -> R) -> R {
|
2021-01-05 00:57:05 +00:00
|
|
|
let this = unsafe { self.get_unchecked_mut() };
|
2021-01-06 21:48:54 +00:00
|
|
|
let (state, irq) = unwrap!(this.inner.as_mut());
|
2021-01-05 00:57:05 +00:00
|
|
|
|
|
|
|
irq.disable();
|
2021-01-03 00:40:40 +00:00
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-01-05 00:57:05 +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.
|
|
|
|
let state = unsafe { &mut *(p as *mut S) };
|
|
|
|
state.on_interrupt();
|
|
|
|
},
|
|
|
|
state.get() as *mut (),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
|
|
|
|
let state = unsafe { &mut *state.get() };
|
|
|
|
|
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-01-11 09:39:59 +00:00
|
|
|
this.inner.take().map(|(state, irq)| {
|
|
|
|
irq.disable();
|
|
|
|
irq.remove_handler();
|
|
|
|
(state.into_inner(), irq)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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-14 00:41:36 +00:00
|
|
|
if let Some((_state, irq)) = &mut self.inner {
|
2021-01-05 00:57:05 +00:00
|
|
|
irq.disable();
|
|
|
|
irq.remove_handler();
|
|
|
|
}
|
2021-01-03 00:40:40 +00:00
|
|
|
}
|
|
|
|
}
|