Skip to content

Commit

Permalink
Remove ChannelCreator types
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 4, 2024
1 parent 40c2658 commit 2752db1
Show file tree
Hide file tree
Showing 39 changed files with 201 additions and 320 deletions.
57 changes: 31 additions & 26 deletions esp-hal/src/dma/gdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,20 +534,23 @@ macro_rules! impl_channel {
}
}

impl ChannelCreator<$num> {
/// Configure the channel for use with blocking APIs
pub fn configure<'a>(
self,
burst_mode: bool,
priority: DmaPriority,
) -> Channel<'a, [<DmaChannel $num>], Blocking> {
impl [<DmaChannel $num>] {
/// Unsafely constructs a new DMA channel.
///
/// # Safety
///
/// The caller must ensure that only a single instance is used.
pub unsafe fn steal<'a>() -> Channel<'a, Self, Blocking> {
let tx_impl = ChannelTxImpl(SpecificGdmaChannel::<$num> {});
let rx_impl = ChannelRxImpl(SpecificGdmaChannel::<$num> {});

let mut this = Channel {
tx: ChannelTx::new(ChannelTxImpl(SpecificGdmaChannel::<$num> {})),
rx: ChannelRx::new(ChannelRxImpl(SpecificGdmaChannel::<$num> {})),
tx: ChannelTx::new(tx_impl),
rx: ChannelRx::new(rx_impl),
phantom: PhantomData,
};

this.configure(burst_mode, priority);
this.configure(false, DmaPriority::Priority0);

this
}
Expand Down Expand Up @@ -643,19 +646,19 @@ crate::impl_dma_eligible! {
pub struct Dma<'d> {
_inner: PeripheralRef<'d, crate::peripherals::DMA>,
/// Channel 0
pub channel0: ChannelCreator<0>,
pub channel0: Channel<'d, DmaChannel0, Blocking>,
/// Channel 1
#[cfg(not(esp32c2))]
pub channel1: ChannelCreator<1>,
pub channel1: Channel<'d, DmaChannel1, Blocking>,
/// Channel 2
#[cfg(not(esp32c2))]
pub channel2: ChannelCreator<2>,
pub channel2: Channel<'d, DmaChannel2, Blocking>,
/// Channel 3
#[cfg(esp32s3)]
pub channel3: ChannelCreator<3>,
pub channel3: Channel<'d, DmaChannel3, Blocking>,
/// Channel 4
#[cfg(esp32s3)]
pub channel4: ChannelCreator<4>,
pub channel4: Channel<'d, DmaChannel4, Blocking>,
}

impl<'d> Dma<'d> {
Expand All @@ -671,17 +674,19 @@ impl<'d> Dma<'d> {
.modify(|_, w| w.ahbm_rst_inter().clear_bit());
dma.misc_conf().modify(|_, w| w.clk_en().set_bit());

Dma {
_inner: dma,
channel0: ChannelCreator {},
#[cfg(not(esp32c2))]
channel1: ChannelCreator {},
#[cfg(not(esp32c2))]
channel2: ChannelCreator {},
#[cfg(esp32s3)]
channel3: ChannelCreator {},
#[cfg(esp32s3)]
channel4: ChannelCreator {},
unsafe {
Dma {
_inner: dma,
channel0: DmaChannel0::steal(),
#[cfg(not(esp32c2))]
channel1: DmaChannel1::steal(),
#[cfg(not(esp32c2))]
channel2: DmaChannel2::steal(),
#[cfg(esp32s3)]
channel3: DmaChannel3::steal(),
#[cfg(esp32s3)]
channel4: DmaChannel4::steal(),
}
}
}
}
7 changes: 2 additions & 5 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//! # use esp_hal::dma_buffers;
//! # use esp_hal::gpio::Io;
//! # use esp_hal::spi::{master::Spi, SpiMode};
//! # use esp_hal::dma::{Dma, DmaPriority};
//! # use esp_hal::dma::Dma;
//! let dma = Dma::new(peripherals.DMA);
#![cfg_attr(any(esp32, esp32s2), doc = "let dma_channel = dma.spi2channel;")]
#![cfg_attr(not(any(esp32, esp32s2)), doc = "let dma_channel = dma.channel0;")]
Expand All @@ -39,10 +39,7 @@
//! .with_mosi(mosi)
//! .with_miso(miso)
//! .with_cs(cs)
//! .with_dma(dma_channel.configure(
//! false,
//! DmaPriority::Priority0,
//! ));
//! .with_dma(dma_channel);
//! # }
//! ```
//!
Expand Down
75 changes: 38 additions & 37 deletions esp-hal/src/dma/pdma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,24 +406,23 @@ macro_rules! ImplSpiChannel {

impl $crate::private::Sealed for [<Spi $num DmaChannel>] {}

#[doc = concat!("Creates a channel for SPI", $num)]
#[non_exhaustive]
pub struct [<Spi $num DmaChannelCreator>] {}

impl [<Spi $num DmaChannelCreator>] {
/// Configure the channel for use with blocking APIs
pub fn configure<'a>(
self,
burst_mode: bool,
priority: DmaPriority,
) -> Channel<'a, [<Spi $num DmaChannel>], Blocking> {
impl [<Spi $num DmaChannel>] {
/// Unsafely constructs a new DMA channel.
///
/// # Safety
///
/// The caller must ensure that only a single instance is used.
pub unsafe fn steal<'a>() -> Channel<'a, Self, Blocking> {
let tx_impl = SpiDmaTxChannelImpl(Self {});
let rx_impl = SpiDmaRxChannelImpl(Self {});

let mut this = Channel {
tx: ChannelTx::new(SpiDmaTxChannelImpl([<Spi $num DmaChannel>] {})),
rx: ChannelRx::new(SpiDmaRxChannelImpl([<Spi $num DmaChannel>] {})),
tx: ChannelTx::new(tx_impl),
rx: ChannelRx::new(rx_impl),
phantom: PhantomData,
};

this.configure(burst_mode, priority);
this.configure(false, DmaPriority::Priority0);

this
}
Expand Down Expand Up @@ -816,23 +815,23 @@ macro_rules! ImplI2sChannel {
}
}

#[doc = concat!("Creates a channel for I2S", $num)]
pub struct [<I2s $num DmaChannelCreator>] {}
impl [<I2s $num DmaChannel>] {
/// Unsafely constructs a new DMA channel.
///
/// # Safety
///
/// The caller must ensure that only a single instance is used.
pub unsafe fn steal<'a>() -> Channel<'a, Self, Blocking> {
let tx_impl = I2sDmaTxChannelImpl(Self {});
let rx_impl = I2sDmaRxChannelImpl(Self {});

impl [<I2s $num DmaChannelCreator>] {
/// Configure the channel for use with blocking APIs
pub fn configure<'a>(
self,
burst_mode: bool,
priority: DmaPriority,
) -> Channel<'a, [<I2s $num DmaChannel>], Blocking> {
let mut this = Channel {
tx: ChannelTx::new(I2sDmaTxChannelImpl([<I2s $num DmaChannel>] {})),
rx: ChannelRx::new(I2sDmaRxChannelImpl([<I2s $num DmaChannel>] {})),
tx: ChannelTx::new(tx_impl),
rx: ChannelRx::new(rx_impl),
phantom: PhantomData,
};

this.configure(burst_mode, priority);
this.configure(false, DmaPriority::Priority0);

this
}
Expand Down Expand Up @@ -863,14 +862,14 @@ crate::impl_dma_eligible!([I2s1DmaChannel] I2S1 => I2s1);
pub struct Dma<'d> {
_inner: PeripheralRef<'d, crate::peripherals::DMA>,
/// DMA channel for SPI2
pub spi2channel: Spi2DmaChannelCreator,
pub spi2channel: Channel<'d, Spi2DmaChannel, Blocking>,
/// DMA channel for SPI3
pub spi3channel: Spi3DmaChannelCreator,
pub spi3channel: Channel<'d, Spi3DmaChannel, Blocking>,
/// DMA channel for I2S0
pub i2s0channel: I2s0DmaChannelCreator,
pub i2s0channel: Channel<'d, I2s0DmaChannel, Blocking>,
/// DMA channel for I2S1
#[cfg(i2s1)]
pub i2s1channel: I2s1DmaChannelCreator,
pub i2s1channel: Channel<'d, I2s1DmaChannel, Blocking>,
}

impl<'d> Dma<'d> {
Expand All @@ -893,13 +892,15 @@ impl<'d> Dma<'d> {
});
}

Dma {
_inner: dma.into_ref(),
spi2channel: Spi2DmaChannelCreator {},
spi3channel: Spi3DmaChannelCreator {},
i2s0channel: I2s0DmaChannelCreator {},
#[cfg(i2s1)]
i2s1channel: I2s1DmaChannelCreator {},
unsafe {
Dma {
_inner: dma.into_ref(),
spi2channel: Spi2DmaChannel::steal(),
spi3channel: Spi3DmaChannel::steal(),
i2s0channel: I2s0DmaChannel::steal(),
#[cfg(i2s1)]
i2s1channel: I2s1DmaChannel::steal(),
}
}
}
}
Expand Down
7 changes: 2 additions & 5 deletions esp-hal/src/i2s.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//! # use esp_hal::i2s::DataFormat;
//! # use esp_hal::gpio::Io;
//! # use esp_hal::dma_buffers;
//! # use esp_hal::dma::{Dma, DmaPriority};
//! # use esp_hal::dma::Dma;
//! # let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
//! let dma = Dma::new(peripherals.DMA);
#![cfg_attr(any(esp32, esp32s2), doc = "let dma_channel = dma.i2s0channel;")]
Expand All @@ -47,10 +47,7 @@
//! Standard::Philips,
//! DataFormat::Data16Channel16,
//! 44100.Hz(),
//! dma_channel.configure(
//! false,
//! DmaPriority::Priority0,
//! ),
//! dma_channel,
//! rx_descriptors,
//! tx_descriptors,
//! );
Expand Down
7 changes: 1 addition & 6 deletions esp-hal/src/lcd_cam/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,14 @@
//! # use esp_hal::lcd_cam::{cam::{Camera, RxEightBits}, LcdCam};
//! # use fugit::RateExtU32;
//! # use esp_hal::dma_rx_stream_buffer;
//! # use esp_hal::dma::{Dma, DmaPriority};
//! # use esp_hal::dma::Dma;
//! # let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
//!
//! # let dma = Dma::new(peripherals.DMA);
//! # let channel = dma.channel0;
//!
//! # let dma_buf = dma_rx_stream_buffer!(20 * 1000, 1000);
//!
//! # let channel = channel.configure(
//! # false,
//! # DmaPriority::Priority0,
//! # );
//!
//! let mclk_pin = io.pins.gpio15;
//! let vsync_pin = io.pins.gpio6;
//! let href_pin = io.pins.gpio7;
Expand Down
7 changes: 1 addition & 6 deletions esp-hal/src/lcd_cam/lcd/i8080.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,14 @@
//! # use esp_hal::gpio::Io;
//! # use esp_hal::lcd_cam::{LcdCam, lcd::i8080::{Config, I8080, TxEightBits}};
//! # use esp_hal::dma_tx_buffer;
//! # use esp_hal::dma::{Dma, DmaPriority, DmaTxBuf};
//! # use esp_hal::dma::{Dma, DmaTxBuf};
//! # let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
//!
//! # let dma = Dma::new(peripherals.DMA);
//! # let channel = dma.channel0;
//!
//! # let mut dma_buf = dma_tx_buffer!(32678).unwrap();
//!
//! # let channel = channel.configure(
//! # false,
//! # DmaPriority::Priority0,
//! # );
//!
//! let tx_pins = TxEightBits::new(
//! io.pins.gpio9,
//! io.pins.gpio46,
Expand Down
2 changes: 2 additions & 0 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,8 @@ mod dma {
Rx,
Tx,
},
Async,
Blocking,
InterruptConfigurable,
};

