Checkpoint.

This commit is contained in:
Bob McWhirter 2021-07-20 09:19:23 -04:00
parent a345dd9e2b
commit fe66f0f8f8
2 changed files with 68 additions and 11 deletions

View file

@ -1,13 +1,14 @@
#![macro_use]
#[cfg_attr(spi_v1, path = "v1.rs")]
#[cfg_attr(spi_v2, path = "v2.rs")]
//#[cfg_attr(spi_v1, path = "v1.rs")]
//#[cfg_attr(spi_v2, path = "v2.rs")]
#[cfg_attr(spi_v3, path = "v3.rs")]
mod _version;
use crate::{peripherals, rcc::RccPeripheral};
use crate::{dma, peripherals, rcc::RccPeripheral};
pub use _version::*;
use crate::gpio::Pin;
use core::marker::PhantomData;
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
@ -62,6 +63,14 @@ pub(crate) mod sealed {
pub trait MisoPin<T: Instance>: Pin {
fn af_num(&self) -> u8;
}
pub trait TxDmaChannel<T: Instance> {
fn request(&self) -> dma::Request;
}
pub trait RxDmaChannel<T: Instance> {
fn request(&self) -> dma::Request;
}
}
pub trait Instance: sealed::Instance + RccPeripheral + 'static {}
@ -72,6 +81,20 @@ pub trait MosiPin<T: Instance>: sealed::MosiPin<T> + 'static {}
pub trait MisoPin<T: Instance>: sealed::MisoPin<T> + 'static {}
pub trait TxDmaChannel<T: Instance>: sealed::TxDmaChannel<T> + 'static {}
pub trait RxDmaChannel<T: Instance>: sealed::RxDmaChannel<T> + 'static {}
pub trait SpiDma {}
pub struct DmaPair<T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> {
tx: Tx,
rx: Rx,
_phantom: PhantomData<T>,
}
impl<T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> SpiDma for DmaPair<T, Tx, Rx> {}
crate::pac::peripherals!(
(spi, $inst:ident) => {
impl sealed::Instance for peripherals::$inst {

View file

@ -4,7 +4,10 @@ use crate::gpio::{AnyPin, Pin};
use crate::pac::gpio::vals::{Afr, Moder};
use crate::pac::gpio::Gpio;
use crate::pac::spi;
use crate::spi::{ByteOrder, Config, Error, Instance, MisoPin, MosiPin, SckPin, WordSize};
use crate::spi::{
ByteOrder, Config, DmaPair, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, SpiDma,
TxDmaChannel, WordSize,
};
use crate::time::Hertz;
use core::marker::PhantomData;
use core::ptr;
@ -28,26 +31,28 @@ impl WordSize {
}
}
pub struct Spi<'d, T: Instance> {
pub struct Spi<'d, T: Instance, D = NoDma> {
sck: AnyPin,
mosi: AnyPin,
miso: AnyPin,
dma: D,
phantom: PhantomData<&'d mut T>,
}
impl<'d, T: Instance> Spi<'d, T> {
impl<'d, T: Instance, D> Spi<'d, T, D> {
pub fn new<F>(
_peri: impl Unborrow<Target = T> + 'd,
sck: impl Unborrow<Target = impl SckPin<T>>,
mosi: impl Unborrow<Target = impl MosiPin<T>>,
miso: impl Unborrow<Target = impl MisoPin<T>>,
dma: impl Unborrow<Target = D>,
freq: F,
config: Config,
) -> Self
where
F: Into<Hertz>,
{
unborrow!(sck, mosi, miso);
unborrow!(sck, mosi, miso, dma);
unsafe {
Self::configure_pin(sck.block(), sck.pin() as _, sck.af_num());
@ -113,6 +118,7 @@ impl<'d, T: Instance> Spi<'d, T> {
sck,
mosi,
miso,
dma,
phantom: PhantomData,
}
}
@ -173,7 +179,7 @@ impl<'d, T: Instance> Drop for Spi<'d, T> {
}
}
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T> {
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T, NoDma> {
type Error = Error;
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
@ -210,7 +216,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T> {
}
}
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T> {
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T, NoDma> {
type Error = Error;
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
@ -267,7 +273,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T> {
}
}
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T> {
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T, NoDma> {
type Error = Error;
fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> {
@ -304,7 +310,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T> {
}
}
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T> {
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T, NoDma> {
type Error = Error;
fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> {
@ -357,3 +363,31 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T>
Ok(words)
}
}
use crate::dma::NoDma;
use core::future::Future;
use embassy_traits::spi::FullDuplex;
#[rustfmt::skip]
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> FullDuplex<u8> for Spi<'d, T, DmaPair<T, Tx, Rx>> {
type Error = super::Error;
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
unimplemented!()
}
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
unimplemented!()
}
fn read_write<'a>(
&'a mut self,
read: &'a mut [u8],
write: &'a [u8],
) -> Self::WriteReadFuture<'a> {
unimplemented!()
}
}