Approved version. Approved on: 13:47, 11 October 2022
You are viewing an old version of this page. Return to the latest version.
Difference between revisions of "Getting started with GPIO"
[unchecked revision] | [quality revision] |
Sarra Saidi (talk | contribs)
m (→GPIO configuration)
|
m (Reverted edits by Sarra Saidi (talk) to last revision by SOUADE MENDJELI)
(Tag: Rollback)
|
This article explains what is GPIO and how to use it through examples
Contents
1 GPIO definition[edit]
GPIO stands for general purpose input/output. It is a type of pin found on an integrated circuit that does not have a specific function. While most pins have a dedicated purpose, such as sending a signal to a certain component, the function of a GPIO pin is customizable and can be controlled by the software.
2 Configure GPIO for LED toggling[edit]
2.1 Objective[edit]
- Learn how to setup the pin and GPIO port in STM32CubeMX
- Modify the code generated by STM32CubeMX and use the HAL functions
2.2 How[edit]
- Configure the GPIO pin in STM32CubeMX and generate the code
- Add into the project the HAL_Delay function and HAL_GPIO_Toggle function
- Verify the correct functionality on toggling LED
2.3 Create the project in STM32CubeMX[edit]
- New project > Access to board selector on main panel or Menu > File > New Project
- Select NUCLEO-L476RG
- If you want to start the project with a board, the LED pin is already selected (PA5 on NucleoL476RG. For other boards refer to the user manual)
Note: During the download of a firmware package with STM32CubeMX, existing examples can be find at the following path for example: c:\Users\YourUserName\STM32Cube\Repository\STM32Cube_FW_G0_V1.3.0\Projects\NUCLEO-G071RB\Examples\GPIO\GPIO_IOToggle\GPIO_IOToggle.ioc and open them with STM32CubeMX.
2.3.1 GPIO configuration[edit]
- Select the push-pull mode
- No pull-up and pull-down
- Output speed set to very high is important for faster peripherals such as SPI or USART.
2.3.2 GPIO (pin) output-speed configuration[edit]
- Change the rising and falling edge when the pin state changes from high to low or low to high.
- A higher GPIO speed increases the EMI noise from STM32 and increases the STM32 consumption.
- It is good to adapt the GPIO speed with the peripheral speed. For example toggling GPIO on 1 Hz is low optimal settings, but with SPI on 45 MHz the very high must be set..
2.3.3 Set the project details for generation[edit]
2.3.4 Open the main.c in our IDE[edit]
- We do the LED toggling in a function inside main.c
![]() |
Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags |
/* USER CODE BEGIN 3 */
/* Infinite loop */
while (1)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
HAL_Delay(500);
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET);
HAL_Delay(500);
}
/* USER CODE END 3 */
2.4 Compile and flash[edit]
- Every 500 ms the green LED state changes.
![]() |
All GPIOs are able to drive 5 V and 3 V in input mode, but they are only able to generate 3 V in output push-pull mode |
This article explains what is GPIO and how to use it through examples ==GPIO definition== '''''GPIO''''' stands for '''''general purpose input/output'''''. It is a type of pin found on an integrated circuit that does not have a specific function. While most pins have a dedicated purpose, such as sending a signal to a certain component, the function of a GPIO pin is customizable and can be controlled by the software. ==Configure GPIO for LED toggling== ====Objective==== * Learn how to setup the pin and GPIO port in STM32CubeMX * Modify the code generated by STM32CubeMX and use the HAL functions ====How==== * Configure the GPIO pin in STM32CubeMX and generate the code * Add into the project the HAL_Delay function and HAL_GPIO_Toggle function * Verify the correct functionality on toggling LED ====Create the project in STM32CubeMX==== * '''''New project''''' > '''''Access to board selector''''' on main panel or '''''Menu''''' > '''''File''''' > '''''New Project''''' * Select NUCLEO-L476RG [[File:SelectL476.png|600px]] * If you want to start the project with a board, the LED pin is already selected (PA5 on NucleoL476RG. For other boards refer to the user manual) [[File:LedPinGPIO.png|300px]] [[File:LED_UM_GPIO.png|300px]] Note: During the download of a firmware package with STM32CubeMX, existing examples can be find at the following path for example: '''''c:\Users\YourUserName\STM32Cube\Repository\STM32Cube_FW_G0_V1.3.0\Projects\NUCLEO-G071RB\Examples\GPIO\GPIO_IOToggle\GPIO_IOToggle.ioc''''' and open them with STM32CubeMX. =====GPIO configuration===== * Select the push-pull mode * No pull-up and pull-down * Output speed set to very high is important for faster peripherals such as SPI or USART. [[File:GPIO_config.png|600px]] =====GPIO (pin) output-speed configuration===== * Change the rising and falling edge when the pin state changes from high to low or low to high. * A higher GPIO speed increases the EMI noise from STM32 and increases the STM32 consumption. * It is good to adapt the GPIO speed with the peripheral speed. For example toggling GPIO on 1 Hz is '''''low''''' optimal settings, but with SPI on 45 MHz the '''''very high''''' must be set.. [[File:GPIO_Speed.png|600px]] =====Set the project details for generation===== [[File:Manage_GPIO_Project.png|600px]] =====Open the main.c in our IDE===== * We do the LED toggling in a function inside '''''main.c''''' {{Info|Between '''/* USER CODE BEGIN 3 */''' and '''/* USER CODE END 3 */''' tags}}<syntaxhighlight lang="c"> /* USER CODE BEGIN 3 */ /* Infinite loop */ while (1) { HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET); HAL_Delay(500); HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET); HAL_Delay(500); } /* USER CODE END 3 */</syntaxhighlight> ====Compile and flash==== *Every 500 ms the green LED state changes. {{Warning | All GPIOs are able to drive 5 V and 3 V in input mode, but they are only able to generate 3 V in output push-pull mode}} <noinclude> {{ArticleBasedOnModel | Example STM32 features/examples article}} {{PublicationRequestId | 23751 | 2022-06-13 | }} {{PublicationRequestId | 17185 | 2020-08-26 | Alain FONTBONNE}} [[category:Getting_started_with_STM32_system_peripherals | 15]]</noinclude>
Line 25: | Line 25: | ||
=====GPIO configuration===== |
=====GPIO configuration===== |
||
− | * Select the push-pull mode |
+ | * Select the push-pull mode |
* No pull-up and pull-down |
* No pull-up and pull-down |
||
* Output speed set to very high is important for faster peripherals such as SPI or USART. |
* Output speed set to very high is important for faster peripherals such as SPI or USART. |