Skip to content

Commit

Permalink
Allow users to only specify the relevant burst config
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Dec 5, 2024
1 parent 5c033d7 commit e3c4f84
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
43 changes: 38 additions & 5 deletions esp-hal/src/dma/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,24 @@ cfg_if::cfg_if! {
Self::DEFAULT
}
}

impl From<InternalBurstConfig> for BurstConfig {
fn from(internal_memory: InternalBurstConfig) -> Self {
Self {
external_memory: ExternalBurstConfig::DEFAULT,
internal_memory,
}
}
}

impl From<ExternalBurstConfig> for BurstConfig {
fn from(external_memory: ExternalBurstConfig) -> Self {
Self {
external_memory,
internal_memory: InternalBurstConfig::DEFAULT,
}
}
}
} else {
/// Burst transfer configuration.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
Expand Down Expand Up @@ -462,12 +480,12 @@ impl DmaTxBuf {
pub fn new_with_config(
descriptors: &'static mut [DmaDescriptor],
buffer: &'static mut [u8],
config: BurstConfig,
config: impl Into<BurstConfig>,
) -> Result<Self, DmaBufError> {
let mut buf = Self {
descriptors: DescriptorSet::new(descriptors)?,
buffer,
burst: config,
burst: BurstConfig::default(),
};

let capacity = buf.capacity();
Expand All @@ -476,7 +494,12 @@ impl DmaTxBuf {
Ok(buf)
}

fn configure(&mut self, burst: BurstConfig, length: usize) -> Result<(), DmaBufError> {
fn configure(
&mut self,
burst: impl Into<BurstConfig>,
length: usize,
) -> Result<(), DmaBufError> {
let burst = burst.into();
self.set_length_fallible(length, burst)?;

self.descriptors.link_with_buffer(
Expand Down Expand Up @@ -632,7 +655,12 @@ impl DmaRxBuf {
Ok(buf)
}

fn configure(&mut self, burst: BurstConfig, length: usize) -> Result<(), DmaBufError> {
fn configure(
&mut self,
burst: impl Into<BurstConfig>,
length: usize,
) -> Result<(), DmaBufError> {
let burst = burst.into();
self.set_length_fallible(length, burst)?;

self.descriptors.link_with_buffer(
Expand Down Expand Up @@ -822,7 +850,12 @@ impl DmaRxTxBuf {
Ok(buf)
}

fn configure(&mut self, burst: BurstConfig, length: usize) -> Result<(), DmaBufError> {
fn configure(
&mut self,
burst: impl Into<BurstConfig>,
length: usize,
) -> Result<(), DmaBufError> {
let burst = burst.into();
self.set_length_fallible(length, burst)?;

self.rx_descriptors.link_with_buffer(
Expand Down
13 changes: 3 additions & 10 deletions examples/src/bin/spi_loopback_dma_psram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use esp_backtrace as _;
use esp_hal::{
delay::Delay,
dma::{BurstConfig, DmaRxBuf, DmaTxBuf, ExternalBurstConfig},
dma::{DmaRxBuf, DmaTxBuf, ExternalBurstConfig},
peripheral::Peripheral,
prelude::*,
spi::{
Expand Down Expand Up @@ -76,15 +76,8 @@ fn main() -> ! {
tx_buffer.len(),
tx_descriptors.len()
);
let mut dma_tx_buf = DmaTxBuf::new_with_config(
tx_descriptors,
tx_buffer,
BurstConfig {
external: DMA_ALIGNMENT,
..BurstConfig::default()
},
)
.unwrap();
let mut dma_tx_buf =
DmaTxBuf::new_with_config(tx_descriptors, tx_buffer, DMA_ALIGNMENT).unwrap();
let (rx_buffer, rx_descriptors, _, _) = esp_hal::dma_buffers!(DMA_BUFFER_SIZE, 0);
info!(
"RX: {:p} len {} ({} descripters)",
Expand Down
22 changes: 3 additions & 19 deletions hil-test/tests/spi_half_duplex_write_psram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use defmt::error;
use esp_alloc as _;
use esp_hal::{
dma::{BurstConfig, DmaRxBuf, DmaTxBuf, ExternalBurstConfig, InternalBurstConfig},
dma::{DmaRxBuf, DmaTxBuf, ExternalBurstConfig},
dma_buffers,
dma_descriptors_chunk_size,
gpio::interconnect::InputSignal,
Expand Down Expand Up @@ -88,15 +88,7 @@ mod tests {

let (_, descriptors) = dma_descriptors_chunk_size!(0, DMA_BUFFER_SIZE, DMA_CHUNK_SIZE);
let buffer = dma_alloc_buffer!(DMA_BUFFER_SIZE, DMA_ALIGNMENT as usize);
let mut dma_tx_buf = DmaTxBuf::new_with_config(
descriptors,
buffer,
BurstConfig {
internal: InternalBurstConfig::default(),
external: ExternalBurstConfig::Size32,
},
)
.unwrap();
let mut dma_tx_buf = DmaTxBuf::new_with_config(descriptors, buffer, DMA_ALIGNMENT).unwrap();

let unit = ctx.pcnt_unit;
let mut spi = ctx.spi;
Expand Down Expand Up @@ -146,15 +138,7 @@ mod tests {

let (_, descriptors) = dma_descriptors_chunk_size!(0, DMA_BUFFER_SIZE, DMA_CHUNK_SIZE);
let buffer = dma_alloc_buffer!(DMA_BUFFER_SIZE, DMA_ALIGNMENT as usize);
let dma_tx_buf = DmaTxBuf::new_with_config(
descriptors,
buffer,
BurstConfig {
internal: InternalBurstConfig::default(),
external: ExternalBurstConfig::Size32,
},
)
.unwrap();
let dma_tx_buf = DmaTxBuf::new_with_config(descriptors, buffer, DMA_ALIGNMENT).unwrap();

let (rx, rxd, _, _) = dma_buffers!(1, 0);
let dma_rx_buf = DmaRxBuf::new(rxd, rx).unwrap();
Expand Down

0 comments on commit e3c4f84

Please sign in to comment.