@@ -77,83 +77,117 @@ mod nb {
77
77
mod blocking {
78
78
use super :: super :: { Error , Instance , Spi , TransferModeBidi , TransferModeNormal } ;
79
79
use embedded_hal_one:: spi:: {
80
- blocking:: { Operation , Transactional , TransferInplace , Write , WriteIter } ,
80
+ blocking:: { Operation , Read , Transactional , Transfer , TransferInplace , Write , WriteIter } ,
81
81
nb:: FullDuplex ,
82
82
} ;
83
83
84
- impl < SPI , PINS , TRANSFER_MODE > TransferInplace < u8 > for Spi < SPI , PINS , TRANSFER_MODE >
84
+ impl < SPI , PINS , TRANSFER_MODE , W : Copy + ' static > TransferInplace < W >
85
+ for Spi < SPI , PINS , TRANSFER_MODE >
85
86
where
86
- Self : FullDuplex < u8 , Error = Error > ,
87
+ Self : FullDuplex < W , Error = Error > ,
87
88
SPI : Instance ,
88
89
{
89
- fn transfer_inplace ( & mut self , words : & mut [ u8 ] ) -> Result < ( ) , Self :: Error > {
90
+ fn transfer_inplace ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
90
91
for word in words. iter_mut ( ) {
91
- nb:: block!( self . write( * word) ) ?;
92
- * word = nb:: block!( self . read( ) ) ?;
92
+ nb:: block!( < Self as FullDuplex < W >> :: write( self , * word) ) ?;
93
+ * word = nb:: block!( < Self as FullDuplex < W >> :: read( self ) ) ?;
93
94
}
94
95
95
96
Ok ( ( ) )
96
97
}
97
98
}
98
99
99
- impl < SPI , PINS > Write < u8 > for Spi < SPI , PINS , TransferModeNormal >
100
+ impl < SPI , PINS , TRANSFER_MODE , W : Copy + ' static > Transfer < W > for Spi < SPI , PINS , TRANSFER_MODE >
100
101
where
101
- Self : FullDuplex < u8 , Error = Error > ,
102
+ Self : FullDuplex < W , Error = Error > ,
102
103
SPI : Instance ,
103
104
{
104
- fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
105
+ fn transfer ( & mut self , buff : & mut [ W ] , data : & [ W ] ) -> Result < ( ) , Self :: Error > {
106
+ assert_eq ! ( data. len( ) , buff. len( ) ) ;
107
+
108
+ for i in 0 ..data. len ( ) {
109
+ nb:: block!( <Self as FullDuplex <W >>:: write( self , data[ i] ) ) ?;
110
+ buff[ i] = nb:: block!( <Self as FullDuplex <W >>:: read( self ) ) ?;
111
+ }
112
+
113
+ Ok ( ( ) )
114
+ }
115
+ }
116
+
117
+ impl < SPI , PINS , W : Copy + ' static > Write < W > for Spi < SPI , PINS , TransferModeNormal >
118
+ where
119
+ Self : FullDuplex < W , Error = Error > ,
120
+ SPI : Instance ,
121
+ {
122
+ fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > {
105
123
for word in words {
106
- nb:: block!( <Self as FullDuplex <u8 >>:: write( self , * word) ) ?;
107
- nb:: block!( self . read( ) ) ?;
124
+ nb:: block!( <Self as FullDuplex <W >>:: write( self , * word) ) ?;
125
+ nb:: block!( < Self as FullDuplex < W >> :: read( self ) ) ?;
108
126
}
109
127
110
128
Ok ( ( ) )
111
129
}
112
130
}
113
131
114
- impl < SPI , PINS > Write < u8 > for Spi < SPI , PINS , TransferModeBidi >
132
+ impl < SPI , PINS , W : Copy + ' static > Write < W > for Spi < SPI , PINS , TransferModeBidi >
115
133
where
116
- Self : FullDuplex < u8 , Error = Error > ,
134
+ Self : FullDuplex < W , Error = Error > ,
117
135
SPI : Instance ,
118
136
{
119
- fn write ( & mut self , words : & [ u8 ] ) -> Result < ( ) , Self :: Error > {
137
+ fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Self :: Error > {
120
138
for word in words {
121
- nb:: block!( <Self as FullDuplex <u8 >>:: write( self , * word) ) ?;
139
+ nb:: block!( <Self as FullDuplex <W >>:: write( self , * word) ) ?;
122
140
}
123
141
124
142
Ok ( ( ) )
125
143
}
126
144
}
127
145
128
- impl < SPI , PINS > WriteIter < u8 > for Spi < SPI , PINS , TransferModeNormal >
146
+ impl < SPI , PINS , W : Copy + ' static > WriteIter < W > for Spi < SPI , PINS , TransferModeNormal >
129
147
where
130
- Self : FullDuplex < u8 , Error = Error > ,
148
+ Self : FullDuplex < W , Error = Error > ,
131
149
SPI : Instance ,
132
150
{
133
151
fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
134
152
where
135
- WI : IntoIterator < Item = u8 > ,
153
+ WI : IntoIterator < Item = W > ,
136
154
{
137
155
for word in words. into_iter ( ) {
138
- nb:: block!( <Self as FullDuplex <u8 >>:: write( self , word) ) ?;
139
- nb:: block!( self . read( ) ) ?;
156
+ nb:: block!( <Self as FullDuplex <W >>:: write( self , word) ) ?;
157
+ nb:: block!( < Self as FullDuplex < W >> :: read( self ) ) ?;
140
158
}
141
159
142
160
Ok ( ( ) )
143
161
}
144
162
}
145
163
146
- impl < SPI , PINS > WriteIter < u8 > for Spi < SPI , PINS , TransferModeBidi >
164
+ impl < SPI , PINS , W : Copy + ' static > WriteIter < W > for Spi < SPI , PINS , TransferModeBidi >
147
165
where
148
- Self : FullDuplex < u8 , Error = Error > ,
166
+ Self : FullDuplex < W , Error = Error > ,
149
167
SPI : Instance ,
150
168
{
151
169
fn write_iter < WI > ( & mut self , words : WI ) -> Result < ( ) , Self :: Error >
152
170
where
153
- WI : IntoIterator < Item = u8 > ,
171
+ WI : IntoIterator < Item = W > ,
154
172
{
155
173
for word in words. into_iter ( ) {
156
- nb:: block!( <Self as FullDuplex <u8 >>:: write( self , word) ) ?;
174
+ nb:: block!( <Self as FullDuplex <W >>:: write( self , word) ) ?;
175
+ }
176
+
177
+ Ok ( ( ) )
178
+ }
179
+ }
180
+
181
+ impl < SPI , PINS , TRANSFER_MODE , W : Copy + Default + ' static > Read < W >
182
+ for Spi < SPI , PINS , TRANSFER_MODE >
183
+ where
184
+ Self : FullDuplex < W , Error = Error > ,
185
+ SPI : Instance ,
186
+ {
187
+ fn read ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Self :: Error > {
188
+ for word in words {
189
+ nb:: block!( <Self as FullDuplex <W >>:: write( self , W :: default ( ) ) ) ?;
190
+ * word = nb:: block!( <Self as FullDuplex <W >>:: read( self ) ) ?;
157
191
}
158
192
159
193
Ok ( ( ) )
@@ -162,14 +196,18 @@ mod blocking {
162
196
163
197
impl < SPI , PINS , TRANSFER_MODE , W : Copy + ' static > Transactional < W > for Spi < SPI , PINS , TRANSFER_MODE >
164
198
where
165
- Self : Write < W , Error = Error > + TransferInplace < W , Error = Error > ,
199
+ Self : Write < W , Error = Error >
200
+ + Read < W , Error = Error >
201
+ + TransferInplace < W , Error = Error >
202
+ + Transfer < W , Error = Error > ,
166
203
{
167
204
fn exec < ' a > ( & mut self , operations : & mut [ Operation < ' a , W > ] ) -> Result < ( ) , Error > {
168
205
for op in operations {
169
206
match op {
170
207
Operation :: Write ( w) => self . write ( w) ?,
171
208
Operation :: TransferInplace ( t) => self . transfer_inplace ( t) ?,
172
- _ => todo ! ( ) ,
209
+ Operation :: Read ( r) => self . read ( r) ?,
210
+ Operation :: Transfer ( w, r) => self . transfer ( w, r) ?,
173
211
}
174
212
}
175
213
0 commit comments