diff options
| author | Elias Hanelt <[email protected]> | 2025-11-06 17:04:20 -0800 |
|---|---|---|
| committer | Elias Hanelt <[email protected]> | 2025-11-11 21:07:48 -0800 |
| commit | 576fb23faabf6df7f2c9ed2039e94d3586a3788f (patch) | |
| tree | a76a7bb68a517c4533c490d5615c9ca8ef4112f8 | |
| parent | 0d1fc76a10863b85961a63db4a9e1e2807f35957 (diff) | |
add bidi mode to spi
| -rw-r--r-- | embassy-stm32/src/spi/mod.rs | 46 |
1 files changed, 46 insertions, 0 deletions
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 { | |||
| 54 | MsbFirst, | 54 | MsbFirst, |
| 55 | } | 55 | } |
| 56 | 56 | ||
| 57 | /// SPI Direction. | ||
| 58 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
| 59 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 60 | pub enum Direction { | ||
| 61 | /// Transmit | ||
| 62 | Transmit, | ||
| 63 | /// Receive | ||
| 64 | Receive, | ||
| 65 | } | ||
| 66 | |||
| 57 | /// SPI configuration. | 67 | /// SPI configuration. |
| 58 | #[non_exhaustive] | 68 | #[non_exhaustive] |
| 59 | #[derive(Copy, Clone)] | 69 | #[derive(Copy, Clone)] |
| @@ -348,6 +358,19 @@ impl<'d, M: PeriMode, CM: CommunicationMode> Spi<'d, M, CM> { | |||
| 348 | Ok(()) | 358 | Ok(()) |
| 349 | } | 359 | } |
| 350 | 360 | ||
| 361 | /// Set SPI direction | ||
| 362 | pub fn set_direction(&mut self, dir: Option<Direction>) { | ||
| 363 | let (bidimode, bidioe) = match dir { | ||
| 364 | Some(Direction::Transmit) => (vals::Bidimode::BIDIRECTIONAL, vals::Bidioe::TRANSMIT), | ||
| 365 | Some(Direction::Receive) => (vals::Bidimode::BIDIRECTIONAL, vals::Bidioe::RECEIVE), | ||
| 366 | None => (vals::Bidimode::UNIDIRECTIONAL, vals::Bidioe::TRANSMIT), | ||
| 367 | }; | ||
| 368 | self.info.regs.cr1().modify(|w| { | ||
| 369 | w.set_bidimode(bidimode); | ||
| 370 | w.set_bidioe(bidioe); | ||
| 371 | }); | ||
| 372 | } | ||
| 373 | |||
| 351 | /// Get current SPI configuration. | 374 | /// Get current SPI configuration. |
| 352 | pub fn get_current_config(&self) -> Config { | 375 | pub fn get_current_config(&self) -> Config { |
| 353 | #[cfg(any(spi_v1, spi_v2, spi_v3))] | 376 | #[cfg(any(spi_v1, spi_v2, spi_v3))] |
| @@ -708,6 +731,29 @@ impl<'d> Spi<'d, Async, Master> { | |||
| 708 | ) | 731 | ) |
| 709 | } | 732 | } |
| 710 | 733 | ||
| 734 | /// Create a new SPI driver, in bidirectional mode | ||
| 735 | pub fn new_bidi<T: Instance, #[cfg(afio)] A>( | ||
| 736 | peri: Peri<'d, T>, | ||
| 737 | sck: Peri<'d, if_afio!(impl SckPin<T, A>)>, | ||
| 738 | sdio: Peri<'d, if_afio!(impl MosiPin<T, A>)>, | ||
| 739 | tx_dma: Peri<'d, impl TxDma<T>>, | ||
| 740 | rx_dma: Peri<'d, impl RxDma<T>>, | ||
| 741 | config: Config, | ||
| 742 | ) -> Self { | ||
| 743 | let mut this = Self::new_inner( | ||
| 744 | peri, | ||
| 745 | new_pin!(sck, config.sck_af()), | ||
| 746 | new_pin!(sdio, AfType::output(OutputType::PushPull, config.gpio_speed)), | ||
| 747 | None, | ||
| 748 | None, | ||
| 749 | new_dma!(tx_dma), | ||
| 750 | new_dma!(rx_dma), | ||
| 751 | config, | ||
| 752 | ); | ||
| 753 | this.set_direction(Some(Direction::Transmit)); | ||
| 754 | this | ||
| 755 | } | ||
| 756 | |||
| 711 | /// Create a new SPI driver, in TX-only mode, without SCK pin. | 757 | /// Create a new SPI driver, in TX-only mode, without SCK pin. |
| 712 | /// | 758 | /// |
| 713 | /// This can be useful for bit-banging non-SPI protocols. | 759 | /// This can be useful for bit-banging non-SPI protocols. |
