Move nRF's util into a separate crate
This commit is contained in:
@ -20,6 +20,7 @@ defmt-error = [ ]
|
||||
|
||||
[dependencies]
|
||||
embassy = { version = "0.1.0", path = "../embassy" }
|
||||
embassy-extras = {version = "0.1.0", path = "../embassy-extras" }
|
||||
|
||||
defmt = { version = "0.2.0", optional = true }
|
||||
log = { version = "0.4.11", optional = true }
|
||||
|
@ -13,14 +13,15 @@ use core::task::{Context, Poll};
|
||||
use embassy::interrupt::InterruptExt;
|
||||
use embassy::io::{AsyncBufRead, AsyncWrite, Result};
|
||||
use embassy::util::WakerRegistration;
|
||||
use embassy_extras::low_power_wait_until;
|
||||
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
|
||||
use embassy_extras::ring_buffer::RingBuffer;
|
||||
use embedded_hal::digital::v2::OutputPin;
|
||||
|
||||
use crate::fmt::*;
|
||||
use crate::hal::ppi::ConfigurablePpi;
|
||||
use crate::interrupt::{self, Interrupt};
|
||||
use crate::pac;
|
||||
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
|
||||
use crate::util::ring_buffer::RingBuffer;
|
||||
use crate::{fmt::*, util::low_power_wait_until};
|
||||
|
||||
// Re-export SVD variants to allow user to directly set values
|
||||
pub use crate::hal::uarte::Pins;
|
||||
@ -116,7 +117,6 @@ impl<'a, U: Instance, T: TimerInstance, P1: ConfigurablePpi, P2: ConfigurablePpi
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// Enable UARTE instance
|
||||
uarte.enable.write(|w| w.enable().enabled());
|
||||
|
||||
|
@ -90,7 +90,6 @@ pub(crate) fn slice_in_ram_or<T>(slice: &[u8], err: T) -> Result<(), T> {
|
||||
|
||||
// This mod MUST go first, so that the others see its macros.
|
||||
pub(crate) mod fmt;
|
||||
pub(crate) mod util;
|
||||
|
||||
pub mod buffered_uarte;
|
||||
pub mod gpiote;
|
||||
|
@ -2,6 +2,8 @@ use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
use core::task::Poll;
|
||||
|
||||
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
|
||||
|
||||
use crate::fmt::{assert, assert_eq, *};
|
||||
use crate::hal::gpio::{Output, Pin as GpioPin, PushPull};
|
||||
use crate::interrupt::{self};
|
||||
@ -11,7 +13,6 @@ pub use crate::pac::qspi::ifconfig0::ADDRMODE_A as AddressMode;
|
||||
pub use crate::pac::qspi::ifconfig0::PPSIZE_A as WritePageSize;
|
||||
pub use crate::pac::qspi::ifconfig0::READOC_A as ReadOpcode;
|
||||
pub use crate::pac::qspi::ifconfig0::WRITEOC_A as WriteOpcode;
|
||||
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
|
||||
|
||||
// TODO
|
||||
// - config:
|
||||
|
@ -3,10 +3,10 @@ use core::pin::Pin;
|
||||
use core::sync::atomic::{compiler_fence, Ordering};
|
||||
use core::task::Poll;
|
||||
use embassy::util::WakerRegistration;
|
||||
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
|
||||
use futures::future::poll_fn;
|
||||
|
||||
use crate::interrupt::{self, Interrupt};
|
||||
use crate::util::peripheral::{PeripheralMutex, PeripheralState};
|
||||
use crate::{pac, slice_in_ram_or};
|
||||
|
||||
pub use crate::hal::spim::{
|
||||
|
@ -16,8 +16,8 @@ use crate::fmt::{assert, *};
|
||||
use crate::hal::pac;
|
||||
use crate::hal::prelude::*;
|
||||
use crate::hal::target_constants::EASY_DMA_SIZE;
|
||||
use crate::interrupt;
|
||||
use crate::interrupt::Interrupt;
|
||||
use crate::{interrupt, util};
|
||||
|
||||
pub use crate::hal::uarte::Pins;
|
||||
// Re-export SVD variants to allow user to directly set values.
|
||||
@ -45,7 +45,7 @@ where
|
||||
/// Creates the interface to a UARTE instance.
|
||||
/// Sets the baud rate, parity and assigns the pins to the UARTE peripheral.
|
||||
///
|
||||
/// # Unsafe
|
||||
/// # Safety
|
||||
///
|
||||
/// The returned API is safe unless you use `mem::forget` (or similar safe mechanisms)
|
||||
/// on stack allocated buffers which which have been passed to [`send()`](Uarte::send)
|
||||
@ -327,7 +327,7 @@ where
|
||||
.tasks_stoprx
|
||||
.write(|w| unsafe { w.bits(1) });
|
||||
|
||||
util::low_power_wait_until(|| T::state().rx_done.signaled())
|
||||
embassy_extras::low_power_wait_until(|| T::state().rx_done.signaled())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
pub mod peripheral;
|
||||
pub mod ring_buffer;
|
||||
|
||||
/// Low power blocking wait loop using WFE/SEV.
|
||||
pub fn low_power_wait_until(mut condition: impl FnMut() -> bool) {
|
||||
while !condition() {
|
||||
// WFE might "eat" an event that would have otherwise woken the executor.
|
||||
cortex_m::asm::wfe();
|
||||
}
|
||||
// Retrigger an event to be transparent to the executor.
|
||||
cortex_m::asm::sev();
|
||||
}
|
@ -1,120 +0,0 @@
|
||||
use core::cell::UnsafeCell;
|
||||
use core::marker::{PhantomData, PhantomPinned};
|
||||
use core::mem::MaybeUninit;
|
||||
use core::pin::Pin;
|
||||
use core::sync::atomic::{compiler_fence, Ordering};
|
||||
|
||||
use embassy::interrupt::InterruptExt;
|
||||
|
||||
use crate::fmt::{assert, *};
|
||||
use crate::interrupt::Interrupt;
|
||||
|
||||
pub trait PeripheralState {
|
||||
type Interrupt: Interrupt;
|
||||
fn on_interrupt(&mut self);
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||
enum Life {
|
||||
Ready,
|
||||
Created,
|
||||
Freed,
|
||||
}
|
||||
|
||||
pub struct PeripheralMutex<S: PeripheralState> {
|
||||
life: Life,
|
||||
|
||||
state: MaybeUninit<UnsafeCell<S>>, // Init if life != Freed
|
||||
irq: MaybeUninit<S::Interrupt>, // Init if life != Freed
|
||||
|
||||
_not_send: PhantomData<*mut ()>,
|
||||
_pinned: PhantomPinned,
|
||||
}
|
||||
|
||||
impl<S: PeripheralState> PeripheralMutex<S> {
|
||||
pub fn new(state: S, irq: S::Interrupt) -> Self {
|
||||
Self {
|
||||
life: Life::Created,
|
||||
state: MaybeUninit::new(UnsafeCell::new(state)),
|
||||
irq: MaybeUninit::new(irq),
|
||||
_not_send: PhantomData,
|
||||
_pinned: PhantomPinned,
|
||||
}
|
||||
}
|
||||
|
||||
/// safety: self must be pinned.
|
||||
unsafe fn setup(&mut self) {
|
||||
assert!(self.life == Life::Created);
|
||||
|
||||
let irq = &mut *self.irq.as_mut_ptr();
|
||||
irq.disable();
|
||||
compiler_fence(Ordering::SeqCst);
|
||||
|
||||
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 = &mut *(p as *mut S);
|
||||
state.on_interrupt();
|
||||
});
|
||||
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);
|
||||
|
||||
// Safety: it's OK to get a &mut to the state, since the irq is disabled.
|
||||
let state = unsafe { &mut *(*this.state.as_ptr()).get() };
|
||||
|
||||
let r = f(state, irq);
|
||||
|
||||
compiler_fence(Ordering::SeqCst);
|
||||
irq.enable();
|
||||
|
||||
r
|
||||
}
|
||||
|
||||
pub fn try_free(self: Pin<&mut Self>) -> Option<(S, S::Interrupt)> {
|
||||
let this = unsafe { self.get_unchecked_mut() };
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn free(self: Pin<&mut Self>) -> (S, S::Interrupt) {
|
||||
unwrap!(self.try_free())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S: PeripheralState> Drop for PeripheralMutex<S> {
|
||||
fn drop(&mut self) {
|
||||
if self.life != Life::Freed {
|
||||
let irq = unsafe { &mut *self.irq.as_mut_ptr() };
|
||||
irq.disable();
|
||||
irq.remove_handler();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,80 +0,0 @@
|
||||
use crate::fmt::{assert, *};
|
||||
|
||||
pub struct RingBuffer<'a> {
|
||||
buf: &'a mut [u8],
|
||||
start: usize,
|
||||
end: usize,
|
||||
empty: bool,
|
||||
}
|
||||
|
||||
impl<'a> RingBuffer<'a> {
|
||||
pub fn new(buf: &'a mut [u8]) -> Self {
|
||||
Self {
|
||||
buf,
|
||||
start: 0,
|
||||
end: 0,
|
||||
empty: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push_buf(&mut self) -> &mut [u8] {
|
||||
if self.start == self.end && !self.empty {
|
||||
trace!(" ringbuf: push_buf empty");
|
||||
return &mut self.buf[..0];
|
||||
}
|
||||
|
||||
let n = if self.start <= self.end {
|
||||
self.buf.len() - self.end
|
||||
} else {
|
||||
self.start - self.end
|
||||
};
|
||||
|
||||
trace!(" ringbuf: push_buf {:?}..{:?}", self.end, self.end + n);
|
||||
&mut self.buf[self.end..self.end + n]
|
||||
}
|
||||
|
||||
pub fn push(&mut self, n: usize) {
|
||||
trace!(" ringbuf: push {:?}", n);
|
||||
if n == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.end = self.wrap(self.end + n);
|
||||
self.empty = false;
|
||||
}
|
||||
|
||||
pub fn pop_buf(&mut self) -> &mut [u8] {
|
||||
if self.empty {
|
||||
trace!(" ringbuf: pop_buf empty");
|
||||
return &mut self.buf[..0];
|
||||
}
|
||||
|
||||
let n = if self.end <= self.start {
|
||||
self.buf.len() - self.start
|
||||
} else {
|
||||
self.end - self.start
|
||||
};
|
||||
|
||||
trace!(" ringbuf: pop_buf {:?}..{:?}", self.start, self.start + n);
|
||||
&mut self.buf[self.start..self.start + n]
|
||||
}
|
||||
|
||||
pub fn pop(&mut self, n: usize) {
|
||||
trace!(" ringbuf: pop {:?}", n);
|
||||
if n == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
self.start = self.wrap(self.start + n);
|
||||
self.empty = self.start == self.end;
|
||||
}
|
||||
|
||||
fn wrap(&self, n: usize) -> usize {
|
||||
assert!(n <= self.buf.len());
|
||||
if n == self.buf.len() {
|
||||
0
|
||||
} else {
|
||||
n
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user