2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(bdma)]
|
|
|
|
mod bdma;
|
|
|
|
#[cfg(dma)]
|
|
|
|
mod dma;
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
mod dmamux;
|
2021-05-16 02:57:46 +02:00
|
|
|
|
2021-07-17 07:49:49 +02:00
|
|
|
#[cfg(dmamux)]
|
|
|
|
pub use dmamux::*;
|
|
|
|
|
2021-12-08 03:04:39 +01:00
|
|
|
use core::future::Future;
|
|
|
|
use core::marker::PhantomData;
|
|
|
|
use core::pin::Pin;
|
|
|
|
use core::task::Waker;
|
|
|
|
use core::task::{Context, Poll};
|
2021-07-15 05:42:06 +02:00
|
|
|
use embassy::util::Unborrow;
|
2021-12-08 03:04:39 +01:00
|
|
|
use embassy_hal_common::unborrow;
|
2021-05-25 04:17:24 +02:00
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
#[cfg(feature = "unstable-pac")]
|
|
|
|
pub use transfers::*;
|
|
|
|
|
|
|
|
#[cfg(not(feature = "unstable-pac"))]
|
|
|
|
pub(crate) use transfers::*;
|
|
|
|
|
2021-07-15 05:42:06 +02:00
|
|
|
#[cfg(any(bdma_v2, dma_v2, dmamux))]
|
|
|
|
pub type Request = u8;
|
|
|
|
#[cfg(not(any(bdma_v2, dma_v2, dmamux)))]
|
|
|
|
pub type Request = ();
|
|
|
|
|
|
|
|
pub(crate) mod sealed {
|
2021-11-19 19:15:55 +01:00
|
|
|
use super::*;
|
2021-12-08 03:04:39 +01:00
|
|
|
|
2021-12-08 03:18:15 +01:00
|
|
|
pub trait Word {}
|
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
pub trait Channel {
|
|
|
|
/// Starts this channel for writing a stream of words.
|
2021-12-08 03:18:15 +01:00
|
|
|
///
|
|
|
|
/// Safety:
|
|
|
|
/// - `buf` must be alive for the entire duration of the DMA transfer.
|
|
|
|
/// - `reg_addr` must be a valid peripheral register address to write to.
|
|
|
|
unsafe fn start_write<W: super::Word>(
|
|
|
|
&mut self,
|
|
|
|
request: Request,
|
|
|
|
buf: &[W],
|
|
|
|
reg_addr: *mut W,
|
|
|
|
);
|
2021-11-19 19:15:55 +01:00
|
|
|
|
|
|
|
/// Starts this channel for writing a word repeatedly.
|
2021-12-08 03:18:15 +01:00
|
|
|
///
|
|
|
|
/// Safety:
|
|
|
|
/// - `reg_addr` must be a valid peripheral register address to write to.
|
|
|
|
unsafe fn start_write_repeated<W: super::Word>(
|
2021-11-19 19:15:55 +01:00
|
|
|
&mut self,
|
|
|
|
request: Request,
|
|
|
|
repeated: W,
|
|
|
|
count: usize,
|
2021-12-08 03:18:15 +01:00
|
|
|
reg_addr: *mut W,
|
2021-11-19 19:15:55 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/// Starts this channel for reading a stream of words.
|
2021-12-08 03:18:15 +01:00
|
|
|
///
|
|
|
|
/// Safety:
|
|
|
|
/// - `buf` must be alive for the entire duration of the DMA transfer.
|
|
|
|
/// - `reg_addr` must be a valid peripheral register address to write to.
|
|
|
|
unsafe fn start_read<W: super::Word>(
|
2021-11-19 19:15:55 +01:00
|
|
|
&mut self,
|
|
|
|
request: Request,
|
2021-12-08 03:18:15 +01:00
|
|
|
reg_addr: *mut W,
|
2021-11-19 19:15:55 +01:00
|
|
|
buf: &mut [W],
|
|
|
|
);
|
|
|
|
|
2021-12-08 03:18:15 +01:00
|
|
|
/// Requests the channel to stop.
|
|
|
|
/// NOTE: The channel does not immediately stop, you have to wait
|
|
|
|
/// for `is_running() = false`.
|
2021-11-19 19:15:55 +01:00
|
|
|
fn request_stop(&mut self);
|
|
|
|
|
2021-12-08 03:18:15 +01:00
|
|
|
/// Returns whether this channel is running or stopped.
|
|
|
|
///
|
|
|
|
/// The channel stops running when it either completes or is manually stopped.
|
2021-12-08 01:51:39 +01:00
|
|
|
fn is_running(&self) -> bool;
|
2021-11-19 19:15:55 +01:00
|
|
|
|
|
|
|
/// Returns the total number of remaining transfers.
|
|
|
|
fn remaining_transfers(&mut self) -> u16;
|
|
|
|
|
2021-12-08 03:18:15 +01:00
|
|
|
/// Sets the waker that is called when this channel stops (either completed or manually stopped)
|
2021-11-19 19:15:55 +01:00
|
|
|
fn set_waker(&mut self, waker: &Waker);
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
pub enum WordSize {
|
|
|
|
OneByte,
|
|
|
|
TwoBytes,
|
|
|
|
FourBytes,
|
|
|
|
}
|
2021-12-08 03:18:15 +01:00
|
|
|
pub trait Word: sealed::Word {
|
2021-11-19 19:15:55 +01:00
|
|
|
fn bits() -> WordSize;
|
|
|
|
}
|
|
|
|
|
2021-12-08 03:18:15 +01:00
|
|
|
impl sealed::Word for u8 {}
|
2021-11-19 19:15:55 +01:00
|
|
|
impl Word for u8 {
|
|
|
|
fn bits() -> WordSize {
|
|
|
|
WordSize::OneByte
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
|
2021-12-08 03:18:15 +01:00
|
|
|
impl sealed::Word for u16 {}
|
2021-11-19 19:15:55 +01:00
|
|
|
impl Word for u16 {
|
|
|
|
fn bits() -> WordSize {
|
|
|
|
WordSize::TwoBytes
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 03:18:15 +01:00
|
|
|
|
|
|
|
impl sealed::Word for u32 {}
|
2021-11-19 19:15:55 +01:00
|
|
|
impl Word for u32 {
|
|
|
|
fn bits() -> WordSize {
|
|
|
|
WordSize::FourBytes
|
|
|
|
}
|
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
mod transfers {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[allow(unused)]
|
2021-12-08 03:04:39 +01:00
|
|
|
pub fn read<'a, W: Word>(
|
|
|
|
channel: impl Unborrow<Target = impl Channel> + 'a,
|
2021-07-15 05:42:06 +02:00
|
|
|
request: Request,
|
2021-12-08 03:18:15 +01:00
|
|
|
reg_addr: *mut W,
|
2021-11-19 19:15:55 +01:00
|
|
|
buf: &'a mut [W],
|
2021-12-08 03:04:39 +01:00
|
|
|
) -> impl Future<Output = ()> + 'a {
|
2021-11-19 19:15:55 +01:00
|
|
|
assert!(buf.len() <= 0xFFFF);
|
|
|
|
unborrow!(channel);
|
2021-07-15 05:42:06 +02:00
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
unsafe { channel.start_read::<W>(request, reg_addr, buf) };
|
2021-12-08 03:04:39 +01:00
|
|
|
|
|
|
|
Transfer {
|
|
|
|
channel,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
2021-12-08 03:04:39 +01:00
|
|
|
pub fn write<'a, W: Word>(
|
|
|
|
channel: impl Unborrow<Target = impl Channel> + 'a,
|
2021-07-15 05:42:06 +02:00
|
|
|
request: Request,
|
2021-11-19 19:15:55 +01:00
|
|
|
buf: &'a [W],
|
2021-12-08 03:18:15 +01:00
|
|
|
reg_addr: *mut W,
|
2021-12-08 03:04:39 +01:00
|
|
|
) -> impl Future<Output = ()> + 'a {
|
2021-11-19 19:15:55 +01:00
|
|
|
assert!(buf.len() <= 0xFFFF);
|
|
|
|
unborrow!(channel);
|
|
|
|
|
|
|
|
unsafe { channel.start_write::<W>(request, buf, reg_addr) };
|
2021-12-08 03:04:39 +01:00
|
|
|
|
|
|
|
Transfer {
|
|
|
|
channel,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
2021-07-20 21:20:16 +02:00
|
|
|
|
2021-11-19 19:15:55 +01:00
|
|
|
#[allow(unused)]
|
2021-12-08 03:04:39 +01:00
|
|
|
pub fn write_repeated<'a, W: Word>(
|
|
|
|
channel: impl Unborrow<Target = impl Channel> + 'a,
|
2021-07-20 21:20:16 +02:00
|
|
|
request: Request,
|
2021-11-19 19:15:55 +01:00
|
|
|
repeated: W,
|
|
|
|
count: usize,
|
2021-12-08 03:18:15 +01:00
|
|
|
reg_addr: *mut W,
|
2021-12-08 03:04:39 +01:00
|
|
|
) -> impl Future<Output = ()> + 'a {
|
2021-11-19 19:15:55 +01:00
|
|
|
unborrow!(channel);
|
|
|
|
|
|
|
|
unsafe { channel.start_write_repeated::<W>(request, repeated, count, reg_addr) };
|
2021-12-08 03:04:39 +01:00
|
|
|
|
|
|
|
Transfer {
|
|
|
|
channel,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
|
|
|
|
2021-12-08 03:04:39 +01:00
|
|
|
struct Transfer<'a, C: Channel> {
|
|
|
|
channel: C,
|
|
|
|
_phantom: PhantomData<&'a mut C>,
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2021-12-08 03:04:39 +01:00
|
|
|
impl<'a, C: Channel> Drop for Transfer<'a, C> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.channel.request_stop();
|
|
|
|
while self.channel.is_running() {}
|
|
|
|
}
|
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2021-12-08 03:04:39 +01:00
|
|
|
impl<'a, C: Channel> Unpin for Transfer<'a, C> {}
|
|
|
|
impl<'a, C: Channel> Future for Transfer<'a, C> {
|
|
|
|
type Output = ();
|
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
self.channel.set_waker(cx.waker());
|
|
|
|
if self.channel.is_running() {
|
2021-11-19 19:15:55 +01:00
|
|
|
Poll::Pending
|
2021-12-08 01:51:39 +01:00
|
|
|
} else {
|
|
|
|
Poll::Ready(())
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
2021-12-08 03:04:39 +01:00
|
|
|
}
|
2021-11-19 19:15:55 +01:00
|
|
|
}
|
2021-07-15 05:42:06 +02:00
|
|
|
}
|
|
|
|
|
2021-12-08 03:04:39 +01:00
|
|
|
pub trait Channel: sealed::Channel + Unborrow<Target = Self> + 'static {}
|
2021-11-19 19:15:55 +01:00
|
|
|
|
2021-07-15 05:42:06 +02:00
|
|
|
pub struct NoDma;
|
|
|
|
|
|
|
|
unsafe impl Unborrow for NoDma {
|
|
|
|
type Target = NoDma;
|
|
|
|
|
|
|
|
unsafe fn unborrow(self) -> Self::Target {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// safety: must be called only once at startup
|
|
|
|
pub(crate) unsafe fn init() {
|
|
|
|
#[cfg(bdma)]
|
|
|
|
bdma::init();
|
|
|
|
#[cfg(dma)]
|
|
|
|
dma::init();
|
|
|
|
#[cfg(dmamux)]
|
|
|
|
dmamux::init();
|
|
|
|
}
|