Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bitmask #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion svd-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased

- Revert the `riscv` elements, as well as the `unstable-riscv` feature.
- Add `bitmask` for `FieldInfo`, `Field` and `RegisterInfo`

## [v0.14.9] - 2024-08-20

Expand Down Expand Up @@ -143,4 +144,3 @@ Previous versions in common [changelog](../CHANGELOG.md).
[v0.11.2]: https://github.com/rust-embedded/svd/compare/svd-rs-v0.11.1...svd-rs-v0.11.2
[v0.11.1]: https://github.com/rust-embedded/svd/compare/v0.11.0...svd-rs-v0.11.1
[v0.11.0]: https://github.com/rust-embedded/svd/compare/v0.10.2...v0.11.0

21 changes: 21 additions & 0 deletions svd-rs/src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ impl FieldInfo {
self.bit_range.msb()
}

/// Get bits which is affected by field
pub fn bitmask(&self) -> u64 {
let BitRange { offset, width, .. } = self.bit_range;
(!0u64 >> (64 - width)) << offset
}

/// Get enumeratedValues cluster by usage
pub fn get_enumerated_values(&self, usage: Usage) -> Option<&EnumeratedValues> {
match self.enumerated_values.len() {
Expand Down Expand Up @@ -406,6 +412,21 @@ impl Field {
}
self.deref().validate_all(lvl)
}

/// Get bits which is affected by field or field array
pub fn bitmask(&self) -> u64 {
match self {
Field::Single(f) => f.bitmask(),
Field::Array(f, d) => {
let mask = f.bitmask();
let mut bits = 0;
for i in 0..d.dim {
bits |= mask << (i * d.dim_increment);
}
bits
}
}
}
}

impl Name for FieldInfo {
Expand Down
5 changes: 5 additions & 0 deletions svd-rs/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,11 @@ impl RegisterInfo {
pub fn get_mut_field(&mut self, name: &str) -> Option<&mut Field> {
self.fields_mut().find(|f| f.name == name)
}

/// Get bits which is affected by register fields
pub fn bitmask(&self) -> u64 {
self.fields().fold(0, |mask, f| mask | f.bitmask())
}
}

impl Register {
Expand Down
Loading