Macro peripherals::register[][src]

macro_rules! register {
    ($(#[$($attr:tt)*])* $reg:ident: $type:ty = $reset:literal {$($fields:tt)*}) => { ... };
}
Expand description

Define a register model that can be shared accross peripherals

For an example of the generated types, see the example module.

Usage

The macro takes the name of the register, its underlying type (u8, u16, etc.) and its reset value. The reset value is the value the register has right after a reset or a boot, 0x1234 in the example below.

This is followed by the fields list. It takes the field name, the position of the field and the type that represents the field. The position is either an inclusive range of bits or a single bit. Fields defined with a single bit are toggleable.

The field type can be one of:

  • struct: A unit struct over an other type, which must implement ::core::convert::Into and TryFrom for the register type (e.g. u16)
  • enum: An enum over all possible values, which is expected to be exhaustive
  • extern: An existing type that can be converted to and from the register type. This allows to define and use more complex types. Note that you can’t use the same type twice.
peripherals::register! {
    RegisterName: u16 = 0x1234 {
        EXTERN: 0..2 = extern Type;
        NEWTYPE: 3 = struct Newtype(bool);
        ENUM: 4..5 = enum Enum {
            False = 0,
            True = 1,
        }
    }
}

peripherals::field_type! {
    struct Type [u16] (u8);
}