Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-blocking spi::Send trait #121

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 33 additions & 1 deletion src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@ 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
///
/// - The slave select line shouldn't be released before the `flush` call succeeds
#[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>;

/// Ensures that none of the previously written words are still buffered
fn completed(&mut self) -> nb::Result<(), Self::Error>;
}

/// Write (master mode)
#[cfg(feature = "unproven")]
impl<W, E> Send<W> for FullDuplex<W, Error = E> {
type Error = E;
fn send(&mut self, word: W) -> nb::Result<(), Self::Error> {
self.send(word)
}

fn completed(&mut self) -> nb::Result<(), Self::Error> {
self.read().map(|_| ())
}
}

/// Clock polarity
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Polarity {
Expand Down Expand Up @@ -75,4 +107,4 @@ pub const MODE_2: Mode = Mode {
pub const MODE_3: Mode = Mode {
polarity: Polarity::IdleHigh,
phase: Phase::CaptureOnSecondTransition,
};
};