Skip to content

Commit 3501d5b

Browse files
committed
Make LocalRegisterCopy modifiable
It's useful in some cases to operate on locally defined structured register copy and then use it elsewhere.
1 parent 084ea34 commit 3501d5b

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,15 +331,17 @@ impl<T: IntLike, R: RegisterLongName, W: RegisterLongName> Aliased<T, R, W> {
331331
}
332332
}
333333

334-
/// A read-only copy register contents
334+
/// A read-write copy of register contents.
335335
///
336-
/// This behaves very similarly to a read-only register, but instead of doing a
336+
/// This behaves very similarly to a read-write register, but instead of doing a
337337
/// volatile read to MMIO to get the value for each function call, a copy of the
338338
/// register contents are stored locally in memory. This allows a peripheral
339339
/// to do a single read on a register, and then check which bits are set without
340340
/// having to do a full MMIO read each time. It also allows the value of the
341341
/// register to be "cached" in case the peripheral driver needs to clear the
342342
/// register in hardware yet still be able to check the bits.
343+
/// You can write to a local register, which will modify the stored value, but
344+
/// will not modify any hardware because it operates only on local copy.
343345
#[derive(Copy, Clone)]
344346
pub struct LocalRegisterCopy<T: IntLike, R: RegisterLongName = ()> {
345347
value: T,
@@ -354,31 +356,55 @@ impl<T: IntLike, R: RegisterLongName> LocalRegisterCopy<T, R> {
354356
}
355357
}
356358

359+
/// Get the raw register value
357360
#[inline]
358361
pub fn get(&self) -> T {
359362
self.value
360363
}
361364

365+
/// Set the raw register value
366+
#[inline]
367+
pub fn set(&mut self, value: T) {
368+
self.value = value;
369+
}
370+
371+
/// Read the value of the given field
362372
#[inline]
363373
pub fn read(&self, field: Field<T, R>) -> T {
364374
field.read(self.get())
365375
}
366376

377+
/// Read value of the given field as an enum member
367378
#[inline]
368379
pub fn read_as_enum<E: TryFromValue<T, EnumType = E>>(&self, field: Field<T, R>) -> Option<E> {
369380
field.read_as_enum(self.get())
370381
}
371382

383+
/// Write the value of one or more fields, overwriting the other fields with zero
384+
#[inline]
385+
pub fn write(&mut self, field: FieldValue<T, R>) {
386+
self.set(field.value);
387+
}
388+
389+
/// Write the value of one or more fields, leaving the other fields unchanged
390+
#[inline]
391+
pub fn modify(&mut self, field: FieldValue<T, R>) {
392+
self.set(field.modify(self.get()));
393+
}
394+
395+
/// Check if one or more bits in a field are set
372396
#[inline]
373397
pub fn is_set(&self, field: Field<T, R>) -> bool {
374398
field.is_set(self.get())
375399
}
376400

401+
/// Check if any specified parts of a field match
377402
#[inline]
378403
pub fn matches_any(&self, field: FieldValue<T, R>) -> bool {
379404
field.matches_any(self.get())
380405
}
381406

407+
/// Check if all specified parts of a field match
382408
#[inline]
383409
pub fn matches_all(&self, field: FieldValue<T, R>) -> bool {
384410
field.matches_all(self.get())

0 commit comments

Comments
 (0)