Skip to content

Commit

Permalink
Add helper traits to simplify DMA channel trait bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 13, 2024
1 parent 44a0ea1 commit 5eda9a5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 22 deletions.
4 changes: 2 additions & 2 deletions esp-hal/src/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ pub mod dma {
Channel,
ChannelRx,
ChannelTx,
CompatibleWith,
DescriptorChain,
DmaChannelConvert,
DmaChannelFor,
DmaDescriptor,
DmaPeripheral,
Expand Down Expand Up @@ -293,7 +293,7 @@ pub mod dma {
tx_descriptors: &'static mut [DmaDescriptor],
) -> AesDma<'d>
where
CH: DmaChannelConvert<DmaChannelFor<AES>>,
CH: CompatibleWith<AES>,
{
let channel = Channel::new(channel.map(|ch| ch.degrade()));
channel.runtime_ensure_compatible(&self.aes);
Expand Down
63 changes: 63 additions & 0 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1633,6 +1633,69 @@ impl<DEG: DmaChannel> DmaChannelConvert<DEG> for DEG {
}
}

/// Trait implemented for DMA channels that are compatible with a particular
/// peripheral.
#[cfg_attr(pdma, doc = "")]
#[cfg_attr(
pdma,
doc = "Note that using mismatching channels (e.g. trying to use `spi2channel` with SPI3) may compile, but will panic in runtime."
)]
#[cfg_attr(pdma, doc = "")]
/// ## Example
///
/// The following example demonstrates how this trait can be used to only accept
/// types compatible with a specific peripheral.
///
/// ```rust,no_run
#[doc = crate::before_snippet!()]
/// use esp_hal::spi::master::{Spi, Config, Instance as SpiInstance};
/// use esp_hal::dma::CompatibleWith;
/// use esp_hal::Blocking;
/// use esp_hal::dma::Dma;
///
/// fn takes_spi<S: SpiInstance>(spi: Spi<'_, Blocking, S>, channel: impl
/// CompatibleWith<S>) {}
///
/// let dma = Dma::new(peripherals.DMA);
#[cfg_attr(pdma, doc = "let dma_channel = dma.spi2channel;")]
#[cfg_attr(gdma, doc = "let dma_channel = dma.channel0;")]
#[doc = ""]
/// let spi = Spi::new_with_config(
/// peripherals.SPI2,
/// Config::default(),
/// );
///
/// takes_spi(spi, dma_channel);
/// # }
/// ```
pub trait CompatibleWith<P: DmaEligible>: DmaChannel + DmaChannelConvert<DmaChannelFor<P>> {}
impl<P, CH> CompatibleWith<P> for CH
where
P: DmaEligible,
CH: DmaChannel + DmaChannelConvert<DmaChannelFor<P>>,
{
}

/// Trait implemented for the RX half of split DMA channels that are compatible
/// with a particular peripheral.
pub trait RxCompatibleWith<P: DmaEligible>: DmaChannelConvert<RxChannelFor<P>> {}
impl<P, RX> RxCompatibleWith<P> for RX
where
P: DmaEligible,
RX: DmaChannelConvert<RxChannelFor<P>>,
{
}

/// Trait implemented for the TX half of split DMA channels that are compatible
/// with a particular peripheral.
pub trait TxCompatibleWith<PER: DmaEligible>: DmaChannelConvert<TxChannelFor<PER>> {}
impl<P, TX> TxCompatibleWith<P> for TX
where
P: DmaEligible,
TX: DmaChannelConvert<TxChannelFor<P>>,
{
}

