2
2
3
3
use super :: ErrorType ;
4
4
5
- /// Blocking transfer with separate buffers
6
- pub trait Transfer < W = u8 > : ErrorType {
7
- /// Writes and reads simultaneously. `write` is written to the slave on MOSI and
8
- /// words received on MISO are stored in `read`.
9
- ///
10
- /// It is allowed for `read` and `write` to have different lengths, even zero length.
11
- /// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
12
- /// incoming words after `read` has been filled will be discarded. If `write` is shorter,
13
- /// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
14
- /// typically `0x00`, `0xFF`, or configurable.
15
- fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > ;
16
- }
17
-
18
- impl < T : Transfer < W > , W > Transfer < W > for & mut T {
19
- fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > {
20
- T :: transfer ( self , read, write)
21
- }
22
- }
23
-
24
- /// Blocking transfer with single buffer (in-place)
25
- pub trait TransferInplace < W = u8 > : ErrorType {
26
- /// Writes and reads simultaneously. The contents of `words` are
27
- /// written to the slave, and the received words are stored into the same
28
- /// `words` buffer, overwriting it.
29
- fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > ;
30
- }
31
-
32
- impl < T : TransferInplace < W > , W > TransferInplace < W > for & mut T {
33
- fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
34
- T :: transfer_inplace ( self , words)
35
- }
36
- }
37
-
38
- /// Blocking read
5
+ /// Blocking read-only SPI
39
6
pub trait Read < W = u8 > : ErrorType {
40
7
/// Reads `words` from the slave.
41
8
///
@@ -50,27 +17,22 @@ impl<T: Read<W>, W> Read<W> for &mut T {
50
17
}
51
18
}
52
19
53
- /// Blocking write
20
+ /// Blocking write-only SPI
54
21
pub trait Write < W = u8 > : ErrorType {
55
22
/// Writes `words` to the slave, ignoring all the incoming words
56
23
fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > ;
57
- }
58
-
59
- impl < T : Write < W > , W > Write < W > for & mut T {
60
- fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > {
61
- T :: write ( self , words)
62
- }
63
- }
64
24
65
- /// Blocking write (iterator version)
66
- pub trait WriteIter < W = u8 > : ErrorType {
67
25
/// Writes `words` to the slave, ignoring all the incoming words
68
26
fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
69
27
where
70
28
WI : IntoIterator < Item = W > ;
71
29
}
72
30
73
- impl < T : WriteIter < W > , W > WriteIter < W > for & mut T {
31
+ impl < T : Write < W > , W > Write < W > for & mut T {
32
+ fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > {
33
+ T :: write ( self , words)
34
+ }
35
+
74
36
fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
75
37
where
76
38
WI : IntoIterator < Item = W > ,
@@ -94,14 +56,36 @@ pub enum Operation<'a, W: 'static = u8> {
94
56
TransferInplace ( & ' a mut [ W ] ) ,
95
57
}
96
58
97
- /// Transactional trait allows multiple actions to be executed
98
- /// as part of a single SPI transaction
99
- pub trait Transactional < W : ' static = u8 > : ErrorType {
100
- /// Execute the provided transactions
59
+ /// Blocking read-write SPI
60
+ pub trait ReadWrite < W = u8 > : Read < W > + Write < W > {
61
+ /// Writes and reads simultaneously. `write` is written to the slave on MOSI and
62
+ /// words received on MISO are stored in `read`.
63
+ ///
64
+ /// It is allowed for `read` and `write` to have different lengths, even zero length.
65
+ /// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
66
+ /// incoming words after `read` has been filled will be discarded. If `write` is shorter,
67
+ /// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
68
+ /// typically `0x00`, `0xFF`, or configurable.
69
+ fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > ;
70
+
71
+ /// Writes and reads simultaneously. The contents of `words` are
72
+ /// written to the slave, and the received words are stored into the same
73
+ /// `words` buffer, overwriting it.
74
+ fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > ;
75
+
76
+ /// Execute multiple actions as part of a single SPI transaction
101
77
fn exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Self :: Error > ;
102
78
}
103
79
104
- impl < T : Transactional < W > , W : ' static > Transactional < W > for & mut T {
80
+ impl < T : ReadWrite < W > , W > ReadWrite < W > for & mut T {
81
+ fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > {
82
+ T :: transfer ( self , read, write)
83
+ }
84
+
85
+ fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
86
+ T :: transfer_inplace ( self , words)
87
+ }
88
+
105
89
fn exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Self :: Error > {
106
90
T :: exec ( self , operations)
107
91
}
0 commit comments