Skip to content

Commit

Permalink
Mem2Mem::new now accepts the peripheral to use
Browse files Browse the repository at this point in the history
  • Loading branch information
liebman committed Jun 28, 2024
1 parent fdfbdbc commit efa4c89
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
23 changes: 15 additions & 8 deletions esp-hal/src/dma/gdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ mod m2m {

use super::dma_private::{DmaSupport, DmaSupportRx};
use crate::dma::{
Channel,
ChannelTypes,
DescriptorChain,
DmaDescriptor,
Expand All @@ -641,9 +642,10 @@ mod m2m {
C: ChannelTypes,
MODE: crate::Mode,
{
channel: crate::dma::Channel<'d, C, MODE>,
channel: Channel<'d, C, MODE>,
tx_chain: DescriptorChain,
rx_chain: DescriptorChain,
peripheral: DmaPeripheral,
}

impl<'d, C, MODE> Mem2Mem<'d, C, MODE>
Expand All @@ -652,15 +654,22 @@ mod m2m {
MODE: crate::Mode,
{
/// Create a new Mem2Mem instance.
///
/// SAFTEY:
///
/// TODO: Add safety requirements dealing with the DmaPeripheral value
/// chosen on the esp32s3 (and maybe others)
pub fn new(
mut channel: crate::dma::Channel<'d, C, MODE>,
mut channel: Channel<'d, C, MODE>,
peripheral: DmaPeripheral,
tx_descriptors: &'static mut [DmaDescriptor],
rx_descriptors: &'static mut [DmaDescriptor],
) -> Self {
channel.tx.init_channel();
channel.rx.init_channel();
Mem2Mem {
channel,
peripheral,
tx_chain: DescriptorChain::new(tx_descriptors),
rx_chain: DescriptorChain::new(rx_descriptors),
}
Expand All @@ -683,15 +692,13 @@ mod m2m {
unsafe {
self.channel
.tx
.prepare_transfer_without_start(DmaPeripheral::Mem2Mem, &self.tx_chain)?;
.prepare_transfer_without_start(self.peripheral, &self.tx_chain)?;
self.channel
.rx
.prepare_transfer_without_start(DmaPeripheral::Mem2Mem, &self.rx_chain)?;
}
let result = self.channel.tx.start_transfer();
if let Err(err) = result {
return Err(err);
.prepare_transfer_without_start(self.peripheral, &self.rx_chain)?;
self.channel.rx.set_mem2mem_mode();
}
self.channel.tx.start_transfer()?;
self.channel.rx.start_transfer()?;
Ok(DmaTransferRx::new(self))
}
Expand Down
33 changes: 8 additions & 25 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,9 @@ pub trait RxPrivate: crate::private::Sealed {

fn start_transfer(&mut self) -> Result<(), DmaError>;

#[cfg(gdma)]
fn set_mem2mem_mode(&mut self);

fn listen_ch_in_done(&self);

fn clear_ch_in_done(&self);
Expand Down Expand Up @@ -962,19 +965,6 @@ where
R::clear_in_interrupts();
R::reset_in();
R::set_in_descriptors(descriptors.first() as u32);
#[cfg(gdma)]
if peri == DmaPeripheral::Mem2Mem {
// Mem2Mem mode does not require a peripheral but if we leave this set to the
// default reset value (63) the DMA will not start and issues a
// descriptor error. So we set it to 0 to avoid this.
// TODO: we need to find safe a way to pass this value in.
// see: https://github.com/esp-rs/esp-hal/pull/1738
R::set_in_peripheral(0);
R::set_mem2mem_mode();
} else {
R::set_out_peripheral(peri as u8);
}
#[cfg(not(gdma))]
R::set_in_peripheral(peri as u8);

Ok(())
Expand Down Expand Up @@ -1069,6 +1059,11 @@ where
self.rx_impl.start_transfer()
}

#[cfg(gdma)]
fn set_mem2mem_mode(&mut self) {
R::set_mem2mem_mode();
}

fn listen_ch_in_done(&self) {
R::listen_ch_in_done();
}
Expand Down Expand Up @@ -1240,18 +1235,6 @@ where
R::clear_out_interrupts();
R::reset_out();
R::set_out_descriptors(descriptors.first() as u32);
#[cfg(gdma)]
if peri == DmaPeripheral::Mem2Mem {
// Mem2Mem mode does not require a peripheral but if we leave this set to the
// default reset value (63) the DMA will not start and issues a
// descriptor error. So we set it to 0 to avoid this.
// TODO: we need to find safe a way to pass this value in.
// see: https://github.com/esp-rs/esp-hal/pull/1738
R::set_out_peripheral(0);
} else {
R::set_out_peripheral(peri as u8);
}
#[cfg(not(gdma))]
R::set_out_peripheral(peri as u8);

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/dma_mem2mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
delay::Delay,
dma::{Dma, DmaPriority, Mem2Mem},
dma::{Dma, DmaPeripheral, DmaPriority, Mem2Mem},
dma_buffers,
peripherals::Peripherals,
prelude::*,
Expand All @@ -34,7 +34,7 @@ fn main() -> ! {
let dma = Dma::new(peripherals.DMA);
let channel = dma.channel0.configure(false, DmaPriority::Priority0);

let mut mem2mem = Mem2Mem::new(channel, tx_descriptors, rx_descriptors);
let mut mem2mem = Mem2Mem::new(channel, DmaPeripheral::Adc, tx_descriptors, rx_descriptors);

for i in 0..core::mem::size_of_val(tx_buffer) {
tx_buffer[i] = (i % 256) as u8;
Expand Down

0 comments on commit efa4c89

Please sign in to comment.