Simpliify PeripheralMutex a bit.
This commit is contained in:
parent
77bdb5428e
commit
deb3c93892
@ -61,7 +61,7 @@ struct State<'a, U: Instance> {
|
|||||||
/// - nrf52832: Section 15.2
|
/// - nrf52832: Section 15.2
|
||||||
/// - nrf52840: Section 6.1.2
|
/// - nrf52840: Section 6.1.2
|
||||||
pub struct BufferedUarte<'a, U: Instance> {
|
pub struct BufferedUarte<'a, U: Instance> {
|
||||||
inner: PeripheralMutex<U::Interrupt, State<'a, U>>,
|
inner: PeripheralMutex<State<'a, U>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, U: Instance> Unpin for BufferedUarte<'a, U> {}
|
impl<'a, U: Instance> Unpin for BufferedUarte<'a, U> {}
|
||||||
@ -143,7 +143,6 @@ impl<'a, U: Instance> BufferedUarte<'a, U> {
|
|||||||
|
|
||||||
BufferedUarte {
|
BufferedUarte {
|
||||||
inner: PeripheralMutex::new(
|
inner: PeripheralMutex::new(
|
||||||
irq,
|
|
||||||
State {
|
State {
|
||||||
inner: uarte,
|
inner: uarte,
|
||||||
|
|
||||||
@ -155,11 +154,12 @@ impl<'a, U: Instance> BufferedUarte<'a, U> {
|
|||||||
tx_state: TxState::Idle,
|
tx_state: TxState::Idle,
|
||||||
tx_waker: WakerRegistration::new(),
|
tx_waker: WakerRegistration::new(),
|
||||||
},
|
},
|
||||||
|
irq,
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<U::Interrupt, State<'a, U>>> {
|
fn inner(self: Pin<&mut Self>) -> Pin<&mut PeripheralMutex<State<'a, U>>> {
|
||||||
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
|
unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().inner) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -173,7 +173,7 @@ impl<'a, U: Instance> Drop for BufferedUarte<'a, U> {
|
|||||||
|
|
||||||
impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
|
impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
|
||||||
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
|
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<&[u8]>> {
|
||||||
self.inner().with(|_irq, state| {
|
self.inner().with(|state, _irq| {
|
||||||
// Conservative compiler fence to prevent optimizations that do not
|
// Conservative compiler fence to prevent optimizations that do not
|
||||||
// take in to account actions by DMA. The fence has been placed here,
|
// take in to account actions by DMA. The fence has been placed here,
|
||||||
// before any DMA action has started
|
// before any DMA action has started
|
||||||
@ -203,7 +203,7 @@ impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn consume(self: Pin<&mut Self>, amt: usize) {
|
fn consume(self: Pin<&mut Self>, amt: usize) {
|
||||||
self.inner().with(|irq, state| {
|
self.inner().with(|state, irq| {
|
||||||
trace!("consume {:?}", amt);
|
trace!("consume {:?}", amt);
|
||||||
state.rx.pop(amt);
|
state.rx.pop(amt);
|
||||||
irq.pend();
|
irq.pend();
|
||||||
@ -213,7 +213,7 @@ impl<'a, U: Instance> AsyncBufRead for BufferedUarte<'a, U> {
|
|||||||
|
|
||||||
impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> {
|
impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> {
|
||||||
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
|
fn poll_write(self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8]) -> Poll<Result<usize>> {
|
||||||
self.inner().with(|irq, state| {
|
self.inner().with(|state, irq| {
|
||||||
trace!("poll_write: {:?}", buf.len());
|
trace!("poll_write: {:?}", buf.len());
|
||||||
|
|
||||||
let tx_buf = state.tx.push_buf();
|
let tx_buf = state.tx.push_buf();
|
||||||
@ -242,6 +242,8 @@ impl<'a, U: Instance> AsyncWrite for BufferedUarte<'a, U> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, U: Instance> PeripheralState for State<'a, U> {
|
impl<'a, U: Instance> PeripheralState for State<'a, U> {
|
||||||
|
type Interrupt = U::Interrupt;
|
||||||
|
|
||||||
fn on_interrupt(&mut self) {
|
fn on_interrupt(&mut self) {
|
||||||
trace!("irq: start");
|
trace!("irq: start");
|
||||||
let mut more_work = true;
|
let mut more_work = true;
|
||||||
|
@ -6,25 +6,26 @@ use crate::fmt::*;
|
|||||||
use crate::interrupt::OwnedInterrupt;
|
use crate::interrupt::OwnedInterrupt;
|
||||||
|
|
||||||
pub trait PeripheralState {
|
pub trait PeripheralState {
|
||||||
|
type Interrupt: OwnedInterrupt;
|
||||||
fn on_interrupt(&mut self);
|
fn on_interrupt(&mut self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PeripheralMutex<I: OwnedInterrupt, S: PeripheralState> {
|
pub struct PeripheralMutex<S: PeripheralState> {
|
||||||
inner: Option<(I, UnsafeCell<S>)>,
|
inner: Option<(UnsafeCell<S>, S::Interrupt)>,
|
||||||
not_send: PhantomData<*mut ()>,
|
not_send: PhantomData<*mut ()>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> {
|
impl<S: PeripheralState> PeripheralMutex<S> {
|
||||||
pub fn new(irq: I, state: S) -> Self {
|
pub fn new(state: S, irq: S::Interrupt) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: Some((irq, UnsafeCell::new(state))),
|
inner: Some((UnsafeCell::new(state), irq)),
|
||||||
not_send: PhantomData,
|
not_send: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with<R>(self: Pin<&mut Self>, f: impl FnOnce(&mut I, &mut S) -> R) -> R {
|
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() };
|
let this = unsafe { self.get_unchecked_mut() };
|
||||||
let (irq, state) = unwrap!(this.inner.as_mut());
|
let (state, irq) = unwrap!(this.inner.as_mut());
|
||||||
|
|
||||||
irq.disable();
|
irq.disable();
|
||||||
compiler_fence(Ordering::SeqCst);
|
compiler_fence(Ordering::SeqCst);
|
||||||
@ -43,7 +44,7 @@ impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> {
|
|||||||
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
|
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
|
||||||
let state = unsafe { &mut *state.get() };
|
let state = unsafe { &mut *state.get() };
|
||||||
|
|
||||||
let r = f(irq, state);
|
let r = f(state, irq);
|
||||||
|
|
||||||
compiler_fence(Ordering::SeqCst);
|
compiler_fence(Ordering::SeqCst);
|
||||||
irq.enable();
|
irq.enable();
|
||||||
@ -51,18 +52,18 @@ impl<I: OwnedInterrupt, S: PeripheralState> PeripheralMutex<I, S> {
|
|||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn free(self: Pin<&mut Self>) -> (I, S) {
|
pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) {
|
||||||
let this = unsafe { self.get_unchecked_mut() };
|
let this = unsafe { self.get_unchecked_mut() };
|
||||||
let (irq, state) = unwrap!(this.inner.take());
|
let (state, irq) = unwrap!(this.inner.take());
|
||||||
irq.disable();
|
irq.disable();
|
||||||
irq.remove_handler();
|
irq.remove_handler();
|
||||||
(irq, state.into_inner())
|
(state.into_inner(), irq)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: OwnedInterrupt, S: PeripheralState> Drop for PeripheralMutex<I, S> {
|
impl<S: PeripheralState> Drop for PeripheralMutex<S> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some((irq, state)) = &mut self.inner {
|
if let Some((state, irq)) = &mut self.inner {
|
||||||
irq.disable();
|
irq.disable();
|
||||||
irq.remove_handler();
|
irq.remove_handler();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user