Skip to content

Commit

Permalink
DMA: Don't require implementors of Read/WriteBuffer to be Sealed (#1921)
Browse files Browse the repository at this point in the history
* DMA: Don't require implementors of Read/WriteBuffer to be Sealed

* CHANGELOG

* mark dma::ReadBuffer and dma::WriteBuffer traits unsafe
  • Loading branch information
liebman authored Aug 13, 2024
1 parent a3a3ef1 commit 23f76b0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,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

0 comments on commit 23f76b0

Please sign in to comment.