Skip to content

Commit 44e0da1

Browse files
committed
spi/blocking: group methods into read-only, write-only and read-write traits.
1 parent 2c40bbf commit 44e0da1

File tree

1 file changed

+34
-50
lines changed

1 file changed

+34
-50
lines changed

src/spi/blocking.rs

Lines changed: 34 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,7 @@
22
33
use super::ErrorType;
44

5-
/// Blocking transfer with separate buffers
6-
pub trait Transfer<W = u8>: ErrorType {
7-
/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
8-
/// words received on MISO are stored in `read`.
9-
///
10-
/// It is allowed for `read` and `write` to have different lengths, even zero length.
11-
/// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
12-
/// incoming words after `read` has been filled will be discarded. If `write` is shorter,
13-
/// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
14-
/// typically `0x00`, `0xFF`, or configurable.
15-
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>;
16-
}
17-
18-
impl<T: Transfer<W>, W> Transfer<W> for &mut T {
19-
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> {
20-
T::transfer(self, read, write)
21-
}
22-
}
23-
24-
/// Blocking transfer with single buffer (in-place)
25-
pub trait TransferInplace<W = u8>: ErrorType {
26-
/// Writes and reads simultaneously. The contents of `words` are
27-
/// written to the slave, and the received words are stored into the same
28-
/// `words` buffer, overwriting it.
29-
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
30-
}
31-
32-
impl<T: TransferInplace<W>, W> TransferInplace<W> for &mut T {
33-
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
34-
T::transfer_inplace(self, words)
35-
}
36-
}
37-
38-
/// Blocking read
5+
/// Blocking read-only SPI
396
pub trait Read<W = u8>: ErrorType {
407
/// Reads `words` from the slave.
418
///
@@ -50,27 +17,22 @@ impl<T: Read<W>, W> Read<W> for &mut T {
5017
}
5118
}
5219

53-
/// Blocking write
20+
/// Blocking write-only SPI
5421
pub trait Write<W = u8>: ErrorType {
5522
/// Writes `words` to the slave, ignoring all the incoming words
5623
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
57-
}
58-
59-
impl<T: Write<W>, W> Write<W> for &mut T {
60-
fn write(&mut self, words: &[W]) -> Result<(), Self::Error> {
61-
T::write(self, words)
62-
}
63-
}
6424

65-
/// Blocking write (iterator version)
66-
pub trait WriteIter<W = u8>: ErrorType {
6725
/// Writes `words` to the slave, ignoring all the incoming words
6826
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
6927
where
7028
WI: IntoIterator<Item = W>;
7129
}
7230

73-
impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
31+
impl<T: Write<W>, W> Write<W> for &mut T {
32+
fn write(&mut self, words: &[W]) -> Result<(), Self::Error> {
33+
T::write(self, words)
34+
}
35+
7436
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
7537
where
7638
WI: IntoIterator<Item = W>,
@@ -94,14 +56,36 @@ pub enum Operation<'a, W: 'static = u8> {
9456
TransferInplace(&'a mut [W]),
9557
}
9658

97-
/// Transactional trait allows multiple actions to be executed
98-
/// as part of a single SPI transaction
99-
pub trait Transactional<W: 'static = u8>: ErrorType {
100-
/// Execute the provided transactions
59+
/// Blocking read-write SPI
60+
pub trait ReadWrite<W = u8>: Read<W> + Write<W> {
61+
/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
62+
/// words received on MISO are stored in `read`.
63+
///
64+
/// It is allowed for `read` and `write` to have different lengths, even zero length.
65+
/// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
66+
/// incoming words after `read` has been filled will be discarded. If `write` is shorter,
67+
/// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
68+
/// typically `0x00`, `0xFF`, or configurable.
69+
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>;
70+
71+
/// Writes and reads simultaneously. The contents of `words` are
72+
/// written to the slave, and the received words are stored into the same
73+
/// `words` buffer, overwriting it.
74+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
75+
76+
/// Execute multiple actions as part of a single SPI transaction
10177
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
10278
}
10379

104-
impl<T: Transactional<W>, W: 'static> Transactional<W> for &mut T {
80+
impl<T: ReadWrite<W>, W> ReadWrite<W> for &mut T {
81+
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> {
82+
T::transfer(self, read, write)
83+
}
84+
85+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
86+
T::transfer_inplace(self, words)
87+
}
88+
10589
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error> {
10690
T::exec(self, operations)
10791
}

0 commit comments

Comments
 (0)