Skip to content

Commit

Permalink
Merge pull request #100 from thalesfragoso/stable-deref
Browse files Browse the repository at this point in the history
Revert back DMA API to StableDeref
  • Loading branch information
MabezDev authored Apr 9, 2020
2 parents 19dfe86 + 3086b23 commit be8c4ea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ default-features = false
version = "1.0.2"
default-features = false

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

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
Expand Down
9 changes: 6 additions & 3 deletions examples/rtfm_frame_serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
#![no_main]
#![no_std]

use panic_halt as _;
use hal::{
dma::{self, consts, FrameReader, FrameSender, DMAFrame},
dma::{self, consts, DMAFrame, FrameReader, FrameSender},
prelude::*,
rcc::{ClockSecuritySystem, CrystalBypass, MsiFreq},
serial::{self, Config, Serial},
Expand All @@ -19,11 +18,15 @@ use heapless::{
pool,
pool::singleton::{Box, Pool},
};
use panic_halt as _;
use rtfm::app;
use stm32l4xx_hal as hal;

// The pool gives out `Box<DMAFrame>`s that can hold 8 bytes
pool!(SerialDMAPool: DMAFrame<consts::U8>);
pool!(
#[allow(non_upper_case_globals)]
SerialDMAPool: DMAFrame<consts::U8>
);

#[app(device = stm32l4xx_hal::stm32, peripherals = true)]
const APP: () = {
Expand Down
26 changes: 14 additions & 12 deletions src/dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::rcc::AHB1;
use as_slice::AsSlice;
pub use generic_array::typenum::consts;
use generic_array::{ArrayLength, GenericArray};
use stable_deref_trait::StableDeref;

#[derive(Debug)]
pub enum Error {
Expand All @@ -36,7 +37,7 @@ pub enum Half {
/// Frame reader "worker", access and handling of frame reads is made through this structure.
pub struct FrameReader<BUFFER, CHANNEL, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
buffer: BUFFER,
Expand All @@ -47,7 +48,7 @@ where

impl<BUFFER, CHANNEL, N> FrameReader<BUFFER, CHANNEL, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
pub(crate) fn new(
Expand All @@ -68,7 +69,7 @@ where
/// structure.
pub struct FrameSender<BUFFER, CHANNEL, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
buffer: Option<BUFFER>,
Expand All @@ -78,7 +79,7 @@ where

impl<BUFFER, CHANNEL, N> FrameSender<BUFFER, CHANNEL, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
pub(crate) fn new(channel: CHANNEL) -> FrameSender<BUFFER, CHANNEL, N> {
Expand Down Expand Up @@ -275,7 +276,7 @@ where
impl<BUFFER, CHANNEL> CircBuffer<BUFFER, CHANNEL> {
pub(crate) fn new<H>(buf: BUFFER, chan: CHANNEL) -> Self
where
BUFFER: Deref<Target = [H; 2]> + 'static,
BUFFER: StableDeref<Target = [H; 2]> + 'static,
{
CircBuffer {
buffer: buf,
Expand All @@ -301,7 +302,7 @@ pub struct Transfer<MODE, BUFFER, CHANNEL, PAYLOAD> {

impl<BUFFER, CHANNEL, PAYLOAD> Transfer<R, BUFFER, CHANNEL, PAYLOAD>
where
BUFFER: Deref + 'static,
BUFFER: StableDeref + 'static,
{
pub(crate) fn r(buffer: BUFFER, channel: CHANNEL, payload: PAYLOAD) -> Self {
Transfer {
Expand All @@ -315,7 +316,7 @@ where

impl<BUFFER, CHANNEL, PAYLOAD> Transfer<W, BUFFER, CHANNEL, PAYLOAD>
where
BUFFER: Deref + 'static,
BUFFER: StableDeref + 'static,
{
pub(crate) fn w(buffer: BUFFER, channel: CHANNEL, payload: PAYLOAD) -> Self {
Transfer {
Expand Down Expand Up @@ -368,8 +369,9 @@ macro_rules! dma {
use crate::stm32::{$DMAX, dma1};
use core::mem::MaybeUninit;
use generic_array::ArrayLength;
use core::ops::{Deref, DerefMut};
use core::ops::DerefMut;
use core::ptr;
use stable_deref_trait::StableDeref;

use crate::dma::{CircBuffer, FrameReader, FrameSender, DMAFrame, DmaExt, Error, Event, Half, Transfer, W};
use crate::rcc::AHB1;
Expand Down Expand Up @@ -491,7 +493,7 @@ macro_rules! dma {

impl<BUFFER, N> FrameSender<BUFFER, $CX, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
/// This method should be called in the transfer complete interrupt of the
Expand Down Expand Up @@ -559,7 +561,7 @@ macro_rules! dma {

impl<BUFFER, N> FrameReader<BUFFER, $CX, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
/// This function should be called from the transfer complete interrupt of
Expand Down Expand Up @@ -689,7 +691,7 @@ macro_rules! dma {
pub fn partial_peek<R, F, H, T>(&mut self, f: F) -> Result<R, Error>
where
F: FnOnce(&[T], Half) -> Result<(usize, R), ()>,
B: Deref<Target = [H; 2]> + 'static,
B: StableDeref<Target = [H; 2]> + 'static,
H: AsSlice<Element=T>,
{
// this inverts expectation and returns the half being _written_
Expand Down Expand Up @@ -730,7 +732,7 @@ macro_rules! dma {
pub fn peek<R, F, H, T>(&mut self, f: F) -> Result<R, Error>
where
F: FnOnce(&[T], Half) -> R,
B: Deref<Target = [H; 2]> + 'static,
B: StableDeref<Target = [H; 2]> + 'static,
H: AsSlice<Element=T>,
{
let half_being_read = self.readable_half()?;
Expand Down
9 changes: 5 additions & 4 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ use as_slice::AsMutSlice;
use core::fmt;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::ops::{Deref, DerefMut};
use core::ops::DerefMut;
use core::ptr;
use core::sync::atomic::{self, Ordering};
use generic_array::ArrayLength;
use stable_deref_trait::StableDeref;

use crate::hal::serial::{self, Write};
use nb;
Expand Down Expand Up @@ -909,7 +910,7 @@ macro_rules! hal {
mut buffer: B,
) -> CircBuffer<B, $rx_chan>
where
B: Deref<Target = [H; 2]> + DerefMut + 'static,
B: StableDeref<Target = [H; 2]> + DerefMut + 'static,
H: AsMutSlice<Element = u8>
{
let buf = buffer[0].as_mut_slice();
Expand Down Expand Up @@ -957,7 +958,7 @@ macro_rules! hal {
buffer: BUFFER,
) -> FrameReader<BUFFER, $rx_chan, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
let usart = unsafe{ &(*$USARTX::ptr()) };
Expand Down Expand Up @@ -1055,7 +1056,7 @@ macro_rules! hal {
mut channel: $tx_chan,
) -> FrameSender<BUFFER, $tx_chan, N>
where
BUFFER: Sized + Deref<Target = DMAFrame<N>> + DerefMut + 'static,
BUFFER: Sized + StableDeref<Target = DMAFrame<N>> + DerefMut + 'static,
N: ArrayLength<MaybeUninit<u8>>,
{
let usart = unsafe{ &(*$USARTX::ptr()) };
Expand Down

0 comments on commit be8c4ea

Please sign in to comment.