Skip to content

Commit 57ff41d

Browse files
committed
spi/blocking: group methods into read-only, write-only and read-write traits.
1 parent f972218 commit 57ff41d

File tree

1 file changed

+38
-72
lines changed

1 file changed

+38
-72
lines changed

src/spi/blocking.rs

Lines changed: 38 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,20 @@
44
//! traits. To save boilerplate when that's the case a `Default` marker trait may be provided.
55
//! Implementing that marker trait will opt in your type into a blanket implementation.
66
7-
/// Blocking transfer with separate buffers
8-
pub trait Transfer<W> {
9-
/// Error type
10-
type Error: crate::spi::Error;
11-
12-
/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
13-
/// words received on MISO are stored in `read`.
14-
///
15-
/// It is allowed for `read` and `write` to have different lengths, even zero length.
16-
/// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
17-
/// incoming words after `read` has been filled will be discarded. If `write` is shorter,
18-
/// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
19-
/// typically `0x00`, `0xFF`, or configurable.
20-
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>;
21-
}
22-
23-
impl<T: Transfer<W>, W> Transfer<W> for &mut T {
24-
type Error = T::Error;
25-
26-
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> {
27-
T::transfer(self, read, write)
28-
}
29-
}
30-
31-
/// Blocking transfer with single buffer (in-place)
32-
pub trait TransferInplace<W> {
7+
/// Base SPI trait
8+
///
9+
/// This just defines the error type, to be used by the other SPI traits.
10+
pub trait Spi {
3311
/// Error type
3412
type Error: crate::spi::Error;
35-
36-
/// Writes and reads simultaneously. The contents of `words` are
37-
/// written to the slave, and the received words are stored into the same
38-
/// `words` buffer, overwriting it.
39-
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
4013
}
4114

42-
impl<T: TransferInplace<W>, W> TransferInplace<W> for &mut T {
15+
impl<T: Spi> Spi for &mut T {
4316
type Error = T::Error;
44-
45-
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
46-
T::transfer_inplace(self, words)
47-
}
4817
}
4918

50-
/// Blocking read
51-
pub trait Read<W> {
52-
/// Error type
53-
type Error: crate::spi::Error;
54-
19+
/// Blocking read-only SPI
20+
pub trait Read<W>: Spi {
5521
/// Reads `words` from the slave.
5622
///
5723
/// The word value sent on MOSI during reading is implementation-defined,
@@ -60,43 +26,26 @@ pub trait Read<W> {
6026
}
6127

6228
impl<T: Read<W>, W> Read<W> for &mut T {
63-
type Error = T::Error;
64-
6529
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
6630
T::read(self, words)
6731
}
6832
}
6933

70-
/// Blocking write
71-
pub trait Write<W> {
72-
/// Error type
73-
type Error: crate::spi::Error;
74-
34+
/// Blocking write-only SPI
35+
pub trait Write<W>: Spi {
7536
/// Writes `words` to the slave, ignoring all the incoming words
7637
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
77-
}
78-
79-
impl<T: Write<W>, W> Write<W> for &mut T {
80-
type Error = T::Error;
81-
82-
fn write(&mut self, words: &[W]) -> Result<(), Self::Error> {
83-
T::write(self, words)
84-
}
85-
}
86-
87-
/// Blocking write (iterator version)
88-
pub trait WriteIter<W> {
89-
/// Error type
90-
type Error: crate::spi::Error;
9138

9239
/// Writes `words` to the slave, ignoring all the incoming words
9340
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
9441
where
9542
WI: IntoIterator<Item = W>;
9643
}
9744

98-
impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
99-
type Error = T::Error;
45+
impl<T: Write<W>, W> Write<W> for &mut T {
46+
fn write(&mut self, words: &[W]) -> Result<(), Self::Error> {
47+
T::write(self, words)
48+
}
10049

10150
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
10251
where
@@ -121,18 +70,35 @@ pub enum Operation<'a, W: 'static> {
12170
TransferInplace(&'a mut [W]),
12271
}
12372

124-
/// Transactional trait allows multiple actions to be executed
125-
/// as part of a single SPI transaction
126-
pub trait Transactional<W: 'static> {
127-
/// Associated error type
128-
type Error: crate::spi::Error;
73+
/// Blocking read-write SPI
74+
pub trait ReadWrite<W>: Read<W> + Write<W> {
75+
/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
76+
/// words received on MISO are stored in `read`.
77+
///
78+
/// It is allowed for `read` and `write` to have different lengths, even zero length.
79+
/// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
80+
/// incoming words after `read` has been filled will be discarded. If `write` is shorter,
81+
/// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
82+
/// typically `0x00`, `0xFF`, or configurable.
83+
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>;
12984

130-
/// Execute the provided transactions
85+
/// Writes and reads simultaneously. The contents of `words` are
86+
/// written to the slave, and the received words are stored into the same
87+
/// `words` buffer, overwriting it.
88+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
89+
90+
/// Execute multiple actions as part of a single SPI transaction
13191
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
13292
}
13393

134-
impl<T: Transactional<W>, W: 'static> Transactional<W> for &mut T {
135-
type Error = T::Error;
94+
impl<T: ReadWrite<W>, W> ReadWrite<W> for &mut T {
95+
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> {
96+
T::transfer(self, read, write)
97+
}
98+
99+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
100+
T::transfer_inplace(self, words)
101+
}
136102

137103
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error> {
138104
T::exec(self, operations)

0 commit comments

Comments
 (0)