Skip to content

Commit

Permalink
changed(spi): Remove explicit flushes from SpiBus impl
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Dec 6, 2024
1 parent 438396e commit 45394f3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 25 deletions.
18 changes: 18 additions & 0 deletions hal/src/sercom/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@
//! let rcvd: u16 = block!(spi.read());
//! ```
//!
//! ## Flushing the bus
//!
//! The [`SpiBus`](crate::ehal::spi::SpiBus) methods do not flush the bus when a
//! transaction is complete. This is in part to increase performance and allow
//! for pipelining SPI transactions. This is true for both sync and async
//! operation. As such, you should ensure you manually call
//! [`flush`](crate::ehal::spi::SpiBus::flush) when:
//! * You must synchronize SPI activity and GPIO activity, for example before
//! deasserting a CS pin.
//! * Before deinitializing the SPI peripheral.
//!
//! Take note that the [`SpiDevice`](crate::ehal::spi::SpiDevice)
//! implementations automatically take care of flushing, so no further flushing
//! is needed.
//!
//! [See the embedded-hal spec](https://docs.rs/embedded-hal/latest/embedded_hal/spi/index.html#flushing)
//! for more information.
//!
//! # [`PanicOnRead`] and [`PanicOnWrite`]
//!
//! Some driver libraries take a type implementing [`embedded_hal::spi::SpiBus`]
Expand Down
5 changes: 2 additions & 3 deletions hal/src/sercom/spi/async_api/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ where
self.spi.config.as_mut().regs.rx_enable();
}

self.flush_tx().await;
tx_result?;
Ok(words.len())
}
Expand Down Expand Up @@ -229,7 +228,7 @@ where
);

// Check for overflows or DMA errors
self.flush_tx_rx().await?;
self.spi.read_status().check_bus_error()?;
rx_result.and(tx_result)?;
Ok(())
}
Expand Down Expand Up @@ -365,7 +364,7 @@ where
};

// Check for overflows or DMA errors
self.flush_tx_rx().await?;
self.spi.read_status().check_bus_error()?;
rx_result.and(tx_result)?;
Ok(())
}
Expand Down
7 changes: 0 additions & 7 deletions hal/src/sercom/spi/async_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,13 +283,6 @@ where
async fn flush_rx(&mut self) -> Result<(), Error> {
self.wait_flags(Flags::RXC).await
}

/// Wait on TXC and RXC flags
#[inline]
#[cfg(feature = "dma")]
async fn flush_tx_rx(&mut self) -> Result<(), Error> {
self.wait_flags(Flags::TXC | Flags::RXC).await
}
}

impl<C, A, R, T> AsRef<Spi<C, A, R, T>> for SpiFuture<C, A, R, T>
Expand Down
7 changes: 3 additions & 4 deletions hal/src/sercom/spi/impl_ehal/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ where
}

self._tx_channel.as_mut().xfer_success()?;
self.flush_tx();
Ok(buf.len())
}
}
Expand Down Expand Up @@ -119,7 +118,7 @@ where
rx.stop();

// Check for overflows or DMA errors
self.flush_tx_rx()?;
self.read_status().check_bus_error()?;
self._rx_channel
.as_mut()
.xfer_success()
Expand Down Expand Up @@ -259,7 +258,7 @@ where
rx.stop();

// Check for overflows or DMA errors
self.flush_tx_rx()?;
self.read_status().check_bus_error()?;
self._rx_channel
.as_mut()
.xfer_success()
Expand Down Expand Up @@ -368,7 +367,7 @@ where
rx.stop();

// Check for overflows or DMA errors
self.flush_rx()?;
self.read_status().check_bus_error()?;
self._rx_channel.as_mut().xfer_success()?;
Ok(buf.len())
}
Expand Down
11 changes: 0 additions & 11 deletions hal/src/sercom/spi/impl_ehal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ where
*r = self.transfer_word_in_place(*w)?;
}

self.flush_tx();
Ok(())
}

Expand All @@ -129,13 +128,6 @@ where
fn flush_rx(&mut self) -> Result<(), Error> {
self.block_on_flags(Flags::RXC)
}

/// Wait on TXC and RXC flags
#[inline]
#[cfg(feature = "dma")]
fn flush_tx_rx(&mut self) -> Result<(), Error> {
self.block_on_flags(Flags::TXC | Flags::RXC)
}
}

// Implementations specific to Master mode SPIs.
Expand All @@ -156,7 +148,6 @@ where
for word in words.iter_mut() {
*word = self.transfer_word_in_place(self.config.nop_word.as_())?;
}
self.flush_tx();
Ok(())
}

Expand All @@ -171,7 +162,6 @@ where
self.write_data(word.as_());
}
}
self.flush_tx();

// Reenable receiver only if necessary
if D::RX_ENABLE {
Expand Down Expand Up @@ -213,7 +203,6 @@ where
*word = read;
}

self.flush_tx();
Ok(())
}

Expand Down

0 comments on commit 45394f3

Please sign in to comment.