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
DynPinId and DynPinModestruct representing pin IDsEnums
enum for alternate peripheral function configurationsenum for disabled configurationsenum for pin groupsenum for input configurationsenum for interrupt configurationsenum for output configurationsenum representing pin modesConstants
DynPinMode for alternate peripheral function BDynPinMode for alternate peripheral function CDynPinMode for alternate peripheral function DDynPinMode for alternate peripheral function EDynPinMode for alternate peripheral function FDynPinMode for alternate peripheral function GDynPinMode for alternate peripheral function HDynPinMode for floating disabled modeDynPinMode for floating input modeDynPinMode for floating interrupt modeDynPinMode for pull-down disabled modeDynPinMode for pull-down input modeDynPinMode for pull-down interrupt modeDynPinMode for pull-up disabled modeDynPinMode for pull-up input modeDynPinMode for pull-up interrupt modeDynPinMode for push-pull output modeDynPinMode for readable push-pull output mode