diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index e3057a3b..a4e4c0ba 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs @@ -2,6 +2,7 @@ use crate::gpio::{sealed::Pin, AnyPin}; use crate::pac::spi; +use crate::rcc::RccPeripheral; use crate::spi::{ByteOrder, Config, Error, Instance, MisoPin, MosiPin, SckPin, WordSize}; use crate::time::Hertz; use core::marker::PhantomData; @@ -19,7 +20,7 @@ impl WordSize { } } -pub struct Spi<'d, T: Instance> { +pub struct Spi<'d, T: Instance + RccPeripheral> { sck: AnyPin, mosi: AnyPin, miso: AnyPin, @@ -27,7 +28,7 @@ pub struct Spi<'d, T: Instance> { phantom: PhantomData<&'d mut T>, } -impl<'d, T: Instance> Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> Spi<'d, T> { pub fn new( pclk: Hertz, _peri: impl Unborrow + 'd, @@ -61,6 +62,8 @@ impl<'d, T: Instance> Spi<'d, T> { let br = Self::compute_baud_rate(pclk, freq.into()); unsafe { + T::enable(); + T::reset(); T::regs().cr1().modify(|w| { w.set_cpha( match config.mode.phase == Phase::CaptureOnSecondTransition { @@ -128,7 +131,7 @@ impl<'d, T: Instance> Spi<'d, T> { } } -impl<'d, T: Instance> Drop for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> Drop for Spi<'d, T> { fn drop(&mut self) { unsafe { self.sck.set_as_analog(); @@ -138,7 +141,7 @@ impl<'d, T: Instance> Drop for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Write for Spi<'d, T> { type Error = Error; fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { @@ -174,7 +177,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { @@ -215,7 +218,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Write for Spi<'d, T> { type Error = Error; fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> { @@ -251,7 +254,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> { diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index da4686b9..3b768751 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -4,6 +4,7 @@ use crate::gpio::{AnyPin, Pin}; use crate::pac::gpio::vals::{Afr, Moder}; use crate::pac::gpio::Gpio; use crate::pac::spi; +use crate::rcc::RccPeripheral; use crate::spi::{ByteOrder, Config, Error, Instance, MisoPin, MosiPin, SckPin, WordSize}; use crate::time::Hertz; use core::marker::PhantomData; @@ -28,14 +29,14 @@ impl WordSize { } } -pub struct Spi<'d, T: Instance> { +pub struct Spi<'d, T: Instance + RccPeripheral> { sck: AnyPin, mosi: AnyPin, miso: AnyPin, phantom: PhantomData<&'d mut T>, } -impl<'d, T: Instance> Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> Spi<'d, T> { pub fn new( pclk: Hertz, _peri: impl Unborrow + 'd, @@ -64,6 +65,8 @@ impl<'d, T: Instance> Spi<'d, T> { let br = Self::compute_baud_rate(pclk, freq.into()); unsafe { + T::enable(); + T::reset(); T::regs().ifcr().write(|w| w.0 = 0xffff_ffff); T::regs().cfg2().modify(|w| { //w.set_ssoe(true); @@ -161,7 +164,7 @@ impl<'d, T: Instance> Spi<'d, T> { } } -impl<'d, T: Instance> Drop for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> Drop for Spi<'d, T> { fn drop(&mut self) { unsafe { Self::unconfigure_pin(self.sck.block(), self.sck.pin() as _); @@ -171,7 +174,7 @@ impl<'d, T: Instance> Drop for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Write for Spi<'d, T> { type Error = Error; fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { @@ -208,7 +211,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { @@ -265,7 +268,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Write for Spi<'d, T> { type Error = Error; fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> { @@ -302,7 +305,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T> { } } -impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { +impl<'d, T: Instance + RccPeripheral> embedded_hal::blocking::spi::Transfer for Spi<'d, T> { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> {