|
1 | 1 | //! Blocking SPI API
|
2 | 2 |
|
3 |
| -/// Blocking transfer |
| 3 | +/// Blocking transfer with separate buffers |
4 | 4 | pub trait Transfer<W = u8> {
|
5 | 5 | /// Error type
|
6 | 6 | type Error: crate::spi::Error;
|
7 | 7 |
|
| 8 | + /// Writes and reads simultaneously. `write` is written to the slave on MOSI and |
| 9 | + /// words received on MISO are stored in `read`. |
| 10 | + /// |
| 11 | + /// It is allowed for `read` and `write` to have different lengths, even zero length. |
| 12 | + /// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter, |
| 13 | + /// incoming words after `read` has been filled will be discarded. If `write` is shorter, |
| 14 | + /// the value of words sent in MOSI after all `write` has been sent is implementation-defined, |
| 15 | + /// typically `0x00`, `0xFF`, or configurable. |
| 16 | + fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>; |
| 17 | +} |
| 18 | + |
| 19 | +impl<T: Transfer<W>, W> Transfer<W> for &mut T { |
| 20 | + type Error = T::Error; |
| 21 | + |
| 22 | + fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> { |
| 23 | + T::transfer(self, read, write) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/// Blocking transfer with single buffer (in-place) |
| 28 | +pub trait TransferInplace<W = u8> { |
| 29 | + /// Error type |
| 30 | + type Error: crate::spi::Error; |
| 31 | + |
8 | 32 | /// Writes and reads simultaneously. The contents of `words` are
|
9 | 33 | /// written to the slave, and the received words are stored into the same
|
10 | 34 | /// `words` buffer, overwriting it.
|
11 |
| - fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>; |
| 35 | + fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>; |
12 | 36 | }
|
13 | 37 |
|
14 |
| -impl<T: Transfer<W>, W> Transfer<W> for &mut T { |
| 38 | +impl<T: TransferInplace<W>, W> TransferInplace<W> for &mut T { |
15 | 39 | type Error = T::Error;
|
16 | 40 |
|
17 |
| - fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error> { |
18 |
| - T::transfer(self, words) |
| 41 | + fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> { |
| 42 | + T::transfer_inplace(self, words) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +/// Blocking read |
| 47 | +pub trait Read<W = u8> { |
| 48 | + /// Error type |
| 49 | + type Error: crate::spi::Error; |
| 50 | + |
| 51 | + /// Reads `words` from the slave. |
| 52 | + /// |
| 53 | + /// The word value sent on MOSI during reading is implementation-defined, |
| 54 | + /// typically `0x00`, `0xFF`, or configurable. |
| 55 | + fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error>; |
| 56 | +} |
| 57 | + |
| 58 | +impl<T: Read<W>, W> Read<W> for &mut T { |
| 59 | + type Error = T::Error; |
| 60 | + |
| 61 | + fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> { |
| 62 | + T::read(self, words) |
19 | 63 | }
|
20 | 64 | }
|
21 | 65 |
|
@@ -63,10 +107,14 @@ impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
|
63 | 107 | /// This allows composition of SPI operations into a single bus transaction
|
64 | 108 | #[derive(Debug, PartialEq)]
|
65 | 109 | pub enum Operation<'a, W: 'static = u8> {
|
| 110 | + /// Read data into the provided buffer. |
| 111 | + Read(&'a mut [W]), |
66 | 112 | /// Write data from the provided buffer, discarding read data
|
67 | 113 | Write(&'a [W]),
|
68 | 114 | /// Write data out while reading data into the provided buffer
|
69 |
| - Transfer(&'a mut [W]), |
| 115 | + Transfer(&'a mut [W], &'a [W]), |
| 116 | + /// Write data out while reading data into the provided buffer |
| 117 | + TransferInplace(&'a mut [W]), |
70 | 118 | }
|
71 | 119 |
|
72 | 120 | /// Transactional trait allows multiple actions to be executed
|
|
0 commit comments