Skip to content

Commit

Permalink
Merge pull request #253 from rust-embedded/html-derive
Browse files Browse the repository at this point in the history
Support derived registers/fields in "svdtools html"
  • Loading branch information
burrbull authored Nov 14, 2024
2 parents cb60c01 + 1735659 commit 4bd75cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG-rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ This changelog tracks the Rust `svdtools` project. See

## [Unreleased]

* Support derived registers/fields in `svdtools html`

## [v0.3.19] 2024-10-18

* Fix deletion childrens on cluster modify
* Sugar for simple `_split` and `_merge`
* Sugar for simple `_split` and `_merge`

## [v0.3.18] 2024-08-10

Expand Down Expand Up @@ -64,7 +66,7 @@ This changelog tracks the Rust `svdtools` project. See
## [v0.3.9] 2024-01-19

* Use `<details>` instead of JavaScript in `html` template
* Fix escape special characters on Windows
* Fix escape special characters on Windows

## [v0.3.8] 2023-12-23

Expand Down
41 changes: 28 additions & 13 deletions src/html/html_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use liquid::{
};
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use svd_parser::expand::{
derive_cluster, derive_enumerated_values, derive_register, BlockPath, RegisterPath,
derive_cluster, derive_enumerated_values, derive_field, derive_register, BlockPath,
RegisterPath,
};
use svd_parser::{
expand::{derive_peripheral, Index},
Expand Down Expand Up @@ -122,9 +123,12 @@ fn parse_cluster(
cpath: &BlockPath,
index: &Index,
) -> anyhow::Result<()> {
let mut cpath = cpath.clone();
let ctag = if let Some(dfname) = ctag.derived_from.as_ref() {
let mut ctag = ctag.clone();
derive_cluster(&mut ctag, dfname, &cpath.parent().unwrap(), index)?;
if let Some(path) = derive_cluster(&mut ctag, dfname, &cpath.parent().unwrap(), index)? {
cpath = path;
}
Cow::Owned(ctag)
} else {
Cow::Borrowed(ctag)
Expand Down Expand Up @@ -162,16 +166,19 @@ fn parse_register_array(
rpath: &RegisterPath,
index: &Index,
) -> anyhow::Result<()> {
let mut rpath = rpath.clone();
let rtag = if let Some(dfname) = rtag.derived_from.as_ref() {
let mut rtag = rtag.clone();
derive_register(&mut rtag, dfname, &rpath.block, index)?;
if let Some(path) = derive_register(&mut rtag, dfname, &rpath.block, index)? {
rpath = path;
}
Cow::Owned(rtag)
} else {
Cow::Borrowed(rtag)
};
match rtag.as_ref() {
Register::Single(r) => {
let register = parse_register(r, rpath, index)
let register = parse_register(r, &rpath, index)
.with_context(|| format!("In register {}", r.name))?;
registers.push(register);
}
Expand All @@ -184,7 +191,7 @@ fn parse_register_array(
r.description = r
.description
.map(|d| d.replace("[%s]", &idx).replace("%s", &idx));
let register = parse_register(&r, rpath, index)
let register = parse_register(&r, &rpath, index)
.with_context(|| format!("In register {}", r.name))?;
registers.push(register);
}
Expand All @@ -209,9 +216,19 @@ fn parse_register(

let mut flds = Vec::new();
for f in rtag.fields() {
let mut fpath = rpath.new_field(&f.name);
let f = if let Some(dfname) = f.derived_from.as_ref() {
let mut f = f.clone();
if let Some(path) = derive_field(&mut f, dfname, rpath, index)? {
fpath = path;
}
f
} else {
f.clone()
};
match f {
Field::Single(f) => {
flds.push(Cow::Borrowed(f));
flds.push((f, fpath));
}
Field::Array(f, d) => {
for (i, idx) in d.indexes().enumerate() {
Expand All @@ -225,22 +242,20 @@ fn parse_register(
f.description = f
.description
.map(|d| d.replace("[%s]", &idx).replace("%s", &idx));
flds.push(Cow::Owned(f));
flds.push((f, fpath.clone()));
}
}
}
}

flds.sort_by_key(|f| f.bit_offset());
flds.sort_by_key(|f| f.0.bit_offset());

let mut filling = 0_u64;

let mut fields = Vec::with_capacity(flds.len());
for ftag in &flds {
for (ftag, fpath) in &flds {
register_fields_total += 1;

let fpath = rpath.new_field(&ftag.name);

let foffset = ftag.bit_offset();
let fwidth = ftag.bit_width();
let bit_mask = (u64::MAX >> (u64::BITS - fwidth)) << foffset;
Expand All @@ -256,7 +271,7 @@ fn parse_register(
let mut doc = "Allowed values:<br>".to_string();
let enums = if let Some(dfname) = enums.derived_from.as_ref() {
let mut enums = enums.clone();
derive_enumerated_values(&mut enums, dfname, &fpath, index)?;
derive_enumerated_values(&mut enums, dfname, fpath, index)?;
Cow::Owned(enums)
} else {
Cow::Borrowed(enums)
Expand Down Expand Up @@ -310,7 +325,7 @@ fn parse_register(
2
];

for ftag in flds.iter().rev() {
for (ftag, _) in flds.iter().rev() {
let foffset = ftag.bit_offset();
let faccs = ftag.access.map(Access::as_str).unwrap_or(raccs);
let access = short_access(faccs);
Expand Down

0 comments on commit 4bd75cd

Please sign in to comment.