Skip to content

Commit bc3349a

Browse files
committed
add register write constraints support
1 parent 4e12ba7 commit bc3349a

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/patch/peripheral.rs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use anyhow::{anyhow, Context};
22
use svd_parser::svd::{
33
self, Cluster, ClusterInfo, DimElement, Interrupt, Peripheral, Register, RegisterCluster,
4-
RegisterInfo,
4+
RegisterInfo, WriteConstraint, WriteConstraintRange,
55
};
66
use yaml_rust::{yaml::Hash, Yaml};
77

@@ -417,6 +417,30 @@ impl RegisterBlockExt for Peripheral {
417417
let dim = make_dim_element(rmod)?;
418418
for rtag in rtags {
419419
modify_dim_element(rtag, &dim)?;
420+
if let Some(value) = rmod
421+
.get(&"_write_constraint".to_yaml())
422+
.or_else(|| rmod.get(&"writeConstraint".to_yaml()))
423+
{
424+
let wc = match value {
425+
Yaml::String(s) if s == "none" => {
426+
// Completely remove the existing writeConstraint
427+
None
428+
}
429+
Yaml::String(s) if s == "enum" => {
430+
// Only allow enumerated values
431+
Some(WriteConstraint::UseEnumeratedValues(true))
432+
}
433+
Yaml::Array(a) => {
434+
// Allow a certain range
435+
Some(WriteConstraint::Range(WriteConstraintRange {
436+
min: a[0].i64()? as u64,
437+
max: a[1].i64()? as u64,
438+
}))
439+
}
440+
_ => return Err(anyhow!("Unknown writeConstraint type {value:?}")),
441+
};
442+
rtag.write_constraint = wc;
443+
}
420444
rtag.modify_from(register_builder.clone(), VAL_LVL)?;
421445
if let Some("") = rmod.get_str("access")? {
422446
rtag.properties.access = None;
@@ -881,6 +905,30 @@ impl RegisterBlockExt for Cluster {
881905
let dim = make_dim_element(rmod)?;
882906
for rtag in rtags {
883907
modify_dim_element(rtag, &dim)?;
908+
if let Some(value) = rmod
909+
.get(&"_write_constraint".to_yaml())
910+
.or_else(|| rmod.get(&"writeConstraint".to_yaml()))
911+
{
912+
let wc = match value {
913+
Yaml::String(s) if s == "none" => {
914+
// Completely remove the existing writeConstraint
915+
None
916+
}
917+
Yaml::String(s) if s == "enum" => {
918+
// Only allow enumerated values
919+
Some(WriteConstraint::UseEnumeratedValues(true))
920+
}
921+
Yaml::Array(a) => {
922+
// Allow a certain range
923+
Some(WriteConstraint::Range(WriteConstraintRange {
924+
min: a[0].i64()? as u64,
925+
max: a[1].i64()? as u64,
926+
}))
927+
}
928+
_ => return Err(anyhow!("Unknown writeConstraint type {value:?}")),
929+
};
930+
rtag.write_constraint = wc;
931+
}
884932
rtag.modify_from(register_builder.clone(), VAL_LVL)?;
885933
if let Some("") = rmod.get_str("access")? {
886934
rtag.properties.access = None;

0 commit comments

Comments
 (0)