Skip to content

Commit a17425f

Browse files
committed
fix @richardeoin's comments
1 parent 52d08ee commit a17425f

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/spi.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -558,17 +558,8 @@ pub trait HalEnabledSpi:
558558
/// disabled.
559559
fn disable(self) -> Self::Disabled;
560560

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);
572563

573564
/// Sets up a frame transaction with the given amount of data words.
574565
///
@@ -689,7 +680,7 @@ macro_rules! spi {
689680
// For each $TY
690681
$(
691682

692-
impl Spi<$SPIX, Enabled, $TY> {
683+
impl Spi<$SPIX, Enabled, $TY> {
693684
fn $spiX<T, CONFIG>(
694685
spi: $SPIX,
695686
config: CONFIG,
@@ -813,14 +804,33 @@ macro_rules! spi {
813804
}
814805
}
815806

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+
816822
impl HalEnabledSpi for Spi<$SPIX, Enabled, $TY> {
817823
type Disabled = Spi<Self::Spi, Disabled, Self::Word>;
818824

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> {
820831
// 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+
824834
Spi {
825835
spi: self.spi,
826836
hardware_cs_mode: self.hardware_cs_mode,
@@ -872,8 +882,7 @@ macro_rules! spi {
872882
type Enabled = Spi<Self::Spi, Enabled, Self::Word>;
873883

874884
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();
877886
Spi {
878887
spi: self.spi,
879888
hardware_cs_mode: self.hardware_cs_mode,

0 commit comments

Comments
 (0)