@@ -119,6 +119,18 @@ pub struct Master;
119
119
/// Spi in Slave mode (type state)
120
120
pub struct Slave ;
121
121
122
+ pub trait Ms {
123
+ const MSTR : bool ;
124
+ }
125
+
126
+ impl Ms for Slave {
127
+ const MSTR : bool = false ;
128
+ }
129
+
130
+ impl Ms for Master {
131
+ const MSTR : bool = true ;
132
+ }
133
+
122
134
#[ derive( Debug ) ]
123
135
pub struct Spi < SPI , PINS , const BIDI : bool = false, OPERATION = Master > {
124
136
spi : SPI ,
@@ -253,12 +265,14 @@ impl<SPI: Instance> SpiExt for SPI {
253
265
}
254
266
}
255
267
256
- impl < SPI : Instance , PINS , const BIDI : bool , OPERATION > Spi < SPI , PINS , BIDI , OPERATION > {
268
+ impl < SPI : Instance , PINS , const BIDI : bool , OPERATION : Ms > Spi < SPI , PINS , BIDI , OPERATION > {
257
269
pub fn init ( self ) -> Self {
258
270
self . spi . cr1 . modify ( |_, w| {
259
271
// bidimode: 2-line or 1-line unidirectional
260
272
w. bidimode ( ) . bit ( BIDI ) ;
261
273
w. bidioe ( ) . bit ( BIDI ) ;
274
+ // master/slave mode
275
+ w. mstr ( ) . bit ( OPERATION :: MSTR ) ;
262
276
// spe: enable the SPI bus
263
277
w. spe ( ) . set_bit ( )
264
278
} ) ;
@@ -267,35 +281,27 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
267
281
}
268
282
}
269
283
270
- impl < SPI : Instance , PINS , OPERATION > Spi < SPI , PINS , false , OPERATION > {
284
+ impl < SPI : Instance , PINS , OPERATION : Ms > Spi < SPI , PINS , false , OPERATION > {
271
285
pub fn to_bidi_transfer_mode ( self ) -> Spi < SPI , PINS , true , OPERATION > {
272
- let mut dev_w_new_t_mode = self . into_mode :: < true > ( ) ;
273
- dev_w_new_t_mode. enable ( false ) ;
274
- dev_w_new_t_mode. init ( )
286
+ self . into_mode ( )
275
287
}
276
288
}
277
289
278
- impl < SPI : Instance , PINS , OPERATION > Spi < SPI , PINS , true , OPERATION > {
290
+ impl < SPI : Instance , PINS , OPERATION : Ms > Spi < SPI , PINS , true , OPERATION > {
279
291
pub fn to_normal_transfer_mode ( self ) -> Spi < SPI , PINS , false , OPERATION > {
280
- let mut dev_w_new_t_mode = self . into_mode :: < false > ( ) ;
281
- dev_w_new_t_mode. enable ( false ) ;
282
- dev_w_new_t_mode. init ( )
292
+ self . into_mode ( )
283
293
}
284
294
}
285
295
286
296
impl < SPI : Instance , PINS , const BIDI : bool > Spi < SPI , PINS , BIDI , Master > {
287
297
pub fn to_slave_operation ( self ) -> Spi < SPI , PINS , BIDI , Slave > {
288
- let mut dev_w_new_operation = self . into_operation :: < Slave > ( ) ;
289
- dev_w_new_operation. enable ( false ) ;
290
- dev_w_new_operation. init ( )
298
+ self . into_mode ( )
291
299
}
292
300
}
293
301
294
302
impl < SPI : Instance , PINS , const BIDI : bool > Spi < SPI , PINS , BIDI , Slave > {
295
303
pub fn to_master_operation ( self ) -> Spi < SPI , PINS , BIDI , Master > {
296
- let mut dev_w_new_operation = self . into_operation :: < Master > ( ) ;
297
- dev_w_new_operation. enable ( false ) ;
298
- dev_w_new_operation. init ( )
304
+ self . into_mode ( )
299
305
}
300
306
}
301
307
@@ -424,14 +430,11 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
424
430
}
425
431
}
426
432
427
- /// Convert the spi to another transfer mode.
428
- fn into_mode < const BIDI2 : bool > ( self ) -> Spi < SPI , PINS , BIDI2 , OPERATION > {
429
- Spi :: _new ( self . spi , self . pins )
430
- }
431
-
432
- /// Convert the spi to another operation mode.
433
- fn into_operation < OPERATION2 > ( self ) -> Spi < SPI , PINS , BIDI , OPERATION2 > {
434
- Spi :: _new ( self . spi , self . pins )
433
+ /// Convert the spi to another mode.
434
+ fn into_mode < const BIDI2 : bool , OPERATION2 : Ms > ( self ) -> Spi < SPI , PINS , BIDI2 , OPERATION2 > {
435
+ let mut spi = Spi :: _new ( self . spi , self . pins ) ;
436
+ spi. enable ( false ) ;
437
+ spi. init ( )
435
438
}
436
439
437
440
/// Enable/disable spi
@@ -505,18 +508,32 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
505
508
506
509
/// Return `true` if the TXE flag is set, i.e. new data to transmit
507
510
/// can be written to the SPI.
511
+ #[ inline]
508
512
pub fn is_tx_empty ( & self ) -> bool {
509
513
self . spi . sr . read ( ) . txe ( ) . bit_is_set ( )
510
514
}
515
+ #[ inline]
516
+ #[ deprecated( since = "0.14.0" , note = "please use `is_tx_empty` instead" ) ]
517
+ pub fn is_txe ( & self ) -> bool {
518
+ self . is_tx_empty ( )
519
+ }
511
520
512
521
/// Return `true` if the RXNE flag is set, i.e. new data has been received
513
522
/// and can be read from the SPI.
523
+ #[ inline]
514
524
pub fn is_rx_not_empty ( & self ) -> bool {
515
525
self . spi . sr . read ( ) . rxne ( ) . bit_is_set ( )
516
526
}
517
527
528
+ #[ inline]
529
+ #[ deprecated( since = "0.14.0" , note = "please use `is_rx_not_empty` instead" ) ]
530
+ pub fn is_rxne ( & self ) -> bool {
531
+ self . is_rx_not_empty ( )
532
+ }
533
+
518
534
/// Return `true` if the MODF flag is set, i.e. the SPI has experienced a
519
535
/// Master Mode Fault. (see chapter 28.3.10 of the STM32F4 Reference Manual)
536
+ #[ inline]
520
537
pub fn is_modf ( & self ) -> bool {
521
538
self . spi . sr . read ( ) . modf ( ) . bit_is_set ( )
522
539
}
@@ -529,6 +546,7 @@ impl<SPI: Instance, PINS, const BIDI: bool, OPERATION> Spi<SPI, PINS, BIDI, OPER
529
546
530
547
/// Return `true` if the OVR flag is set, i.e. new data has been received
531
548
/// while the receive data register was already filled.
549
+ #[ inline]
532
550
pub fn is_overrun ( & self ) -> bool {
533
551
self . spi . sr . read ( ) . ovr ( ) . bit_is_set ( )
534
552
}
0 commit comments