forked from esp-rs/esp-hal
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dma: add Mem2Mem to support memory to memory transfer
- Loading branch information
Showing
4 changed files
with
289 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
//! Uses DMA to copy memory to memory. | ||
//! | ||
//% FEATURES: esp-hal/log | ||
//% CHIPS: esp32s3 | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
use esp_backtrace as _; | ||
use esp_hal::clock::ClockControl; | ||
use esp_hal::delay::Delay; | ||
use esp_hal::dma::Dma; | ||
use esp_hal::dma::DmaPriority; | ||
use esp_hal::dma::Mem2Mem; | ||
use esp_hal::dma_buffers; | ||
use esp_hal::peripherals::Peripherals; | ||
use esp_hal::prelude::*; | ||
use esp_hal::system::SystemControl; | ||
use log::info; | ||
use log::error; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
esp_println::logger::init_logger(log::LevelFilter::Info); | ||
|
||
let peripherals = Peripherals::take(); | ||
let system = SystemControl::new(peripherals.SYSTEM); | ||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | ||
let delay = Delay::new(&clocks); | ||
|
||
const DATA_SIZE: usize = 1024*100; | ||
let (tx_buffer, tx_descriptors, mut rx_buffer, rx_descriptors) = dma_buffers!(DATA_SIZE); | ||
|
||
let dma = Dma::new(peripherals.DMA); | ||
let channel = dma.channel0.configure( | ||
false, | ||
DmaPriority::Priority0, | ||
); | ||
|
||
let mut mem2mem = Mem2Mem::new(channel, tx_descriptors, rx_descriptors); | ||
|
||
for i in 0..core::mem::size_of_val(tx_buffer) { | ||
tx_buffer[i] = (i % 256) as u8; | ||
} | ||
|
||
info!("Starting transfer of {} bytes", DATA_SIZE); | ||
let result = mem2mem.start_transfer(&tx_buffer, &mut rx_buffer); | ||
match result { | ||
Ok(dma_wait) => { | ||
info!("Transfer started"); | ||
dma_wait.wait().unwrap(); | ||
info!("Transfer completed, comparing buffer"); | ||
let mut error = false; | ||
for i in 0..core::mem::size_of_val(tx_buffer) { | ||
if rx_buffer[i] != tx_buffer[i] { | ||
error!( | ||
"Error: tx_buffer[{}] = {}, rx_buffer[{}] = {}", | ||
i, tx_buffer[i], i, rx_buffer[i] | ||
); | ||
error = true; | ||
break; | ||
} | ||
} | ||
if !error { | ||
info!("Buffers are equal"); | ||
} | ||
info!("Done"); | ||
}, | ||
Err(e) => { | ||
error!("start_transfer: Error: {:?}", e); | ||
} | ||
} | ||
|
||
loop { | ||
delay.delay(2.secs()); | ||
} | ||
} |