Skip to content

Commit c7497ba

Browse files
bors[bot]Dirbaio
andauthored
Merge #287
287: spi: add Read and separate-buffers Transfer r=eldruin a=Dirbaio ~~Depends on #286~~ - Added spi::Read for only reading - Renamed Transfer to TransferInplace - Added a new Transfer, for reading+writing simultaneously with different buffers. Open question - ~~Should Transactional gain Read, Transfer (not-inplace) support?~~ yes, done Co-authored-by: Dario Nieuwenhuis <[email protected]>
2 parents ac66e33 + d15e817 commit c7497ba

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
@@ -35,6 +35,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3535
dynamically.
3636
- Added `Debug` to all spi mode types.
3737
- Add impls of all traits for references (`&T` or `&mut T` depending on the trait) when `T` implements the trait.
38+
- SPI: Added blocking `Read` trait and `Read` transactional operation
39+
- SPI: Added blocking `Transfer` trait with separate buffers (single-buffer `Transfer` has been renamed `TransferInplace`)
3840

3941
### Changed
4042
- Swap PWM channel arguments to references

src/spi/blocking.rs

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,65 @@
11
//! Blocking SPI API
22
3-
/// Blocking transfer
3+
/// Blocking transfer with separate buffers
44
pub trait Transfer<W = u8> {
55
/// Error type
66
type Error: crate::spi::Error;
77

8+
/// Writes and reads simultaneously. `write` is written to the slave on MOSI and
9+
/// words received on MISO are stored in `read`.
10+
///
11+
/// It is allowed for `read` and `write` to have different lengths, even zero length.
12+
/// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
13+
/// incoming words after `read` has been filled will be discarded. If `write` is shorter,
14+
/// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
15+
/// typically `0x00`, `0xFF`, or configurable.
16+
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error>;
17+
}
18+
19+
impl<T: Transfer<W>, W> Transfer<W> for &mut T {
20+
type Error = T::Error;
21+
22+
fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> {
23+
T::transfer(self, read, write)
24+
}
25+
}
26+
27+
/// Blocking transfer with single buffer (in-place)
28+
pub trait TransferInplace<W = u8> {
29+
/// Error type
30+
type Error: crate::spi::Error;
31+
832
/// Writes and reads simultaneously. The contents of `words` are
933
/// written to the slave, and the received words are stored into the same
1034
/// `words` buffer, overwriting it.
11-
fn transfer(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
35+
fn transfer_inplace(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
1236
}
1337

14-
impl<T: Transfer<W>, W> Transfer<W> for &mut T {
38+
impl<T: TransferInplace<W>, W> TransferInplace<W> for &mut T {
1539
type Error = T::Error;
1640

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

@@ -63,10 +107,14 @@ impl<T: WriteIter<W>, W> WriteIter<W> for &mut T {
63107
/// This allows composition of SPI operations into a single bus transaction
64108
#[derive(Debug, PartialEq)]
65109
pub enum Operation<'a, W: 'static = u8> {
110+
/// Read data into the provided buffer.
111+
Read(&'a mut [W]),
66112
/// Write data from the provided buffer, discarding read data
67113
Write(&'a [W]),
68114
/// Write data out while reading data into the provided buffer
69-
Transfer(&'a mut [W]),
115+
Transfer(&'a mut [W], &'a [W]),
116+
/// Write data out while reading data into the provided buffer
117+
TransferInplace(&'a mut [W]),
70118
}
71119

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

0 commit comments

Comments
 (0)