diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index 76857ae64..f39fc746d 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -7,9 +7,11 @@ use crate::pac::spi::{regs, vals}; use crate::peripherals; use crate::rcc::RccPeripheral; use crate::time::Hertz; +use core::future::Future; use core::marker::PhantomData; use embassy::util::Unborrow; use embassy_hal_common::unborrow; +use embassy_traits::spi as traits; #[cfg_attr(spi_v1, path = "v1.rs")] #[cfg_attr(spi_f1, path = "v1.rs")] @@ -406,6 +408,45 @@ trait Word {} impl Word for u8 {} impl Word for u16 {} +impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { + type Error = Error; +} + +impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { + #[rustfmt::skip] + type WriteFuture<'a> where Self: 'a = impl Future> + 'a; + + fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { + self.write_dma_u8(data) + } +} + +impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read + for Spi<'d, T, Tx, Rx> +{ + #[rustfmt::skip] + type ReadFuture<'a> where Self: 'a = impl Future> + 'a; + + fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { + self.read_dma_u8(data) + } +} + +impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDuplex + for Spi<'d, T, Tx, Rx> +{ + #[rustfmt::skip] + type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; + + fn read_write<'a>( + &'a mut self, + read: &'a mut [u8], + write: &'a [u8], + ) -> Self::WriteReadFuture<'a> { + self.read_write_dma_u8(read, write) + } +} + pub(crate) mod sealed { use super::*; diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index 3a7d948ae..efe7e468c 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs @@ -4,9 +4,7 @@ use crate::dma::NoDma; use crate::spi::{ check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, }; -use core::future::Future; use core::ptr; -use embassy_traits::spi as traits; pub use embedded_hal::blocking; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join3; @@ -14,8 +12,7 @@ use futures::future::join3; use super::Spi; impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { - #[allow(unused)] - async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> + pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, { @@ -43,8 +40,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - #[allow(unused)] - async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> + pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -96,8 +92,11 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - #[allow(unused)] - async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> + pub(super) async fn read_write_dma_u8( + &mut self, + read: &mut [u8], + write: &[u8], + ) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -221,45 +220,6 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, } } -impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { - type Error = super::Error; -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { - #[rustfmt::skip] - type WriteFuture<'a> where Self: 'a = impl Future> + 'a; - - fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { - self.write_dma_u8(data) - } -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read - for Spi<'d, T, Tx, Rx> -{ - #[rustfmt::skip] - type ReadFuture<'a> where Self: 'a = impl Future> + 'a; - - fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read_dma_u8(data) - } -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDuplex - for Spi<'d, T, Tx, Rx> -{ - #[rustfmt::skip] - type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; - - fn read_write<'a>( - &'a mut self, - read: &'a mut [u8], - write: &'a [u8], - ) -> Self::WriteReadFuture<'a> { - self.read_write_dma_u8(read, write) - } -} - use super::Word; fn write_word(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(), Error> { diff --git a/embassy-stm32/src/spi/v2.rs b/embassy-stm32/src/spi/v2.rs index 2f02dc867..e905a1b9c 100644 --- a/embassy-stm32/src/spi/v2.rs +++ b/embassy-stm32/src/spi/v2.rs @@ -1,18 +1,17 @@ #![macro_use] use crate::dma::NoDma; -use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags}; -use core::future::Future; +use crate::spi::{ + check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, +}; use core::ptr; -use embassy_traits::spi as traits; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::{join, join3}; use super::Spi; impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { - #[allow(unused)] - async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> + pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, { @@ -49,8 +48,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - #[allow(unused)] - async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> + pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -102,8 +100,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - #[allow(unused)] - async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> + pub(super) async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -270,42 +267,3 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, Ok(words) } } - -impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { - type Error = super::Error; -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { - #[rustfmt::skip] - type WriteFuture<'a> where Self: 'a = impl Future> + 'a; - - fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { - self.write_dma_u8(data) - } -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read - for Spi<'d, T, Tx, Rx> -{ - #[rustfmt::skip] - type ReadFuture<'a> where Self: 'a = impl Future> + 'a; - - fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read_dma_u8(data) - } -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDuplex - for Spi<'d, T, Tx, Rx> -{ - #[rustfmt::skip] - type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; - - fn read_write<'a>( - &'a mut self, - read: &'a mut [u8], - write: &'a [u8], - ) -> Self::WriteReadFuture<'a> { - self.read_write_dma_u8(read, write) - } -} diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index e63dbcb29..cbe2861e0 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -1,10 +1,10 @@ #![macro_use] use crate::dma::NoDma; -use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags}; -use core::future::Future; +use crate::spi::{ + check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, +}; use core::ptr; -use embassy_traits::spi as traits; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join3; @@ -12,8 +12,7 @@ use futures::future::join3; use super::Spi; impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { - #[allow(unused)] - async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> + pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, { @@ -53,8 +52,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - #[allow(unused)] - async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> + pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -107,8 +105,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { Ok(()) } - #[allow(unused)] - async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> + pub(super) async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, @@ -344,42 +341,3 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, Ok(words) } } - -impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { - type Error = super::Error; -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { - #[rustfmt::skip] - type WriteFuture<'a> where Self: 'a = impl Future> + 'a; - - fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { - self.write_dma_u8(data) - } -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read - for Spi<'d, T, Tx, Rx> -{ - #[rustfmt::skip] - type ReadFuture<'a> where Self: 'a = impl Future> + 'a; - - fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { - self.read_dma_u8(data) - } -} - -impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDuplex - for Spi<'d, T, Tx, Rx> -{ - #[rustfmt::skip] - type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; - - fn read_write<'a>( - &'a mut self, - read: &'a mut [u8], - write: &'a [u8], - ) -> Self::WriteReadFuture<'a> { - self.read_write_dma_u8(read, write) - } -}