4
4
//! traits. To save boilerplate when that's the case a `Default` marker trait may be provided.
5
5
//! Implementing that marker trait will opt in your type into a blanket implementation.
6
6
7
- /// Blocking transfer with separate buffers
8
- pub trait Transfer < W > {
9
- /// Error type
10
- type Error : crate :: spi:: Error ;
11
-
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
- impl < T : Transfer < W > , W > Transfer < W > for & mut T {
24
- type Error = T :: Error ;
25
-
26
- fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > {
27
- T :: transfer ( self , read, write)
28
- }
29
- }
30
-
31
- /// Blocking transfer with single buffer (in-place)
32
- pub trait TransferInplace < W > {
7
+ /// Base SPI trait
8
+ ///
9
+ /// This just defines the error type, to be used by the other SPI traits.
10
+ pub trait Spi {
33
11
/// Error type
34
12
type Error : crate :: spi:: Error ;
35
-
36
- /// Writes and reads simultaneously. The contents of `words` are
37
- /// written to the slave, and the received words are stored into the same
38
- /// `words` buffer, overwriting it.
39
- fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > ;
40
13
}
41
14
42
- impl < T : TransferInplace < W > , W > TransferInplace < W > for & mut T {
15
+ impl < T : Spi > Spi for & mut T {
43
16
type Error = T :: Error ;
44
-
45
- fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
46
- T :: transfer_inplace ( self , words)
47
- }
48
17
}
49
18
50
- /// Blocking read
51
- pub trait Read < W > {
52
- /// Error type
53
- type Error : crate :: spi:: Error ;
54
-
19
+ /// Blocking read-only SPI
20
+ pub trait Read < W > : Spi {
55
21
/// Reads `words` from the slave.
56
22
///
57
23
/// The word value sent on MOSI during reading is implementation-defined,
@@ -60,43 +26,26 @@ pub trait Read<W> {
60
26
}
61
27
62
28
impl < T : Read < W > , W > Read < W > for & mut T {
63
- type Error = T :: Error ;
64
-
65
29
fn read ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
66
30
T :: read ( self , words)
67
31
}
68
32
}
69
33
70
- /// Blocking write
71
- pub trait Write < W > {
72
- /// Error type
73
- type Error : crate :: spi:: Error ;
74
-
34
+ /// Blocking write-only SPI
35
+ pub trait Write < W > : Spi {
75
36
/// Writes `words` to the slave, ignoring all the incoming words
76
37
fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > ;
77
- }
78
-
79
- impl < T : Write < W > , W > Write < W > for & mut T {
80
- type Error = T :: Error ;
81
-
82
- fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > {
83
- T :: write ( self , words)
84
- }
85
- }
86
-
87
- /// Blocking write (iterator version)
88
- pub trait WriteIter < W > {
89
- /// Error type
90
- type Error : crate :: spi:: Error ;
91
38
92
39
/// Writes `words` to the slave, ignoring all the incoming words
93
40
fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
94
41
where
95
42
WI : IntoIterator < Item = W > ;
96
43
}
97
44
98
- impl < T : WriteIter < W > , W > WriteIter < W > for & mut T {
99
- type Error = T :: Error ;
45
+ impl < T : Write < W > , W > Write < W > for & mut T {
46
+ fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > {
47
+ T :: write ( self , words)
48
+ }
100
49
101
50
fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
102
51
where
@@ -121,18 +70,35 @@ pub enum Operation<'a, W: 'static> {
121
70
TransferInplace ( & ' a mut [ W ] ) ,
122
71
}
123
72
124
- /// Transactional trait allows multiple actions to be executed
125
- /// as part of a single SPI transaction
126
- pub trait Transactional < W : ' static > {
127
- /// Associated error type
128
- type Error : crate :: spi:: Error ;
73
+ /// Blocking read-write SPI
74
+ pub trait ReadWrite < W > : Read < W > + Write < W > {
75
+ /// Writes and reads simultaneously. `write` is written to the slave on MOSI and
76
+ /// words received on MISO are stored in `read`.
77
+ ///
78
+ /// It is allowed for `read` and `write` to have different lengths, even zero length.
79
+ /// The transfer runs for `max(read.len(), write.len())` words. If `read` is shorter,
80
+ /// incoming words after `read` has been filled will be discarded. If `write` is shorter,
81
+ /// the value of words sent in MOSI after all `write` has been sent is implementation-defined,
82
+ /// typically `0x00`, `0xFF`, or configurable.
83
+ fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > ;
129
84
130
- /// Execute the provided transactions
85
+ /// Writes and reads simultaneously. The contents of `words` are
86
+ /// written to the slave, and the received words are stored into the same
87
+ /// `words` buffer, overwriting it.
88
+ fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > ;
89
+
90
+ /// Execute multiple actions as part of a single SPI transaction
131
91
fn exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Self :: Error > ;
132
92
}
133
93
134
- impl < T : Transactional < W > , W : ' static > Transactional < W > for & mut T {
135
- type Error = T :: Error ;
94
+ impl < T : ReadWrite < W > , W > ReadWrite < W > for & mut T {
95
+ fn transfer ( & mut self , read : & mut [ W ] , write : & [ W ] ) -> Result < ( ) , Self :: Error > {
96
+ T :: transfer ( self , read, write)
97
+ }
98
+
99
+ fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
100
+ T :: transfer_inplace ( self , words)
101
+ }
136
102
137
103
fn exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Self :: Error > {
138
104
T :: exec ( self , operations)
0 commit comments