Skip to content

Commit 4696969

Browse files
committed
spi: add Read and separate-buffers Transfer
1 parent bd7f607 commit 4696969

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
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Added `IoPin` trait for pins that can change between being inputs or outputs
1717
dynamically.
1818
- Added `Debug` to all spi mode types.
19+
- spi: added `Read`
20+
- spi: added `Transfer` with separate buffers (single-buffer `Transfer` is now `TransferInplace`)
1921

2022
### Changed
2123
- 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: core::fmt::Debug;
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
/// Blocking write
@@ -40,10 +66,14 @@ pub trait WriteIter<W> {
4066
/// This allows composition of SPI operations into a single bus transaction
4167
#[derive(Debug, PartialEq)]
4268
pub enum Operation<'a, W: 'static> {
69+
/// Read data into the provided buffer.
70+
Read(&'a mut [W]),
4371
/// Write data from the provided buffer, discarding read data
4472
Write(&'a [W]),
4573
/// Write data out while reading data into the provided buffer
46-
Transfer(&'a mut [W]),
74+
Transfer(&'a mut [W], &'a [W]),
75+
/// Write data out while reading data into the provided buffer
76+
TransferInplace(&'a mut [W]),
4777
}
4878

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

0 commit comments

Comments
 (0)