Skip to content

Commit bab1abb

Browse files
committed
expand properties
1 parent 33dd7df commit bab1abb

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

svd-parser/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## Unreleased
99

10+
- Add `expand_properties` (under `expand` feature)
11+
1012
## [v0.13.2] - 2022-04-23
1113

1214
- Add `expand` (under `expand` feature) and `ignore_enums` options

svd-parser/src/expand.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::HashMap;
55
use std::mem::take;
66
use svd_rs::{
77
array::names, cluster, field, peripheral, register, BitRange, Cluster, ClusterInfo, DeriveFrom,
8-
Device, EnumeratedValues, Field, Peripheral, Register, RegisterCluster,
8+
Device, EnumeratedValues, Field, Peripheral, Register, RegisterCluster, RegisterProperties,
99
};
1010

1111
#[derive(Clone, Debug, Default)]
@@ -398,3 +398,31 @@ pub fn expand(indevice: &Device) -> Result<Device> {
398398

399399
Ok(device)
400400
}
401+
402+
/// Takes register `size`, `access`, `reset_value` and `reset_mask`
403+
/// from peripheral or device properties if absent in register
404+
pub fn expand_properties(device: &mut Device) {
405+
let default = device.default_register_properties.clone();
406+
for p in &mut device.peripherals {
407+
let default = p.default_register_properties.derive_from(&default);
408+
if let Some(regs) = p.registers.as_mut() {
409+
for rc in regs {
410+
expand_properties_register_cluster(rc, &default);
411+
}
412+
}
413+
}
414+
}
415+
416+
fn expand_properties_register_cluster(rc: &mut RegisterCluster, default: &RegisterProperties) {
417+
match rc {
418+
RegisterCluster::Cluster(c) => {
419+
let default = c.default_register_properties.derive_from(&default);
420+
for rc in &mut c.children {
421+
expand_properties_register_cluster(rc, &default);
422+
}
423+
}
424+
RegisterCluster::Register(r) => {
425+
r.properties = r.properties.derive_from(default);
426+
}
427+
}
428+
}

svd-parser/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub struct Config {
4646
/// Expand arrays and resolve derivedFrom
4747
// TODO: split it on several independent options
4848
pub expand: bool,
49+
/// Derive register properties from parents
50+
pub expand_properties: bool,
4951
/// Skip parsing and emitting `enumeratedValues` and `writeConstraint` in `Field`
5052
pub ignore_enums: bool,
5153
}
@@ -64,6 +66,14 @@ impl Config {
6466
self
6567
}
6668

69+
#[cfg(feature = "expand")]
70+
/// Takes register `size`, `access`, `reset_value` and `reset_mask`
71+
/// from peripheral or device properties if absent in register
72+
pub fn expand_properties(mut self, val: bool) -> Self {
73+
self.expand_properties = val;
74+
self
75+
}
76+
6777
/// Skip parsing `enumeratedValues` and `writeConstraint` in `Field`
6878
pub fn ignore_enums(mut self, val: bool) -> Self {
6979
self.ignore_enums = val;
@@ -162,6 +172,11 @@ pub fn parse_with_config(xml: &str, config: &Config) -> anyhow::Result<Device> {
162172
if config.expand {
163173
device = expand::expand(&device)?;
164174
}
175+
176+
#[cfg(feature = "expand")]
177+
if config.expand_properties {
178+
expand::expand_properties(&mut device);
179+
}
165180
Ok(device)
166181
}
167182

@@ -203,7 +218,7 @@ mod writeconstraint;
203218
mod expand;
204219

205220
#[cfg(feature = "expand")]
206-
pub use expand::expand;
221+
pub use expand::{expand, expand_properties};
207222
/// SVD parse Errors.
208223
#[derive(Clone, Debug, PartialEq, thiserror::Error)]
209224
pub enum SVDError {

0 commit comments

Comments
 (0)