Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STM32F0/F3 Remap DMA channels #3653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion embassy-stm32/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,27 @@ fn main() {
// ========
// Generate dma_trait_impl!

let syscfg_peripheral = METADATA
.peripherals
.iter()
.find(|p| p.name.eq_ignore_ascii_case("syscfg"))
.unwrap();

let syscfg_ir = syscfg_peripheral.registers.as_ref().unwrap().ir;

let cfgr1_offset = syscfg_ir
.blocks
.iter()
.find(|b| b.name.eq_ignore_ascii_case("syscfg"))
.unwrap()
.items
.iter()
.find(|i| i.name.eq_ignore_ascii_case("cfgr1"))
.unwrap()
.byte_offset;

let cfgr1 = syscfg_peripheral.address as u32 + cfgr1_offset;

let mut signals: HashMap<_, _> = [
// (kind, signal) => trait
(("adc", "ADC"), quote!(crate::adc::RxDma)),
Expand Down Expand Up @@ -1334,9 +1355,45 @@ fn main() {
quote!(())
};

let remap = ch.syscfg_cfgr1_remap_bit.map_or(quote! { () }, |remap| {
let fields: Vec<_> = syscfg_ir
.fieldsets
.iter()
.find(|i| i.name.eq_ignore_ascii_case("cfgr1"))
.unwrap()
.fields
.iter()
.filter(|f| {
let field_name = f.name.to_lowercase();
field_name.contains("dma_rmp")
&& field_name.contains(p.name.to_lowercase().as_str())
})
.collect();

let remap_field = if fields.len() == 1 {
fields.first().unwrap()
} else {
fields
.iter()
.find(|f| f.name.contains(ch.signal.to_lowercase().as_str()))
.unwrap()
};

let BitOffset::Regular(ref bit_offset) = remap_field.bit_offset else {
panic!("cursed bit offset")
};
let bit_offset: u8 = bit_offset.offset.try_into().unwrap();

if remap {
quote! { (#cfgr1 as *mut u32).write((#cfgr1 as *mut u32).read() | 1 << #bit_offset) }
} else {
quote! { (#cfgr1 as *mut u32).write((#cfgr1 as *mut u32).read() & !(1 << #bit_offset)) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you make this use the PAC to do the register writes, like all the other build.rs-generated code? pac::SYSCFG.cfgr1().modify(|w| w.set_whatever(val))

}
});

let channel = format_ident!("{}", channel);
g.extend(quote! {
dma_trait_impl!(#tr, #peri, #channel, #request);
dma_trait_impl!(#tr, #peri, #channel, #request, #remap);
});
}
}
Expand Down
14 changes: 13 additions & 1 deletion embassy-stm32/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,24 +70,36 @@ macro_rules! dma_trait {
/// Note: in some chips, ST calls this the "channel", and calls channels "streams".
/// `embassy-stm32` always uses the "channel" and "request number" names.
fn request(&self) -> crate::dma::Request;
#[doc = "Remap the DMA channel"]
fn remap(&self);
}
};
}

#[allow(unused)]
macro_rules! dma_trait_impl {
(crate::$mod:ident::$trait:ident$(<$mode:ident>)?, $instance:ident, $channel:ident, $request:expr) => {
(crate::$mod:ident::$trait:ident$(<$mode:ident>)?, $instance:ident, $channel:ident, $request:expr, $remap:expr) => {
impl crate::$mod::$trait<crate::peripherals::$instance $(, crate::$mod::$mode)?> for crate::peripherals::$channel {
fn request(&self) -> crate::dma::Request {
$request
}

fn remap(&self) {
critical_section::with(|_| {
#[allow(unused_unsafe)]
unsafe {
$remap;
}
});
}
}
};
}

macro_rules! new_dma {
($name:ident) => {{
let dma = $name.into_ref();
dma.remap();
let request = dma.request();
Some(crate::dma::ChannelAndRequest {
channel: dma.map_into(),
Expand Down