Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DMA ergonomics when working with generics #239

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Breaking changes

- Replace DMA buffer types with `Deref` ans `Unpin`

## [v0.6.1] - 2020-06-25

### Added
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ version = "0.2.2"
version = "0.2.3"
features = ["unproven"]

[dependencies.stable_deref_trait]
default-features = false
version = "1.1"

[dependencies.stm32-usbd]
version = "0.5.0"
features = ["ram_access_1x16"]
Expand Down
28 changes: 18 additions & 10 deletions src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
use core::marker::PhantomData;
use core::ops;

use stable_deref_trait::StableDeref;

use crate::rcc::AHB;

#[derive(Debug)]
Expand Down Expand Up @@ -70,6 +72,11 @@ pub trait TransferPayload {
fn stop(&mut self);
}

pub trait Transferable<BUFFER, DMA> {
fn is_done(&self) -> bool;
fn wait(self) -> (BUFFER, DMA);
}

pub struct Transfer<MODE, BUFFER, PAYLOAD> {
_mode: PhantomData<MODE>,
buffer: BUFFER,
Expand Down Expand Up @@ -128,7 +135,7 @@ macro_rules! dma {

use crate::pac::{$DMAX, dma1};

use crate::dma::{CircBuffer, DmaExt, Error, Event, Half, Transfer, W, RxDma, TxDma, TransferPayload};
use crate::dma::{CircBuffer, DmaExt, Error, Event, Half, Transfer, W, RxDma, TxDma, TransferPayload, Transferable};
use crate::rcc::{AHB, Enable};

pub struct Channels((), $(pub $CX),+);
Expand Down Expand Up @@ -294,15 +301,15 @@ macro_rules! dma {
}
}

impl<BUFFER, PAYLOAD, MODE> Transfer<MODE, BUFFER, RxDma<PAYLOAD, $CX>>
impl<BUFFER, PAYLOAD, MODE> Transferable<BUFFER, RxDma<PAYLOAD, $CX>> for Transfer<MODE, BUFFER, RxDma<PAYLOAD, $CX>>
where
RxDma<PAYLOAD, $CX>: TransferPayload,
{
pub fn is_done(&self) -> bool {
fn is_done(&self) -> bool {
!self.payload.channel.in_progress()
}

pub fn wait(mut self) -> (BUFFER, RxDma<PAYLOAD, $CX>) {
fn wait(mut self) -> (BUFFER, RxDma<PAYLOAD, $CX>) {
while !self.is_done() {}

atomic::compiler_fence(Ordering::Acquire);
Expand All @@ -320,15 +327,15 @@ macro_rules! dma {
}
}

impl<BUFFER, PAYLOAD, MODE> Transfer<MODE, BUFFER, TxDma<PAYLOAD, $CX>>
impl<BUFFER, PAYLOAD, MODE> Transferable<BUFFER, TxDma<PAYLOAD, $CX>> for Transfer<MODE, BUFFER, TxDma<PAYLOAD, $CX>>
where
TxDma<PAYLOAD, $CX>: TransferPayload,
{
pub fn is_done(&self) -> bool {
fn is_done(&self) -> bool {
!self.payload.channel.in_progress()
}

pub fn wait(mut self) -> (BUFFER, TxDma<PAYLOAD, $CX>) {
fn wait(mut self) -> (BUFFER, TxDma<PAYLOAD, $CX>) {
while !self.is_done() {}

atomic::compiler_fence(Ordering::Acquire);
Expand Down Expand Up @@ -496,10 +503,11 @@ where
fn read(self, buffer: &'static mut B) -> Transfer<W, &'static mut B, Self>;
}

pub trait WriteDma<A, B, TS>: Transmit
pub trait WriteDma<B, TS>: Transmit
where
A: as_slice::AsSlice<Element = TS>,
B: Static<A>,
B: core::ops::Deref + 'static,
B::Target: as_slice::AsSlice<Element = TS> + Unpin,
B: StableDeref,
Self: core::marker::Sized,
{
fn write(self, buffer: B) -> Transfer<R, B, Self>;
Expand Down
14 changes: 11 additions & 3 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,14 @@ use core::sync::atomic::{self, Ordering};

use crate::pac::{USART1, USART2, USART3};
use core::convert::Infallible;

use as_slice::AsSlice;
use stable_deref_trait::StableDeref;

use embedded_hal::serial::Write;

use crate::afio::MAPR;
use crate::dma::{dma1, CircBuffer, RxDma, Static, Transfer, TxDma, R, W};
use crate::dma::{dma1, CircBuffer, RxDma, Transfer, TxDma, R, W};
use crate::gpio::gpioa::{PA10, PA2, PA3, PA9};
use crate::gpio::gpiob::{PB10, PB11, PB6, PB7};
use crate::gpio::gpioc::{PC10, PC11};
Expand Down Expand Up @@ -637,12 +641,16 @@ macro_rules! serialdma {
}
}

impl<A, B> crate::dma::WriteDma<A, B, u8> for $txdma where A: as_slice::AsSlice<Element=u8>, B: Static<A> {
impl<B> crate::dma::WriteDma<B, u8> for $txdma
where
B: StableDeref + core::ops::Deref + 'static,
B::Target: AsSlice<Element = u8> + Unpin,
{
fn write(mut self, buffer: B
) -> Transfer<R, B, Self>
{
{
let buffer = buffer.borrow().as_slice();
let buffer = (*buffer).as_slice();

self.channel.set_peripheral_address(unsafe{ &(*$USARTX::ptr()).dr as *const _ as u32 }, false);

Expand Down
11 changes: 6 additions & 5 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::pac::{SPI1, SPI2};

use crate::afio::MAPR;
use crate::dma::dma1::{C3, C5};
use crate::dma::{Static, Transfer, TransferPayload, Transmit, TxDma, R};
use crate::dma::{Transfer, TransferPayload, Transmit, TxDma, R};
use crate::gpio::gpioa::{PA5, PA6, PA7};
use crate::gpio::gpiob::{PB13, PB14, PB15, PB3, PB4, PB5};
#[cfg(feature = "connectivity")]
Expand All @@ -53,6 +53,7 @@ use crate::time::Hertz;
use core::sync::atomic::{self, Ordering};

use as_slice::AsSlice;
use stable_deref_trait::StableDeref;

/// SPI error
#[derive(Debug)]
Expand Down Expand Up @@ -450,14 +451,14 @@ macro_rules! spi_dma {
}
}

impl<A, B, REMAP, PIN> crate::dma::WriteDma<A, B, u8> for SpiTxDma<$SPIi, REMAP, PIN, $TCi>
impl<B, REMAP, PIN> crate::dma::WriteDma<B, u8> for SpiTxDma<$SPIi, REMAP, PIN, $TCi>
where
A: AsSlice<Element = u8>,
B: Static<A>,
B: StableDeref + core::ops::Deref + 'static,
B::Target: as_slice::AsSlice<Element = u8> + Unpin,
{
fn write(mut self, buffer: B) -> Transfer<R, B, Self> {
{
let buffer = buffer.borrow().as_slice();
let buffer = buffer.as_slice();
self.channel.set_peripheral_address(
unsafe { &(*$SPIi::ptr()).dr as *const _ as u32 },
false,
Expand Down