Expand description
§peripherals
Define and access your microcontroller peripherals
§Features
- A macro to generate code for peripherals, registers, and register fields
- Zero-sized structs to represent peripherals and registers
- Enums or newtype struct to represent fields values
- Combine values to write to a regiser with
|
(binary or) - Read/write access at the regiser level
- Strong typing ensure you don’t mix up registers
- Generic over the peripheral instance
§Usage
Peripherals are defined with the periph!
macro. Registers and fields can be defined in the
same macro invocation or separately with the register!
and field_type!
macros.
Microcontrollers / devices are then defined with the device!
macro.
These macros generate types to represent fields and their values, and marker types for
registers and peripherals. These types can be seen in the example
module.
Registers are accessed with the Reg
struct. Value
s are used to read and write them.
To use the generated device struct, create it from ()
as part of your initialisation routine.
There sould be only one instance of the device (and the right one) in your whole program.
§Operators
Most types implements the |
(bit or), &
(bit and) and ^
(xor) operators. Operations that
returns that same type as their left operand can be written in the assign form, i.e. |=
, &=
and ^=
. The operands can be of the type written in the table below, or anything that converts
to that type.
Operation | Resulting type | Description |
---|---|---|
Fields | Fields | Fields | Fields in either or both operands |
Fields & Fields | Fields | Fields in both operands |
Fields ^ Fields | Fields | Fields in only one of the operands |
FieldValues | FieldValues | FieldValues | Field values in either operand, with the value of the right operand if the field is specified in both |
FieldValues & Fields | FieldValues | Field values of fields in both operands |
FieldValues ^ Fields | FieldValues | Left operand with values of fields in both operands toggled (inverted) |
Value | FieldValues | Value | Left operand with the values of the fields in the right operand |
Value & Fields | FieldValues | Values of fields in right operand |
Value ^ Fields | Value | Left operand with values of the fields in right operand toggled (inverted) |
§Attribute in macros
By default, attributes exand on generated types and fields, and not expand on generated impls.
This can be changed by inserting type:
, field:
, impl:
or all:
to specify on what the
attribute should be present. See the attributes
module for examples.
This allows to use e.g. cfg
and repr
attribute by writting #[all: cfg(...)]
and #[type: repr(...)]
.
Modules§
- Example of attribute in macros
- Example of generated types
Macros§
- Define a microcontroller and which peripherals it has
- A helper hacro to define a type that can be used in field value.
- Define a peripheral and all its associated registers, fields and values
- Define a register model that can be shared accross peripherals
Structs§
- A register from a peripheral only known at runtime
- A single register field
- Some fields’ values read from or to be written to a register
- A set of register fields
- Error returned when converting an interger to a field value fails
- Base struct for all registers
- A value read from or to be written to a register
Enums§
- A marker type for toggleable fields
Traits§
- Whether the fields or fields values may be toggled
- A trait for peripheral instances
- A marker trait for readable registers
- A trait for registers This trait is implemented by the [
periph!
] macro for marker types that indicate registers. - A trait for the register associated with a value
- A marker trait for writeable register