Skip to content

Commit 8f85c4a

Browse files
committed
Factor out create_peripherals_for_chip and add documentation
1 parent 2f9d318 commit 8f85c4a

File tree

1 file changed

+63
-41
lines changed

1 file changed

+63
-41
lines changed

stm32-data-gen/src/generator.rs

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,68 @@ fn process_core(
124124
let core_name = create_short_core_name(long_core_name);
125125
let defines = h.get_defines(&core_name);
126126

127+
let mut peripherals =
128+
create_peripherals_for_chip(chip_name, group, peripheral_to_clock, rcc_block, chip_af, defines);
129+
130+
apply_family_extras(group, &mut peripherals);
131+
132+
for p in peripherals.values_mut() {
133+
// sort and dedup pins, put the ones with AF number first, so we keep them
134+
p.pins
135+
.sort_by_key(|x| (x.pin.clone(), x.signal.clone(), x.af.is_none()));
136+
p.pins.dedup_by_key(|x| (x.pin.clone(), x.signal.clone()));
137+
}
138+
139+
let mut peripherals: Vec<_> = peripherals.into_values().collect();
140+
peripherals.sort_by_key(|x| x.name.clone());
141+
142+
let dmas = collect_dma_instances(group, dma_channels);
143+
let dma_channels = extract_relevant_dma_channels(&peripherals, &dmas, chip_name);
144+
associate_peripherals_dma_channels(&mut peripherals, dmas, &dma_channels);
145+
146+
let mut pins: Vec<_> = group
147+
.pins
148+
.keys()
149+
.map(|x| x.replace("_C", ""))
150+
.collect::<BTreeSet<_>>()
151+
.into_iter()
152+
.map(|name| stm32_data_serde::chip::core::Pin { name })
153+
.collect();
154+
155+
pins.sort_by_key(|p| pin_sort_key(&p.name));
156+
157+
let mut core = stm32_data_serde::chip::Core {
158+
name: core_name.clone(),
159+
peripherals,
160+
nvic_priority_bits: None,
161+
interrupts: vec![],
162+
dma_channels,
163+
pins,
164+
};
165+
166+
chip_interrupts.process(&mut core, chip_name, h, group)?;
167+
168+
Ok(core)
169+
}
170+
171+
/// Constructs a map of peripherals for a given chip.
172+
///
173+
/// This function processes the provided `group` and extracts relevant peripheral information
174+
/// for the specified `chip_name`.
175+
/// It fills in addresses from `headers`, registers from their YAML definitions via `PERIMAP`,
176+
/// RCC configurations from `peripheral_to_clock` and `rcc_block`, and alternate function pins
177+
/// from `chip_af`.
178+
///
179+
/// Returns a `HashMap` where each key is the name of a peripheral, and each value is a `Peripheral`
180+
/// struct containing the peripheral's configuration details.
181+
fn create_peripherals_for_chip(
182+
chip_name: &str,
183+
group: &ChipGroup,
184+
peripheral_to_clock: &rcc::ParsedRccs,
185+
rcc_block: (&str, &str, &str),
186+
chip_af: Option<&HashMap<String, Vec<Pin>>>,
187+
defines: &header::Defines,
188+
) -> HashMap<String, stm32_data_serde::chip::core::Peripheral> {
127189
let peri_kinds = create_peripheral_map(chip_name, group, defines);
128190
let periph_pins = extract_pins_from_chip_group(group);
129191
let mut peripherals = HashMap::new();
@@ -170,46 +232,7 @@ fn process_core(
170232

171233
peripherals.insert(p.name.clone(), p);
172234
}
173-
174-
apply_family_extras(group, &mut peripherals);
175-
176-
for p in peripherals.values_mut() {
177-
// sort and dedup pins, put the ones with AF number first, so we keep them
178-
p.pins
179-
.sort_by_key(|x| (x.pin.clone(), x.signal.clone(), x.af.is_none()));
180-
p.pins.dedup_by_key(|x| (x.pin.clone(), x.signal.clone()));
181-
}
182-
183-
let mut peripherals: Vec<_> = peripherals.into_values().collect();
184-
peripherals.sort_by_key(|x| x.name.clone());
185-
186-
let dmas = collect_dma_instances(group, dma_channels);
187-
let dma_channels = extract_relevant_dma_channels(&peripherals, &dmas, chip_name);
188-
associate_peripherals_dma_channels(&mut peripherals, dmas, &dma_channels);
189-
190-
let mut pins: Vec<_> = group
191-
.pins
192-
.keys()
193-
.map(|x| x.replace("_C", ""))
194-
.collect::<BTreeSet<_>>()
195-
.into_iter()
196-
.map(|name| stm32_data_serde::chip::core::Pin { name })
197-
.collect();
198-
199-
pins.sort_by_key(|p| pin_sort_key(&p.name));
200-
201-
let mut core = stm32_data_serde::chip::Core {
202-
name: core_name.clone(),
203-
peripherals,
204-
nvic_priority_bits: None,
205-
interrupts: vec![],
206-
dma_channels,
207-
pins,
208-
};
209-
210-
chip_interrupts.process(&mut core, chip_name, h, group)?;
211-
212-
Ok(core)
235+
peripherals
213236
}
214237

215238
/// Create a short core name from a long name.
@@ -464,7 +487,6 @@ fn merge_i2s_into_spi_pins(
464487
Vec::new()
465488
}
466489

467-
468490
/// Merge AF information from GPIO file into peripheral pins.
469491
///
470492
/// `core_pins` is modified in-place and updated with AF information from `af_pins`.

0 commit comments

Comments
 (0)