Skip to content

Commit 2c71164

Browse files
authored
Add support for parsing Flash and RAM sizes (#5)
1 parent 9e210cb commit 2c71164

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

src/family.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ pub struct Mcu {
6363
pub name: String,
6464
pub package_name: String,
6565
pub ref_name: String,
66+
#[serde(rename = "Flash")]
67+
pub flash_size: String,
68+
#[serde(rename = "Ram")]
69+
pub ram_size: String,
70+
}
71+
72+
impl Mcu {
73+
pub fn flash_size(&self) -> Option<u32> {
74+
self.flash_size.parse().ok()
75+
}
76+
77+
pub fn ram_size(&self) -> Option<u32> {
78+
self.ram_size.parse().ok()
79+
}
6680
}
6781

6882
impl Families {

src/main.rs

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ fn eeprom_size_to_feature(size: u32) -> String {
3737
format!("eeprom-{}", size)
3838
}
3939

40+
/// Get the Flash size feature for a certain size.
41+
fn flash_size_to_feature(size: u32) -> String {
42+
format!("flash-{}", size)
43+
}
44+
45+
/// Get the RAM size feature for a certain size.
46+
fn ram_size_to_feature(size: u32) -> String {
47+
format!("ram-{}", size)
48+
}
49+
4050
fn main() -> Result<(), String> {
4151
let args = App::new("cube-parse")
4252
.version(env!("CARGO_PKG_VERSION"))
@@ -87,7 +97,7 @@ fn main() -> Result<(), String> {
8797
// MCU map
8898
//
8999
// This maps a MCU ref name to the corresponding `mcu::Mcu` instance.
90-
let mut mcu_map: HashMap<String, mcu::Mcu> = HashMap::new();
100+
let mut mcu_map: HashMap<String, (&family::Mcu, mcu::Mcu)> = HashMap::new();
91101

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

118+
// Flash size map
119+
//
120+
// The keys of this map are flash sizes, the values are Vecs of MCU ref names.
121+
let mut mcu_flash_size_map: HashMap<u32, Vec<String>> = HashMap::new();
122+
123+
// RAM size map
124+
//
125+
// The keys of this map are RAM sizes, the values are Vecs of MCU ref names.
126+
let mut mcu_ram_size_map: HashMap<u32, Vec<String>> = HashMap::new();
127+
108128
// Iterate through subfamilies, then through MCUs. Fill the maps above with
109129
// aggregated data.
110130
for sf in family {
111131
for mcu in sf {
112132
// Load MCU data from the XML files
113133
let mcu_dat = mcu::Mcu::load(&db_dir, &mcu.name)
114-
.map_err(|e| format!("Could not load MCU data: {}", e))?;
134+
.map_err(|e| format!("Could not load MCU data for mcu {}: {}", &mcu.name, e))?;
115135

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

137-
mcu_map.insert(mcu.ref_name.clone(), mcu_dat);
157+
// Fill flash size map
158+
if let Some(flash_size) = mcu.flash_size() {
159+
mcu_flash_size_map
160+
.entry(flash_size)
161+
.or_insert(vec![])
162+
.push(mcu.ref_name.clone());
163+
}
164+
165+
// Fill RAM size map
166+
if let Some(ram_size) = mcu.ram_size() {
167+
mcu_ram_size_map
168+
.entry(ram_size)
169+
.or_insert(vec![])
170+
.push(mcu.ref_name.clone());
171+
}
172+
173+
mcu_map.insert(mcu.ref_name.clone(), (mcu, mcu_dat));
138174
}
139175
}
140176

@@ -144,6 +180,8 @@ fn main() -> Result<(), String> {
144180
&mcu_gpio_map,
145181
&mcu_package_map,
146182
&mcu_eeprom_size_map,
183+
&mcu_flash_size_map,
184+
&mcu_ram_size_map,
147185
&mcu_family,
148186
)?,
149187
GenerateTarget::PinMappings => generate_pin_mappings(&mcu_gpio_map, &db_dir)?,
@@ -178,10 +216,12 @@ lazy_static! {
178216
/// Finally, the MCU features are printed, they act purely as aliases for the
179217
/// other features.
180218
fn generate_features(
181-
mcu_map: &HashMap<String, mcu::Mcu>,
219+
mcu_map: &HashMap<String, (&family::Mcu, mcu::Mcu)>,
182220
mcu_gpio_map: &HashMap<String, Vec<String>>,
183221
mcu_package_map: &HashMap<String, String>,
184222
mcu_eeprom_size_map: &HashMap<u32, Vec<String>>,
223+
mcu_flash_size_map: &HashMap<u32, Vec<String>>,
224+
mcu_ram_size_map: &HashMap<u32, Vec<String>>,
185225
mcu_family: &str,
186226
) -> Result<(), String> {
187227
// IO features
@@ -206,6 +246,24 @@ fn generate_features(
206246
}
207247
println!();
208248

249+
// Flash sizes
250+
let mut flash_sizes = mcu_flash_size_map.keys().collect::<Vec<_>>();
251+
flash_sizes.sort();
252+
println!("# Features based on Flash size (in kbytes)");
253+
for size in flash_sizes {
254+
println!("{} = []", flash_size_to_feature(*size));
255+
}
256+
println!();
257+
258+
// RAM sizes
259+
let mut ram_sizes = mcu_ram_size_map.keys().collect::<Vec<_>>();
260+
ram_sizes.sort();
261+
println!("# Features based on RAM size (in kbytes)");
262+
for size in ram_sizes {
263+
println!("{} = []", ram_size_to_feature(*size));
264+
}
265+
println!();
266+
209267
// Physical packages
210268
if !mcu_package_map.is_empty() {
211269
println!("# Physical packages");
@@ -246,11 +304,22 @@ fn generate_features(
246304
// GPIO version feature
247305
dependencies.push(gpio_version_feature.clone());
248306

307+
let (mcu_info, mcu_dat) = mcu_map.get(mcu).unwrap();
308+
249309
// EEPROM size
250-
if let Some(size) = mcu_map.get(mcu).unwrap().get_eeprom_size() {
310+
if let Some(size) = mcu_dat.get_eeprom_size() {
251311
dependencies.push(eeprom_size_to_feature(size));
252312
}
253313

314+
// Flash & RAM size
315+
if let Some(flash_size) = mcu_info.flash_size() {
316+
dependencies.push(flash_size_to_feature(flash_size));
317+
}
318+
319+
if let Some(ram_size) = mcu_info.ram_size() {
320+
dependencies.push(ram_size_to_feature(ram_size));
321+
}
322+
254323
mcu_aliases.push(format!(
255324
"mcu-{} = [{}]",
256325
mcu,

0 commit comments

Comments
 (0)