Module atsamd_hal::gpio::dynpin

source ·
Expand description

Type-erased, value-level module for GPIO pins

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 pa27: DynPin = pins.pa27.into();

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

// Use one of the literal function names
pa27.into_floating_input();
// Use a method and a DynPinMode variant
pa27.into_mode(DYN_FLOATING_INPUT);

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 pa27: DynPin = pins.pa27.into();
// Change pin mode
pa27.into_floating_input();
// Convert back to a `Pin`
let pa27: Pin<PA27, FloatingInput> = pa27.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

Enums

  • Value-level enum for alternate peripheral function configurations
  • Value-level enum for disabled configurations
  • Value-level enum for pin groups
  • Value-level enum for input configurations
  • Value-level enum for interrupt configurations
  • Value-level enum for output configurations
  • Value-level enum representing pin modes
  • GPIO error type

Constants