Skip to content

Commit b8e9eba

Browse files
bors[bot]qwandor
andauthored
Merge #3014
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]>
2 parents 97b4738 + f80b266 commit b8e9eba

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

libraries/tock-register-interface/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ registers.cr.modify(Control::RANGE.val(2)); // Leaves EN, INT unchanged
294294
// Named constants can be used instead of the raw values:
295295
registers.cr.modify(Control::RANGE::VeryHigh);
296296

297+
// Enum values can also be used:
298+
registers.cr.modify(Control::RANGE::Value::VeryHigh.into())
299+
297300
// Another example of writing a field with a raw value:
298301
registers.cr.modify(Control::EN.val(0)); // Leaves RANGE, INT unchanged
299302

libraries/tock-register-interface/src/fields.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,12 @@ macro_rules! register_bitmasks {
423423
}
424424
}
425425
}
426+
427+
impl From<Value> for FieldValue<$valtype, $reg_desc> {
428+
fn from(v: Value) -> Self {
429+
Self::new($crate::bitmask!($numbits), $offset, v as $valtype)
430+
}
431+
}
426432
}
427433
};
428434
{

0 commit comments

Comments
 (0)