Skip to content

Commit a26a373

Browse files
Add non-blocking spi send trait
1 parent 9628ccc commit a26a373

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

Diff for: src/prelude.rs

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ pub use digital::ToggleableOutputPin as _embedded_hal_digital_ToggleableOutputPi
2828
pub use serial::Read as _embedded_hal_serial_Read;
2929
pub use serial::Write as _embedded_hal_serial_Write;
3030
pub use spi::FullDuplex as _embedded_hal_spi_FullDuplex;
31+
#[cfg(feature = "unproven")]
32+
pub use spi::Send as _embedded_hal_spi_Send;
3133
pub use timer::CountDown as _embedded_hal_timer_CountDown;
3234
#[cfg(feature = "unproven")]
3335
pub use watchdog::Watchdog as _embedded_hal_watchdog_Watchdog;

Diff for: src/spi.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,52 @@ pub trait FullDuplex<Word> {
2626
fn send(&mut self, word: Word) -> nb::Result<(), Self::Error>;
2727
}
2828

29+
/// Write (master mode)
30+
///
31+
/// # Notes
32+
///
33+
/// - It's the task of the user of this interface to manage the slave select lines
34+
#[cfg(feature = "unproven")]
35+
pub trait Send<Word> {
36+
/// An enumeration of SPI errors
37+
type Error;
38+
39+
/// Sends a word to the slave
40+
fn send(&mut self, word: Word) -> nb::Result<(), Self::Error>;
41+
}
42+
43+
/// Write (master mode)
44+
#[cfg(feature = "unproven")]
45+
pub mod full_duplex {
46+
/// Default implementation of `spi::FullDuplex<W>` for implementers of
47+
/// `spi::Send<W>`
48+
///
49+
/// Also needs a `send` method to return the byte read during the last send
50+
pub trait Default<W>: ::spi::Send<W> {
51+
/// Reads the word stored in the shift register
52+
///
53+
/// **NOTE** A word must be sent to the slave before attempting to call this
54+
/// method.
55+
fn read(&mut self) -> nb::Result<W, Self::Error>;
56+
}
57+
58+
impl<W, S> ::spi::FullDuplex<W> for S
59+
where
60+
S: Default<W>,
61+
W: Clone,
62+
{
63+
type Error = S::Error;
64+
65+
fn read(&mut self) -> nb::Result<W, Self::Error> {
66+
self.read()
67+
}
68+
69+
fn send(&mut self, word: W) -> nb::Result<(), Self::Error> {
70+
self.send(word)
71+
}
72+
}
73+
}
74+
2975
/// Clock polarity
3076
#[derive(Clone, Copy, PartialEq)]
3177
pub enum Polarity {
@@ -75,4 +121,4 @@ pub const MODE_2: Mode = Mode {
75121
pub const MODE_3: Mode = Mode {
76122
polarity: Polarity::IdleHigh,
77123
phase: Phase::CaptureOnSecondTransition,
78-
};
124+
};

0 commit comments

Comments
 (0)