Module atsamd_hal::gpio::pin
source · [−]Expand description
Type-level module for GPIO pins
This module provides a type-level API for GPIO pins. It uses the type system
to track the state of pins at compile-time. Representing GPIO pins in this
manner incurs no run-time overhead. Each Pin
struct is zero-sized, so
there is no data to copy around. Instead, real code is generated as a side
effect of type transformations, and the resulting assembly is nearly
identical to the equivalent, hand-written C.
To track the state of pins at compile-time, this module uses traits to
represent type classes and types as instances of those type classes. For
example, the trait InputConfig
acts as a type-level enum of the
available input configurations, and the types Floating
, PullDown
and
PullUp
are its type-level variants.
Type-level Pin
s are parameterized by two type-level enums, PinId
and
PinMode
.
pub struct Pin<I, M>
where
I: PinId,
M: PinMode,
{
// ...
}
A PinId
identifies a pin by it’s group (A, B, C or D) and pin number. Each
PinId
instance is named according to its datasheet identifier, e.g.
PA02
.
A PinMode
represents the various pin modes. The available PinMode
variants are Disabled
, Input
, Interrupt
, Output
and
Alternate
, each with its own corresponding configurations.
It is not possible for users to create new instances of a Pin
. Singleton
instances of each pin are made available to users through the Pins
struct.
To create the Pins
struct, users must supply the PAC
PORT
peripheral. The Pins
struct takes
ownership of the PORT
and provides the corresponding pins. Each Pin
within the Pins
struct can be moved out and used individually.
let mut peripherals = Peripherals::take().unwrap();
let pins = Pins::new(peripherals.PORT);
Pins can be converted between modes using several different methods.
// Use one of the literal function names
let pa27 = pins.pa27.into_floating_input();
// Use a generic method and one of the `PinMode` variant types
let pa27 = pins.pa27.into_mode::<FloatingInput>();
// Specify the target type and use `From`/`Into`
let pa27: Pin<PA27, FloatingInput> = pins.pa27.into();
Embedded HAL traits
This module implements all of the embedded HAL GPIO traits for each Pin
in the corresponding PinMode
s, namely: InputPin
, OutputPin
,
ToggleableOutputPin
and StatefulOutputPin
.
For example, you can control the logic level of an OutputPin
like so
use atsamd_hal::pac::Peripherals;
use atsamd_hal::gpio::Pins;
use embedded_hal::digital::v2::OutputPin;
let mut peripherals = Peripherals::take().unwrap();
let mut pins = Pins::new(peripherals.PORT);
pins.pa27.set_high();
Type-level features
This module also provides additional, type-level tools to work with GPIO pins.
The OptionalPinId
and OptionalPin
traits use the OptionalKind
pattern to act as type-level versions of Option
for PinId
and Pin
respectively. And the AnyPin
trait defines an AnyKind
type class
for all Pin
types.
Structs
Enums
AlternateConfig
for alternate peripheral function BAlternateConfig
for alternate peripheral function CAlternateConfig
for alternate peripheral function DAlternateConfig
for alternate peripheral function EAlternateConfig
for alternate peripheral function FDisabledConfig
and InputConfig
AlternateConfig
for alternate peripheral function GAlternateConfig
for alternate peripheral function HAlternateConfig
for alternate peripheral function IAlternateConfig
for alternate peripheral function JAlternateConfig
for alternate peripheral function KAlternateConfig
for alternate peripheral function LAlternateConfig
for alternate peripheral function MAlternateConfig
for alternate peripheral function NDisabledConfig
and InputConfig
DisabledConfig
and InputConfig
OutputConfig
for a push-pull configurationOutputConfig
for a readable push-pull
configurationTraits
enum
for Interrupt configurationsOption<PinId>
Option<PinId>
Some(PinId)
Some(PinId)
Type Definitions
PinMode
for floating disabled modePinMode
for floating input modePinMode
for floating Interrupt modePinMode
for pull-down disabled modePinMode
for pull-down input modePinMode
for pull-down Interrupt modePinMode
for pull-up disabled modePinMode
for pull-up input modePinMode
for pull-up Interrupt modePinMode
for push-pull output modePinMode
for readable push-pull output mode