From a26a373e2ca170fc86511a50673ec7c3bbf7cce6 Mon Sep 17 00:00:00 2001 From: David Sawatzke Date: Wed, 2 Jan 2019 21:18:54 +0100 Subject: [PATCH] Add non-blocking spi send trait --- src/prelude.rs | 2 ++ src/spi.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/prelude.rs b/src/prelude.rs index 4d17043ad..55f0b5d8d 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -28,6 +28,8 @@ pub use digital::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPi pub use serial::Read as _embedded_hal_serial_Read; pub use serial::Write as _embedded_hal_serial_Write; pub use spi::FullDuplex as _embedded_hal_spi_FullDuplex; +#[cfg(feature = "unproven")] +pub use spi::Send as _embedded_hal_spi_Send; pub use timer::CountDown as _embedded_hal_timer_CountDown; #[cfg(feature = "unproven")] pub use watchdog::Watchdog as _embedded_hal_watchdog_Watchdog; diff --git a/src/spi.rs b/src/spi.rs index 54753fbe1..4a2d7545b 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -26,6 +26,52 @@ pub trait FullDuplex { fn send(&mut self, word: Word) -> nb::Result<(), Self::Error>; } +/// Write (master mode) +/// +/// # Notes +/// +/// - It's the task of the user of this interface to manage the slave select lines +#[cfg(feature = "unproven")] +pub trait Send { + /// An enumeration of SPI errors + type Error; + + /// Sends a word to the slave + fn send(&mut self, word: Word) -> nb::Result<(), Self::Error>; +} + +/// Write (master mode) +#[cfg(feature = "unproven")] +pub mod full_duplex { + /// Default implementation of `spi::FullDuplex` for implementers of + /// `spi::Send` + /// + /// Also needs a `send` method to return the byte read during the last send + pub trait Default: ::spi::Send { + /// Reads the word stored in the shift register + /// + /// **NOTE** A word must be sent to the slave before attempting to call this + /// method. + fn read(&mut self) -> nb::Result; + } + + impl ::spi::FullDuplex for S + where + S: Default, + W: Clone, + { + type Error = S::Error; + + fn read(&mut self) -> nb::Result { + self.read() + } + + fn send(&mut self, word: W) -> nb::Result<(), Self::Error> { + self.send(word) + } + } +} + /// Clock polarity #[derive(Clone, Copy, PartialEq)] pub enum Polarity { @@ -75,4 +121,4 @@ pub const MODE_2: Mode = Mode { pub const MODE_3: Mode = Mode { polarity: Polarity::IdleHigh, phase: Phase::CaptureOnSecondTransition, -}; \ No newline at end of file +};