Skip to content

Commit

Permalink
remove duplex traits from spi
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev committed Aug 27, 2024
1 parent a1e111d commit 549cb57
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 36 deletions.
1 change: 1 addition & 0 deletions esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The `AesFlavour` trait no longer has the `ENCRYPT_MODE`/`DECRYPT_MODE` associated constants (#1849)
- Removed `FlashSafeDma` (#1856)
- Remove redundant WithDmaSpi traits (#1975)
- `IsFullDuplex` and `IsHalfDuplex` traits (#1985)

## [0.19.0] - 2024-07-15

Expand Down
49 changes: 20 additions & 29 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ use super::{
Error,
FullDuplexMode,
HalfDuplexMode,
IsFullDuplex,
IsHalfDuplex,
SpiBitOrder,
SpiDataMode,
SpiMode,
Expand Down Expand Up @@ -464,10 +462,9 @@ pub struct Spi<'d, T, M> {
_mode: PhantomData<M>,
}

impl<'d, T, M> Spi<'d, T, M>
impl<'d, T> Spi<'d, T, FullDuplexMode>
where
T: Instance,
M: IsFullDuplex,
{
/// Read bytes from SPI.
///
Expand Down Expand Up @@ -860,10 +857,9 @@ where
}
}

impl<T, M> HalfDuplexReadWrite for Spi<'_, T, M>
impl<T> HalfDuplexReadWrite for Spi<'_, T, HalfDuplexMode>
where
T: Instance,
M: IsHalfDuplex,
{
type Error = Error;

Expand Down Expand Up @@ -908,10 +904,9 @@ where
}

#[cfg(feature = "embedded-hal-02")]
impl<T, M> embedded_hal_02::spi::FullDuplex<u8> for Spi<'_, T, M>
impl<T> embedded_hal_02::spi::FullDuplex<u8> for Spi<'_, T, FullDuplexMode>
where
T: Instance,
M: IsFullDuplex,
{
type Error = Error;

Expand All @@ -925,10 +920,9 @@ where
}

#[cfg(feature = "embedded-hal-02")]
impl<T, M> embedded_hal_02::blocking::spi::Transfer<u8> for Spi<'_, T, M>
impl<T> embedded_hal_02::blocking::spi::Transfer<u8> for Spi<'_, T, FullDuplexMode>
where
T: Instance,
M: IsFullDuplex,
{
type Error = Error;

Expand All @@ -938,10 +932,9 @@ where
}

#[cfg(feature = "embedded-hal-02")]
impl<T, M> embedded_hal_02::blocking::spi::Write<u8> for Spi<'_, T, M>
impl<T> embedded_hal_02::blocking::spi::Write<u8> for Spi<'_, T, FullDuplexMode>
where
T: Instance,
M: IsFullDuplex,
{
type Error = Error;

Expand Down Expand Up @@ -1274,12 +1267,11 @@ pub mod dma {
}
}

impl<'d, T, C, D, M> SpiDma<'d, T, C, D, M>
impl<'d, T, C, M> SpiDma<'d, T, C, FullDuplexMode, M>
where
T: InstanceDma,
C: DmaChannel,
C::P: SpiPeripheral,
D: IsFullDuplex,
M: Mode,
{
/// Perform a DMA write.
Expand All @@ -1292,7 +1284,8 @@ pub mod dma {
pub fn dma_write(
mut self,
buffer: DmaTxBuf,
) -> Result<SpiDmaTransfer<'d, T, C, D, M, DmaTxBuf>, (Error, Self, DmaTxBuf)> {
) -> Result<SpiDmaTransfer<'d, T, C, FullDuplexMode, M, DmaTxBuf>, (Error, Self, DmaTxBuf)>
{
let bytes_to_write = buffer.len();
if bytes_to_write > MAX_DMA_SIZE {
return Err((Error::MaxDmaTransferSizeExceeded, self, buffer));
Expand All @@ -1319,7 +1312,8 @@ pub mod dma {
pub fn dma_read(
mut self,
buffer: DmaRxBuf,
) -> Result<SpiDmaTransfer<'d, T, C, D, M, DmaRxBuf>, (Error, Self, DmaRxBuf)> {
) -> Result<SpiDmaTransfer<'d, T, C, FullDuplexMode, M, DmaRxBuf>, (Error, Self, DmaRxBuf)>
{
let bytes_to_read = buffer.len();
if bytes_to_read > MAX_DMA_SIZE {
return Err((Error::MaxDmaTransferSizeExceeded, self, buffer));
Expand Down Expand Up @@ -1347,7 +1341,7 @@ pub mod dma {
tx_buffer: DmaTxBuf,
rx_buffer: DmaRxBuf,
) -> Result<
SpiDmaTransfer<'d, T, C, D, M, (DmaTxBuf, DmaRxBuf)>,
SpiDmaTransfer<'d, T, C, FullDuplexMode, M, (DmaTxBuf, DmaRxBuf)>,
(Error, Self, DmaTxBuf, DmaRxBuf),
> {
let bytes_to_write = tx_buffer.len();
Expand Down Expand Up @@ -1385,12 +1379,11 @@ pub mod dma {
}
}

impl<'d, T, C, D, M> SpiDma<'d, T, C, D, M>
impl<'d, T, C, M> SpiDma<'d, T, C, HalfDuplexMode, M>
where
T: InstanceDma,
C: DmaChannel,
C::P: SpiPeripheral,
D: IsHalfDuplex,
M: Mode,
{
/// Perform a half-duplex read operation using DMA.
Expand All @@ -1403,7 +1396,8 @@ pub mod dma {
address: Address,
dummy: u8,
buffer: DmaRxBuf,
) -> Result<SpiDmaTransfer<'d, T, C, D, M, DmaRxBuf>, (Error, Self, DmaRxBuf)> {
) -> Result<SpiDmaTransfer<'d, T, C, HalfDuplexMode, M, DmaRxBuf>, (Error, Self, DmaRxBuf)>
{
let bytes_to_read = buffer.len();
if bytes_to_read > MAX_DMA_SIZE {
return Err((Error::MaxDmaTransferSizeExceeded, self, buffer));
Expand Down Expand Up @@ -1480,7 +1474,8 @@ pub mod dma {
address: Address,
dummy: u8,
buffer: DmaTxBuf,
) -> Result<SpiDmaTransfer<'d, T, C, D, M, DmaTxBuf>, (Error, Self, DmaTxBuf)> {
) -> Result<SpiDmaTransfer<'d, T, C, HalfDuplexMode, M, DmaTxBuf>, (Error, Self, DmaTxBuf)>
{
let bytes_to_write = buffer.len();
if bytes_to_write > MAX_DMA_SIZE {
return Err((Error::MaxDmaTransferSizeExceeded, self, buffer));
Expand Down Expand Up @@ -1620,12 +1615,11 @@ pub mod dma {
}
}

impl<'d, T, C, D, M> SpiDmaBus<'d, T, C, D, M>
impl<'d, T, C, M> SpiDmaBus<'d, T, C, FullDuplexMode, M>
where
T: InstanceDma,
C: DmaChannel,
C::P: SpiPeripheral,
D: IsFullDuplex,
M: Mode,
{
/// Reads data from the SPI bus using DMA.
Expand Down Expand Up @@ -1744,12 +1738,11 @@ pub mod dma {
}
}

impl<'d, T, C, D, M> HalfDuplexReadWrite for SpiDmaBus<'d, T, C, D, M>
impl<'d, T, C, M> HalfDuplexReadWrite for SpiDmaBus<'d, T, C, HalfDuplexMode, M>
where
T: InstanceDma,
C: DmaChannel,
C::P: SpiPeripheral,
D: IsHalfDuplex,
M: Mode,
{
type Error = super::Error;
Expand Down Expand Up @@ -2140,10 +2133,9 @@ mod ehal1 {
type Error = super::Error;
}

impl<T, M> FullDuplex for Spi<'_, T, M>
impl<T> FullDuplex for Spi<'_, T, FullDuplexMode>
where
T: Instance,
M: IsFullDuplex,
{
fn read(&mut self) -> nb::Result<u8, Self::Error> {
self.spi.read_byte()
Expand All @@ -2154,10 +2146,9 @@ mod ehal1 {
}
}

impl<T, M> SpiBus for Spi<'_, T, M>
impl<T> SpiBus for Spi<'_, T, FullDuplexMode>
where
T: Instance,
M: IsFullDuplex,
{
fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> {
self.spi.read_bytes(words)
Expand Down
10 changes: 3 additions & 7 deletions esp-hal/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ pub enum SpiBitOrder {
}

/// Trait marker for defining SPI duplex modes.
pub trait DuplexMode {}
/// Trait marker for SPI full-duplex mode.
pub trait IsFullDuplex: DuplexMode {}
/// Trait marker for SPI half-duplex mode.
pub trait IsHalfDuplex: DuplexMode {}
pub trait DuplexMode: crate::private::Sealed {}

/// SPI data mode
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand All @@ -100,9 +96,9 @@ pub enum SpiDataMode {
/// Full-duplex operation
pub struct FullDuplexMode {}
impl DuplexMode for FullDuplexMode {}
impl IsFullDuplex for FullDuplexMode {}
impl crate::private::Sealed for FullDuplexMode {}

/// Half-duplex operation
pub struct HalfDuplexMode {}
impl DuplexMode for HalfDuplexMode {}
impl IsHalfDuplex for HalfDuplexMode {}
impl crate::private::Sealed for HalfDuplexMode {}

0 comments on commit 549cb57

Please sign in to comment.