Expand Down
6 changes: 1 addition & 5 deletions esp-hal/src/spi/slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
//!
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! # use esp_hal::dma::DmaPriority;
//! # use esp_hal::dma_buffers;
//! # use esp_hal::spi::SpiMode;
//! # use esp_hal::spi::slave::Spi;
Expand All @@ -39,10 +38,7 @@
//! cs,
//! SpiMode::Mode0,
//! )
//! .with_dma(dma_channel.configure(
//! false,
//! DmaPriority::Priority0,
//! ), rx_descriptors, tx_descriptors);
//! .with_dma(dma_channel, rx_descriptors, tx_descriptors);
//!
//! let mut receive = rx_buffer;
//! let mut send = tx_buffer;
Expand Down
5 changes: 2 additions & 3 deletions examples/src/bin/dma_extmem2mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use esp_alloc as _;
use esp_backtrace as _;
use esp_hal::{
delay::Delay,
dma::{Dma, DmaPriority, Mem2Mem},
dma::{Dma, Mem2Mem},
dma_descriptors_chunk_size,
prelude::*,
};
Expand Down Expand Up @@ -68,11 +68,10 @@ fn main() -> ! {
let (rx_descriptors, tx_descriptors) = dma_descriptors_chunk_size!(DATA_SIZE, CHUNK_SIZE);

let dma = Dma::new(peripherals.DMA);
let channel = dma.channel0.configure(false, DmaPriority::Priority0);
let dma_peripheral = peripherals.SPI2;

let mut mem2mem = Mem2Mem::new_with_chunk_size(
channel,
dma.channel0,
dma_peripheral,
rx_descriptors,
tx_descriptors,
Expand Down
5 changes: 2 additions & 3 deletions examples/src/bin/dma_mem2mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use esp_backtrace as _;
use esp_hal::{
delay::Delay,
dma::{Dma, DmaPriority, Mem2Mem},
dma::{Dma, Mem2Mem},
dma_buffers,
prelude::*,
};
Expand All @@ -28,14 +28,13 @@ fn main() -> ! {
let (mut rx_buffer, rx_descriptors, tx_buffer, tx_descriptors) = dma_buffers!(DATA_SIZE);

let dma = Dma::new(peripherals.DMA);
let channel = dma.channel0.configure(false, DmaPriority::Priority0);
#[cfg(any(feature = "esp32c2", feature = "esp32c3", feature = "esp32s3"))]
let dma_peripheral = peripherals.SPI2;
#[cfg(not(any(feature = "esp32c2", feature = "esp32c3", feature = "esp32s3")))]
let dma_peripheral = peripherals.MEM2MEM1;

let mut mem2mem =
Mem2Mem::new(channel, dma_peripheral, rx_descriptors, tx_descriptors).unwrap();
Mem2Mem::new(dma.channel0, dma_peripheral, rx_descriptors, tx_descriptors).unwrap();

for i in 0..core::mem::size_of_val(tx_buffer) {
tx_buffer[i] = (i % 256) as u8;
Expand Down
4 changes: 2 additions & 2 deletions examples/src/bin/embassy_i2s_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use embassy_executor::Spawner;
use esp_backtrace as _;
use esp_hal::{
dma::{Dma, DmaPriority},
dma::Dma,
dma_buffers,
gpio::Io,
i2s::{DataFormat, I2s, Standard},
Expand Down Expand Up @@ -52,7 +52,7 @@ async fn main(_spawner: Spawner) {
Standard::Philips,
DataFormat::Data16Channel16,
44100u32.Hz(),
dma_channel.configure(false, DmaPriority::Priority0),
dma_channel,
rx_descriptors,
tx_descriptors,
)
Expand Down
Loading

0 comments on commit 2752db1

Please sign in to comment.