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.
initial non-working attemt for dma from psram on esp32s3
- Loading branch information
Showing
5 changed files
with
206 additions
and
5 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
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,126 @@ | ||
//! Uses DMA to copy psram to internal memory. | ||
//% FEATURES: esp-hal/log opsram-2m aligned | ||
//% CHIPS: esp32s3 | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
use aligned::{Aligned, A64}; | ||
use esp_backtrace as _; | ||
use esp_hal::{ | ||
clock::ClockControl, | ||
delay::Delay, | ||
dma::{Dma, DmaPriority, Mem2Mem}, | ||
dma_descriptors_chunk_size, | ||
peripherals::Peripherals, | ||
prelude::*, | ||
system::SystemControl, | ||
}; | ||
use log::{error, info}; | ||
extern crate alloc; | ||
|
||
const DATA_SIZE: usize = 1024 * 10; | ||
const CHUNK_SIZE: usize = 4032; // size is aligned to 64 bytes | ||
|
||
macro_rules! dma_buffer_aligned { | ||
($size:expr, $align:ty) => {{ | ||
static mut BUFFER: Aligned<$align, [u8; $size]> = Aligned([0; $size]); | ||
unsafe { &mut *BUFFER } | ||
}}; | ||
} | ||
|
||
macro_rules! dma_alloc_buffer { | ||
($size:expr, $align:expr) => {{ | ||
let layout = core::alloc::Layout::from_size_align($size, $align).unwrap(); | ||
unsafe { | ||
let ptr = alloc::alloc::alloc(layout); | ||
if ptr.is_null() { | ||
error!("make_buffers: alloc failed"); | ||
alloc::alloc::handle_alloc_error(layout); | ||
} | ||
core::slice::from_raw_parts_mut(ptr, $size) | ||
} | ||
}}; | ||
} | ||
|
||
#[global_allocator] | ||
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty(); | ||
|
||
fn init_heap(psram: impl esp_hal::peripheral::Peripheral<P = esp_hal::peripherals::PSRAM>) { | ||
esp_hal::psram::init_psram(psram); | ||
info!( | ||
"init_heap: start: 0x{:0x}", | ||
esp_hal::psram::psram_vaddr_start() | ||
); | ||
unsafe { | ||
ALLOCATOR.init( | ||
esp_hal::psram::psram_vaddr_start() as *mut u8, | ||
esp_hal::psram::PSRAM_BYTES, | ||
); | ||
} | ||
} | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
esp_println::logger::init_logger(log::LevelFilter::Info); | ||
|
||
let peripherals = Peripherals::take(); | ||
init_heap(peripherals.PSRAM); | ||
let system = SystemControl::new(peripherals.SYSTEM); | ||
let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); | ||
let delay = Delay::new(&clocks); | ||
|
||
let mut rx_buffer: &mut [u8] = dma_alloc_buffer!(DATA_SIZE, 64); | ||
let tx_buffer = dma_buffer_aligned!(DATA_SIZE, A64); | ||
let (tx_descriptors, rx_descriptors) = dma_descriptors_chunk_size!(DATA_SIZE, CHUNK_SIZE); | ||
|
||
let dma = Dma::new(peripherals.DMA); | ||
let channel = dma.channel0.configure(false, DmaPriority::Priority0); | ||
let dma_peripheral = peripherals.SPI2; | ||
|
||
let mut mem2mem = Mem2Mem::new_with_chunk_size( | ||
channel, | ||
dma_peripheral, | ||
tx_descriptors, | ||
rx_descriptors, | ||
CHUNK_SIZE, | ||
) | ||
.unwrap(); | ||
|
||
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()); | ||
} | ||
} |