@@ -207,15 +207,13 @@ pub trait SpiExt: Sized + Instance {
207
207
208
208
fn spi_bidi (
209
209
self ,
210
- pins : (
211
- impl Into < Self :: Sck > ,
212
- impl Into < Self :: Miso > ,
213
- impl Into < Self :: Mosi > ,
214
- ) ,
210
+ pins : ( impl Into < Self :: Sck > , impl Into < Self :: Mosi > ) ,
215
211
mode : impl Into < Mode > ,
216
212
freq : Hertz ,
217
213
clocks : & Clocks ,
218
- ) -> Spi < Self , true , u8 > ;
214
+ ) -> Spi < Self , true , u8 >
215
+ where
216
+ NoPin : Into < Self :: Miso > ;
219
217
220
218
fn spi_slave (
221
219
self ,
@@ -233,11 +231,12 @@ pub trait SpiExt: Sized + Instance {
233
231
pins : (
234
232
impl Into < Self :: Sck > ,
235
233
impl Into < Self :: Miso > ,
236
- impl Into < Self :: Mosi > ,
237
234
Option < Self :: Nss > ,
238
235
) ,
239
236
mode : impl Into < Mode > ,
240
- ) -> SpiSlave < Self , true , u8 > ;
237
+ ) -> SpiSlave < Self , true , u8 >
238
+ where
239
+ NoPin : Into < Self :: Mosi > ;
241
240
}
242
241
243
242
impl < SPI : Instance > SpiExt for SPI {
@@ -266,15 +265,14 @@ impl<SPI: Instance> SpiExt for SPI {
266
265
/// Otherwise it may lead to the 'wrong last bit in every received byte' problem.
267
266
fn spi_bidi (
268
267
self ,
269
- pins : (
270
- impl Into < Self :: Sck > ,
271
- impl Into < Self :: Miso > ,
272
- impl Into < Self :: Mosi > ,
273
- ) ,
268
+ pins : ( impl Into < Self :: Sck > , impl Into < Self :: Mosi > ) ,
274
269
mode : impl Into < Mode > ,
275
270
freq : Hertz ,
276
271
clocks : & Clocks ,
277
- ) -> Spi < Self , true , u8 > {
272
+ ) -> Spi < Self , true , u8 >
273
+ where
274
+ NoPin : Into < Self :: Miso > ,
275
+ {
278
276
Spi :: new_bidi ( self , pins, mode, freq, clocks)
279
277
}
280
278
/// Enables the SPI clock, resets the peripheral, sets `Alternate` mode for `pins` and initialize the peripheral as SPI Slave Normal mode.
@@ -304,11 +302,13 @@ impl<SPI: Instance> SpiExt for SPI {
304
302
pins : (
305
303
impl Into < Self :: Sck > ,
306
304
impl Into < Self :: Miso > ,
307
- impl Into < Self :: Mosi > ,
308
305
Option < Self :: Nss > ,
309
306
) ,
310
307
mode : impl Into < Mode > ,
311
- ) -> SpiSlave < Self , true , u8 > {
308
+ ) -> SpiSlave < Self , true , u8 >
309
+ where
310
+ NoPin : Into < Self :: Mosi > ,
311
+ {
312
312
SpiSlave :: new_bidi ( self , pins, mode)
313
313
}
314
314
}
@@ -447,21 +447,20 @@ impl<SPI: Instance> Spi<SPI, true, u8> {
447
447
/// Otherwise it may lead to the 'wrong last bit in every received byte' problem.
448
448
pub fn new_bidi (
449
449
spi : SPI ,
450
- pins : (
451
- impl Into < SPI :: Sck > ,
452
- impl Into < SPI :: Miso > ,
453
- impl Into < SPI :: Mosi > ,
454
- ) ,
450
+ pins : ( impl Into < SPI :: Sck > , impl Into < SPI :: Mosi > ) ,
455
451
mode : impl Into < Mode > ,
456
452
freq : Hertz ,
457
453
clocks : & Clocks ,
458
- ) -> Self {
454
+ ) -> Self
455
+ where
456
+ NoPin : Into < SPI :: Miso > ,
457
+ {
459
458
unsafe {
460
459
SPI :: enable_unchecked ( ) ;
461
460
SPI :: reset_unchecked ( ) ;
462
461
}
463
462
464
- let pins = ( pins. 0 . into ( ) , pins . 1 . into ( ) , pins. 2 . into ( ) ) ;
463
+ let pins = ( pins. 0 . into ( ) , NoPin :: new ( ) . into ( ) , pins. 1 . into ( ) ) ;
465
464
466
465
Self :: _new ( spi, pins)
467
466
. pre_init ( mode. into ( ) , freq, SPI :: clock ( clocks) )
@@ -504,20 +503,18 @@ impl<SPI: Instance> SpiSlave<SPI, true, u8> {
504
503
/// Otherwise it may lead to the 'wrong last bit in every received byte' problem.
505
504
pub fn new_bidi (
506
505
spi : SPI ,
507
- pins : (
508
- impl Into < SPI :: Sck > ,
509
- impl Into < SPI :: Miso > ,
510
- impl Into < SPI :: Mosi > ,
511
- Option < SPI :: Nss > ,
512
- ) ,
506
+ pins : ( impl Into < SPI :: Sck > , impl Into < SPI :: Miso > , Option < SPI :: Nss > ) ,
513
507
mode : impl Into < Mode > ,
514
- ) -> Self {
508
+ ) -> Self
509
+ where
510
+ NoPin : Into < SPI :: Mosi > ,
511
+ {
515
512
unsafe {
516
513
SPI :: enable_unchecked ( ) ;
517
514
SPI :: reset_unchecked ( ) ;
518
515
}
519
516
520
- let pins = ( pins. 0 . into ( ) , pins. 1 . into ( ) , pins . 2 . into ( ) , pins. 3 ) ;
517
+ let pins = ( pins. 0 . into ( ) , pins. 1 . into ( ) , NoPin :: new ( ) . into ( ) , pins. 2 ) ;
521
518
522
519
Self :: _new ( spi, pins) . pre_init ( mode. into ( ) ) . init ( )
523
520
}
@@ -729,6 +726,16 @@ impl<SPI: Instance> Inner<SPI> {
729
726
self . spi . sr . read ( ) . ovr ( ) . bit_is_set ( )
730
727
}
731
728
729
+ #[ inline]
730
+ fn bidi_output ( & mut self ) {
731
+ self . spi . cr1 . modify ( |_, w| w. bidioe ( ) . set_bit ( ) ) ;
732
+ }
733
+
734
+ #[ inline]
735
+ fn bidi_input ( & mut self ) {
736
+ self . spi . cr1 . modify ( |_, w| w. bidioe ( ) . set_bit ( ) ) ;
737
+ }
738
+
732
739
fn read_data_reg < W : FrameSize > ( & mut self ) -> W {
733
740
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
734
741
// reading a half-word)
@@ -863,14 +870,14 @@ unsafe impl<SPI, STREAM, const CHANNEL: u8> DMASet<STREAM, CHANNEL, MemoryToPeri
863
870
impl < SPI : Instance , const BIDI : bool , W : FrameSize > Spi < SPI , BIDI , W > {
864
871
pub fn read_nonblocking ( & mut self ) -> nb:: Result < W , Error > {
865
872
if BIDI {
866
- self . spi . cr1 . modify ( |_ , w| w . bidioe ( ) . clear_bit ( ) ) ;
873
+ self . bidi_input ( ) ;
867
874
}
868
875
self . check_read ( )
869
876
}
870
877
871
878
pub fn write_nonblocking ( & mut self , byte : W ) -> nb:: Result < ( ) , Error > {
872
879
if BIDI {
873
- self . spi . cr1 . modify ( |_ , w| w . bidioe ( ) . set_bit ( ) ) ;
880
+ self . bidi_output ( ) ;
874
881
}
875
882
self . check_send ( byte)
876
883
}
@@ -900,20 +907,48 @@ impl<SPI: Instance, const BIDI: bool, W: FrameSize> Spi<SPI, BIDI, W> {
900
907
}
901
908
902
909
pub fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Error > {
903
- for word in words {
904
- nb:: block!( self . write_nonblocking( * word) ) ?;
905
- if !BIDI {
906
- nb:: block!( self . read_nonblocking( ) ) ?;
910
+ if BIDI {
911
+ self . bidi_output ( ) ;
912
+ for word in words {
913
+ nb:: block!( self . check_send( * word) ) ?;
914
+ }
915
+ } else {
916
+ for word in words {
917
+ nb:: block!( self . check_send( * word) ) ?;
918
+ nb:: block!( self . check_read:: <W >( ) ) ?;
919
+ }
920
+ }
921
+
922
+ Ok ( ( ) )
923
+ }
924
+
925
+ pub fn write_iter ( & mut self , words : impl IntoIterator < Item = W > ) -> Result < ( ) , Error > {
926
+ if BIDI {
927
+ self . bidi_output ( ) ;
928
+ for word in words. into_iter ( ) {
929
+ nb:: block!( self . check_send( word) ) ?;
930
+ }
931
+ } else {
932
+ for word in words. into_iter ( ) {
933
+ nb:: block!( self . check_send( word) ) ?;
934
+ nb:: block!( self . check_read:: <W >( ) ) ?;
907
935
}
908
936
}
909
937
910
938
Ok ( ( ) )
911
939
}
912
940
913
941
pub fn read ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Error > {
914
- for word in words {
915
- nb:: block!( self . write_nonblocking( W :: default ( ) ) ) ?;
916
- * word = nb:: block!( self . read_nonblocking( ) ) ?;
942
+ if BIDI {
943
+ self . bidi_input ( ) ;
944
+ for word in words {
945
+ * word = nb:: block!( self . check_read( ) ) ?;
946
+ }
947
+ } else {
948
+ for word in words {
949
+ nb:: block!( self . check_send( W :: default ( ) ) ) ?;
950
+ * word = nb:: block!( self . check_read( ) ) ?;
951
+ }
917
952
}
918
953
919
954
Ok ( ( ) )
@@ -923,14 +958,14 @@ impl<SPI: Instance, const BIDI: bool, W: FrameSize> Spi<SPI, BIDI, W> {
923
958
impl < SPI : Instance , const BIDI : bool , W : FrameSize > SpiSlave < SPI , BIDI , W > {
924
959
pub fn read_nonblocking ( & mut self ) -> nb:: Result < W , Error > {
925
960
if BIDI {
926
- self . spi . cr1 . modify ( |_ , w| w . bidioe ( ) . clear_bit ( ) ) ;
961
+ self . bidi_input ( ) ;
927
962
}
928
963
self . check_read ( )
929
964
}
930
965
931
966
pub fn write_nonblocking ( & mut self , byte : W ) -> nb:: Result < ( ) , Error > {
932
967
if BIDI {
933
- self . spi . cr1 . modify ( |_ , w| w . bidioe ( ) . set_bit ( ) ) ;
968
+ self . bidi_output ( ) ;
934
969
}
935
970
self . check_send ( byte)
936
971
}
@@ -960,20 +995,32 @@ impl<SPI: Instance, const BIDI: bool, W: FrameSize> SpiSlave<SPI, BIDI, W> {
960
995
}
961
996
962
997
pub fn write ( & mut self , words : & [ W ] ) -> Result < ( ) , Error > {
963
- for word in words {
964
- nb:: block!( self . write_nonblocking( * word) ) ?;
965
- if !BIDI {
966
- nb:: block!( self . read_nonblocking( ) ) ?;
998
+ if BIDI {
999
+ self . bidi_output ( ) ;
1000
+ for word in words {
1001
+ nb:: block!( self . check_send( * word) ) ?;
1002
+ }
1003
+ } else {
1004
+ for word in words {
1005
+ nb:: block!( self . check_send( * word) ) ?;
1006
+ nb:: block!( self . check_read:: <W >( ) ) ?;
967
1007
}
968
1008
}
969
1009
970
1010
Ok ( ( ) )
971
1011
}
972
1012
973
1013
pub fn read ( & mut self , words : & mut [ W ] ) -> Result < ( ) , Error > {
974
- for word in words {
975
- nb:: block!( self . write_nonblocking( W :: default ( ) ) ) ?;
976
- * word = nb:: block!( self . read_nonblocking( ) ) ?;
1014
+ if BIDI {
1015
+ self . bidi_input ( ) ;
1016
+ for word in words {
1017
+ * word = nb:: block!( self . check_read( ) ) ?;
1018
+ }
1019
+ } else {
1020
+ for word in words {
1021
+ nb:: block!( self . check_send( W :: default ( ) ) ) ?;
1022
+ * word = nb:: block!( self . check_read( ) ) ?;
1023
+ }
977
1024
}
978
1025
979
1026
Ok ( ( ) )
0 commit comments