Skip to content

Commit f988ce6

Browse files
committed
expand functions for arrays
1 parent b042d03 commit f988ce6

File tree

6 files changed

+90
-54
lines changed

6 files changed

+90
-54
lines changed

svd-parser/src/expand.rs

+7-52
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use std::collections::HashMap;
55
use std::fmt;
66
use std::mem::take;
77
use svd_rs::{
8-
array::{descriptions, names},
9-
cluster, field, peripheral, register, BitRange, Cluster, ClusterInfo, DeriveFrom, Device,
8+
array::names, cluster, field, peripheral, register, Cluster, ClusterInfo, DeriveFrom, Device,
109
EnumeratedValues, Field, Peripheral, Register, RegisterCluster, RegisterProperties,
1110
};
1211

@@ -320,17 +319,7 @@ fn expand_cluster_array(
320319
match c {
321320
Cluster::Single(c) => expand_cluster(regs, c),
322321
Cluster::Array(info, dim) => {
323-
for c in names(&info, &dim)
324-
.zip(descriptions(&info, &dim))
325-
.zip(cluster::address_offsets(&info, &dim))
326-
.map(|((name, description), address_offset)| {
327-
let mut info = info.clone();
328-
info.name = name;
329-
info.description = description;
330-
info.address_offset = address_offset;
331-
info
332-
})
333-
{
322+
for c in cluster::expand(&info, &dim) {
334323
expand_cluster(regs, c);
335324
}
336325
}
@@ -468,19 +457,7 @@ fn expand_register_array(
468457
regs.push(r.into());
469458
}
470459
Register::Array(info, dim) => {
471-
for rx in names(&info, &dim)
472-
.zip(descriptions(&info, &dim))
473-
.zip(register::address_offsets(&info, &dim))
474-
.map(|((name, description), address_offset)| {
475-
let mut info = info.clone();
476-
info.name = name;
477-
info.description = description;
478-
info.address_offset = address_offset;
479-
info.single()
480-
})
481-
{
482-
regs.push(rx.into());
483-
}
460+
regs.extend(register::expand(&info, &dim).map(|r| r.single().into()));
484461
}
485462
}
486463
Ok(())
@@ -511,19 +488,7 @@ fn expand_field(
511488
fields.push(f);
512489
}
513490
Field::Array(info, dim) => {
514-
for fx in names(&info, &dim)
515-
.zip(descriptions(&info, &dim))
516-
.zip(field::bit_offsets(&info, &dim))
517-
.map(|((name, description), bit_offset)| {
518-
let mut info = info.clone();
519-
info.name = name;
520-
info.description = description;
521-
info.bit_range = BitRange::from_offset_width(bit_offset, info.bit_width());
522-
Field::Single(info)
523-
})
524-
{
525-
fields.push(fx);
526-
}
491+
fields.extend(field::expand(&info, &dim).map(Field::Single));
527492
}
528493
}
529494

@@ -636,19 +601,9 @@ pub fn expand(indevice: &Device) -> Result<Device> {
636601
device.peripherals.push(p);
637602
}
638603
Peripheral::Array(info, dim) => {
639-
for px in names(&info, &dim)
640-
.zip(descriptions(&info, &dim))
641-
.zip(peripheral::base_addresses(&info, &dim))
642-
.map(|((name, description), base_address)| {
643-
let mut info = info.clone();
644-
info.name = name;
645-
info.description = description;
646-
info.base_address = base_address;
647-
Peripheral::Single(info)
648-
})
649-
{
650-
device.peripherals.push(px);
651-
}
604+
device
605+
.peripherals
606+
.extend(peripheral::expand(&info, &dim).map(Peripheral::Single));
652607
}
653608
}
654609
}

svd-rs/CHANGELOG.md

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

88
## Unreleased
99

10+
- Add `expand` functions for arrays
1011
- Fix `indexes_as_range`
1112

1213
## [v0.14.3] - 2023-04-04

svd-rs/src/cluster.rs

