Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify dma usage #1238

Merged
merged 6 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixing `esp-wifi` + `TRNG` issue on `ESP32-S2` (#1272)

### Changed
- Prefer mutable references over moving for DMA transactions (#1238)

### Removed

Expand Down
61 changes: 17 additions & 44 deletions esp-hal/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,6 @@ pub enum Endianness {

#[cfg(any(esp32c3, esp32c6, esp32h2, esp32s3))]
pub mod dma {
use core::mem;

use embedded_dma::{ReadBuffer, WriteBuffer};

use crate::{
Expand Down Expand Up @@ -303,28 +301,22 @@ pub mod dma {
}

/// An in-progress DMA transfer
pub struct AesDmaTransferRxTx<'d, C, RBUFFER, TBUFFER>
#[must_use]
pub struct AesDmaTransferRxTx<'t, 'd, C>
where
C: ChannelTypes,
C::P: AesPeripheral,
{
aes_dma: AesDma<'d, C>,
rbuffer: RBUFFER,
tbuffer: TBUFFER,
aes_dma: &'t mut AesDma<'d, C>,
}

impl<'d, C, RXBUF, TXBUF> DmaTransferRxTx<RXBUF, TXBUF, AesDma<'d, C>>
for AesDmaTransferRxTx<'d, C, RXBUF, TXBUF>
impl<'t, 'd, C> DmaTransferRxTx for AesDmaTransferRxTx<'t, 'd, C>
where
C: ChannelTypes,
C::P: AesPeripheral,
{
/// Wait for the DMA transfer to complete and return the buffers and the
/// AES instance.
fn wait(
self,
) -> Result<(RXBUF, TXBUF, AesDma<'d, C>), (DmaError, RXBUF, TXBUF, AesDma<'d, C>)>
{
/// Wait for the DMA transfer to complete
fn wait(self) -> Result<(), DmaError> {
// Waiting for the DMA transfer is not enough. We need to wait for the
// peripheral to finish flushing its buffers, too.
while self.aes_dma.aes.aes.state().read().state().bits() != 2 // DMA status DONE == 2
Expand All @@ -335,25 +327,10 @@ pub mod dma {

self.aes_dma.finish_transform();

let err = self.aes_dma.channel.rx.has_error() || self.aes_dma.channel.tx.has_error();

// `DmaTransferRxTx` needs to have a `Drop` implementation, because we accept
// managed buffers that can free their memory on drop. Because of that
// we can't move out of the `DmaTransferRxTx`'s fields, so we use `ptr::read`
// and `mem::forget`.
//
// NOTE(unsafe) There is no panic branch between getting the resources
// and forgetting `self`.
unsafe {
let rbuffer = core::ptr::read(&self.rbuffer);
let tbuffer = core::ptr::read(&self.tbuffer);
let payload = core::ptr::read(&self.aes_dma);
mem::forget(self);
if err {
Err((DmaError::DescriptorError, rbuffer, tbuffer, payload))
} else {
Ok((rbuffer, tbuffer, payload))
}
if self.aes_dma.channel.rx.has_error() || self.aes_dma.channel.tx.has_error() {
Err(DmaError::DescriptorError)
} else {
Ok(())
}
}

Expand All @@ -364,7 +341,7 @@ pub mod dma {
}
}

impl<'d, C, RXBUF, TXBUF> Drop for AesDmaTransferRxTx<'d, C, RXBUF, TXBUF>
impl<'t, 'd, C> Drop for AesDmaTransferRxTx<'t, 'd, C>
where
C: ChannelTypes,
C::P: AesPeripheral,
Expand Down Expand Up @@ -409,14 +386,14 @@ pub mod dma {
/// This will return a [AesDmaTransferRxTx] owning the buffer(s) and the
/// AES instance. The maximum amount of data to be sent/received
/// is 32736 bytes.
pub fn process<TXBUF, RXBUF>(
mut self,
words: TXBUF,
mut read_buffer: RXBUF,
pub fn process<'t, TXBUF, RXBUF>(
&'t mut self,
words: &'t TXBUF,
read_buffer: &'t mut RXBUF,
mode: Mode,
cipher_mode: CipherMode,
key: [u8; 16],
) -> Result<AesDmaTransferRxTx<'d, C, RXBUF, TXBUF>, crate::dma::DmaError>
) -> Result<AesDmaTransferRxTx<'t, 'd, C>, crate::dma::DmaError>
where
TXBUF: ReadBuffer<Word = u8>,
RXBUF: WriteBuffer<Word = u8>,
Expand All @@ -434,11 +411,7 @@ pub mod dma {
key,
)?;

Ok(AesDmaTransferRxTx {
aes_dma: self,
rbuffer: read_buffer,
tbuffer: words,
})
Ok(AesDmaTransferRxTx { aes_dma: self })
}

#[allow(clippy::too_many_arguments)]
Expand Down
8 changes: 4 additions & 4 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,18 +1298,18 @@ where

/// Trait to be implemented for an in progress dma transfer.
#[allow(drop_bounds)]
pub trait DmaTransfer<B, T>: Drop {
pub trait DmaTransfer: Drop {
/// Wait for the transfer to finish.
fn wait(self) -> Result<(B, T), (DmaError, B, T)>;
fn wait(self) -> Result<(), DmaError>;
/// Check if the transfer is finished.
fn is_done(&self) -> bool;
}

/// Trait to be implemented for an in progress dma transfer.
#[allow(clippy::type_complexity, drop_bounds)]
pub trait DmaTransferRxTx<BR, BT, T>: Drop {
pub trait DmaTransferRxTx: Drop {
/// Wait for the transfer to finish.
fn wait(self) -> Result<(BR, BT, T), (DmaError, BR, BT, T)>;
fn wait(self) -> Result<(), DmaError>;
/// Check if the transfer is finished.
fn is_done(&self) -> bool;
}
Expand Down
Loading
Loading