From 576fb23faabf6df7f2c9ed2039e94d3586a3788f Mon Sep 17 00:00:00 2001 From: Elias Hanelt Date: Thu, 6 Nov 2025 17:04:20 -0800 Subject: add bidi mode to spi --- embassy-stm32/src/spi/mod.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index abb80ed26..89553f129 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -54,6 +54,16 @@ pub enum BitOrder { MsbFirst, } +/// SPI Direction. +#[derive(Debug, PartialEq, Eq, Clone, Copy)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +pub enum Direction { + /// Transmit + Transmit, + /// Receive + Receive, +} + /// SPI configuration. #[non_exhaustive] #[derive(Copy, Clone)] @@ -348,6 +358,19 @@ impl<'d, M: PeriMode, CM: CommunicationMode> Spi<'d, M, CM> { Ok(()) } + /// Set SPI direction + pub fn set_direction(&mut self, dir: Option) { + let (bidimode, bidioe) = match dir { + Some(Direction::Transmit) => (vals::Bidimode::BIDIRECTIONAL, vals::Bidioe::TRANSMIT), + Some(Direction::Receive) => (vals::Bidimode::BIDIRECTIONAL, vals::Bidioe::RECEIVE), + None => (vals::Bidimode::UNIDIRECTIONAL, vals::Bidioe::TRANSMIT), + }; + self.info.regs.cr1().modify(|w| { + w.set_bidimode(bidimode); + w.set_bidioe(bidioe); + }); + } + /// Get current SPI configuration. pub fn get_current_config(&self) -> Config { #[cfg(any(spi_v1, spi_v2, spi_v3))] @@ -708,6 +731,29 @@ impl<'d> Spi<'d, Async, Master> { ) } + /// Create a new SPI driver, in bidirectional mode + pub fn new_bidi( + peri: Peri<'d, T>, + sck: Peri<'d, if_afio!(impl SckPin)>, + sdio: Peri<'d, if_afio!(impl MosiPin)>, + tx_dma: Peri<'d, impl TxDma>, + rx_dma: Peri<'d, impl RxDma>, + config: Config, + ) -> Self { + let mut this = Self::new_inner( + peri, + new_pin!(sck, config.sck_af()), + new_pin!(sdio, AfType::output(OutputType::PushPull, config.gpio_speed)), + None, + None, + new_dma!(tx_dma), + new_dma!(rx_dma), + config, + ); + this.set_direction(Some(Direction::Transmit)); + this + } + /// Create a new SPI driver, in TX-only mode, without SCK pin. /// /// This can be useful for bit-banging non-SPI protocols. -- cgit From 88ce8314c5cd653ac245cc5dbf367f6814f24be9 Mon Sep 17 00:00:00 2001 From: Elias Hanelt Date: Wed, 26 Nov 2025 10:08:13 -0800 Subject: fix i2c slave RX early termination handling --- embassy-stm32/src/i2c/v2.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs index b2ba94e21..6b213484c 100644 --- a/embassy-stm32/src/i2c/v2.rs +++ b/embassy-stm32/src/i2c/v2.rs @@ -1600,7 +1600,8 @@ impl<'d, M: Mode> I2c<'d, M, MultiMaster> { for byte in chunk { // Wait until we have received something match self.wait_rxne(timeout) { - Ok(ReceiveResult::StopReceived) | Ok(ReceiveResult::NewStart) => { + Ok(ReceiveResult::StopReceived) => {} + Ok(ReceiveResult::NewStart) => { trace!("--- Slave RX transmission end (early)"); return Ok(total_len - remaining_len); // Return N bytes read } -- cgit From dd0a3a1f0b183b547a3fc574c8ddf82703ecb10e Mon Sep 17 00:00:00 2001 From: Elias Hanelt Date: Fri, 28 Nov 2025 12:09:26 -0800 Subject: modify changelog --- embassy-stm32/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/embassy-stm32/CHANGELOG.md b/embassy-stm32/CHANGELOG.md index 2b273482c..2a99d0a96 100644 --- a/embassy-stm32/CHANGELOG.md +++ b/embassy-stm32/CHANGELOG.md @@ -85,6 +85,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - fix: build script ensures EXTI2_TSC is listed as the IRQ of EXTI2 even if the PAC doesn't - feat: stm32/lcd: added implementation - change: add error messages to can timing calculations ([#4961](https://github.com/embassy-rs/embassy/pull/4961)) +- fix: stm32/i2c v2: add stop flag on stop received ## 0.4.0 - 2025-08-26 -- cgit