Skip to content

Commit

Permalink
Merge #3014
Browse files Browse the repository at this point in the history
3014: tock-registers: Implement From field enum value type for FieldValue. r=brghena a=qwandor

### Pull Request Overview

This makes it easier to set a field based on the enum value.

Fixes #3012.

### Testing Strategy

This pull request was tested by `cargo test`.

### Documentation Updated

- [x] Updated the relevant files in `/docs`, or no updates are required.

### Formatting

- [x] Ran `make prepush`.


Co-authored-by: Andrew Walbran <[email protected]>
  • Loading branch information
bors[bot] and qwandor authored Apr 15, 2022
2 parents 97b4738 + f80b266 commit b8e9eba
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions libraries/tock-register-interface/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ registers.cr.modify(Control::RANGE.val(2)); // Leaves EN, INT unchanged
// Named constants can be used instead of the raw values:
registers.cr.modify(Control::RANGE::VeryHigh);

// Enum values can also be used:
registers.cr.modify(Control::RANGE::Value::VeryHigh.into())

// Another example of writing a field with a raw value:
registers.cr.modify(Control::EN.val(0)); // Leaves RANGE, INT unchanged

Expand Down
6 changes: 6 additions & 0 deletions libraries/tock-register-interface/src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,12 @@ macro_rules! register_bitmasks {
}
}
}

impl From<Value> for FieldValue<$valtype, $reg_desc> {
fn from(v: Value) -> Self {
Self::new($crate::bitmask!($numbits), $offset, v as $valtype)
}
}
}
};
{
Expand Down

0 comments on commit b8e9eba

Please sign in to comment.