Skip to content

Commit c248f06

Browse files
committed
spi/blocking: add read_batch, write_batch.
1 parent 2c4ae95 commit c248f06

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/spi/blocking.rs

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ pub trait Read<W = u8>: Spi {
1919
/// The word value sent on MOSI during reading is implementation-defined,
2020
/// typically `0x00`, `0xFF`, or configurable.
2121
fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error>;
22+
23+
/// Reads all slices in `words` from the slave as part of a single SPI transaction.
24+
///
25+
/// The word value sent on MOSI during reading is implementation-defined,
26+
/// typically `0x00`, `0xFF`, or configurable.
27+
fn read_batch(&mut self, words: &mut [&mut [W]]) -> Result<(), Self::Error> {
28+
for buf in words {
29+
self.read(buf)?;
30+
}
31+
Ok(())
32+
}
2233
}
2334

2435
impl<T: Read<W>, W> Read<W> for &mut T {
@@ -32,6 +43,14 @@ pub trait Write<W = u8>: Spi {
3243
/// Writes `words` to the slave, ignoring all the incoming words
3344
fn write(&mut self, words: &[W]) -> Result<(), Self::Error>;
3445

46+
/// Writes all slices in `words` to the slave as part of a single SPI transaction, ignoring all the incoming words
47+
fn write_batch(&mut self, words: &[&[W]]) -> Result<(), Self::Error> {
48+
for buf in words {
49+
self.write(buf)?;
50+
}
51+
Ok(())
52+
}
53+
3554
/// Writes `words` to the slave, ignoring all the incoming words
3655
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
3756
where
@@ -49,6 +68,10 @@ impl<T: Write<W>, W> Write<W> for &mut T {
4968
T::write(self, words)
5069
}
5170

71+
fn write_batch(&mut self, words: &[&[W]]) -> Result<(), Self::Error> {
72+
T::write_batch(self, words)
73+
}
74+
5275
fn write_iter<WI>(&mut self, words: WI) -> Result<(), Self::Error>
5376
where
5477
WI: IntoIterator<Item = W>,

0 commit comments

Comments
 (0)