Skip to content

Commit fa7865a

Browse files
bors[bot]burrbull
andauthored
Merge #201
201: expand properties r=Emilgardis a=burrbull Closes #200 r? `@Emilgardis` Co-authored-by: Andrey Zgarbul <zgarbul.andrey@gmail.com>
2 parents 33dd7df + 07e28ae commit fa7865a

File tree

4 files changed

+59
-4
lines changed

4 files changed

+59
-4
lines changed

svd-parser/CHANGELOG.md

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

88
## Unreleased
99

10+
## [v0.13.3] - 2022-05-09
11+
12+
- Add `expand_properties` (under `expand` feature)
13+
1014
## [v0.13.2] - 2022-04-23
1115

1216
- Add `expand` (under `expand` feature) and `ignore_enums` options
@@ -31,7 +35,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3135

3236
Previous versions in common [changelog](../CHANGELOG.md).
3337

34-
[Unreleased]: https://github.com/rust-embedded/svd/compare/svd-parser-v0.13.2...HEAD
38+
[Unreleased]: https://github.com/rust-embedded/svd/compare/svd-parser-v0.13.3...HEAD
39+
[v0.13.3]: https://github.com/rust-embedded/svd/compare/svd-parser-v0.13.3...svd-parser-v0.13.2
3540
[v0.13.2]: https://github.com/rust-embedded/svd/compare/svd-rs-v0.13.2...svd-parser-v0.13.2
3641
[v0.13.1]: https://github.com/rust-embedded/svd/compare/v0.13.0...svd-parser-v0.13.1
3742
[v0.13.0]: https://github.com/rust-embedded/svd/compare/v0.12.0...v0.13.0

svd-parser/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = "MIT OR Apache-2.0"
1010
name = "svd-parser"
1111
repository = "https://github.com/rust-embedded/svd"
1212
edition = "2018"
13-
version = "0.13.2"
13+
version = "0.13.3"
1414
readme = "README.md"
1515

1616
[features]

svd-parser/src/expand.rs

Lines changed: 36 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,38 @@ 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+
if p.derived_from.is_some() {
408+
continue;
409+
}
410+
let default = p.default_register_properties.derive_from(&default);
411+
if let Some(regs) = p.registers.as_mut() {
412+
expand_properties_registers(regs, &default);
413+
}
414+
}
415+
}
416+
417+
fn expand_properties_registers(regs: &mut [RegisterCluster], default: &RegisterProperties) {
418+
for rc in regs {
419+
match rc {
420+
RegisterCluster::Cluster(c) => {
421+
if c.derived_from.is_some() {
422+
continue;
423+
}
424+
let default = c.default_register_properties.derive_from(&default);
425+
expand_properties_registers(&mut c.children, &default);
426+
}
427+
RegisterCluster::Register(r) => {
428+
if r.derived_from.is_some() {
429+
continue;
430+
}
431+
r.properties = r.properties.derive_from(default);
432+
}
433+
}
434+
}
435+
}

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;
@@ -158,6 +168,11 @@ pub fn parse_with_config(xml: &str, config: &Config) -> anyhow::Result<Device> {
158168
}
159169
}?;
160170

171+
#[cfg(feature = "expand")]
172+
if config.expand_properties {
173+
expand::expand_properties(&mut device);
174+
}
175+
161176
#[cfg(feature = "expand")]
162177
if config.expand {
163178
device = expand::expand(&device)?;
@@ -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)