Skip to content

Commit

Permalink
Remove duplicate Register and Bitfield entries
Browse files Browse the repository at this point in the history
  • Loading branch information
gergoerdi committed Dec 22, 2022
1 parent da9ee10 commit 6fcef0a
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions 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 Expand Up @@ -148,7 +172,9 @@ pub fn write_usarts(mcu: &Mcu, w: &mut dyn Write) -> Result<(), io::Error> {
"DataRegister".to_owned()
} else if register.name.starts_with("UCSR") { // one of the three control/status registers.
let suffix = register.name.chars().rev().next().unwrap();
format!("ControlRegister{}", suffix)
if suffix == "A" || suffix == "B" || suffix == "C" {
format!("ControlRegister{}", suffix)
}
} else if register.name.starts_with("UBRR") { // the baud rate register.
"BaudRateRegister".to_owned()
} else {
Expand Down

0 comments on commit 6fcef0a

Please sign in to comment.