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 Pins 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 PinModes, 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

  • Type-level variant of AlternateConfig for alternate peripheral function B
  • Type-level variant of AlternateConfig for alternate peripheral function C
  • Type-level variant of AlternateConfig for alternate peripheral function D
  • Type-level variant of AlternateConfig for alternate peripheral function E
  • Type-level variant of AlternateConfig for alternate peripheral function F
  • Type-level variant of both DisabledConfig and InputConfig
  • Type-level variant of AlternateConfig for alternate peripheral function G
  • Type-level variant of AlternateConfig for alternate peripheral function H
  • Pin ID representing pin PA00
  • Pin ID representing pin PA01
  • Pin ID representing pin PA02
  • Pin ID representing pin PA03
  • Pin ID representing pin PA04
  • Pin ID representing pin PA05
  • Pin ID representing pin PA06
  • Pin ID representing pin PA07
  • Pin ID representing pin PA08
  • Pin ID representing pin PA09
  • Pin ID representing pin PA10
  • Pin ID representing pin PA11
  • Pin ID representing pin PA12
  • Pin ID representing pin PA13
  • Pin ID representing pin PA14
  • Pin ID representing pin PA15
  • Pin ID representing pin PA16
  • Pin ID representing pin PA17
  • Pin ID representing pin PA18
  • Pin ID representing pin PA19
  • Pin ID representing pin PA20
  • Pin ID representing pin PA21
  • Pin ID representing pin PA22
  • Pin ID representing pin PA23
  • Pin ID representing pin PA24
  • Pin ID representing pin PA25
  • Pin ID representing pin PA27
  • Pin ID representing pin PA28
  • Pin ID representing pin PA30
  • Pin ID representing pin PA31
  • Pin ID representing pin PB02
  • Pin ID representing pin PB03
  • Pin ID representing pin PB08
  • Pin ID representing pin PB09
  • Pin ID representing pin PB10
  • Pin ID representing pin PB11
  • Pin ID representing pin PB22
  • Pin ID representing pin PB23
  • Type-level variant of both DisabledConfig and InputConfig
  • Type-level variant of both DisabledConfig and InputConfig
  • Type-level variant of OutputConfig for a push-pull configuration
  • Type-level variant of OutputConfig for a readable push-pull configuration

Traits

Type Definitions