Skip to content

Commit

Permalink
Remove duplicate Register and Bitfield entries avr-rust#48
Browse files Browse the repository at this point in the history
  • Loading branch information
gergoerdi committed Dec 22, 2022
1 parent 538a700 commit 6e83cf0
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion core_generator/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
use avr_mcu::*;
use std::io;
use std::io::prelude::*;
use std::collections::HashMap;
use std::collections::HashSet;

fn collect_register_bitfields<'a, I: Iterator<Item = &'a Register>>(registers: I) -> impl Iterator<Item = Register> {
let mut canonical_registers: HashMap<String, Register> = HashMap::new();
for register in registers {
let k = register.name.clone();
match canonical_registers.get_mut(&k) {
Some(existing_register) => {
let mut old_bitfields = HashSet::new();
for bitfield in &existing_register.bitfields {
old_bitfields.insert(bitfield.name.clone());
}
let mut new_bitfields: Vec<Bitfield> =
register.bitfields.clone().into_iter().filter(|bitfield| old_bitfields.take(&bitfield.name).is_none()).collect();
existing_register.bitfields.append(&mut new_bitfields)
}
None => {
canonical_registers.insert(k, register.clone());
}
}
}
canonical_registers.into_values()
}

pub fn write_registers(mcu: &Mcu, w: &mut dyn Write) -> Result<(), io::Error> {
for register in mcu.registers() {
for register in collect_register_bitfields(mcu.registers()) {
let ty = if register.size == 1 { "u8" } else { "u16" };

// HACK: Skip, atmeg328p pack defines two of these.
Expand Down

0 comments on commit 6e83cf0

Please sign in to comment.