Skip to content

Commit 52d08ee

Browse files
committed
improve spi traits
1 parent 9469cf4 commit 52d08ee

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/dma/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ macro_rules! peripheral_target_instance {
125125
unsafe impl TargetAddress<M2P> for spi::Spi<$peripheral, spi::Disabled, $size> {
126126
#[inline(always)]
127127
fn address(&self) -> usize {
128-
use spi::SpiAll;
128+
use spi::HalSpi;
129129
&self.inner().$txreg as *const _ as usize
130130
}
131131

@@ -137,7 +137,7 @@ macro_rules! peripheral_target_instance {
137137
unsafe impl TargetAddress<P2M> for spi::Spi<$peripheral, spi::Disabled, $size> {
138138
#[inline(always)]
139139
fn address(&self) -> usize {
140-
use spi::SpiAll;
140+
use spi::HalSpi;
141141
&self.inner().$rxreg as *const _ as usize
142142
}
143143

src/prelude.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ pub use crate::sai::SaiPdmExt as _stm32h7xx_hal_spi_SaiPdmExt;
2323
#[cfg(feature = "sdmmc")]
2424
pub use crate::sdmmc::SdmmcExt as _stm32h7xx_hal_sdmmc_SdmmcExt;
2525
pub use crate::serial::SerialExt as _stm32h7xx_hal_serial_SerialExt;
26-
pub use crate::spi::SpiAll as _stm32h7xx_hal_spi_SpiAll;
27-
pub use crate::spi::SpiDisabled as _stm32h7xx_hal_spi_SpiDisabled;
28-
pub use crate::spi::SpiEnabled as _stm32h7xx_hal_spi_SpiEnabled;
26+
pub use crate::spi::HalDisabledSpi as _stm32h7xx_hal_spi_HalDisabledSpi;
27+
pub use crate::spi::HalEnabledSpi as _stm32h7xx_hal_spi_HalEnabledSpi;
28+
pub use crate::spi::HalSpi as _stm32h7xx_hal_spi_HalSpi;
2929
pub use crate::spi::SpiExt as _stm32h7xx_hal_spi_SpiExt;
3030
pub use crate::time::U32Ext as _stm32h7xx_hal_time_U32Ext;
3131
pub use crate::timer::TimerExt as _stm32h7xx_hal_timer_TimerExt;

src/spi.rs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -541,8 +541,10 @@ pub trait SpiExt<SPI, WORD>: Sized {
541541
CONFIG: Into<Config>;
542542
}
543543

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<
546548
Spi = Self::Spi,
547549
Word = Self::Word,
548550
Enabled = Self,
@@ -556,10 +558,16 @@ pub trait SpiEnabled: SpiAll + FullDuplex<Self::Word, Error = Error> {
556558
/// disabled.
557559
fn disable(self) -> Self::Disabled;
558560

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();
563571
}
564572

565573
/// 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> {
585593
fn end_transaction(&mut self) -> Result<(), Error>;
586594
}
587595

588-
pub trait SpiDisabled: SpiAll {
596+
pub trait HalDisabledSpi: HalSpi {
589597
type Rec;
590-
type Enabled: SpiEnabled<Spi = Self::Spi, Word = Self::Word>;
598+
type Enabled: HalEnabledSpi<Spi = Self::Spi, Word = Self::Word>;
591599

592600
/// Enables the SPI peripheral.
593601
/// Clears the MODF flag, the SSI flag, and sets the SPE bit.
@@ -611,7 +619,7 @@ pub trait SpiDisabled: SpiAll {
611619
fn free(self) -> (Self::Spi, Self::Rec);
612620
}
613621

614-
pub trait SpiAll: Sized {
622+
pub trait HalSpi: Sized {
615623
type Spi;
616624
type Word;
617625

@@ -805,7 +813,7 @@ macro_rules! spi {
805813
}
806814
}
807815

808-
impl SpiEnabled for Spi<$SPIX, Enabled, $TY> {
816+
impl HalEnabledSpi for Spi<$SPIX, Enabled, $TY> {
809817
type Disabled = Spi<Self::Spi, Disabled, Self::Word>;
810818

811819
fn disable(self) -> Spi<$SPIX, Disabled, $TY> {
@@ -859,7 +867,7 @@ macro_rules! spi {
859867
}
860868
}
861869

862-
impl SpiDisabled for Spi<$SPIX, Disabled, $TY> {
870+
impl HalDisabledSpi for Spi<$SPIX, Disabled, $TY> {
863871
type Rec = rec::$Rec;
864872
type Enabled = Spi<Self::Spi, Enabled, Self::Word>;
865873

@@ -895,7 +903,7 @@ macro_rules! spi {
895903
}
896904
}
897905

898-
impl<EN> SpiAll for Spi<$SPIX, EN, $TY>
906+
impl<EN> HalSpi for Spi<$SPIX, EN, $TY>
899907
{
900908
type Word = $TY;
901909
type Spi = $SPIX;

0 commit comments

Comments
 (0)