Skip to content

Commit 459e935

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

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-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: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,64 @@
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: core::fmt::Debug;
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;
54+
55+
/// Reads `words` to the slave. The word value sent on MOSI during
56+
/// reading is implementation-defined, typically `0x00`, `0xFF`, or configurable.
57+
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
58+
}
59+
60+
impl<T: Read<W>, W> Read<W> for &mut T {
61+
type Error = T::Error;
62+
63+
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> {
64+
T::read(self, words)
2365
}
2466
}
2567

@@ -67,10 +109,14 @@ impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
67109
/// This allows composition of SPI operations into a single bus transaction
68110
#[derive(Debug, PartialEq)]
69111
pub enum Operation<'a, W: 'static> {
112+
/// Read data into the provided buffer.
113+
Read(&'a mut [W]),
70114
/// Write data from the provided buffer, discarding read data
71115
Write(&'a [W]),
72116
/// Write data out while reading data into the provided buffer
73-
Transfer(&'a mut [W]),
117+
Transfer(&'a mut [W], &'a [W]),
118+
/// Write data out while reading data into the provided buffer
119+
TransferInplace(&'a mut [W]),
74120
}
75121

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

0 commit comments

Comments
 (0)