Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing flash and ram sizes #5

Merged
merged 3 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ pub struct Mcu {
pub name: String,
pub package_name: String,
pub ref_name: String,
#[serde(rename = "Flash")]
pub flash_size: String,
#[serde(rename = "Ram")]
pub ram_size: String,
}

impl Mcu {
pub fn flash_size(&self) -> Option<u32> {
self.flash_size.parse().ok()
}

pub fn ram_size(&self) -> Option<u32> {
self.ram_size.parse().ok()
}
}

impl Families {
Expand Down
79 changes: 74 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ fn eeprom_size_to_feature(size: u32) -> String {
format!("eeprom-{}", size)
}

/// Get the Flash size feature for a certain size.
fn flash_size_to_feature(size: u32) -> String {
format!("flash-{}", size)
}

/// Get the RAM size feature for a certain size.
fn ram_size_to_feature(size: u32) -> String {
format!("ram-{}", size)
}

fn main() -> Result<(), String> {
let args = App::new("cube-parse")
.version(env!("CARGO_PKG_VERSION"))
Expand Down Expand Up @@ -87,7 +97,7 @@ fn main() -> Result<(), String> {
// MCU map
//
// This maps a MCU ref name to the corresponding `mcu::Mcu` instance.
let mut mcu_map: HashMap<String, mcu::Mcu> = HashMap::new();
let mut mcu_map: HashMap<String, (&family::Mcu, mcu::Mcu)> = HashMap::new();

// GPIO map
//
Expand All @@ -105,13 +115,23 @@ fn main() -> Result<(), String> {
// The keys of this map are EEPROM sizes, the values are Vecs of MCU ref names.
let mut mcu_eeprom_size_map: HashMap<u32, Vec<String>> = HashMap::new();

// Flash size map
//
// The keys of this map are flash sizes, the values are Vecs of MCU ref names.
let mut mcu_flash_size_map: HashMap<u32, Vec<String>> = HashMap::new();

// RAM size map
//
// The keys of this map are RAM sizes, the values are Vecs of MCU ref names.
let mut mcu_ram_size_map: HashMap<u32, Vec<String>> = HashMap::new();

// Iterate through subfamilies, then through MCUs. Fill the maps above with
// aggregated data.
for sf in family {
for mcu in sf {
// Load MCU data from the XML files
let mcu_dat = mcu::Mcu::load(&db_dir, &mcu.name)
.map_err(|e| format!("Could not load MCU data: {}", e))?;
.map_err(|e| format!("Could not load MCU data for mcu {}: {}", &mcu.name, e))?;

// Fill GPIO map
let gpio_version = mcu_dat.get_ip("GPIO").unwrap().get_version().to_string();
Expand All @@ -134,7 +154,23 @@ fn main() -> Result<(), String> {
.push(mcu.ref_name.clone());
}

mcu_map.insert(mcu.ref_name.clone(), mcu_dat);
// Fill flash size map
if let Some(flash_size) = mcu.flash_size() {
mcu_flash_size_map
.entry(flash_size)
.or_insert(vec![])
.push(mcu.ref_name.clone());
}

// Fill RAM size map
if let Some(ram_size) = mcu.ram_size() {
mcu_ram_size_map
.entry(ram_size)
.or_insert(vec![])
.push(mcu.ref_name.clone());
}

mcu_map.insert(mcu.ref_name.clone(), (mcu, mcu_dat));
}
}

Expand All @@ -144,6 +180,8 @@ fn main() -> Result<(), String> {
&mcu_gpio_map,
&mcu_package_map,
&mcu_eeprom_size_map,
&mcu_flash_size_map,
&mcu_ram_size_map,
&mcu_family,
)?,
GenerateTarget::PinMappings => generate_pin_mappings(&mcu_gpio_map, &db_dir)?,
Expand Down Expand Up @@ -178,10 +216,12 @@ lazy_static! {
/// Finally, the MCU features are printed, they act purely as aliases for the
/// other features.
fn generate_features(
mcu_map: &HashMap<String, mcu::Mcu>,
mcu_map: &HashMap<String, (&family::Mcu, mcu::Mcu)>,
mcu_gpio_map: &HashMap<String, Vec<String>>,
mcu_package_map: &HashMap<String, String>,
mcu_eeprom_size_map: &HashMap<u32, Vec<String>>,
mcu_flash_size_map: &HashMap<u32, Vec<String>>,
mcu_ram_size_map: &HashMap<u32, Vec<String>>,
mcu_family: &str,
) -> Result<(), String> {
// IO features
Expand All @@ -206,6 +246,24 @@ fn generate_features(
}
println!();

// Flash sizes
let mut flash_sizes = mcu_flash_size_map.keys().collect::<Vec<_>>();
flash_sizes.sort();
println!("# Features based on Flash size (in kbytes)");
for size in flash_sizes {
println!("{} = []", flash_size_to_feature(*size));
}
println!();

// RAM sizes
let mut ram_sizes = mcu_ram_size_map.keys().collect::<Vec<_>>();
ram_sizes.sort();
println!("# Features based on RAM size (in kbytes)");
for size in ram_sizes {
println!("{} = []", ram_size_to_feature(*size));
}
println!();

// Physical packages
if !mcu_package_map.is_empty() {
println!("# Physical packages");
Expand Down Expand Up @@ -246,11 +304,22 @@ fn generate_features(
// GPIO version feature
dependencies.push(gpio_version_feature.clone());

let (mcu_info, mcu_dat) = mcu_map.get(mcu).unwrap();

// EEPROM size
if let Some(size) = mcu_map.get(mcu).unwrap().get_eeprom_size() {
if let Some(size) = mcu_dat.get_eeprom_size() {
dependencies.push(eeprom_size_to_feature(size));
}

// Flash & RAM size
if let Some(flash_size) = mcu_info.flash_size() {
dependencies.push(flash_size_to_feature(flash_size));
}

if let Some(ram_size) = mcu_info.ram_size() {
dependencies.push(ram_size_to_feature(ram_size));
}

mcu_aliases.push(format!(
"mcu-{} = [{}]",
mcu,
Expand Down