Module stm32f4xx_hal::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

Structs

Enums

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

Traits

Type Aliases