Macro peripherals::periph[][src]

macro_rules! periph {
    (
        $(#[$($periph_attr:tt)*])*
        $periph:ident;
        $($(#[$($reg_attr:tt)*])* $rw:ident $reg:ident @ $offset:literal : $int:ty = $desc1:tt $desc2:tt)*
    ) => { ... };
}
Expand description

Define a peripheral and all its associated registers, fields and values

It is recommended to have one module per peripheral and thus to invoke this macros in its own module. For an example of the generated types, see the example module.

Usage

The macro begins with the peripheral name. Note that you can’t actually define an empty peripheral.

peripherals::periph!{
    /// Optional documentation
    MyPeripheral;
}

Then each register is described as follow:

peripherals::periph!{
    MyPeripheral;
    // access  name    offset size  reset value
         rw   MY_REG @  0x00: u16 =   0x1234    {

        // fields go here
    }
}
  • The access can be r, w or rw for read-only, write-only or read-write.
  • The name of the register is generaly written in uppercase. It is used to name all types related to this register, as well as the field (in lowercase) in the peripheral struct.
  • The offset (here 0x00) is the offset of the register in the register block / peripheral. This allows the code to be generic over the peripheral instance.
  • The size describes the width of the register (8, 16, 32, or more bits) as well as the type used for all accesses.
  • The reset value (here 0x1234) is the “default” value of the register, i.e. the one after a reset of the microcontroller.

The field description is the same a for the register! macro, but leading + are not needed.

A_SRTUCT: 0..4 = struct Struct(u8);
BOOL_STRUCT: 5 = struct Bool(bool);
AN_ENUM: 6..7 = enum Enum {
     A = 0,
     B = 1,
     C = 2,
     D = 3,
}
SINGLE_BIT_ENUM: 8 = enum SingleBit {
     Off = 0,
     On = 1,
}