diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs index 6bef745f..fd9a07ce 100644 --- a/embassy-stm32/build.rs +++ b/embassy-stm32/build.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream; use quote::{format_ident, quote}; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::env; use std::fs; use std::path::PathBuf; @@ -230,6 +230,61 @@ fn main() { }) } + // ======== + // Generate dma_trait_impl! + + let signals: HashMap<_, _> = [ + // (kind, signal) => trait + (("usart", "RX"), quote!(crate::usart::RxDma)), + (("usart", "TX"), quote!(crate::usart::TxDma)), + (("spi", "RX"), quote!(crate::spi::RxDma)), + (("spi", "TX"), quote!(crate::spi::TxDma)), + (("i2c", "RX"), quote!(crate::i2c::RxDma)), + (("i2c", "TX"), quote!(crate::i2c::TxDma)), + (("dcmi", "DCMI"), quote!(crate::dcmi::FrameDma)), + (("dcmi", "PSSI"), quote!(crate::dcmi::FrameDma)), + ] + .into(); + + for p in METADATA.peripherals { + if let Some(regs) = &p.registers { + let mut dupe = HashSet::new(); + for ch in p.dma_channels { + // Some chips have multiple request numbers for the same (peri, signal, channel) combos. + // Ignore the dupes, picking the first one. Otherwise this causes conflicting trait impls + let key = (ch.signal, ch.channel); + if !dupe.insert(key) { + continue; + } + + if let Some(tr) = signals.get(&(regs.kind, ch.signal)) { + let peri = format_ident!("{}", p.name); + + let channel = if let Some(channel) = &ch.channel { + let channel = format_ident!("{}", channel); + quote!({channel: #channel}) + } else if let Some(dmamux) = &ch.dmamux { + let dmamux = format_ident!("{}", dmamux); + quote!({dmamux: #dmamux}) + } else { + unreachable!(); + }; + + let request = if let Some(request) = ch.request { + let request = request as u8; + quote!(#request) + } else { + quote!(()) + }; + + g.extend(quote! { + dma_trait_impl!(#tr, #peri, #channel, #request); + }); + } + } + } + } + // ======== // Write generated.rs diff --git a/embassy-stm32/src/dcmi.rs b/embassy-stm32/src/dcmi.rs index 1fa19e87..ff6f0970 100644 --- a/embassy-stm32/src/dcmi.rs +++ b/embassy-stm32/src/dcmi.rs @@ -482,15 +482,6 @@ crate::pac::interrupts! { dma_trait!(FrameDma, Instance); -crate::pac::peripheral_dma_channels! { - ($peri:ident, dcmi, $kind:ident, PSSI, $channel:tt, $request:expr) => { - dma_trait_impl!(FrameDma, $peri, $channel, $request); - }; - ($peri:ident, dcmi, $kind:ident, DCMI, $channel:tt, $request:expr) => { - dma_trait_impl!(FrameDma, $peri, $channel, $request); - }; -} - crate::pac::peripheral_pins!( ($inst:ident, dcmi, DCMI, $pin:ident, D0, $af:expr) => { pin_trait_impl!(D0Pin, $inst, $pin, $af); diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs index 0980fd9e..ff13d9f1 100644 --- a/embassy-stm32/src/i2c/mod.rs +++ b/embassy-stm32/src/i2c/mod.rs @@ -91,12 +91,3 @@ crate::pac::peripheral_pins!( pin_trait_impl!(SdaPin, $inst, $pin, 0); }; ); - -crate::pac::peripheral_dma_channels! { - ($peri:ident, i2c, $kind:ident, RX, $channel:tt, $request:expr) => { - dma_trait_impl!(RxDma, $peri, $channel, $request); - }; - ($peri:ident, i2c, $kind:ident, TX, $channel:tt, $request:expr) => { - dma_trait_impl!(TxDma, $peri, $channel, $request); - }; -} diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index 04f95820..e91241e9 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -911,12 +911,3 @@ crate::pac::peripheral_pins!( pin_trait_impl!(MisoPin, $inst, $pin, 0); }; ); - -crate::pac::peripheral_dma_channels! { - ($peri:ident, spi, $kind:ident, RX, $channel:tt, $request:expr) => { - dma_trait_impl!(RxDma, $peri, $channel, $request); - }; - ($peri:ident, spi, $kind:ident, TX, $channel:tt, $request:expr) => { - dma_trait_impl!(TxDma, $peri, $channel, $request); - }; -} diff --git a/embassy-stm32/src/traits.rs b/embassy-stm32/src/traits.rs index b2762870..e006e31a 100644 --- a/embassy-stm32/src/traits.rs +++ b/embassy-stm32/src/traits.rs @@ -31,8 +31,8 @@ macro_rules! dma_trait { #[allow(unused)] macro_rules! dma_trait_impl { // DMAMUX - ($signal:ident, $instance:ident, {dmamux: $dmamux:ident}, $request:expr) => { - impl $signal for T + (crate::$mod:ident::$trait:ident, $instance:ident, {dmamux: $dmamux:ident}, $request:expr) => { + impl crate::$mod::$trait for T where T: crate::dma::MuxChannel, { @@ -43,8 +43,8 @@ macro_rules! dma_trait_impl { }; // No DMAMUX - ($signal:ident, $instance:ident, {channel: $channel:ident}, $request:expr) => { - impl $signal for crate::peripherals::$channel { + (crate::$mod:ident::$trait:ident, $instance:ident, {channel: $channel:ident}, $request:expr) => { + impl crate::$mod::$trait for crate::peripherals::$channel { fn request(&self) -> crate::dma::Request { $request } diff --git a/embassy-stm32/src/usart/mod.rs b/embassy-stm32/src/usart/mod.rs index 71855981..52c2cd84 100644 --- a/embassy-stm32/src/usart/mod.rs +++ b/embassy-stm32/src/usart/mod.rs @@ -712,18 +712,3 @@ crate::pac::peripheral_pins!( pin_trait_impl!(CkPin, $inst, $pin, 0); }; ); - -crate::pac::peripheral_dma_channels! { - ($peri:ident, usart, $kind:ident, RX, $channel:tt, $request:expr) => { - dma_trait_impl!(RxDma, $peri, $channel, $request); - }; - ($peri:ident, usart, $kind:ident, TX, $channel:tt, $request:expr) => { - dma_trait_impl!(TxDma, $peri, $channel, $request); - }; - ($peri:ident, uart, $kind:ident, RX, $channel:tt, $request:expr) => { - dma_trait_impl!(RxDma, $peri, $channel, $request); - }; - ($peri:ident, uart, $kind:ident, TX, $channel:tt, $request:expr) => { - dma_trait_impl!(TxDma, $peri, $channel, $request); - }; -}