@@ -558,17 +558,8 @@ pub trait HalEnabledSpi:
558
558
/// disabled.
559
559
fn disable ( self ) -> Self :: Disabled ;
560
560
561
- /// Resets the SPI peripheral. This is just a call to [HalEnabledSpi::disable]
562
- /// and [HalDisabledSpi::enable]
563
- fn reset ( & mut self ) {
564
- // SAFETY: we are essentially `core::mem::take`ing here without replacing with a default
565
- // value. This is safe because we are not using the old value and are replacing it with
566
- // a valid value at the end of the function. `mem::take`ing is needed because the `disable`
567
- // and `enable` methods take ownership of the HAL SPI.
568
- let spi_enabled: Self = unsafe { core:: mem:: transmute_copy ( self ) } ;
569
- let spi_disabled = spi_enabled. disable ( ) ;
570
- * self = spi_disabled. enable ( ) ;
571
- }
561
+ /// Resets the SPI peripheral.
562
+ fn reset ( & mut self ) ;
572
563
573
564
/// Sets up a frame transaction with the given amount of data words.
574
565
///
@@ -689,7 +680,7 @@ macro_rules! spi {
689
680
// For each $TY
690
681
$(
691
682
692
- impl Spi <$SPIX, Enabled , $TY> {
683
+ impl Spi <$SPIX, Enabled , $TY> {
693
684
fn $spiX<T , CONFIG >(
694
685
spi: $SPIX,
695
686
config: CONFIG ,
@@ -813,14 +804,33 @@ macro_rules! spi {
813
804
}
814
805
}
815
806
807
+ impl <Ed > Spi <$SPIX, Ed , $TY> {
808
+ /// internally disable the SPI without changing its type-state
809
+ fn internal_disable( & mut self ) {
810
+ self . spi. cr1. modify( |_, w| w. csusp( ) . requested( ) ) ;
811
+ while self . spi. sr. read( ) . eot( ) . is_completed( ) { }
812
+ self . spi. cr1. write( |w| w. ssi( ) . slave_not_selected( ) . spe( ) . disabled( ) ) ;
813
+ }
814
+
815
+ /// internally enable the SPI without changing its type-state
816
+ fn internal_enable( & mut self ) {
817
+ self . clear_modf( ) ; // SPE cannot be set when MODF is set
818
+ self . spi. cr1. write( |w| w. ssi( ) . slave_not_selected( ) . spe( ) . enabled( ) ) ;
819
+ }
820
+ }
821
+
816
822
impl HalEnabledSpi for Spi <$SPIX, Enabled , $TY> {
817
823
type Disabled = Spi <Self :: Spi , Disabled , Self :: Word >;
818
824
819
- fn disable( self ) -> Spi <$SPIX, Disabled , $TY> {
825
+ fn reset( & mut self ) {
826
+ self . internal_disable( ) ;
827
+ self . internal_enable( ) ;
828
+ }
829
+
830
+ fn disable( mut self ) -> Spi <$SPIX, Disabled , $TY> {
820
831
// Master communication must be suspended before the peripheral is disabled
821
- self . spi. cr1. modify( |_, w| w. csusp( ) . requested( ) ) ;
822
- while self . spi. sr. read( ) . eot( ) . is_completed( ) { }
823
- self . spi. cr1. write( |w| w. ssi( ) . slave_not_selected( ) . spe( ) . disabled( ) ) ;
832
+ self . internal_disable( ) ;
833
+
824
834
Spi {
825
835
spi: self . spi,
826
836
hardware_cs_mode: self . hardware_cs_mode,
@@ -872,8 +882,7 @@ macro_rules! spi {
872
882
type Enabled = Spi <Self :: Spi , Enabled , Self :: Word >;
873
883
874
884
fn enable( mut self ) -> Self :: Enabled {
875
- self . clear_modf( ) ; // SPE cannot be set when MODF is set
876
- self . spi. cr1. write( |w| w. ssi( ) . slave_not_selected( ) . spe( ) . enabled( ) ) ;
885
+ self . internal_enable( ) ;
877
886
Spi {
878
887
spi: self . spi,
879
888
hardware_cs_mode: self . hardware_cs_mode,
0 commit comments