Skip to content

Commit

Permalink
PeripheralRef: DMA (#317)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjoernQ authored Dec 15, 2022
1 parent c6a6010 commit a90aa3a
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 deletions.
13 changes: 8 additions & 5 deletions esp-hal-common/src/dma/gdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{
dma::gdma::private::*,
peripheral::PeripheralRef,
system::{Peripheral, PeripheralClockControl},
};

Expand Down Expand Up @@ -351,8 +352,8 @@ pub(crate) mod private {
/// GDMA Peripheral
///
/// This offers the available DMA channels.
pub struct Gdma {
_inner: crate::peripherals::DMA,
pub struct Gdma<'d> {
_inner: PeripheralRef<'d, crate::peripherals::DMA>,
pub channel0: ChannelCreator0,
#[cfg(not(esp32c2))]
pub channel1: ChannelCreator1,
Expand All @@ -364,12 +365,14 @@ pub struct Gdma {
pub channel4: ChannelCreator4,
}

impl Gdma {
impl<'d> Gdma<'d> {
/// Create a DMA instance.
pub fn new(
dma: crate::peripherals::DMA,
dma: impl crate::peripheral::Peripheral<P = crate::peripherals::DMA> + 'd,
peripheral_clock_control: &mut PeripheralClockControl,
) -> Gdma {
) -> Gdma<'d> {
crate::into_ref!(dma);

peripheral_clock_control.enable(Peripheral::Gdma);
dma.misc_conf.modify(|_, w| w.ahbm_rst_inter().set_bit());
dma.misc_conf.modify(|_, w| w.ahbm_rst_inter().clear_bit());
Expand Down
13 changes: 7 additions & 6 deletions esp-hal-common/src/dma/pdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use crate::{
dma::pdma::private::*,
peripheral::PeripheralRef,
system::{Peripheral, PeripheralClockControl},
};

Expand Down Expand Up @@ -469,25 +470,25 @@ pub(crate) mod private {
/// DMA Peripheral
///
/// This offers the available DMA channels.
pub struct Dma {
_inner: crate::system::Dma,
pub struct Dma<'d> {
_inner: PeripheralRef<'d, crate::system::Dma>,
pub spi2channel: Spi2DmaChannelCreator,
pub spi3channel: Spi3DmaChannelCreator,
pub i2s0channel: I2s0DmaChannelCreator,
#[cfg(esp32)]
pub i2s1channel: I2s1DmaChannelCreator,
}

impl Dma {
impl<'d> Dma<'d> {
/// Create a DMA instance.
pub fn new(
dma: crate::system::Dma,
dma: impl crate::peripheral::Peripheral<P = crate::system::Dma> + 'd,
peripheral_clock_control: &mut PeripheralClockControl,
) -> Dma {
) -> Dma<'d> {
peripheral_clock_control.enable(Peripheral::Dma);

Dma {
_inner: dma,
_inner: dma.into_ref(),
spi2channel: Spi2DmaChannelCreator {},
spi3channel: Spi3DmaChannelCreator {},
i2s0channel: I2s0DmaChannelCreator {},
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ mod peripherals {
UART1,
SYSTEM,
LEDC,
DMA,
}
}
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@ mod peripherals {
LEDC,
RMT,
I2S,
DMA,
}
}
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ mod peripherals {
RMT,
I2S0,
I2S1,
DMA,
PWM0,
PWM1,
}
Expand Down
34 changes: 34 additions & 0 deletions esp-hal-common/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,37 @@ impl crate::peripheral::Peripheral for &mut SystemClockControl {
SystemClockControl { _private: () }
}
}

#[cfg(pdma)]
mod dma_peripheral {
use super::Dma;

impl core::ops::Deref for Dma {
type Target = Dma;

fn deref(&self) -> &Self::Target {
self
}
}

impl core::ops::DerefMut for Dma {
fn deref_mut(&mut self) -> &mut Self::Target {
self
}
}

impl crate::peripheral::Peripheral for Dma {
type P = Dma;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
Dma { _private: () }
}
}
impl crate::peripheral::Peripheral for &mut Dma {
type P = Dma;
#[inline]
unsafe fn clone_unchecked(&mut self) -> Self::P {
Dma { _private: () }
}
}
}

0 comments on commit a90aa3a

Please sign in to comment.