diff --git a/CHANGELOG.md b/CHANGELOG.md index 735815926f..b082f77df8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Added `Deque::make_contiguous`. - Added `VecView`, the `!Sized` version of `Vec`. - Added pool implementations for 64-bit architectures. +- Add `embedded_dma` feature to one can send `Vec`, `pool::object::Object` and `pool::boxed::Box` to DMA as read/write buffers [#362]. ### Changed diff --git a/Cargo.toml b/Cargo.toml index 05e42505a0..bdf5b022da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,10 @@ serde = { version = "1", optional = true, default-features = false } ufmt-write = { version = "0.1", optional = true } defmt = { version = ">=0.2.0,<0.4", optional = true } +[dependencies.embedded-dma] +version = "0.2" +optional = true + # for the pool module [target.'cfg(any(target_arch = "arm", target_pointer_width = "32", target_pointer_width = "64"))'.dependencies] stable_deref_trait = { version = "1", default-features = false } diff --git a/src/vec.rs b/src/vec.rs index 0361d41c56..5796f766ab 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -2076,6 +2076,32 @@ where } } +#[cfg(feature = "embedded-dma")] +unsafe impl embedded_dma::ReadTarget for Vec +where + T: embedded_dma::ReadTarget, +{ + type Word = T::Word; + + // Replace default implementation to return self.len() as buffer size + fn as_read_buffer(&self) -> (*const Self::Word, usize) { + (self.as_ptr() as *const T::Word, self.len) + } +} + +#[cfg(feature = "embedded-dma")] +unsafe impl embedded_dma::WriteTarget for Vec +where + T: embedded_dma::WriteTarget, +{ + type Word = T::Word; + + // Replace default implementation to return N as buffer size + fn as_write_buffer(&mut self) -> (*mut Self::Word, usize) { + (self.as_mut_ptr() as *mut T::Word, N) + } +} + #[cfg(test)] mod tests { use core::fmt::Write;