Skip to content

Commit

Permalink
Do not implement DmaTxRxBuf for references
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Sep 24, 2024
1 parent 35dadd5 commit bf8993f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 28 deletions.
32 changes: 26 additions & 6 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1872,13 +1872,23 @@ pub trait DmaTxBuffer {
fn length(&self) -> usize;
}

impl<T: DmaTxBuffer> DmaTxBuffer for &mut T {
pub(crate) struct DmaTxBufferRef<'a, B: DmaTxBuffer> {
buffer: &'a mut B,
}

impl<'a, B: DmaTxBuffer> DmaTxBufferRef<'a, B> {
pub(crate) fn new(buffer: &'a mut B) -> Self {
Self { buffer }
}
}

impl<B: DmaTxBuffer> DmaTxBuffer for DmaTxBufferRef<'_, B> {
fn prepare(&mut self) -> Preparation {
(**self).prepare()
self.buffer.prepare()
}

fn length(&self) -> usize {
(**self).length()
self.buffer.length()
}
}

Expand All @@ -1902,13 +1912,23 @@ pub trait DmaRxBuffer {
fn length(&self) -> usize;
}

impl<T: DmaRxBuffer> DmaRxBuffer for &mut T {
pub(crate) struct DmaRxBufferRef<'a, B: DmaRxBuffer> {
buffer: &'a mut B,
}

impl<'a, B: DmaRxBuffer> DmaRxBufferRef<'a, B> {
pub(crate) fn new(buffer: &'a mut B) -> Self {
Self { buffer }
}
}

impl<B: DmaRxBuffer> DmaRxBuffer for DmaRxBufferRef<'_, B> {
fn prepare(&mut self) -> Preparation {
(**self).prepare()
self.buffer.prepare()
}

fn length(&self) -> usize {
(**self).length()
self.buffer.length()
}
}

Expand Down
51 changes: 29 additions & 22 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,8 +880,10 @@ mod dma {
DmaChannel,
DmaRxBuf,
DmaRxBuffer,
DmaRxBufferRef,
DmaTxBuf,
DmaTxBuffer,
DmaTxBufferRef,
RxPrivate,
Spi2Peripheral,
SpiPeripheral,
Expand Down Expand Up @@ -1718,7 +1720,8 @@ mod dma {
for chunk in words.chunks_mut(self.rx_buf.capacity()) {
self.rx_buf.set_length(chunk.len());

match (&mut self.spi_dma).do_dma_read(&mut self.rx_buf) {
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
match (&mut self.spi_dma).do_dma_read(rx_buffer) {
Ok(transfer) => transfer.wait(),
Err((e, _, _)) => return Err(e),
};
Expand All @@ -1735,7 +1738,8 @@ mod dma {
for chunk in words.chunks(self.tx_buf.capacity()) {
self.tx_buf.fill(chunk);

match (&mut self.spi_dma).do_dma_write(&mut self.tx_buf) {
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
match (&mut self.spi_dma).do_dma_write(tx_buffer) {
Ok(transfer) => transfer.wait(),
Err((e, _, _)) => return Err(e),
};
Expand All @@ -1759,7 +1763,9 @@ mod dma {
self.tx_buf.fill(write_chunk);
self.rx_buf.set_length(read_chunk.len());

match (&mut self.spi_dma).do_dma_transfer(&mut self.rx_buf, &mut self.tx_buf) {
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
match (&mut self.spi_dma).do_dma_transfer(rx_buffer, tx_buffer) {
Ok(transfer) => transfer.wait(),
Err((e, _, _, _)) => return Err(e),
};
Expand All @@ -1785,7 +1791,9 @@ mod dma {
self.tx_buf.fill(chunk);
self.rx_buf.set_length(chunk.len());

match (&mut self.spi_dma).do_dma_transfer(&mut self.rx_buf, &mut self.tx_buf) {
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
match (&mut self.spi_dma).do_dma_transfer(rx_buffer, tx_buffer) {
Ok(transfer) => transfer.wait(),
Err((e, _, _, _)) => return Err(e),
};
Expand Down Expand Up @@ -1821,13 +1829,9 @@ mod dma {
}
self.rx_buf.set_length(buffer.len());

match (&mut self.spi_dma).do_half_duplex_read(
data_mode,
cmd,
address,
dummy,
&mut self.rx_buf,
) {
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
match (&mut self.spi_dma).do_half_duplex_read(data_mode, cmd, address, dummy, rx_buffer)
{
Ok(transfer) => transfer.wait(),
Err((e, _, _)) => return Err(e),
};
Expand All @@ -1852,13 +1856,10 @@ mod dma {
}
self.tx_buf.fill(buffer);

match (&mut self.spi_dma).do_half_duplex_write(
data_mode,
cmd,
address,
dummy,
&mut self.tx_buf,
) {
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
match (&mut self.spi_dma)
.do_half_duplex_write(data_mode, cmd, address, dummy, tx_buffer)
{
Ok(transfer) => {
transfer.wait();

Expand Down Expand Up @@ -1918,7 +1919,8 @@ mod dma {
for chunk in words.chunks_mut(chunk_size) {
self.rx_buf.set_length(chunk.len());

match (&mut self.spi_dma).do_dma_read(&mut self.rx_buf) {
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
match (&mut self.spi_dma).do_dma_read(rx_buffer) {
Ok(mut transfer) => transfer.wait_for_done().await,
Err((e, _, _)) => return Err(e),
}
Expand All @@ -1937,7 +1939,8 @@ mod dma {
for chunk in words.chunks(chunk_size) {
self.tx_buf.fill(chunk);

match (&mut self.spi_dma).do_dma_write(&mut self.tx_buf) {
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
match (&mut self.spi_dma).do_dma_write(tx_buffer) {
Ok(mut transfer) => transfer.wait_for_done().await,
Err((e, _, _)) => return Err(e),
}
Expand Down Expand Up @@ -1966,7 +1969,9 @@ mod dma {
self.tx_buf.fill(write_chunk);
self.rx_buf.set_length(read_chunk.len());

match (&mut self.spi_dma).do_dma_transfer(&mut self.rx_buf, &mut self.tx_buf) {
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
match (&mut self.spi_dma).do_dma_transfer(rx_buffer, tx_buffer) {
Ok(mut transfer) => transfer.wait_for_done().await,
Err((e, _, _, _)) => return Err(e),
}
Expand All @@ -1991,7 +1996,9 @@ mod dma {
self.tx_buf.fill(chunk);
self.rx_buf.set_length(chunk.len());

match (&mut self.spi_dma).do_dma_transfer(&mut self.rx_buf, &mut self.tx_buf) {
let rx_buffer = DmaRxBufferRef::new(&mut self.rx_buf);
let tx_buffer = DmaTxBufferRef::new(&mut self.tx_buf);
match (&mut self.spi_dma).do_dma_transfer(rx_buffer, tx_buffer) {
Ok(mut transfer) => transfer.wait_for_done().await,
Err((e, _, _, _)) => return Err(e),
}
Expand Down

0 comments on commit bf8993f

Please sign in to comment.