Expand description

Type-erased, value-level module for GPIO pins

Based heavily on atsamd-hal.

Although the type-level API is generally preferred, it is not suitable in all cases. Because each pin is represented by a distinct type, it is not possible to store multiple pins in a homogeneous data structure. The value-level API solves this problem by erasing the type information and tracking the pin at run-time.

Value-level pins are represented by the DynPin type. DynPin has two fields, id and mode with types DynPinId and DynPinMode respectively. The implementation of these types closely mirrors the type-level API.

Instances of DynPin cannot be created directly. Rather, they must be created from their type-level equivalents using From/Into.

// Move a pin out of the Pins struct and convert to a DynPin
let gpio12: DynPin = pins.gpio12.into();

Conversions between pin modes use a value-level version of the type-level API.

// Use one of the literal function names
gpio12.into_floating_input();
// Use a method and a DynPinMode variant
gpio12.try_into_mode(DYN_FLOATING_INPUT).unwrap();

Because the pin state cannot be tracked at compile-time, many DynPin operations become fallible. Run-time checks are inserted to ensure that users don’t try to, for example, set the output level of an input pin.

Users may try to convert value-level pins back to their type-level equivalents. However, this option is fallible, because the compiler cannot guarantee the pin has the correct ID or is in the correct mode at compile-time. Use TryFrom/ TryInto for this conversion.

// Convert to a `DynPin`
let mut gpio12: DynPin = pins.gpio12.into();
// Change pin mode
gpio12.into_floating_input();
// Convert back to a `Pin`
let gpio12: Pin<Gpio12, FloatingInput> = gpio12.try_into().unwrap();

Embedded HAL traits

This module implements all of the embedded HAL GPIO traits for DynPin. However, whereas the type-level API uses Error = core::convert::Infallible, the value-level API can return a real error. If the DynPin is not in the correct DynPinMode for the operation, the trait functions will return InvalidPinType.

Structs

A value-level pin, parameterized by DynPinId and DynPinMode

Value-level struct representing pin IDs

Enums

Value-level enum for disabled configurations

Value-level enum for output configurations

Value-level enum for pin groups

Value-level enum for input configurations

Value-level enum for output configurations

Value-level enum representing pin modes

GPIO error type

Constants

Value-level variant of DynPinMode for floating disabled mode

Value-level variant of DynPinMode for floating input mode

Value-level variant of DynPinMode for alternate peripheral function Clock

Value-level variant of DynPinMode for alternate peripheral function I2C

Value-level variant of DynPinMode for alternate peripheral function Pio0

Value-level variant of DynPinMode for alternate peripheral function Pio1

Value-level variant of DynPinMode for alternate peripheral function Pwm

Value-level variant of DynPinMode for alternate peripheral function Spi

Value-level variant of DynPinMode for alternate peripheral function Uart

Value-level variant of DynPinMode for alternate peripheral function UsbAux

Value-level variant of DynPinMode for alternate peripheral function Xip

Value-level variant of DynPinMode for pull-down disabled mode

Value-level variant of DynPinMode for pull-down input mode

Value-level variant of DynPinMode for pull-up disabled mode

Value-level variant of DynPinMode for pull-up input mode

Value-level variant of DynPinMode for push-pull output mode

Value-level variant of DynPinMode for readable push-pull output mode