HAL GPIO How to Use

group GPIO_How_To_Use

GPIO features

  • Each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:

    • Input mode

    • Output mode

    • Alternate function mode

    • Analog mode

  • After startup:

    • The alternate functions are inactive.

    • The I/O ports are configured in analog mode with the exception of some pre-configured pins (debug pins for instance)

  • All GPIO pins have weak internal pull-up and pull-down resistors, which can be activated or not.

    • In Output or Alternate mode:

      • Each IO can be configured on open-drain or push-pull type.

      • The IO speed can be selected depending on the VDD value.

  • The microcontroller IO pins are connected to onboard peripherals/modules through a multiplexer:

    • It allows only one peripheral (alternate function) to be connected to an IO pin at a time. there can be no conflict between peripherals sharing the same IO pin.

  • The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general purpose when LSE is off.

  1. The HSE oscillator pins OSC_IN/OSC_OUT can be used as general purpose when HSE is off.

How to use the GPIO HAL module driver

The GPIO HAL driver can be used as follows:

  • Enable the GPIO peripheral clock:

    • Either at application level by calling the HAL_RCC_GPIOx_EnableClock() API

    • Or by setting the USE_HAL_GPIO_CLK_ENABLE_MODEL define at HAL configuration file to:

      • HAL_CLK_ENABLE_PERIPH_ONLY: in this case the GPIOx clock will be enabled within the HAL_GPIO_Init()

      • HAL_CLK_ENABLE_PERIPH_PWR_SYSTEM: in this case:

        • The GPIOx clock will be enabled within the HAL_GPIO_Init()

        • Some GPIO ports (depending on the device) require additional independent supplies to operate. In this case, activate the independent voltage(s) for dedicated GPIO port using HAL_GPIO_EnableSystemDependencies() .

  • Configure the GPIO pin(s) using HAL_GPIO_Init() .

    • Set the IO mode to the “mode” member from hal_gpio_config_t structure.

    • Select Pull-up or Pull-down resistor using “pull” member from hal_gpio_config_t structure.

    • In case of Output or alternate function mode selection:

    • In case alternate mode is selected:

      • The alternate function connected to the IO is configured through “alternate” member from hal_gpio_config_t structure.

    • In case Output mode is selected:

      • The initial pin state is configured through “init_state” member from hal_gpio_config_t structure.

    • Analog mode is required when a pin is to be used as an ADC channel input or as a DAC output.

    • In case of using a GPIO pin with an external interrupt/event, use HAL EXTI driver to configure the corresponding EXTI line.

  • To reset the configuration of GPIO pin(s), use HAL_GPIO_DeInit() .

  • To get the level of a pin configured in input mode use HAL_GPIO_ReadPin() .

  • To set/reset the level of pin(s) configured in output mode use HAL_GPIO_WritePin() / HAL_GPIO_TogglePin() .

  • To set the level of several pins and reset level of other pins in same cycle, use HAL_GPIO_WriteMultipleStatePin() .

  • To lock a GPIO pin configuration until next reset, use HAL_GPIO_LockPin() .

  • To enable the speed optimization for GPIO pins supporting HSLV mode, use HAL_GPIO_EnableHighSpeedLowVoltage() .

  • To disable the speed optimization use, HAL_GPIO_DisableHighSpeedLowVoltage() .

Note

The LSE has priority over the GPIO function.

Note

The HSE has priority over the GPIO function.