Module gpio

Source
Expand description

General Purpose Input / Output

The GPIO pins are organised into groups of 16 pins which can be accessed through the gpioa, gpiob… modules. To get access to the pins, you first need to convert them into a HAL designed struct from the pac struct using the split function.

// Acquire the GPIOC peripheral
// NOTE: `dp` is the device peripherals from the `PAC` crate
let mut gpioa = dp.GPIOA.split();

This gives you a struct containing all the pins px0..px15. By default pins are in floating input mode. You can change their modes. For example, to set pa5 high, you would call

let output = gpioa.pa5.into_push_pull_output();
output.set_high();

§Modes

Each GPIO pin can be set to various modes:

  • Alternate: Pin mode required when the pin is driven by other peripherals
  • Analog: Analog input to be used with ADC.
  • Dynamic: Pin mode is selected at runtime. See changing configurations for more details
  • Input
    • PullUp: Input connected to high with a weak pull up resistor. Will be high when nothing is connected
    • PullDown: Input connected to high with a weak pull up resistor. Will be low when nothing is connected
    • Floating: Input not pulled to high or low. Will be undefined when nothing is connected
  • Output
    • PushPull: Output which either drives the pin high or low
    • OpenDrain: Output which leaves the gate floating, or pulls it do ground in drain mode. Can be used as an input in the open configuration

§Changing modes

The simplest way to change the pin mode is to use the into_<mode> functions. These return a new struct with the correct mode that you can use the input or output functions on.

If you need a more temporary mode change, and can not use the into_<mode> functions for ownership reasons, you can use the closure based with_<mode> functions to temporarily change the pin type, do some output or input, and then have it change back once done.

§Dynamic Mode Change

The above mode change methods guarantee that you can only call input functions when the pin is in input mode, and output when in output modes, but can lead to some issues. Therefore, there is also a mode where the state is kept track of at runtime, allowing you to change the mode often, and without problems with ownership, or references, at the cost of some performance and the risk of runtime errors.

To make a pin dynamic, use the into_dynamic function, and then use the make_<mode> functions to change the mode

Re-exports§

pub use super::Input as DefaultMode;

Modules§

alt
gpioa
GPIO
gpiob
GPIO
gpioc
GPIO
gpiod
GPIO
gpioe
GPIO
gpiof
GPIO
gpiog
GPIO
gpioh
GPIO
gpioi
GPIO
gpioj
GPIO
gpiok
GPIO
outport

Structs§

Alternate
Some alternate mode (type state)
Analog
Analog mode (type state)
AnyPin
Fully erased pin
DynamicPin
Pin type with dynamic mode
ErasedPin
Fully erased pin
Input
Input mode (type state)
NoPin
A filler pin type
OpenDrain
Open drain input or output (type state)
Output
Output mode (type state)
PEPin
Partially erased pin
PartiallyErasedPin
Partially erased pin
Pin
Generic pin type
PushPull
Push pull output (type state)

Enums§

Dynamic
Tracks the current pin state for dynamic pins
Edge
GPIO interrupt trigger edge selection
PinState
Digital output pin state
Pull
Pull setting for an input.
Speed
GPIO Pin speed selection

Traits§

ExtiPin
External Interrupt Pin
GpioExt
Extension trait to split a GPIO peripheral in independent pins and registers
PinExt
Id, port and mode for any pin
PinMode
Marker trait for valid pin modes (type state).
PinPull
PinSpeed
ReadPin

Type Aliases§

