Implement FullDuplex for nrf spim

This commit is contained in:
Dario Nieuwenhuis 2021-03-18 01:27:30 +01:00
parent c403a47b7f
commit 3de2d5c5bd
3 changed files with 43 additions and 15 deletions

View file

@ -6,6 +6,7 @@
#[path = "../example_common.rs"]
mod example_common;
use embassy_traits::spi::FullDuplex;
use example_common::*;
use cortex_m_rt::entry;
@ -49,7 +50,7 @@ async fn run() {
ncs.set_low().unwrap();
cortex_m::asm::delay(5);
let tx = [0xFF];
unwrap!(spim.as_mut().send_receive(&tx, &mut []).await);
unwrap!(spim.as_mut().read_write(&mut [], &tx).await);
cortex_m::asm::delay(10);
ncs.set_high().unwrap();
@ -62,7 +63,7 @@ async fn run() {
ncs.set_low().unwrap();
cortex_m::asm::delay(5000);
let tx = [0b000_11101, 0];
unwrap!(spim.as_mut().send_receive(&tx, &mut rx).await);
unwrap!(spim.as_mut().read_write(&mut rx, &tx).await);
cortex_m::asm::delay(5000);
ncs.set_high().unwrap();
info!("estat: {=[?]}", rx);
@ -72,7 +73,7 @@ async fn run() {
ncs.set_low().unwrap();
cortex_m::asm::delay(5);
let tx = [0b100_11111, 0b11];
unwrap!(spim.as_mut().send_receive(&tx, &mut rx).await);
unwrap!(spim.as_mut().read_write(&mut rx, &tx).await);
cortex_m::asm::delay(10);
ncs.set_high().unwrap();
@ -81,7 +82,7 @@ async fn run() {
ncs.set_low().unwrap();
cortex_m::asm::delay(5);
let tx = [0b000_10010, 0];
unwrap!(spim.as_mut().send_receive(&tx, &mut rx).await);
unwrap!(spim.as_mut().read_write(&mut rx, &tx).await);
cortex_m::asm::delay(10);
ncs.set_high().unwrap();

View file

@ -2,9 +2,11 @@ use core::future::Future;
use core::pin::Pin;
use core::sync::atomic::{compiler_fence, Ordering};
use core::task::Poll;
use embassy::traits;
use embassy::util::WakerRegistration;
use embassy_extras::peripheral::{PeripheralMutex, PeripheralState};
use futures::future::poll_fn;
use traits::spi::FullDuplex;
use crate::interrupt::{self, Interrupt};
use crate::{pac, slice_in_ram_or};
@ -123,15 +125,33 @@ impl<T: Instance> Spim<T> {
let (state, irq) = self.inner().free();
(state.spim, irq)
}
}
pub fn send_receive<'a>(
impl<T: Instance> FullDuplex<u8> for Spim<T> {
type Error = Error;
#[rustfmt::skip]
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
#[rustfmt::skip]
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
#[rustfmt::skip]
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
fn read<'a>(self: Pin<&'a mut Self>, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
async move { todo!() }
}
fn write<'a>(self: Pin<&'a mut Self>, data: &'a [u8]) -> Self::WriteFuture<'a> {
async move { todo!() }
}
fn read_write<'a>(
mut self: Pin<&'a mut Self>,
tx: &'a [u8],
rx: &'a mut [u8],
) -> impl Future<Output = Result<(), Error>> + 'a {
tx: &'a [u8],
) -> Self::WriteReadFuture<'a> {
async move {
slice_in_ram_or(tx, Error::DMABufferNotInDataMemory)?;
slice_in_ram_or(rx, Error::DMABufferNotInDataMemory)?;
slice_in_ram_or(tx, Error::DMABufferNotInDataMemory)?;
self.as_mut().inner().with(|s, _irq| {
// Conservative compiler fence to prevent optimizations that do not

View file

@ -1,6 +1,7 @@
//! Async SPI API
use core::future::Future;
use core::pin::Pin;
/// Full duplex (master mode)
///
@ -22,15 +23,21 @@ pub trait FullDuplex<Word> {
/// An enumeration of SPI errors
type Error;
type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
type WriteReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a;
type WriteFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
type ReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
type WriteReadFuture<'a>: Future<Output = Result<(), Self::Error>> + 'a
where
Self: 'a;
fn read<'a>(&'a mut self, data: &'a mut [Word]) -> Self::ReadFuture<'_>;
fn write<'a>(&'a mut self, data: &'a [Word]) -> Self::WriteFuture<'_>;
fn read<'a>(self: Pin<&'a mut Self>, data: &'a mut [Word]) -> Self::ReadFuture<'a>;
fn write<'a>(self: Pin<&'a mut Self>, data: &'a [Word]) -> Self::WriteFuture<'a>;
fn read_write<'a>(
&mut self,
self: Pin<&'a mut Self>,
read: &'a mut [Word],
write: &'a [Word],
) -> Self::WriteReadFuture<'_>;
) -> Self::WriteReadFuture<'a>;
}