Skip to content

Commit 603ad0d

Browse files
committed
cleanup and added example in res
1 parent 2026a27 commit 603ad0d

File tree

3 files changed

+234
-0
lines changed

3 files changed

+234
-0
lines changed

res/expand_cluster/mspm0g350x.svd

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="no"?>
2+
<device schemaVersion="1.1"
3+
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
4+
xs:noNamespaceSchemaLocation="CMSIS-SVD_Schema_1_1.xsd">
5+
<name>MSPM0G350x</name>
6+
<peripherals>
7+
<peripheral>
8+
<name>GPIOB</name>
9+
<groupName>GPIOB</groupName>
10+
<version>1.0</version>
11+
<description>PERIPHERALREGION</description>
12+
<baseAddress>0x400A2000</baseAddress>
13+
<addressBlock>
14+
<offset>0x0</offset>
15+
<size>0x1F00</size>
16+
<usage>registers</usage>
17+
</addressBlock>
18+
<registers>
19+
<cluster>
20+
<dim>1</dim>
21+
<dimIncrement>24</dimIncrement>
22+
<dimArrayIndex>0</dimArrayIndex>
23+
<name>GPIOB_GPRCM[%s]</name>
24+
<description></description>
25+
<addressOffset>0x800</addressOffset>
26+
<register>
27+
<name>GPIOB_PWREN</name>
28+
<description>Power enable</description>
29+
<addressOffset>0x0</addressOffset>
30+
<size>32</size>
31+
<access>read-write</access>
32+
<fields>
33+
<field>
34+
<name>ENABLE</name>
35+
<description>Enable the power</description>
36+
<bitOffset>0x0</bitOffset>
37+
<bitWidth>0x1</bitWidth>
38+
<enumeratedValues>
39+
<enumeratedValue>
40+
<name>DISABLE</name>
41+
<description>Disable Power</description>
42+
<value>0x0</value>
43+
</enumeratedValue>
44+
<enumeratedValue>
45+
<name>ENABLE</name>
46+
<description>Enable Power</description>
47+
<value>0x1</value>
48+
</enumeratedValue>
49+
</enumeratedValues>
50+
</field>
51+
</fields>
52+
</register>
53+
<register>
54+
<name>GPIOB_RSTCTL</name>
55+
<description>Reset Control</description>
56+
<addressOffset>0x4</addressOffset>
57+
<size>32</size>
58+
<access>write-only</access>
59+
<fields>
60+
<field>
61+
<name>RESETSTKYCLR</name>
62+
<description>Clear the RESETSTKY bit in the STAT register</description>
63+
<bitOffset>0x1</bitOffset>
64+
<bitWidth>0x1</bitWidth>
65+
<access>write-only</access>
66+
<enumeratedValues>
67+
<enumeratedValue>
68+
<name>NOP</name>
69+
<description>Writing 0 has no effect</description>
70+
<value>0x0</value>
71+
</enumeratedValue>
72+
<enumeratedValue>
73+
<name>CLR</name>
74+
<description>Clear reset sticky bit</description>
75+
<value>0x1</value>
76+
</enumeratedValue>
77+
</enumeratedValues>
78+
</field>
79+
<field>
80+
<name>RESETASSERT</name>
81+
<description>Assert reset to the peripheral</description>
82+
<bitOffset>0x0</bitOffset>
83+
<bitWidth>0x1</bitWidth>
84+
<access>write-only</access>
85+
<enumeratedValues>
86+
<enumeratedValue>
87+
<name>NOP</name>
88+
<description>Writing 0 has no effect</description>
89+
<value>0x0</value>
90+
</enumeratedValue>
91+
<enumeratedValue>
92+
<name>ASSERT</name>
93+
<description>Assert reset</description>
94+
<value>0x1</value>
95+
</enumeratedValue>
96+
</enumeratedValues>
97+
</field>
98+
</fields>
99+
</register>
100+
<register>
101+
<name>GPIOB_STAT</name>
102+
<description>Status Register</description>
103+
<addressOffset>0x14</addressOffset>
104+
<size>32</size>
105+
<access>read-only</access>
106+
<fields>
107+
<field>
108+
<name>RESETSTKY</name>
109+
<description>This bit indicates, if the peripheral was reset, since this bit was
110+
cleared
111+
by RESETSTKYCLR in the RSTCTL register</description>
112+
<bitOffset>0x10</bitOffset>
113+
<bitWidth>0x1</bitWidth>
114+
<access>read-only</access>
115+
<enumeratedValues>
116+
<enumeratedValue>
117+
<name>NORES</name>
118+
<description>The peripheral has not been reset since this bit was last cleared
119+
by
120+
RESETSTKYCLR in the RSTCTL register</description>
121+
<value>0x0</value>
122+
</enumeratedValue>
123+
<enumeratedValue>
124+
<name>RESET</name>
125+
<description>The peripheral was reset since the last bit clear</description>
126+
<value>0x1</value>
127+
</enumeratedValue>
128+
</enumeratedValues>
129+
</field>
130+
</fields>
131+
</register>
132+
</cluster>
133+
</registers>
134+
</peripheral>
135+
</peripherals>
136+
</device>

