Module 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 crate::ehal_02::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§

Alternate
Type-level variant of PinMode for alternate peripheral functions
Disabled
Type-level variant of PinMode for disabled modes
Input
Type-level variant of PinMode for input modes
Interrupt
Type-level variant of PinMode for Interrupt modes
Output
Type-level variant of PinMode for output modes
Pin
A type-level GPIO pin, parameterized by PinId and PinMode types
Pins
Collection of all the individual Pins

Enums§

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

Traits§

AlternateConfig
Type-level enum for alternate peripheral function configurations
AnyPin
Type class for Pin types
DisabledConfig
Type-level enum for disabled configurations
InputConfig
Type-level enum for input configurations
InterruptConfig
Type-level enum for Interrupt configurations
OptionalPin
Type-level equivalent of Option<PinId>
OptionalPinId
Type-level equivalent of Option<PinId>
OutputConfig
Type-level enum for output configurations
PinId
Type-level enum for pin IDs
PinMode
Type-level enum representing pin modes
SomePin
Type-level equivalent of Some(PinId)
SomePinId
Type-level equivalent of Some(PinId)

Type Aliases§

AlternateB
Type-level variant of PinMode for alternate peripheral function B
AlternateC
Type-level variant of PinMode for alternate peripheral function C
AlternateD
Type-level variant of PinMode for alternate peripheral function D
AlternateE
Type-level variant of PinMode for alternate peripheral function E
AlternateF
Type-level variant of PinMode for alternate peripheral function F
AlternateG
Type-level variant of PinMode for alternate peripheral function G
AlternateH
Type-level variant of PinMode for alternate peripheral function H
FloatingDisabled
Type-level variant of PinMode for floating disabled mode
FloatingInput
Type-level variant of PinMode for floating input mode
FloatingInterrupt
Type-level variant of PinMode for floating Interrupt mode
PullDownDisabled
Type-level variant of PinMode for pull-down disabled mode
PullDownInput
Type-level variant of PinMode for pull-down input mode
PullDownInterrupt
Type-level variant of PinMode for pull-down Interrupt mode
PullUpDisabled
Type-level variant of PinMode for pull-up disabled mode
PullUpInput
Type-level variant of PinMode for pull-up input mode
PullUpInterrupt
Type-level variant of PinMode for pull-up Interrupt mode
PushPullOutput
Type-level variant of PinMode for push-pull output mode
ReadableOutput
Type-level variant of PinMode for readable push-pull output mode
Reset
Type alias for the PinMode at reset
SpecificPin
Type alias to recover the specific Pin type from an implementation of AnyPin