aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/build.rs
diff options
context:
space:
mode:
authorDario Nieuwenhuis <[email protected]>2025-09-05 23:00:31 +0200
committerDario Nieuwenhuis <[email protected]>2025-09-05 23:00:31 +0200
commit7419b398bf7cc5c1ff164c504f4a4027cd6bcd3b (patch)
tree9ea26e1059b70502d0e5929a72a9f50c8c43838b /embassy-stm32/build.rs
parenta6562c4f033432e40970aafe82f33c5138adf84e (diff)
stm32/afio: use type inference for timer remaps as well.
Diffstat (limited to 'embassy-stm32/build.rs')
-rw-r--r--embassy-stm32/build.rs131
1 files changed, 41 insertions, 90 deletions
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index f55e1e0c9..0d3582c9d 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -1391,58 +1391,51 @@ fn main() {
1391 }) 1391 })
1392 } 1392 }
1393 1393
1394 let pin_trait_impl = p 1394 let pin_trait_impl = if let Some(afio) = &p.afio {
1395 .afio 1395 let values = afio
1396 .as_ref() 1396 .values
1397 .and_then(|afio| { 1397 .iter()
1398 if p.name.starts_with("TIM") { 1398 .filter(|v| v.pins.contains(&pin.pin))
1399 // timers are handled by timer_afio_impl!() 1399 .map(|v| v.value)
1400 return None; 1400 .collect::<Vec<_>>();
1401 }
1402
1403 let values = afio
1404 .values
1405 .iter()
1406 .filter(|v| v.pins.contains(&pin.pin))
1407 .map(|v| v.value)
1408 .collect::<Vec<_>>();
1409 1401
1410 if values.is_empty() { 1402 if values.is_empty() {
1411 None 1403 None
1412 } else { 1404 } else {
1413 let setter = format_ident!("set_{}", afio.field.to_lowercase()); 1405 let reg = format_ident!("{}", afio.register.to_lowercase());
1414 let type_and_values = if is_bool_field("AFIO", afio.register, afio.field) { 1406 let setter = format_ident!("set_{}", afio.field.to_lowercase());
1415 let values = values.iter().map(|&v| v > 0); 1407 let type_and_values = if is_bool_field("AFIO", afio.register, afio.field) {
1416 quote!(AfioRemapBool, [#(#values),*]) 1408 let values = values.iter().map(|&v| v > 0);
1417 } else { 1409 quote!(AfioRemapBool, [#(#values),*])
1418 quote!(AfioRemap, [#(#values),*])
1419 };
1420
1421 Some(quote! {
1422 pin_trait_afio_impl!(#tr, #peri, #pin_name, {#setter, #type_and_values});
1423 })
1424 }
1425 })
1426 .unwrap_or_else(|| {
1427 let peripherals_with_afio = [
1428 "CAN",
1429 "CEC",
1430 "ETH",
1431 "I2C",
1432 "SPI",
1433 "SUBGHZSPI",
1434 "USART",
1435 "UART",
1436 "LPUART",
1437 ];
1438 let af_or_not_applicable = if peripherals_with_afio.iter().any(|&x| p.name.starts_with(x)) {
1439 quote!(0, crate::gpio::AfioRemapNotApplicable)
1440 } else { 1410 } else {
1441 quote!(#af) 1411 quote!(AfioRemap, [#(#values),*])
1442 }; 1412 };
1443 1413
1444 quote!(pin_trait_impl!(#tr, #peri, #pin_name, #af_or_not_applicable);) 1414 Some(quote! {
1445 }); 1415 pin_trait_afio_impl!(#tr, #peri, #pin_name, {#reg, #setter, #type_and_values});
1416 })
1417 }
1418 } else {
1419 let peripherals_with_afio = [
1420 "CAN",
1421 "CEC",
1422 "ETH",
1423 "I2C",
1424 "SPI",
1425 "SUBGHZSPI",
1426 "USART",
1427 "UART",
1428 "LPUART",
1429 "TIM",
1430 ];
1431 let af_or_not_applicable = if peripherals_with_afio.iter().any(|&x| p.name.starts_with(x)) {
1432 quote!(0, crate::gpio::AfioRemapNotApplicable)
1433 } else {
1434 quote!(#af)
1435 };
1436
1437 Some(quote!(pin_trait_impl!(#tr, #peri, #pin_name, #af_or_not_applicable);))
1438 };
1446 1439
1447 g.extend(pin_trait_impl); 1440 g.extend(pin_trait_impl);
1448 } 1441 }
@@ -1970,48 +1963,6 @@ fn main() {
1970 }); 1963 });
1971 1964
1972 // ======== 1965 // ========
1973 // Generate timer AFIO impls
1974
1975 for p in METADATA.peripherals {
1976 if p.name.starts_with("TIM") {
1977 let pname = format_ident!("{}", p.name);
1978 let afio = if let Some(afio) = &p.afio {
1979 let register = format_ident!("{}", afio.register.to_lowercase());
1980 let setter = format_ident!("set_{}", afio.field.to_lowercase());
1981
1982 let swj_cfg = if afio.register == "MAPR" {
1983 quote!(w.set_swj_cfg(crate::pac::afio::vals::SwjCfg::NO_OP);)
1984 } else {
1985 quote!()
1986 };
1987 let bool_eval = if is_bool_field("AFIO", &afio.register, &afio.field) {
1988 quote!(> 0)
1989 } else {
1990 quote!()
1991 };
1992
1993 let values = afio.values.iter().map(|v| {
1994 let mapr_value = v.value;
1995 let pin = v.pins.iter().map(|p| {
1996 let port_num = p.chars().nth(1).unwrap() as u8 - b'A';
1997 let pin_num = p[2..].parse::<u8>().unwrap();
1998 port_num * 16 + pin_num
1999 });
2000 quote!(#mapr_value, [#(#pin),*])
2001 });
2002
2003 quote! {
2004 , |v| crate::pac::AFIO.#register().modify(|w| { #swj_cfg w.#setter(v #bool_eval); }), #({#values}),*
2005 }
2006 } else {
2007 quote!()
2008 };
2009
2010 g.extend(quote!(timer_afio_impl!(#pname #afio);));
2011 }
2012 }
2013
2014 // ========
2015 // Generate gpio_block() function 1966 // Generate gpio_block() function
2016 1967
2017 let gpio_base = METADATA.peripherals.iter().find(|p| p.name == "GPIOA").unwrap().address as usize; 1968 let gpio_base = METADATA.peripherals.iter().find(|p| p.name == "GPIOA").unwrap().address as usize;