res/expand_cluster/patch.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_svd: "./mspm0g350x.svd"
2+
3+
# Alter top-level information and peripherals for this device
4+
"GPIO*":
5+
_expand_cluster:
6+
- "GPIO?_GPRCM*"
7+
_strip: "GPIO?_"

src/patch/peripheral.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,73 @@ pub(crate) trait RegisterBlockExt: Name {
513513
}
514514
}
515515

516+
fn get_cluster_registers(
517+
&self,
518+
cspec: &str,
519+
bpath: &BlockPath,
520+
) -> anyhow::Result<Vec<(&ClusterInfo, Vec<RegisterCluster>)>> {
521+
// ) -> anyhow::Result<(Vec<RegisterCluster>, ClusterInfo)> {
522+
let (cspec, ignore) = cspec.spec();
523+
println!("Expanding cluster {0}", cspec);
524+
525+
let present = self.present_clusters().clone();
526+
Ok(self
527+
.clstrs()
528+
.matched(cspec)
529+
.map(|ctag| {
530+
let regs = ctag
531+
.clone()
532+
.all_registers()
533+
.map(|reg| {
534+
RegisterCluster::Register(<RegisterInfo as Clone>::clone(reg).single())
535+
})
536+
.collect::<Vec<_>>();
537+
if regs.is_empty() && !ignore {
538+
return Err(anyhow!(
539+
"Could not find cluster `{bpath}:{cspec}. Present clusters: {present}.`"
540+
));
541+
}
542+
Ok((std::ops::Deref::deref(ctag), regs))
543+
})
544+
.flatten()
545+
.collect::<Vec<_>>())
546+
}
547+
/// Expand register cluster
548+
fn expand_cluster(&mut self, cspec: &str, bpath: &BlockPath) -> PatchResult {
549+
let mut info_and_regs = Vec::new();
550+
551+
let mut clusters_to_delete = Vec::new();
552+
// some fancy footwork to satisfy the borrow checker gods
553+
for (ci, rc) in self.get_cluster_registers(cspec, bpath)? {
554+
let mut regs = Vec::new();
555+
for reg in rc {
556+
regs.push(<svd_rs::RegisterCluster as Clone>::clone(&reg));
557+
}
558+
info_and_regs.push((ci.clone(), regs));
559+
}
560+
561+
if let Some(regs) = self.children_mut() {
562+
for (_cinfo, cluster_registers) in info_and_regs {
563+
let mut found = false;
564+
for reg in cluster_registers {
565+
found = true;
566+
println!("Adding register {0}", reg.name());
567+
regs.push(reg.clone())
568+
}
569+
if !found {
570+
return Err(anyhow!("No registers found in cluster {:?}", cspec));
571+
} else {
572+
clusters_to_delete.push(cspec)
573+
}
574+
}
575+
}
576+
for cspec in clusters_to_delete {
577+
self.delete_cluster(cspec)
578+
.with_context(|| format!("Deleting clusters matched to `{cspec}`"))?;
579+
}
580+
Ok(())
581+
}
582+
516583
/// Expand register array
517584
fn expand_array(&mut self, rspec: &str, _rmod: &Hash, _config: &Config) -> PatchResult {
518585
if let Some(regs) = self.children_mut() {
@@ -749,6 +816,27 @@ impl PeripheralExt for Peripheral {
749816
}
750817
}
751818

819+
if let Some(expand_cluster) = pmod.get_yaml("_expand_cluster") {
820+
match expand_cluster {
821+
Yaml::String(cspec) => {
822+
self.expand_cluster(cspec, &ppath)
823+
.with_context(|| format!("During expand of `{cspec}` cluster"))?;
824+
}
825+
Yaml::Array(clusters) => {
826+
for cspec in clusters {
827+
let cspec = cspec.str()?;
828+
self.expand_cluster(cspec, &ppath)
829+
.with_context(|| format!("During expand of `{cspec}` cluster"))?;
830+
}
831+
}
832+
_ => {
833+
return Err(anyhow!(
834+
"`_expand_cluster` requires string value or array of strings"
835+
))
836+
}
837+
}
838+
}
839+
752840
// Handle any copied peripherals
753841
for (rname, rcopy) in pmod.hash_iter("_copy") {
754842
let rname = rname.str()?;
@@ -889,6 +977,7 @@ impl PeripheralExt for Peripheral {
889977
// Handle registers or clusters
890978
for (rcspec, rcmod) in pmod {
891979
let rcspec = rcspec.str()?;
980+
println!("Processing {0}", rcspec);
892981
if Self::KEYWORDS.contains(&rcspec) {
893982
continue;
894983
}
@@ -1153,6 +1242,7 @@ impl ClusterExt for Cluster {
11531242
.with_context(|| format!("According to `{rcspec}`"))?;
11541243
}
11551244

1245+
println!("post process");
11561246
self.post_process(cmod, parent, config)
11571247
}
11581248

@@ -1504,6 +1594,7 @@ fn collect_in_cluster(
15041594
)
15051595
};
15061596
cluster.pre_process(cmod, path, &config)?;
1597+
println!("cluster post process");
15071598
cluster.post_process(cmod, path, &config)?;
15081599
regs.insert(place, RegisterCluster::Cluster(cluster));
15091600
Ok(())

0 commit comments

Comments
 (0)