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