Skip to content

Commit 142145e

Browse files
committed
Peripheral Cluster Defaults
1 parent 958f73c commit 142145e

File tree

7 files changed

+39
-29
lines changed

7 files changed

+39
-29
lines changed

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub enum InvalidBitRange {
6969
}
7070

7171
impl Fail for SVDError {
72-
fn cause(&self) -> Option<&Fail> {
72+
fn cause(&self) -> Option<&dyn Fail> {
7373
self.inner.cause()
7474
}
7575

src/svd/clusterinfo.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,18 @@ use types::Parse;
77
use encode::Encode;
88
#[cfg(feature = "unproven")]
99
use new_element;
10-
use parse;
1110

1211
use error::SVDError;
13-
use svd::access::Access;
1412
use svd::registercluster::RegisterCluster;
13+
use svd::registerproperties::RegisterProperties;
1514

1615
#[derive(Clone, Debug, PartialEq)]
1716
pub struct ClusterInfo {
1817
pub name: String,
1918
pub description: String,
2019
pub header_struct_name: Option<String>,
2120
pub address_offset: u32,
22-
pub size: Option<u32>,
23-
pub access: Option<Access>,
24-
pub reset_value: Option<u32>,
25-
pub reset_mask: Option<u32>,
21+
pub default_register_properties: RegisterProperties,
2622
pub children: Vec<RegisterCluster>,
2723
// Reserve the right to add more fields to this struct
2824
_extensible: (),
@@ -38,11 +34,7 @@ impl Parse for ClusterInfo {
3834
description: tree.get_child_text("description")?,
3935
header_struct_name: tree.get_child_text_opt("headerStructName")?,
4036
address_offset: tree.get_child_u32("addressOffset")?,
41-
size: parse::optional::<u32>("size", tree)?,
42-
//access: tree.get_child("access").map(|t| Access::parse(t).ok() ),
43-
access: parse::optional::<Access>("access", tree)?,
44-
reset_value: parse::optional::<u32>("resetValue", tree)?,
45-
reset_mask: parse::optional::<u32>("resetMask", tree)?,
37+
default_register_properties: RegisterProperties::parse(tree)?,
4638
children: {
4739
let children: Result<Vec<_>, _> = tree.children
4840
.iter()

src/svd/device.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use error::SVDError;
1212
#[cfg(feature = "unproven")]
1313
use new_element;
1414
use svd::cpu::Cpu;
15-
use svd::defaults::Defaults;
15+
use svd::registerproperties::RegisterProperties;
1616
use svd::peripheral::Peripheral;
1717

1818
#[derive(Clone, Debug)]
@@ -25,7 +25,7 @@ pub struct Device {
2525
pub width: Option<u32>,
2626
pub cpu: Option<Cpu>,
2727
pub peripherals: Vec<Peripheral>,
28-
pub defaults: Defaults,
28+
pub default_register_properties: RegisterProperties,
2929
// Reserve the right to add more fields to this struct
3030
_extensible: (),
3131
}
@@ -54,7 +54,7 @@ impl Parse for Device {
5454
.collect();
5555
ps?
5656
},
57-
defaults: Defaults::parse(tree)?,
57+
default_register_properties: RegisterProperties::parse(tree)?,
5858
_extensible: (),
5959
})
6060
}

src/svd/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ pub use self::field::Field;
3434
pub mod registerinfo;
3535
pub use self::registerinfo::RegisterInfo;
3636

37-
pub mod defaults;
38-
pub use self::defaults::Defaults;
37+
pub mod registerproperties;
38+
pub use self::registerproperties::RegisterProperties;
3939

4040
pub mod addressblock;
4141
pub use self::addressblock::AddressBlock;

src/svd/peripheral.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use error::{SVDError, SVDErrorKind};
1717
use svd::addressblock::AddressBlock;
1818
use svd::interrupt::Interrupt;
1919
use svd::registercluster::RegisterCluster;
20+
use svd::registerproperties::RegisterProperties;
2021

2122
#[derive(Clone, Debug)]
2223
pub struct Peripheral {
@@ -28,6 +29,7 @@ pub struct Peripheral {
2829
pub base_address: u32,
2930
pub address_block: Option<AddressBlock>,
3031
pub interrupt: Vec<Interrupt>,
32+
pub default_register_properties: RegisterProperties,
3133
/// `None` indicates that the `<registers>` node is not present
3234
pub registers: Option<Vec<RegisterCluster>>,
3335
pub derived_from: Option<String>,
@@ -65,6 +67,9 @@ impl Peripheral {
6567
derived.description = derived
6668
.description
6769
.or_else(|| other.description.clone());
70+
derived.default_register_properties = derived
71+
.default_register_properties
72+
.derive_from(&other.default_register_properties);
6873
derived.registers = derived
6974
.registers
7075
.or_else(|| other.registers.clone());
@@ -99,6 +104,7 @@ impl Peripheral {
99104
.collect();
100105
interrupt?
101106
},
107+
default_register_properties: RegisterProperties::parse(tree)?,
102108
registers: if let Some(registers) = tree.get_child("registers") {
103109
let rs: Result<Vec<_>, _> = registers
104110
.children

src/svd/registerinfo.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use svd::access::Access;
1717
use svd::field::Field;
1818
use svd::modifiedwritevalues::ModifiedWriteValues;
1919
use svd::writeconstraint::WriteConstraint;
20+
use svd::registerproperties::RegisterProperties;
2021

2122
#[derive(Clone, Debug, PartialEq)]
2223
pub struct RegisterInfo {
@@ -55,17 +56,18 @@ impl Parse for RegisterInfo {
5556

5657
impl RegisterInfo {
5758
fn _parse(tree: &Element, name: String) -> Result<RegisterInfo, SVDError> {
59+
let properties = RegisterProperties::parse(tree)?;
5860
Ok(RegisterInfo {
5961
name,
6062
alternate_group: tree.get_child_text_opt("alternateGroup")?,
6163
alternate_register: tree.get_child_text_opt("alternateRegister")?,
6264
description: tree.get_child_text_opt("description")?,
6365
derived_from: tree.attributes.get("derivedFrom").map(|s| s.to_owned()),
6466
address_offset: tree.get_child_u32("addressOffset")?,
65-
size: parse::optional::<u32>("size", tree)?,
66-
access: parse::optional::<Access>("access", tree)?,
67-
reset_value: parse::optional::<u32>("resetValue", tree)?,
68-
reset_mask: parse::optional::<u32>("resetMask", tree)?,
67+
size: properties.size,
68+
access: properties.access,
69+
reset_value: properties.reset_value,
70+
reset_mask: properties.reset_mask,
6971
fields: {
7072
if let Some(fields) = tree.get_child("fields") {
7173
let fs: Result<Vec<_>, _> = fields

src/svd/defaults.rs renamed to src/svd/registerproperties.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use svd::access::Access;
1414

1515
/// Register default properties
1616
#[derive(Clone, Copy, Debug, PartialEq)]
17-
pub struct Defaults {
17+
pub struct RegisterProperties {
1818
pub size: Option<u32>,
1919
pub reset_value: Option<u32>,
2020
pub reset_mask: Option<u32>,
@@ -23,12 +23,22 @@ pub struct Defaults {
2323
_extensible: (),
2424
}
2525

26-
impl Parse for Defaults {
27-
type Object = Defaults;
26+
impl RegisterProperties {
27+
pub fn derive_from(mut self, other: &Self) -> Self {
28+
self.size = self.size.or(other.size);
29+
self.reset_value = self.reset_value.or(other.reset_value);
30+
self.reset_mask = self.reset_mask.or(other.reset_mask);
31+
self.access = self.access.or(other.access);
32+
self
33+
}
34+
}
35+
36+
impl Parse for RegisterProperties {
37+
type Object = RegisterProperties;
2838
type Error = SVDError;
2939

30-
fn parse(tree: &Element) -> Result<Defaults, SVDError> {
31-
Ok(Defaults {
40+
fn parse(tree: &Element) -> Result<RegisterProperties, SVDError> {
41+
Ok(RegisterProperties {
3242
size: parse::optional::<u32>("size", tree)?,
3343
reset_value: parse::optional::<u32>("resetValue", tree)?,
3444
reset_mask: parse::optional::<u32>("resetMask", tree)?,
@@ -39,7 +49,7 @@ impl Parse for Defaults {
3949
}
4050

4151
#[cfg(feature = "unproven")]
42-
impl EncodeChildren for Defaults {
52+
impl EncodeChildren for RegisterProperties {
4353
type Error = SVDError;
4454
fn encode(&self) -> Result<Vec<Element>, SVDError> {
4555
let mut children = Vec::new();
@@ -103,7 +113,7 @@ mod tests {
103113
",
104114
);
105115

106-
let expected = Defaults {
116+
let expected = RegisterProperties {
107117
size: Some(0xaabbccdd),
108118
reset_value: Some(0x11223344),
109119
reset_mask: Some(0x00000000),
@@ -113,7 +123,7 @@ mod tests {
113123

114124
let tree1 = Element::parse(example.as_bytes()).unwrap();
115125

116-
let parsed = Defaults::parse(&tree1).unwrap();
126+
let parsed = RegisterProperties::parse(&tree1).unwrap();
117127
assert_eq!(parsed, expected, "Parsing tree failed");
118128

119129
let mut tree2 = new_element("mock", None);

0 commit comments

Comments
 (0)