Crate peripherals

Source
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. Values 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.

OperationResulting typeDescription
Fields | FieldsFieldsFields in either or both operands
Fields & FieldsFieldsFields in both operands
Fields ^ FieldsFieldsFields in only one of the operands
FieldValues | FieldValuesFieldValuesField values in either operand, with the value of the right operand if the field is specified in both
FieldValues & FieldsFieldValuesField values of fields in both operands
FieldValues ^ FieldsFieldValuesLeft operand with values of fields in both operands toggled (inverted)
Value | FieldValuesValueLeft operand with the values of the fields in the right operand
Value & FieldsFieldValuesValues of fields in right operand
Value ^ FieldsValueLeft 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§

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