Skip to content

Commit f80b266

Browse files
committed
tock-registers: Implement From field enum value type for FieldValue.
This makes it easier to set a field based on the enum value.
1 parent 0281b23 commit f80b266

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
@@ -413,6 +413,12 @@ macro_rules! register_bitmasks {
413413
}
414414
}
415415
}
416+
417+
impl From<Value> for FieldValue<$valtype, $reg_desc> {
418+
fn from(v: Value) -> Self {
419+
Self::new($crate::bitmask!($numbits), $offset, v as $valtype)
420+
}
421+
}
416422
}
417423
};
418424
{

0 commit comments

Comments
 (0)