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

DMA: Don't require implementors of Read/WriteBuffer to be Sealed #1921

Merged
merged 3 commits into from
Aug 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
2 changes: 1 addition & 1 deletion esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Peripheral driver constructors don't take `InterruptHandler`s anymore. Use `set_interrupt_handler` to explicitly set the interrupt handler now. (#1819)
- Use the peripheral ref pattern for `OneShotTimer` and `PeriodicTimer` (#1855)

- DMA: don't require `Sealed` to implement `ReadBuffer` and `WriteBuffer` (#1921)
- Allow DMA to/from psram for esp32s3 (#1827)
- DMA buffers now don't require a static lifetime. Make sure to never `mem::forget` an in-progress DMA transfer (consider using `#[deny(clippy::mem_forget)]`) (#1837)

Expand Down
32 changes: 22 additions & 10 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ impl<W> crate::private::Sealed for &[W] where W: Word {}
impl<W> crate::private::Sealed for &mut [W] where W: Word {}

/// Trait for buffers that can be given to DMA for reading.
pub trait ReadBuffer: crate::private::Sealed {
///
/// # Safety
///
/// Once the `read_buffer` method has been called, it is unsafe to call any
/// `&mut self` methods on this object as long as the returned value is in use
/// (by DMA).
pub unsafe trait ReadBuffer {
/// Provide a buffer usable for DMA reads.
///
/// The return value is:
Expand All @@ -94,7 +100,7 @@ pub trait ReadBuffer: crate::private::Sealed {
unsafe fn read_buffer(&self) -> (*const u8, usize);
}

impl<W, const S: usize> ReadBuffer for [W; S]
unsafe impl<W, const S: usize> ReadBuffer for [W; S]
where
W: Word,
{
Expand All @@ -103,7 +109,7 @@ where
}
}

impl<W, const S: usize> ReadBuffer for &[W; S]
unsafe impl<W, const S: usize> ReadBuffer for &[W; S]
where
W: Word,
{
Expand All @@ -112,7 +118,7 @@ where
}
}

impl<W, const S: usize> ReadBuffer for &mut [W; S]
unsafe impl<W, const S: usize> ReadBuffer for &mut [W; S]
where
W: Word,
{
Expand All @@ -121,7 +127,7 @@ where
}
}

impl<W> ReadBuffer for &[W]
unsafe impl<W> ReadBuffer for &[W]
where
W: Word,
{
Expand All @@ -130,7 +136,7 @@ where
}
}

impl<W> ReadBuffer for &mut [W]
unsafe impl<W> ReadBuffer for &mut [W]
where
W: Word,
{
Expand All @@ -140,7 +146,13 @@ where
}

/// Trait for buffers that can be given to DMA for writing.
pub trait WriteBuffer: crate::private::Sealed {
///
/// # Safety
///
/// Once the `write_buffer` method has been called, it is unsafe to call any
/// `&mut self` methods, except for `write_buffer`, on this object as long as
/// the returned value is in use (by DMA).
pub unsafe trait WriteBuffer {
/// Provide a buffer usable for DMA writes.
///
/// The return value is:
Expand All @@ -156,7 +168,7 @@ pub trait WriteBuffer: crate::private::Sealed {
unsafe fn write_buffer(&mut self) -> (*mut u8, usize);
}

impl<W, const S: usize> WriteBuffer for [W; S]
unsafe impl<W, const S: usize> WriteBuffer for [W; S]
where
W: Word,
{
Expand All @@ -165,7 +177,7 @@ where
}
}

impl<W, const S: usize> WriteBuffer for &mut [W; S]
unsafe impl<W, const S: usize> WriteBuffer for &mut [W; S]
where
W: Word,
{
Expand All @@ -174,7 +186,7 @@ where
}
}

impl<W> WriteBuffer for &mut [W]
unsafe impl<W> WriteBuffer for &mut [W]
where
W: Word,
{
Expand Down