AF0
Alternate function 0 (type state)
AF1
Alternate function 1 (type state)
AF2
Alternate function 2 (type state)
AF3
Alternate function 3 (type state)
AF4
Alternate function 4 (type state)
AF5
Alternate function 5 (type state)
AF6
Alternate function 6 (type state)
AF7
Alternate function 7 (type state)
AF8
Alternate function 8 (type state)
AF9
Alternate function 9 (type state)
AF10
Alternate function 10 (type state)
AF11
Alternate function 11 (type state)
AF12
Alternate function 12 (type state)
AF13
Alternate function 13 (type state)
AF14
Alternate function 14 (type state)
AF15
Alternate function 15 (type state)
Debugger
JTAG/SWD mote (type state)
PA0
PA0 pin
PA1
PA1 pin
PA2
PA2 pin
PA3
PA3 pin
PA4
PA4 pin
PA5
PA5 pin
PA6
PA6 pin
PA7
PA7 pin
PA8
PA8 pin
PA9
PA9 pin
PA10
PA10 pin
PA11
PA11 pin
PA12
PA12 pin
PA13
PA13 pin
PA14
PA14 pin
PA15
PA15 pin
PB0
PB0 pin
PB1
PB1 pin
PB2
PB2 pin
PB3
PB3 pin
PB4
PB4 pin
PB5
PB5 pin
PB6
PB6 pin
PB7
PB7 pin
PB8
PB8 pin
PB9
PB9 pin
PB10
PB10 pin
PB11
PB11 pin
PB12
PB12 pin
PB13
PB13 pin
PB14
PB14 pin
PB15
PB15 pin
PC0
PC0 pin
PC1
PC1 pin
PC2
PC2 pin
PC3
PC3 pin
PC4
PC4 pin
PC5
PC5 pin
PC6
PC6 pin
PC7
PC7 pin
PC8
PC8 pin
PC9
PC9 pin
PC10
PC10 pin
PC11
PC11 pin
PC12
PC12 pin
PC13
PC13 pin
PC14
PC14 pin
PC15
PC15 pin
PD0
PD0 pin
PD1
PD1 pin
PD2
PD2 pin
PD3
PD3 pin
PD4
PD4 pin
PD5
PD5 pin
PD6
PD6 pin
PD7
PD7 pin
PD8
PD8 pin
PD9
PD9 pin
PD10
PD10 pin
PD11
PD11 pin
PD12
PD12 pin
PD13
PD13 pin
PD14
PD14 pin
PD15
PD15 pin
PE0
PE0 pin
PE1
PE1 pin
PE2
PE2 pin
PE3
PE3 pin
PE4
PE4 pin
PE5
PE5 pin
PE6
PE6 pin
PE7
PE7 pin
PE8
PE8 pin
PE9
PE9 pin
PE10
PE10 pin
PE11
PE11 pin
PE12
PE12 pin
PE13
PE13 pin
PE14
PE14 pin
PE15
PE15 pin
PF0
PF0 pin
PF1
PF1 pin
PF2
PF2 pin
PF3
PF3 pin
PF4
PF4 pin
PF5
PF5 pin
PF6
PF6 pin
PF7
PF7 pin
PF8
PF8 pin
PF9
PF9 pin
PF10
PF10 pin
PF11
PF11 pin
PF12
PF12 pin
PF13
PF13 pin
PF14
PF14 pin
PF15
PF15 pin
PG0
PG0 pin
PG1
PG1 pin
PG2
PG2 pin
PG3
PG3 pin
PG4
PG4 pin
PG5
PG5 pin
PG6
PG6 pin
PG7
PG7 pin
PG8
PG8 pin
PG9
PG9 pin
PG10
PG10 pin
PG11
PG11 pin
PG12
PG12 pin
PG13
PG13 pin
PG14
PG14 pin
PG15
PG15 pin
PH0
PH0 pin
PH1
PH1 pin
PH2
PH2 pin
PH3
PH3 pin
PH4
PH4 pin
PH5
PH5 pin
PH6
PH6 pin
PH7
PH7 pin
PH8
PH8 pin
PH9
PH9 pin
PH10
PH10 pin
PH11
PH11 pin
PH12
PH12 pin
PH13
PH13 pin
PH14
PH14 pin
PH15
PH15 pin
PI0
PI0 pin
PI1
PI1 pin
PI2
PI2 pin
PI3
PI3 pin
PI4
PI4 pin
PI5
PI5 pin
PI6
PI6 pin
PI7
PI7 pin
PI8
PI8 pin
PI9
PI9 pin
PI10
PI10 pin
PI11
PI11 pin
PI12
PI12 pin
PI13
PI13 pin
PI14
PI14 pin
PI15
PI15 pin
PJ0
PJ0 pin
PJ1
PJ1 pin
PJ2
PJ2 pin
PJ3
PJ3 pin
PJ4
PJ4 pin
PJ5
PJ5 pin
PJ6
PJ6 pin
PJ7
PJ7 pin
PJ8
PJ8 pin
PJ9
PJ9 pin
PJ10
PJ10 pin
PJ11
PJ11 pin
PJ12
PJ12 pin
PJ13
PJ13 pin
PJ14
PJ14 pin
PJ15
PJ15 pin
PK0
PK0 pin
PK1
PK1 pin
PK2
PK2 pin
PK3
PK3 pin
PK4
PK4 pin
PK5
PK5 pin
PK6
PK6 pin
PK7
PK7 pin