#[register]Expand description
Generate register handling code for a register definition.
This macro may only be used on structs, and supports only registers with the widths of 8 bits, 16 bits, 32 bits, 64 bits, or 128 bits.
This size must be declared as an attribute parameter, like so:
ⓘ
#[register(size = 16)]
struct MyRegister {}Fields may be declared in a similar syntax to the fields of a normal struct. They must define, as attributes, the least significant bit (lsb) and most significant bit (msb). They must also specify their sign via the “type” of the field. This type must either be “u” for unsigned or “i” for signed. They may also optionally specify whether or not that field may be read or written to, defaulting to true.
ⓘ
#[register(size = 16)]
struct MyRegister {
// Readable, writable, unsigned
#[field(lsb = 0, msb = 7)]
lower: u,
// Readable, signed
#[field(lsb = 8, msb = 15, write = false)]
upper: i,
}
let mut reg = MyRegister::new(0x04AD);
assert_eq!(reg.get_lower(), 0xAD);
assert_eq!(reg.get_upper(), 4);
assert!(reg.set_lower(-4).is_ok());
assert_eq!(reg.get_lower(), -4);