@@ -204,6 +204,7 @@ macro_rules! spi {
204
204
}
205
205
206
206
impl <PINS > Spi <$SPIX, PINS > {
207
+ #[ inline]
207
208
fn nb_read<W : FrameSize >( & mut self ) -> nb:: Result <W , Error > {
208
209
let sr = self . spi. sr. read( ) ;
209
210
Err ( if sr. ovr( ) . bit_is_set( ) {
@@ -222,6 +223,7 @@ macro_rules! spi {
222
223
nb:: Error :: WouldBlock
223
224
} )
224
225
}
226
+ #[ inline]
225
227
fn nb_write<W : FrameSize >( & mut self , word: W ) -> nb:: Result <( ) , Error > {
226
228
let sr = self . spi. sr. read( ) ;
227
229
Err ( if sr. ovr( ) . bit_is_set( ) {
@@ -239,12 +241,14 @@ macro_rules! spi {
239
241
nb:: Error :: WouldBlock
240
242
} )
241
243
}
242
- fn set_tx_only( & mut self ) {
244
+ #[ inline]
245
+ pub fn set_tx_only( & mut self ) {
243
246
self . spi
244
247
. cr1
245
248
. modify( |_, w| w. bidimode( ) . set_bit( ) . bidioe( ) . set_bit( ) ) ;
246
249
}
247
- fn set_bidi( & mut self ) {
250
+ #[ inline]
251
+ pub fn set_bidi( & mut self ) {
248
252
self . spi
249
253
. cr1
250
254
. modify( |_, w| w. bidimode( ) . clear_bit( ) . bidioe( ) . clear_bit( ) ) ;
@@ -285,10 +289,14 @@ macro_rules! spi {
285
289
}
286
290
287
291
fn write( & mut self , words: & [ u8 ] ) -> Result <( ) , Self :: Error > {
292
+ let catch = |spi: & mut Self | Ok ( for w in words {
293
+ nb:: block!( spi. nb_write( * w) ) ?
294
+ } ) ;
295
+
288
296
self . set_tx_only( ) ;
289
- Ok ( for w in words {
290
- nb :: block! ( self . nb_write ( * w ) ) ?
291
- } )
297
+ let res = catch ( self ) ;
298
+ self . set_bidi ( ) ;
299
+ res
292
300
}
293
301
294
302
fn transfer( & mut self , read: & mut [ u8 ] , write: & [ u8 ] ) -> Result <( ) , Self :: Error > {
@@ -333,16 +341,23 @@ macro_rules! spi {
333
341
Ok ( ( ) )
334
342
}
335
343
fn flush( & mut self ) -> Result <( ) , Self :: Error > {
344
+ let catch = |spi: & mut Self | {
345
+ // drain rx fifo
346
+ while match spi. nb_read:: <u8 >( ) {
347
+ Ok ( _) => true ,
348
+ Err ( nb:: Error :: WouldBlock ) => false ,
349
+ Err ( nb:: Error :: Other ( e) ) => { return Err ( e) }
350
+ } { core:: hint:: spin_loop( ) } ;
351
+ // wait for tx fifo to be drained by the peripheral
352
+ while spi. spi. sr. read( ) . ftlvl( ) != 0 { core:: hint:: spin_loop( ) } ;
353
+ Ok ( ( ) )
354
+ } ;
355
+
336
356
// stop receiving data
337
357
self . set_tx_only( ) ;
338
- // wait for tx fifo to be drained by the peripheral
339
- while self . spi. sr. read( ) . ftlvl( ) != 0 { core:: hint:: spin_loop( ) } ;
340
- // drain rx fifo
341
- Ok ( while match self . nb_read:: <u8 >( ) {
342
- Ok ( _) => true ,
343
- Err ( nb:: Error :: WouldBlock ) => false ,
344
- Err ( nb:: Error :: Other ( e) ) => return Err ( e)
345
- } { core:: hint:: spin_loop( ) } )
358
+ let res = catch( self ) ;
359
+ self . set_bidi( ) ;
360
+ res
346
361
}
347
362
}
348
363
0 commit comments