Skip to content

Commit 151a658

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

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
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: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,41 @@
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+
/// Blocking transfer with single buffer (in-place)
24+
pub trait TransferInplace<W> {
25+
/// Error type
26+
type Error: core::fmt::Debug;
27+
1228
/// Writes and reads simultaneously. The contents of `words` are
1329
/// written to the slave, and the received words are stored into the same
1430
/// `words` buffer, overwriting it.
15-
fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
31+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
32+
}
33+
34+
/// Blocking read
35+
pub trait Read<W> {
36+
/// Error type
37+
type Error;
38+
39+
/// Reads `words` to the slave. The word value sent on MOSI during
40+
/// reading is implementation-defined, typically `0x00`, `0xFF`, or configurable.
41+
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
1642
}
1743

1844
impl<T: Transfer<W>, W> Transfer<W> for &mut T {
@@ -67,10 +93,14 @@ impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
6793
/// This allows composition of SPI operations into a single bus transaction
6894
#[derive(Debug, PartialEq)]
6995
pub enum Operation<'a, W: 'static> {
96+
/// Read data into the provided buffer.
97+
Read(&'a mut [W]),
7098
/// Write data from the provided buffer, discarding read data
7199
Write(&'a [W]),
72100
/// Write data out while reading data into the provided buffer
73-
Transfer(&'a mut [W]),
101+
Transfer(&'a mut [W], &'a [W]),
102+
/// Write data out while reading data into the provided buffer
103+
TransferInplace(&'a mut [W]),
74104
}
75105

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

0 commit comments

Comments
 (0)