+18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{
2+
array::{descriptions, names},
23
registercluster::{
34
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, RegisterIter,
45
RegisterIterMut,
@@ -79,6 +80,23 @@ pub fn address_offsets<'a>(
7980
(0..dim.dim).map(move |i| info.address_offset + i * dim.dim_increment)
8081
}
8182

83+
/// Extract `ClusterInfo` items from array
84+
pub fn expand<'a>(
85+
info: &'a ClusterInfo,
86+
dim: &'a DimElement,
87+
) -> impl Iterator<Item = ClusterInfo> + 'a {
88+
names(info, dim)
89+
.zip(descriptions(info, dim))
90+
.zip(address_offsets(info, dim))
91+
.map(|((name, description), address_offset)| {
92+
let mut info = info.clone();
93+
info.name = name;
94+
info.description = description;
95+
info.address_offset = address_offset;
96+
info
97+
})
98+
}
99+
82100
/// Builder for [`ClusterInfo`]
83101
#[derive(Clone, Debug, Default, PartialEq)]
84102
pub struct ClusterInfoBuilder {

svd-rs/src/field.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{
2+
array::{descriptions, names},
23
bitrange, Access, BitRange, BuildError, Description, DimElement, EmptyToNone, EnumeratedValues,
34
MaybeArray, ModifiedWriteValues, Name, ReadAction, SvdError, Usage, ValidateLevel,
45
WriteConstraint,
@@ -84,7 +85,24 @@ pub struct FieldInfo {
8485

8586
/// Return iterator over bit offsets of each field in array
8687
pub fn bit_offsets<'a>(info: &'a FieldInfo, dim: &'a DimElement) -> impl Iterator<Item = u32> + 'a {
87-
(0..dim.dim).map(move |i| info.bit_offset() + i * dim.dim_increment)
88+
(0..dim.dim).map(|i| info.bit_offset() + i * dim.dim_increment)
89+
}
90+
91+
/// Extract `FieldInfo` items from array
92+
pub fn expand<'a>(
93+
info: &'a FieldInfo,
94+
dim: &'a DimElement,
95+
) -> impl Iterator<Item = FieldInfo> + 'a {
96+
names(info, dim)
97+
.zip(descriptions(info, dim))
98+
.zip(bit_offsets(info, dim))
99+
.map(|((name, description), bit_offset)| {
100+
let mut info = info.clone();
101+
info.name = name;
102+
info.description = description;
103+
info.bit_range = BitRange::from_offset_width(bit_offset, info.bit_width());
104+
info
105+
})
88106
}
89107

90108
/// Builder for [`FieldInfo`]

svd-rs/src/peripheral.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{
2+
array::{descriptions, names},
23
registercluster::{
34
AllRegistersIter, AllRegistersIterMut, ClusterIter, ClusterIterMut, RegisterIter,
45
RegisterIterMut,
@@ -128,7 +129,28 @@ pub fn base_addresses<'a>(
128129
info: &'a PeripheralInfo,
129130
dim: &'a DimElement,
130131
) -> impl Iterator<Item = u64> + 'a {
131-
(0..dim.dim as u64).map(move |i| info.base_address + i * dim.dim_increment as u64)
132+
(0..dim.dim as u64).map(|i| info.base_address + i * dim.dim_increment as u64)
133+
}
134+
135+
/// Extract `PeripheralInfo` items from array
136+
pub fn expand<'a>(
137+
info: &'a PeripheralInfo,
138+
dim: &'a DimElement,
139+
) -> impl Iterator<Item = PeripheralInfo> + 'a {
140+
dim.indexes()
141+
.zip(names(info, dim))
142+
.zip(descriptions(info, dim))
143+
.zip(base_addresses(info, dim))
144+
.map(|(((idx, name), description), base_address)| {
145+
let mut info = info.clone();
146+
info.name = name;
147+
info.description = description;
148+
info.base_address = base_address;
149+
info.display_name = info
150+
.display_name
151+
.map(|d| d.replace("[%s]", &idx).replace("%s", &idx));
152+
info
153+
})
132154
}
133155

134156
/// Builder for [`Peripheral`]

svd-rs/src/register.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{
2+
array::{descriptions, names},
23
Access, BuildError, Description, DimElement, EmptyToNone, Field, MaybeArray,
34
ModifiedWriteValues, Name, ReadAction, RegisterProperties, SvdError, ValidateLevel,
45
WriteConstraint,
@@ -110,6 +111,27 @@ pub fn address_offsets<'a>(
110111
(0..dim.dim).map(move |i| info.address_offset + i * dim.dim_increment)
111112
}
112113

114+
/// Extract `RegisterInfo` items from array
115+
pub fn expand<'a>(
116+
info: &'a RegisterInfo,
117+
dim: &'a DimElement,
118+
) -> impl Iterator<Item = RegisterInfo> + 'a {
119+
dim.indexes()
120+
.zip(names(info, dim))
121+
.zip(descriptions(info, dim))
122+
.zip(address_offsets(info, dim))
123+
.map(|(((idx, name), description), address_offset)| {
124+
let mut info = info.clone();
125+
info.name = name;
126+
info.description = description;
127+
info.address_offset = address_offset;
128+
info.display_name = info
129+
.display_name
130+
.map(|d| d.replace("[%s]", &idx).replace("%s", &idx));
131+
info
132+
})
133+
}
134+
113135
/// Builder for [`RegisterInfo`]
114136
#[derive(Clone, Debug, Default, PartialEq, Eq)]
115137
pub struct RegisterInfoBuilder {

0 commit comments

Comments
 (0)