HAL GPIO How to Use ¶
- group GPIO_How_To_Use
-
GPIO features ¶
Configure each port bit of the general-purpose I/O (GPIO) ports in one of the following 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 that can be activated or deactivated.
In output or alternate mode:
Configure each I/O for open-drain or push-pull type.
Select the I/O speed based on the VDD value.
The microcontroller I/O pins are connected to onboard peripherals/modules through a multiplexer:
It allows only one peripheral (alternate function) to be connected to an I/O pin at a time. No conflict occurs between peripherals sharing the same I/O pin.
Use the LSE oscillator pins OSC32_IN and OSC32_OUT as general-purpose I/O when LSE is off.
Use the HSE oscillator pins OSC_IN/OSC_OUT as general-purpose I/O when HSE is off.
How to use the GPIO HAL module driver ¶
Use the GPIO HAL driver 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 in the HAL configuration file to:
HAL_CLK_ENABLE_PERIPH_ONLY or HAL_CLK_ENABLE_PERIPH_PWR_SYSTEM: in this case the GPIOx clock will be enabled within the HAL_GPIO_Init()
Configure the GPIO pin(s) using HAL_GPIO_Init().
Set the I/O mode to the “mode” member from hal_gpio_config_t structure.
Select the pull-up or pull-down resistor using the “pull” member from hal_gpio_config_t structure.
For output or alternate function mode selection:
Configure the speed through the “speed” member from hal_gpio_config_t structure.
For alternate mode:
Configure the alternate function connected to the I/O through the “alternate” member from hal_gpio_config_t structure.
For output mode:
Configure the initial pin state through the “init_state” member from hal_gpio_config_t structure.
Select analog mode when a pin is used as an ADC channel input or as a DAC output.
For a GPIO pin with an external interrupt/event, use the 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 the level of other pins in the same cycle, use HAL_GPIO_WriteMultipleStatePin().
To lock a GPIO pin configuration until the next reset, use HAL_GPIO_LockPin().
Note
The LSE has priority over the GPIO function.
Note
The HSE has priority over the GPIO function.