use core::pin::Pin; use core::sync::atomic::{compiler_fence, Ordering}; use core::task::{Context, Poll}; use embassy_hal_common::{impl_peripheral, into_ref, Peripheral, PeripheralRef}; use futures::Future; use crate::pac::dma::vals; use crate::{pac, peripherals}; pub fn copy<'a, C: Channel, W: Word>(ch: impl Peripheral
+ 'a, from: &[W], to: &mut [W]) -> Transfer<'a, C> { assert!(from.len() == to.len()); into_ref!(ch); unsafe { let p = ch.regs(); p.read_addr().write_value(from.as_ptr() as u32); p.write_addr().write_value(to.as_mut_ptr() as u32); p.trans_count().write_value(from.len() as u32); compiler_fence(Ordering::SeqCst); p.ctrl_trig().write(|w| { w.set_data_size(W::size()); w.set_incr_read(true); w.set_incr_write(true); w.set_chain_to(ch.number()); w.set_en(true); }); compiler_fence(Ordering::SeqCst); } Transfer::new(ch) } pub(crate) struct Transfer<'a, C: Channel> { channel: PeripheralRef<'a, C>, } impl<'a, C: Channel> Transfer<'a, C> { pub(crate) fn new(channel: impl Peripheral
+ 'a) -> Self {
into_ref!(channel);
Self { channel }
}
}
impl<'a, C: Channel> Drop for Transfer<'a, C> {
fn drop(&mut self) {
let p = self.channel.regs();
unsafe {
p.ctrl_trig().write(|w| w.set_en(false));
while p.ctrl_trig().read().busy() {}
}
}
}
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 + sealed::Channel + Into