Skip to content

Commit 6367044

Browse files
authored
Merge pull request #600 from skoe/declarative-pin-cleanup
Move pin cleanup from code to YAML, before adding more clean-up rules
2 parents c4f321f + 23b9069 commit 6367044

File tree

2 files changed

+73
-34
lines changed

2 files changed

+73
-34
lines changed

data/extra/family/STM32H7.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
pin_cleanup:
3+
# H7 has some _C pin variants (e.g. PC2 and PC2_C). Digital stuff should always be in the non-C pin.
4+
# cubedb puts it either in both, or in the -C pin only! (in chips where the package has only the -C pin)
5+
# so we fix that up here.
6+
strip_suffix: "_C"
7+
exclude_peripherals:
8+
- ADC
9+
- DAC
10+
- COMP

stm32-data-gen/src/chips.rs

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -658,44 +658,16 @@ fn process_core(
658658
}));
659659
}
660660

661-
// H7 has some _C pin variants (e.g. PC2 and PC2_C). Digital stuff should always be in the non-C pin.
662-
// cubedb puts it either in both, or in the -C pin only! (in chips where the package has only the -C pin)
663-
// so we fix that up here.
664-
if !pname.starts_with("ADC") && !pname.starts_with("DAC") && !pname.starts_with("COMP") {
665-
for pin in &mut p.pins {
666-
if let Some(p) = pin.pin.strip_suffix("_C") {
667-
pin.pin = p.to_string();
668-
}
669-
}
670-
}
661+
peripherals.insert(p.name.clone(), p);
662+
}
671663

672-
// sort pins to avoid diff for c pins
673-
// put the ones with AF number first, so we keep them.
664+
apply_family_extras(group, &mut peripherals);
665+
666+
for p in peripherals.values_mut() {
667+
// sort and dedup pins, put the ones with AF number first, so we keep them
674668
p.pins
675669
.sort_by_key(|x| (x.pin.clone(), x.signal.clone(), x.af.is_none()));
676670
p.pins.dedup_by_key(|x| (x.pin.clone(), x.signal.clone()));
677-
678-
peripherals.insert(p.name.clone(), p);
679-
}
680-
if let Ok(extra_f) = std::fs::read(format!("data/extra/family/{}.yaml", group.family.as_ref().unwrap())) {
681-
#[derive(serde::Deserialize)]
682-
struct Extra {
683-
peripherals: Vec<stm32_data_serde::chip::core::Peripheral>,
684-
}
685-
686-
let extra: Extra = serde_yaml::from_slice(&extra_f).unwrap();
687-
for mut p in extra.peripherals {
688-
// filter out pins that may not exist in this package.
689-
p.pins = p.pins.into_iter().filter(|p| group.pins.contains_key(&p.pin)).collect();
690-
691-
if let Some(peripheral) = peripherals.get_mut(&p.name) {
692-
// Modify the generated peripheral
693-
peripheral.pins.append(&mut p.pins);
694-
} else if p.address != 0 {
695-
// Only insert the peripheral if the address is not the default
696-
peripherals.insert(p.name.clone(), p);
697-
}
698-
}
699671
}
700672

701673
let mut peripherals: Vec<_> = peripherals.into_values().collect();
@@ -798,6 +770,63 @@ fn process_core(
798770
Ok(core)
799771
}
800772

773+
/// Merge family-specific YAML overlays into the peripheral map.
774+
///
775+
/// Loads `data/extra/family/{family}.yaml` if it exists, which may contain:
776+
/// - `peripherals`: extra or corrective peripheral entries
777+
/// - `pin_cleanup`: rules to modify pin names
778+
///
779+
/// Parameters:
780+
/// - `group`: ChipGroup context (family/package) for filtering
781+
/// - `peripherals`: mutable map of peripheral name → `Peripheral` to modify
782+
fn apply_family_extras(group: &ChipGroup, peripherals: &mut HashMap<String, stm32_data_serde::chip::core::Peripheral>) {
783+
if let Ok(extra_f) = std::fs::read(format!("data/extra/family/{}.yaml", group.family.as_ref().unwrap())) {
784+
#[derive(serde::Deserialize)]
785+
struct PinCleanup {
786+
strip_suffix: String,
787+
exclude_peripherals: Vec<String>,
788+
}
789+
#[derive(serde::Deserialize)]
790+
struct Extra {
791+
peripherals: Option<Vec<stm32_data_serde::chip::core::Peripheral>>,
792+
pin_cleanup: Option<PinCleanup>,
793+
}
794+
795+
let extra: Extra = serde_yaml::from_slice(&extra_f).unwrap();
796+
797+
// merge extra peripherals
798+
if let Some(extras) = extra.peripherals {
799+
for mut p in extras {
800+
// filter out pins that may not exist in this package.
801+
p.pins = p.pins.into_iter().filter(|p| group.pins.contains_key(&p.pin)).collect();
802+
803+
if let Some(peripheral) = peripherals.get_mut(&p.name) {
804+
// Modify the generated peripheral
805+
peripheral.pins.append(&mut p.pins);
806+
} else if p.address != 0 {
807+
// Only insert the peripheral if the address is not the default
808+
peripherals.insert(p.name.clone(), p);
809+
}
810+
}
811+
}
812+
813+
// apply pin_cleanup rules
814+
if let Some(clean) = extra.pin_cleanup {
815+
for (name, peri) in peripherals.iter_mut() {
816+
// skip excluded peripherals
817+
if clean.exclude_peripherals.iter().any(|ex| name.starts_with(ex)) {
818+
continue;
819+
}
820+
for pin in &mut peri.pins {
821+
if let Some(stripped) = pin.pin.strip_suffix(&clean.strip_suffix) {
822+
pin.pin = stripped.to_string();
823+
}
824+
}
825+
}
826+
}
827+
}
828+
}
829+
801830
fn process_chip(
802831
chips: &HashMap<String, Chip>,
803832
chip_name: &str,

0 commit comments

Comments
 (0)