Skip to content

Commit 4916573

Browse files
committed
names & offsets
1 parent dc65966 commit 4916573

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

svd-rs/src/cluster.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::ops::{Deref, DerefMut};
2+
use std::borrow::Cow;
23

34
use super::{ClusterInfo, DimElement};
45

@@ -40,6 +41,25 @@ impl Cluster {
4041
pub const fn is_array(&self) -> bool {
4142
matches!(self, Self::Array(_, _))
4243
}
44+
/// Returns list of register or register array names
45+
pub fn names(&self) -> Vec<Cow<str>> {
46+
match self {
47+
Self::Single(info) => vec![info.name.as_str().into()],
48+
Self::Array(info, dim) => dim
49+
.indexes()
50+
.map(|i| info.name.replace("[%s]", &i).replace("%s", &i).into())
51+
.collect(),
52+
}
53+
}
54+
/// Returns list of register or register array address_offsets
55+
pub fn address_offsets(&self) -> Vec<u32> {
56+
match self {
57+
Self::Single(info) => vec![info.address_offset],
58+
Self::Array(info, dim) => (0..dim.dim)
59+
.map(|n| info.address_offset + n * dim.dim_increment)
60+
.collect(),
61+
}
62+
}
4363
}
4464

4565
#[cfg(feature = "serde")]

svd-rs/src/field.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{DimElement, FieldInfo};
22
use core::ops::{Deref, DerefMut};
3+
use std::borrow::Cow;
34

45
/// Describes a field or fields of a [register](crate::RegisterInfo).
56
#[derive(Clone, Debug, PartialEq)]
@@ -39,6 +40,25 @@ impl Field {
3940
pub const fn is_array(&self) -> bool {
4041
matches!(self, Self::Array(_, _))
4142
}
43+
/// Returns list of register or register array names
44+
pub fn names(&self) -> Vec<Cow<str>> {
45+
match self {
46+
Self::Single(info) => vec![info.name.as_str().into()],
47+
Self::Array(info, dim) => dim
48+
.indexes()
49+
.map(|i| info.name.replace("[%s]", &i).replace("%s", &i).into())
50+
.collect(),
51+
}
52+
}
53+
/// Returns list of register or register array bit offsets
54+
pub fn bit_offsets(&self) -> Vec<u32> {
55+
match self {
56+
Self::Single(info) => vec![info.bit_offset()],
57+
Self::Array(info, dim) => (0..dim.dim)
58+
.map(|n| info.bit_offset() + n * dim.dim_increment)
59+
.collect(),
60+
}
61+
}
4262
}
4363

4464
#[cfg(feature = "serde")]

svd-rs/src/fieldinfo.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,25 @@ impl FieldInfo {
303303
Ok(())
304304
}
305305

306+
/// Get bit offset
307+
pub fn bit_offset(&self) -> u32 {
308+
self.bit_range.offset
309+
}
310+
311+
/// Get bit width
312+
pub fn bit_width(&self) -> u32 {
313+
self.bit_range.width
314+
}
315+
316+
/// Get the position of the least significant bit
317+
pub fn lsb(&self) -> u32 {
318+
self.bit_range.lsb()
319+
}
320+
/// Get the position of the most significant bit
321+
pub fn msb(&self) -> u32 {
322+
self.bit_range.msb()
323+
}
324+
306325
/// Get enumeratedValues cluster by usage
307326
pub fn get_enumerated_values(&self, usage: Usage) -> Option<&EnumeratedValues> {
308327
match self.enumerated_values.len() {

svd-rs/src/peripheral.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::ops::{Deref, DerefMut};
2+
use std::borrow::Cow;
23

34
use super::{DimElement, PeripheralInfo};
45

@@ -40,6 +41,25 @@ impl Peripheral {
4041
pub const fn is_array(&self) -> bool {
4142
matches!(self, Self::Array(_, _))
4243
}
44+
/// Returns list of register or register array names
45+
pub fn names(&self) -> Vec<Cow<str>> {
46+
match self {
47+
Self::Single(info) => vec![info.name.as_str().into()],
48+
Self::Array(info, dim) => dim
49+
.indexes()
50+
.map(|i| info.name.replace("[%s]", &i).replace("%s", &i).into())
51+
.collect(),
52+
}
53+
}
54+
/// Returns list of register or register array memory addresses
55+
pub fn base_addresses(&self) -> Vec<u64> {
56+
match self {
57+
Self::Single(info) => vec![info.base_address],
58+
Self::Array(info, dim) => (0..dim.dim)
59+
.map(|n| info.base_address + (n as u64) * (dim.dim_increment as u64))
60+
.collect(),
61+
}
62+
}
4363
}
4464

4565
#[cfg(feature = "serde")]

svd-rs/src/register.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::ops::{Deref, DerefMut};
2+
use std::borrow::Cow;
23

34
#[doc(hidden)]
45
pub use super::registercluster::{AllRegistersIter as RegIter, AllRegistersIterMut as RegIterMut};
@@ -42,6 +43,25 @@ impl Register {
4243
pub const fn is_array(&self) -> bool {
4344
matches!(self, Self::Array(_, _))
4445
}
46+
/// Returns list of register or register array names
47+
pub fn names(&self) -> Vec<Cow<str>> {
48+
match self {
49+
Self::Single(info) => vec![info.name.as_str().into()],
50+
Self::Array(info, dim) => dim
51+
.indexes()
52+
.map(|i| info.name.replace("[%s]", &i).replace("%s", &i).into())
53+
.collect(),
54+
}
55+
}
56+
/// Returns list of register or register array address_offsets
57+
pub fn address_offsets(&self) -> Vec<u32> {
58+
match self {
59+
Self::Single(info) => vec![info.address_offset],
60+
Self::Array(info, dim) => (0..dim.dim)
61+
.map(|n| info.address_offset + n * dim.dim_increment)
62+
.collect(),
63+
}
64+
}
4565
}
4666

4767
#[cfg(feature = "serde")]

0 commit comments

Comments
 (0)