Skip to content

Commit 1928f77

Browse files
committed
Make futures::spi::{Read, Write, ReadWrite, ReadWriteInPlace} traits
1 parent d9d08fa commit 1928f77

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

src/futures/spi.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,35 @@
11
//! Serial Peripheral Interface
22
3-
use core::future::Future;
3+
use core::{future::Future, mem::MaybeUninit};
44

5-
/// Async transfer
6-
pub trait Transfer<Word: 'static> {
5+
/// Async read + write
6+
pub trait ReadWrite<Word: 'static> {
77
/// Error type
88
type Error;
99

1010
/// Associated future for the `transfer` method.
11-
type TransferFuture<'a>: Future<Output = Result<&'a [Word], Self::Error>> + 'a
11+
type ReadWriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
1212
where
1313
Self: 'a;
1414

15-
/// Writes `words` to the slave. Returns the `words` received from the slave
16-
fn transfer<'a>(&'a mut self, words: &'a mut [Word]) -> Self::TransferFuture<'a>;
15+
/// Writes `words` to the slave from the `write` buffer. Puts the words returned in the `read` buffer.
16+
/// This method uses separate `write` and `read` buffers.
17+
fn readwrite<'a>(&'a mut self, write: &'a [Word], read: &'a mut [MaybeUninit<Word>]) -> Self::ReadWriteFuture<'a>;
18+
}
19+
20+
/// Async read + write in place.
21+
pub trait ReadWriteInPlace<Word: 'static> {
22+
/// Error type
23+
type Error;
24+
25+
/// Associated future for the `transfer` method.
26+
type ReadWriteInPlaceFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
27+
where
28+
Self: 'a;
29+
30+
/// Writes `words` to the slave from the `readwrite` buffer and reads words into the same buffer.
31+
/// This method uses a single `readwrite` buffer.
32+
fn readwrite_inplace<'a>(&'a mut self, readwrite: &'a mut [MaybeUninit<Word>]) -> Self::ReadWriteInPlaceFuture<'a>;
1733
}
1834

1935
/// Async write
@@ -30,6 +46,22 @@ pub trait Write<W> {
3046
fn write<'a>(&'a mut self, words: &'a [W]) -> Self::WriteFuture<'a>;
3147
}
3248

49+
/// Async read
50+
pub trait Read<W> {
51+
/// Error type
52+
type Error;
53+
54+
/// Associated future for the `read` method.
55+
type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
56+
where
57+
Self: 'a;
58+
59+
/// Reads words from the slave without specifying any data to write.
60+
/// The SPI hardware will send data, though what data it sends is not defined
61+
/// by this trait. Some hardware can configure what values (e.g. all zeroes, all ones), some cannot.
62+
fn read<'a>(&'a mut self, words: &'a mut [MaybeUninit<W>]) -> Self::ReadFuture<'a>;
63+
}
64+
3365
// /// Operation for transactional SPI trait
3466
// ///
3567
// /// This allows composition of SPI operations into a single bus transaction

0 commit comments

Comments
 (0)