Crate peripherals[][src]

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

attributes

Example of attribute in macros

example

Example of generated types

Macros

device

Define a microcontroller and which peripherals it has

field_type

A helper hacro to define a type that can be used in field value.

periph

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

register

Define a register model that can be shared accross peripherals

Structs

DynReg

A register from a peripheral only known at runtime

Field

A single register field

FieldValues

Some fields’ values read from or to be written to a register

Fields

A set of register fields

InvalidValue

Error returned when converting an interger to a field value fails

Reg

Base struct for all registers

Value

A value read from or to be written to a register

Enums

Toggle

A marker type for toggleable fields

Traits

MayToggle

Whether the fields or fields values may be toggled

Peripheral

A trait for peripheral instances

ReadRegister

A marker trait for readable registers

Register

A trait for registers This trait is implemented by the periph! macro for marker types that indicate registers.

RegisterValue

A trait for the register associated with a value

WriteRegister

A marker trait for writeable register