|
1 | 1 | //! Blocking SPI API
|
2 | 2 |
|
3 |
| -pub use spi::{Mode, Phase, Polarity}; |
4 |
| - |
5 |
| -/// Blocking full duplex |
6 |
| -pub trait FullDuplex<W> { |
7 |
| - /// An enumeration of SPI errors |
| 3 | +/// Blocking transfer |
| 4 | +pub trait Transfer<W> { |
| 5 | + /// Error type |
8 | 6 | type Error;
|
9 | 7 |
|
10 | 8 | /// Sends `words` to the slave. Returns the `words` received from the slave
|
11 | 9 | fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], Self::Error>;
|
| 10 | +} |
| 11 | + |
| 12 | +/// Blocking write |
| 13 | +pub trait Write<W> { |
| 14 | + /// Error type |
| 15 | + type Error; |
12 | 16 |
|
13 | 17 | /// Sends `words` to the slave, ignoring all the incoming words
|
14 | 18 | fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
|
15 | 19 | }
|
16 | 20 |
|
17 |
| -/// Transfers words to the slave, returns the words received from the slave |
18 |
| -pub fn transfer<'w, S, W>(spi: &mut S, words: &'w mut [W]) -> Result<&'w [W], S::Error> |
19 |
| -where |
20 |
| - S: ::spi::FullDuplex<W>, |
21 |
| - W: Clone, |
22 |
| -{ |
23 |
| - for word in words.iter_mut() { |
24 |
| - block!(spi.send(word.clone()))?; |
25 |
| - *word = block!(spi.read())?; |
| 21 | +/// Blocking transfer |
| 22 | +pub mod transfer { |
| 23 | + /// Default implementation of `blocking::spi::Transfer<W>` for implementers of |
| 24 | + /// `spi::FullDuplex<W>` |
| 25 | + pub trait Default<W>: ::spi::FullDuplex<W> {} |
| 26 | + |
| 27 | + impl<W, S> ::blocking::spi::Transfer<W> for S |
| 28 | + where |
| 29 | + S: Default<W>, |
| 30 | + W: Clone, |
| 31 | + { |
| 32 | + type Error = S::Error; |
| 33 | + |
| 34 | + fn transfer<'w>(&mut self, words: &'w mut [W]) -> Result<&'w [W], S::Error> { |
| 35 | + for word in words.iter_mut() { |
| 36 | + block!(self.send(word.clone()))?; |
| 37 | + *word = block!(self.read())?; |
| 38 | + } |
| 39 | + |
| 40 | + Ok(words) |
| 41 | + } |
26 | 42 | }
|
| 43 | +} |
| 44 | + |
| 45 | +/// Blocking write |
| 46 | +pub mod write { |
| 47 | + /// Default implementation of `blocking::spi::Write<W>` for implementers of `spi::FullDuplex<W>` |
| 48 | + pub trait Default<W>: ::spi::FullDuplex<W> {} |
27 | 49 |
|
28 |
| - Ok(words) |
| 50 | + impl<W, S> ::blocking::spi::Write<W> for S |
| 51 | + where |
| 52 | + S: Default<W>, |
| 53 | + W: Clone, |
| 54 | + { |
| 55 | + type Error = S::Error; |
| 56 | + |
| 57 | + fn write(&mut self, words: &[W]) -> Result<(), S::Error> { |
| 58 | + for word in words { |
| 59 | + block!(self.send(word.clone()))?; |
| 60 | + block!(self.read())?; |
| 61 | + } |
| 62 | + |
| 63 | + Ok(()) |
| 64 | + } |
| 65 | + } |
29 | 66 | }
|
0 commit comments