/// The functions here are not meant to be used outside the HAL
#[doc(hidden)]
pub trait Rx: crate::private::Sealed {
Expand Down
6 changes: 3 additions & 3 deletions esp-hal/src/i2s/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ use crate::{
Channel,
ChannelRx,
ChannelTx,
CompatibleWith,
DescriptorChain,
DmaChannelConvert,
DmaChannelFor,
DmaDescriptor,
DmaEligible,
Expand Down Expand Up @@ -372,7 +372,7 @@ impl<'d> I2s<'d, Blocking> {
tx_descriptors: &'static mut [DmaDescriptor],
) -> Self
where
CH: DmaChannelConvert<DmaChannelFor<AnyI2s>>,
CH: CompatibleWith<AnyI2s>,
{
Self::new_typed(
i2s.map_into(),
Expand Down Expand Up @@ -403,7 +403,7 @@ where
tx_descriptors: &'static mut [DmaDescriptor],
) -> Self
where
CH: DmaChannelConvert<DmaChannelFor<T>>,
CH: CompatibleWith<T>,
{
crate::into_ref!(i2s);
Self::new_internal(
Expand Down
7 changes: 3 additions & 4 deletions esp-hal/src/i2s/parallel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ use crate::{
asynch::DmaTxFuture,
Channel,
ChannelTx,
DmaChannelConvert,
DmaChannelFor,
CompatibleWith,
DmaEligible,
DmaError,
DmaPeripheral,
Expand Down Expand Up @@ -192,7 +191,7 @@ impl<'d> I2sParallel<'d, Blocking> {
clock_pin: impl Peripheral<P = impl PeripheralOutput> + 'd,
) -> Self
where
CH: DmaChannelConvert<DmaChannelFor<AnyI2s>>,
CH: CompatibleWith<AnyI2s>,
{
Self::new_typed(i2s.map_into(), channel, frequency, pins, clock_pin)
}
Expand All @@ -211,7 +210,7 @@ where
clock_pin: impl Peripheral<P = impl PeripheralOutput> + 'd,
) -> Self
where
CH: DmaChannelConvert<DmaChannelFor<I>>,
CH: CompatibleWith<I>,
{
crate::into_ref!(i2s);
crate::into_mapped_ref!(clock_pin);
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/lcd_cam/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use fugit::HertzU32;

use crate::{
clock::Clocks,
dma::{ChannelRx, DmaChannelConvert, DmaError, DmaPeripheral, DmaRxBuffer, Rx, RxChannelFor},
dma::{ChannelRx, DmaError, DmaPeripheral, DmaRxBuffer, Rx, RxChannelFor, RxCompatibleWith},
gpio::{
interconnect::{PeripheralInput, PeripheralOutput},
InputSignal,
Expand Down Expand Up @@ -133,7 +133,7 @@ impl<'d> Camera<'d> {
frequency: HertzU32,
) -> Self
where
CH: DmaChannelConvert<RxChannelFor<LCD_CAM>>,
CH: RxCompatibleWith<LCD_CAM>,
P: RxPins,
{
let rx_channel = ChannelRx::new(channel.map(|ch| ch.degrade()));
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/lcd_cam/lcd/i8080.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use fugit::HertzU32;

use crate::{
clock::Clocks,
dma::{ChannelTx, DmaChannelConvert, DmaError, DmaPeripheral, DmaTxBuffer, Tx, TxChannelFor},
dma::{ChannelTx, DmaError, DmaPeripheral, DmaTxBuffer, Tx, TxChannelFor, TxCompatibleWith},
gpio::{
interconnect::{OutputConnection, PeripheralOutput},
OutputSignal,
Expand Down Expand Up @@ -102,7 +102,7 @@ where
config: Config,
) -> Self
where
CH: DmaChannelConvert<TxChannelFor<LCD_CAM>>,
CH: TxCompatibleWith<LCD_CAM>,
P: TxPins,
{
let tx_channel = ChannelTx::new(channel.map(|ch| ch.degrade()));
Expand Down
11 changes: 6 additions & 5 deletions esp-hal/src/parl_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ use crate::{
Channel,
ChannelRx,
ChannelTx,
CompatibleWith,
DescriptorChain,
DmaChannelConvert,
DmaChannelFor,
DmaDescriptor,
DmaError,
DmaPeripheral,
Expand All @@ -45,8 +44,10 @@ use crate::{
ReadBuffer,
Rx,
RxChannelFor,
RxCompatibleWith,
Tx,
TxChannelFor,
TxCompatibleWith,
WriteBuffer,
},
gpio::{
Expand Down Expand Up @@ -1005,7 +1006,7 @@ impl<'d> ParlIoFullDuplex<'d, Blocking> {
frequency: HertzU32,
) -> Result<Self, Error>
where
CH: DmaChannelConvert<DmaChannelFor<PARL_IO>>,
CH: CompatibleWith<PARL_IO>,
{
let dma_channel = Channel::new(dma_channel.map(|ch| ch.degrade()));
internal_init(frequency)?;
Expand Down Expand Up @@ -1119,7 +1120,7 @@ impl<'d> ParlIoTxOnly<'d, Blocking> {
frequency: HertzU32,
) -> Result<Self, Error>
where
CH: DmaChannelConvert<TxChannelFor<PARL_IO>>,
CH: TxCompatibleWith<PARL_IO>,
{
let tx_channel = ChannelTx::new(dma_channel.map(|ch| ch.degrade()));
internal_init(frequency)?;
Expand Down Expand Up @@ -1221,7 +1222,7 @@ impl<'d> ParlIoRxOnly<'d, Blocking> {
frequency: HertzU32,
) -> Result<Self, Error>
where
CH: DmaChannelConvert<RxChannelFor<PARL_IO>>,
CH: RxCompatibleWith<PARL_IO>,
{
let rx_channel = ChannelRx::new(dma_channel.map(|ch| ch.degrade()));
internal_init(frequency)?;
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/spi/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ use procmacros::ram;
use super::{DmaError, Error, SpiBitOrder, SpiDataMode, SpiMode};
use crate::{
clock::Clocks,
dma::{DmaChannelConvert, DmaChannelFor, DmaEligible, DmaRxBuffer, DmaTxBuffer, Rx, Tx},
dma::{CompatibleWith, DmaEligible, DmaRxBuffer, DmaTxBuffer, Rx, Tx},
gpio::{interconnect::PeripheralOutput, InputSignal, NoPin, OutputSignal},
interrupt::InterruptHandler,
peripheral::{Peripheral, PeripheralRef},
Expand Down Expand Up @@ -538,7 +538,7 @@ where
/// operations.
pub fn with_dma<CH>(self, channel: impl Peripheral<P = CH> + 'd) -> SpiDma<'d, Blocking, T>
where
CH: DmaChannelConvert<DmaChannelFor<T>>,
CH: CompatibleWith<T>,
{
SpiDma::new(self.spi, channel.map(|ch| ch.degrade()).into_ref())
}
Expand Down
5 changes: 3 additions & 2 deletions esp-hal/src/spi/slave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ use core::marker::PhantomData;

use super::{Error, SpiMode};
use crate::{
dma::{DmaChannelConvert, DmaEligible},
dma::DmaEligible,
gpio::{
interconnect::{PeripheralInput, PeripheralOutput},
InputSignal,
Expand Down Expand Up @@ -175,6 +175,7 @@ pub mod dma {
Channel,
ChannelRx,
ChannelTx,
CompatibleWith,
DescriptorChain,
DmaChannelFor,
DmaDescriptor,
Expand Down Expand Up @@ -205,7 +206,7 @@ pub mod dma {
tx_descriptors: &'static mut [DmaDescriptor],
) -> SpiDma<'d, Blocking, T>
where
CH: DmaChannelConvert<DmaChannelFor<T>>,
CH: CompatibleWith<T>,
{
self.spi.info().set_data_mode(self.data_mode, true);
SpiDma::new(
Expand Down

0 comments on commit 5eda9a5

Please sign in to comment.