Expand description
Register bitfield types and macros
To conveniently access and manipulate fields of a register, this library
provides types and macros to describe and access bitfields of a
register. This can be especially useful in conjunction with the APIs defined
in interfaces
, which make use of these types and
hence allow to access and manipulate bitfields of proper registers directly.
A specific section (bitfield) in a register is described by the Field
type, consisting of an unshifted bitmask over the base register UIntLike
type, and a shift parameter. It is further associated with a specific
RegisterLongName
, which can prevent its use with incompatible registers.
A value of a section of a register is described by the FieldValue
type. It stores the information of the respective section in the register,
as well as the associated value. A FieldValue
can be created from a
Field
through the val
method.
§register_bitfields
macro
For defining register layouts with an associated RegisterLongName
, along
with Field
s and matching FieldValue
s, a convenient macro-based
interface can be used.
The following example demonstrates how two registers can be defined, over a
u32
base type:
register_bitfields![u32,
Uart [
ENABLE OFFSET(0) NUMBITS(4) [
ON = 8,
OFF = 0
]
],
Psel [
PIN OFFSET(0) NUMBITS(6),
CONNECT OFFSET(31) NUMBITS(1)
],
];
// In this scope, `Uart` is a module, representing the register and its
// fields. `Uart::Register` is a `RegisterLongName` type identifying this
// register. `Uart::ENABLE` is a field covering the first 4 bits of this
// register. `Uart::ENABLE::ON` is a `FieldValue` over that field, with the
// associated value 8. We can now use the types like so:
let reg: InMemoryRegister<u32, Uart::Register> = InMemoryRegister::new(0);
assert!(reg.read(Uart::ENABLE) == 0x00000000);
reg.modify(Uart::ENABLE::ON);
assert!(reg.get() == 0x00000008);
use tock_registers::interfaces::Debuggable;
assert!(
&format!("{:?}", reg.debug())
== "Uart { ENABLE: ON }"
);
Structs§
- Field
- Specific section of a register.
- Field
Value - Values for the specific register fields.
Traits§
- TryFrom
Value - Conversion of raw register value into enumerated values member. Implemented inside register_bitfields! macro for each bit field.