diff --git a/src/dma.rs b/src/dma.rs new file mode 100644 index 000000000..476f3e07a --- /dev/null +++ b/src/dma.rs @@ -0,0 +1,32 @@ +//! DMA Interface + +/// Static alias for DMA +pub type Static = &'static mut T; + +/// DMA Errors +#[derive(Debug)] +pub enum Error { + /// Previous data got overwritten before it could be read because it was + /// not accessed in a timely fashion + Overrun, + /// Transfer error + Transfer, +} + + +/// DMA Transfer future +pub trait Transfer { + /// Return type + type Item; + /// Return type + type Payload; + + /// Get buffer + fn deref(&self) -> &Self::Item; + + /// Check completion + fn is_done(&self) -> Result; + + /// Block + fn wait(self) -> Result<(Self::Item, Self::Payload), Error>; +} diff --git a/src/lib.rs b/src/lib.rs index 2fb100fc7..1b3c28e20 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -607,6 +607,7 @@ #![deny(missing_docs)] #![deny(warnings)] #![feature(never_type)] +#![feature(unsize)] #![no_std] #[macro_use] @@ -617,6 +618,7 @@ pub mod digital; pub mod prelude; pub mod serial; pub mod spi; +pub mod dma; /// Input capture /// diff --git a/src/spi.rs b/src/spi.rs index 07d775f94..85f75bcd4 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -1,6 +1,7 @@ //! Serial Peripheral Interface - +use core::marker::Unsize; use nb; +use dma; /// Full duplex (master mode) /// @@ -33,6 +34,46 @@ pub trait FullDuplex { fn send(&mut self, word: Word) -> nb::Result<(), Self::Error>; } +/// DMA Write mode +pub trait DmaWrite +where + Self: Sized, + B: Unsize<[Word]> + 'static +{ + /// Sends `words` to the slave. + type Transfer: dma::Transfer; + + /// Sends `words` to the slave. + fn send_dma(self, words: &'static mut B) -> Self::Transfer; +} + + +/// DMA Write mode +pub trait DmaRead { + /// Return type + type Transfer: dma::Transfer + ?Sized; + + /// Recieve `words` from the slave. + fn recieve_dma(self, words: &'static mut Buffer) -> Self::Transfer + where + Buffer: Unsize<[Word]>; +} + +/// DMA Write mode +pub trait DmaReadWrite { + /// Return type + type Transfer: dma::Transfer + ?Sized; + + /// Send and recieve from the slave. + fn transfer_dma( + self, + tx_words: &'static mut Buffer, + rx_words: &'static mut Buffer, + ) -> Self::Transfer + where + Buffer: Unsize<[Word]>; +} + /// Clock polarity #[derive(Clone, Copy, PartialEq)] pub enum Polarity {