Move async trait impls to mod

This commit is contained in:
Grant Miller 2021-12-06 22:06:58 -06:00
parent 20d2151b1d
commit 3a17e3a2a5
4 changed files with 60 additions and 143 deletions

View File

@ -7,9 +7,11 @@ use crate::pac::spi::{regs, vals};
use crate::peripherals; use crate::peripherals;
use crate::rcc::RccPeripheral; use crate::rcc::RccPeripheral;
use crate::time::Hertz; use crate::time::Hertz;
use core::future::Future;
use core::marker::PhantomData; use core::marker::PhantomData;
use embassy::util::Unborrow; use embassy::util::Unborrow;
use embassy_hal_common::unborrow; use embassy_hal_common::unborrow;
use embassy_traits::spi as traits;
#[cfg_attr(spi_v1, path = "v1.rs")] #[cfg_attr(spi_v1, path = "v1.rs")]
#[cfg_attr(spi_f1, path = "v1.rs")] #[cfg_attr(spi_f1, path = "v1.rs")]
@ -406,6 +408,45 @@ trait Word {}
impl Word for u8 {} impl Word for u8 {}
impl Word for u16 {} impl Word for u16 {}
impl<'d, T: Instance, Tx, Rx> traits::Spi<u8> for Spi<'d, T, Tx, Rx> {
type Error = Error;
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> {
#[rustfmt::skip]
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
self.write_dma_u8(data)
}
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::Read<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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<T>, Rx: RxDmaChannel<T>> traits::FullDuplex<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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 { pub(crate) mod sealed {
use super::*; use super::*;

View File

@ -4,9 +4,7 @@ use crate::dma::NoDma;
use crate::spi::{ use crate::spi::{
check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize,
}; };
use core::future::Future;
use core::ptr; use core::ptr;
use embassy_traits::spi as traits;
pub use embedded_hal::blocking; pub use embedded_hal::blocking;
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
use futures::future::join3; use futures::future::join3;
@ -14,8 +12,7 @@ use futures::future::join3;
use super::Spi; use super::Spi;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
#[allow(unused)] pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
{ {
@ -43,8 +40,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
Ok(()) Ok(())
} }
#[allow(unused)] pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>, Rx: RxDmaChannel<T>,
@ -96,8 +92,11 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
Ok(()) Ok(())
} }
#[allow(unused)] pub(super) async fn read_write_dma_u8(
async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> &mut self,
read: &mut [u8],
write: &[u8],
) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>, Rx: RxDmaChannel<T>,
@ -221,45 +220,6 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T,
} }
} }
impl<'d, T: Instance, Tx, Rx> traits::Spi<u8> for Spi<'d, T, Tx, Rx> {
type Error = super::Error;
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> {
#[rustfmt::skip]
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
self.write_dma_u8(data)
}
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::Read<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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<T>, Rx: RxDmaChannel<T>> traits::FullDuplex<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type WriteReadFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + '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; use super::Word;
fn write_word<W: Word>(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(), Error> { fn write_word<W: Word>(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(), Error> {

View File

@ -1,18 +1,17 @@
#![macro_use] #![macro_use]
use crate::dma::NoDma; use crate::dma::NoDma;
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags}; use crate::spi::{
use core::future::Future; check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize,
};
use core::ptr; 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}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
use futures::future::{join, join3}; use futures::future::{join, join3};
use super::Spi; use super::Spi;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
#[allow(unused)] pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
{ {
@ -49,8 +48,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
Ok(()) Ok(())
} }
#[allow(unused)] pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>, Rx: RxDmaChannel<T>,
@ -102,8 +100,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
Ok(()) Ok(())
} }
#[allow(unused)] pub(super) async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error>
async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>, Rx: RxDmaChannel<T>,
@ -270,42 +267,3 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T,
Ok(words) Ok(words)
} }
} }
impl<'d, T: Instance, Tx, Rx> traits::Spi<u8> for Spi<'d, T, Tx, Rx> {
type Error = super::Error;
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> {
#[rustfmt::skip]
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
self.write_dma_u8(data)
}
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::Read<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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<T>, Rx: RxDmaChannel<T>> traits::FullDuplex<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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)
}
}

View File

@ -1,10 +1,10 @@
#![macro_use] #![macro_use]
use crate::dma::NoDma; use crate::dma::NoDma;
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags}; use crate::spi::{
use core::future::Future; check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize,
};
use core::ptr; 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}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
use futures::future::join3; use futures::future::join3;
@ -12,8 +12,7 @@ use futures::future::join3;
use super::Spi; use super::Spi;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
#[allow(unused)] pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
{ {
@ -53,8 +52,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
Ok(()) Ok(())
} }
#[allow(unused)] pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>, Rx: RxDmaChannel<T>,
@ -107,8 +105,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
Ok(()) Ok(())
} }
#[allow(unused)] pub(super) async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error>
async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error>
where where
Tx: TxDmaChannel<T>, Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>, Rx: RxDmaChannel<T>,
@ -344,42 +341,3 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T,
Ok(words) Ok(words)
} }
} }
impl<'d, T: Instance, Tx, Rx> traits::Spi<u8> for Spi<'d, T, Tx, Rx> {
type Error = super::Error;
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> {
#[rustfmt::skip]
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
self.write_dma_u8(data)
}
}
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::Read<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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<T>, Rx: RxDmaChannel<T>> traits::FullDuplex<u8>
for Spi<'d, T, Tx, Rx>
{
#[rustfmt::skip]
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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)
}
}