@@ -541,8 +541,10 @@ pub trait SpiExt<SPI, WORD>: Sized {
541
541
CONFIG : Into < Config > ;
542
542
}
543
543
544
- pub trait SpiEnabled : SpiAll + FullDuplex < Self :: Word , Error = Error > {
545
- type Disabled : SpiDisabled <
544
+ pub trait HalEnabledSpi :
545
+ HalSpi + FullDuplex < Self :: Word , Error = Error >
546
+ {
547
+ type Disabled : HalDisabledSpi <
546
548
Spi = Self :: Spi ,
547
549
Word = Self :: Word ,
548
550
Enabled = Self ,
@@ -556,10 +558,16 @@ pub trait SpiEnabled: SpiAll + FullDuplex<Self::Word, Error = Error> {
556
558
/// disabled.
557
559
fn disable ( self ) -> Self :: Disabled ;
558
560
559
- /// Resets the SPI peripheral. This is just a call to [SpiEnabled::disable]
560
- /// and [SpiDisabled::enable]
561
- fn reset ( self ) -> Self {
562
- self . disable ( ) . enable ( )
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 ( ) ;
563
571
}
564
572
565
573
/// Sets up a frame transaction with the given amount of data words.
@@ -585,9 +593,9 @@ pub trait SpiEnabled: SpiAll + FullDuplex<Self::Word, Error = Error> {
585
593
fn end_transaction ( & mut self ) -> Result < ( ) , Error > ;
586
594
}
587
595
588
- pub trait SpiDisabled : SpiAll {
596
+ pub trait HalDisabledSpi : HalSpi {
589
597
type Rec ;
590
- type Enabled : SpiEnabled < Spi = Self :: Spi , Word = Self :: Word > ;
598
+ type Enabled : HalEnabledSpi < Spi = Self :: Spi , Word = Self :: Word > ;
591
599
592
600
/// Enables the SPI peripheral.
593
601
/// Clears the MODF flag, the SSI flag, and sets the SPE bit.
@@ -611,7 +619,7 @@ pub trait SpiDisabled: SpiAll {
611
619
fn free ( self ) -> ( Self :: Spi , Self :: Rec ) ;
612
620
}
613
621
614
- pub trait SpiAll : Sized {
622
+ pub trait HalSpi : Sized {
615
623
type Spi ;
616
624
type Word ;
617
625
@@ -805,7 +813,7 @@ macro_rules! spi {
805
813
}
806
814
}
807
815
808
- impl SpiEnabled for Spi <$SPIX, Enabled , $TY> {
816
+ impl HalEnabledSpi for Spi <$SPIX, Enabled , $TY> {
809
817
type Disabled = Spi <Self :: Spi , Disabled , Self :: Word >;
810
818
811
819
fn disable( self ) -> Spi <$SPIX, Disabled , $TY> {
@@ -859,7 +867,7 @@ macro_rules! spi {
859
867
}
860
868
}
861
869
862
- impl SpiDisabled for Spi <$SPIX, Disabled , $TY> {
870
+ impl HalDisabledSpi for Spi <$SPIX, Disabled , $TY> {
863
871
type Rec = rec:: $Rec;
864
872
type Enabled = Spi <Self :: Spi , Enabled , Self :: Word >;
865
873
@@ -895,7 +903,7 @@ macro_rules! spi {
895
903
}
896
904
}
897
905
898
- impl <EN > SpiAll for Spi <$SPIX, EN , $TY>
906
+ impl <EN > HalSpi for Spi <$SPIX, EN , $TY>
899
907
{
900
908
type Word = $TY;
901
909
type Spi = $SPIX;
0 commit comments