Skip to content

Commit f972218

Browse files
committed
spi: add Read and separate-buffers Transfer
1 parent 4f3ada1 commit f972218

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3131
dynamically.
3232
- Added `Debug` to all spi mode types.
3333
- Add impls of all traits for references (`&T` or `&mut T` depending on the trait) when `T` implements the trait.
34+
- SPI: Added blocking `Read` trait and `Read` transactional operation
35+
- SPI: Added blocking `Transfer` trait with separate buffers (single-buffer `Transfer` has been renamed `TransferInplace`)
3436

3537
### Changed
3638
- Swap PWM channel arguments to references

src/spi/blocking.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,66 @@
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
7+
/// Blocking transfer with separate buffers
88
pub trait Transfer<W> {
99
/// Error type
1010
type Error: crate::spi::Error;
1111

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> {
33+
/// Error type
34+
type Error: crate::spi::Error;
35+
1236
/// Writes and reads simultaneously. The contents of `words` are
1337
/// written to the slave, and the received words are stored into the same
1438
/// `words` buffer, overwriting it.
15-
fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
39+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
1640
}
1741

18-
impl<T: Transfer<W>, W> Transfer<W> for &mut T {
42+
impl<T: TransferInplace<W>, W> TransferInplace<W> for &mut T {
1943
type Error = T::Error;
2044

21-
fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
22-
T::transfer(self, words)
45+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
46+
T::transfer_inplace(self, words)
47+
}
48+
}
49+
50+
/// Blocking read
51+
pub trait Read<W> {
52+
/// Error type
53+
type Error: crate::spi::Error;
54+
55+
/// Reads `words` from the slave.
56+
///
57+
/// The word value sent on MOSI during reading is implementation-defined,
58+
/// typically `0x00`, `0xFF`, or configurable.
59+
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
60+
}
61+
62+
impl<T: Read<W>, W> Read<W> for &mut T {
63+
type Error = T::Error;
64+
65+
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
66+
T::read(self, words)
2367
}
2468
}
2569

@@ -67,10 +111,14 @@ impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
67111
/// This allows composition of SPI operations into a single bus transaction
68112
#[derive(Debug, PartialEq)]
69113
pub enum Operation<'a, W: 'static> {
114+
/// Read data into the provided buffer.
115+
Read(&'a mut [W]),
70116
/// Write data from the provided buffer, discarding read data
71117
Write(&'a [W]),
72118
/// Write data out while reading data into the provided buffer
73-
Transfer(&'a mut [W]),
119+
Transfer(&'a mut [W], &'a [W]),
120+
/// Write data out while reading data into the provided buffer
121+
TransferInplace(&'a mut [W]),
74122
}
75123

76124
/// Transactional trait allows multiple actions to be executed

0 commit comments

Comments
 (0)