Skip to content

Commit

Permalink
Add non-blocking spi send trait
Browse files Browse the repository at this point in the history
  • Loading branch information
david-sawatzke committed Jan 2, 2019
1 parent 9628ccc commit 4b8a646
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
46 changes: 46 additions & 0 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ pub trait FullDuplex<Word> {
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<Word> {
/// 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<W>` for implementers of
/// `spi::Send<W>`
///
/// Also needs a `send` method to return the bit read during the last send
pub trait Default<W>: ::spi::Send<W> {
/// 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<W, Self::Error>;
}

impl<W, S> ::spi::FullDuplex<W> for S
where
S: Default<W>,
W: Clone,
{
type Error = S::Error;

fn read(&mut self) -> nb::Result<W, Self::Error> {
self.read()
}

fn send(&mut self, word: W) -> nb::Result<(), Self::Error> {
self.send(word)
}
}
}

/// Clock polarity
#[derive(Clone, Copy, PartialEq)]
pub enum Polarity {
Expand Down

0 comments on commit 4b8a646

Please sign in to comment.