Difference between revisions of "Getting started with GPIO"

[quality revision] [pending revision]
m (Reverted edits by Sarra Saidi (talk) to last revision by SOUADE MENDJELI)
(Tag: Rollback)
m
 

This article explains what GPIO is GPIO and how to use it through examples.

1 GPIO definition What is a general purpose input output (GPIO)[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.

  • Pin Mode : Each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:
    • input or output
    • analog
    • alternate function (AF).
  • Pin characteristics :
    • Input : no pull-up and no pull-down or pull-up or pull-down
    • Output : push-pull or open-drain with pull-up or pull-down capability
    • Alternate function : push-pull or open-drain with pull-up or pull-down capability.

GPIO Functional description graph.png

1.1 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 to the peripheral speed. For example, low speed is optimal for toggling GPIO at 1 Hz, while using SPI at 45 MHz requires very high speed setting.

GPIO Edge.png


2 Configure GPIO for LED toggling[edit]

2.1 Objective[edit]

Learn how to Toggle a pin on STM32L476 Nucleo board using Hardware Abstraction Layer (HAL) library and 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

    STM32CubeIDE.

    2.2 Create the project in STM32CubeMXSTM32CubeIDE[edit]

    • File > New project > Access to board selector on main panel or Menu > File > New Project > STM32 Project in main panel.

    create STM32CubeIDE project.png

    This example uses the NUCLEO-L476RG board.

    • Select NUCLEO-L476RG

    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)

    LedPinGPIO.png LED UM GPIO.png

    Note: During the download of a firmware package with STM32CubeMX, existing examples can be find at the following path for example
    •  using the Board Selector as shown in the figure below:

    Select NUCLEO-L476RG board.png

    In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically. This however may take some time.

    Save the project.
    Setup menu GPIO.png

    • You will then get a popup asking if you want to initialize peripherals to their default configuration.
    • No need to configure any peripheral as only the core/Flash/SRAMs are used (default peripherals).
    2.3.1 GPIO configuration[edit]
    Info white.png Information
    Existing examples for products like the STM32G0 can be found using the following path: 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.

    You can open the GPIO_IOToggle.ioc file with STM32CubeIDE

    .
    Warning white.png Warning
    There are no ready-to-use examples for STM32L476.

    2.3 Configure GPIO[edit]

    • 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).

    LedPinGPIO.png LED UM GPIO.png

    • Yellow pins are related to the power supply
    • Unused pins are marked as Grey
    • 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.

    GPIO config.png Setup GPIO.png

    2.

    3.2 GPIO (pin) output-speed configuration

    4 Generate project and edit main.c[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..

    GPIO Speed.png

    2.3.3 Set the project details for generation[edit]

    Manage GPIO Project.png

    2.3.4 Open the main.c in our IDE[edit]
    • We do the LED toggling in a function inside main.c
    Info white.png Information
    Between /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ tags

    The easiest way to generate the code is to save your current project : Ctrl + S
    The code is generated so you can see it in the left side of the screen in the project explorer
    File tree GPIO.png
    Now, open the main.c file which is the main source file for this application

    • The LED toggling is done with a HAL function inside main.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_RESETTogglePin(LD2_GPIO_Port,LD2_Pin);
        HAL_Delay(500);
      }
      /* USER CODE END 3 */
    
    
    Info white.png Information
    Make sure to keep User Code when re-generating !

    Code genreation panel.png

    2.45 Compile and flash[edit]

    • Click on Build button Built.png
    • Click on Debug button (to run step by step) Debug.png
    • Or on Run button (to execute) Run.png

    Every 500 ms the green LED state changes.
    ST10543 NUCLEO L476RG top D2Blink.png

    Warning white.png Warning
    All GPIOs are able to drive 5 V and 3.3 V in input mode, but they are only able to generate 3 V .3V in output push-pull mode




    This article explains what GPIO is GPIO and how to use it through examples
    
    ==GPIO definition.
    ==What is a general purpose input output (GPIO) ==
    '''''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
    * '''''Pin Mode :''''' Each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:<br>
    
    **input or output
    **analog
    **alternate function (AF).
    * '''''Pin characteristics :''''' 
    ** '''''Input''''' : no pull-up and no pull-down or pull-up or pull-down
    ** '''''Output''''' : push-pull or open-drain with pull-up or pull-down capability
    ** '''''Alternate function''''' : push-pull or open-drain with pull-up or pull-down capability.
    [[File:GPIO Functional description graph.png|500px]]
    
    ====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 to the peripheral speed. For example, '''low''' speed is optimal for toggling GPIO at 1 Hz, while using SPI at 45 MHz requires '''''very high''''' speed setting.
    [[File:GPIO_Edge.png|600px]]
    
    ==Configure GPIO for LED toggling==
    ====Objective====
    Learn how to Toggle a pin on STM32L476 Nucleo board using Hardware Abstraction Layer (HAL) library and learn how to setup the pin and GPIO port in STM32CubeIDE.
    ====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: STM32CubeIDE====
    *'''''File''''' > '''''New''''' > '''''STM32 Project''''' in main panel. <br>
    
    [[File:create STM32CubeIDE project.png]]<br><br>
    
    This example uses the NUCLEO-L476RG board.<br>
    
    *Select '''''NUCLEO-L476RG''''' using the '''''Board Selector''''' as shown in the figure below:<br><br>
    
    [[File:Select NUCLEO-L476RG board.png|600px]]<br>
    
    
    In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically. This however may take some time.<br>
    
    
    Save the project.<br>
    
    [[File:Setup_menu_GPIO.png|300px]]<br>
    
    *You will then get a popup asking if you want to initialize peripherals to their default configuration.
    *No need to configure any peripheral as only the core/Flash/SRAMs are used (default peripherals).
    {{info|Existing examples for products like the STM32G0 can be found using the following path: '''''c:\Users\YourUserName\STM32Cube\Repository\STM32Cube_FW_G0_V1.3.0\Projects\NUCLEO-G071RB\Examples\GPIO\GPIO_IOToggle\GPIO_IOToggle.ioc''''' and 
    
    You can open them with STM32CubeMX.
    
    =====GPIO configuration=====
    * the GPIO_IOToggle.ioc file with STM32CubeIDE }}.
    {{Warning |There are no ready-to-use examples for STM32L476.}}
    
    ====Configure GPIO====
    * 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).<br>
    
    [[File:LedPinGPIO.png|300px]]
    [[File:LED_UM_GPIO.png|300px]]
    :*'''''Yellow''''' pins are related to the power supply <br>
    
    :*Unused pins are marked as '''''Grey'''''
    * 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:Setup_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 Generate project and edit main.c====
    The easiest way to generate the code is to save your current project : '''''Ctrl + S ''''' <br>
    
    The code is generated so you can see it in the left side of the screen in the project explorer <br>
    
    [[File:File_tree_GPIO.png|200px]]<br>
    
    Now, open the '''''main.c''''' file which is the main source file for this application
    * The LED toggling is done with a HAL 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====
    *{
        HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
        HAL_Delay(500);
      }
      /* USER CODE END 3 */</syntaxhighlight>
    
    {{Info| Make sure to keep User Code when re-generating !  }}
    [[File:Code_genreation_panel.png|600px]]
    
    ====Compile and flash====
    * Click on '''''Build''''' button [[File:Built.png]]<br>
    
    * Click on '''''Debug''''' button (to run step by step) [[File:Debug.png]]<br>
    
    * Or on '''''Run''''' button (to execute) [[File:Run.png]]<br>
    
    Every 500 ms the green LED state changes. <br>
    
    [[File:ST10543_NUCLEO_L476RG_top_D2Blink.png|400px]]
    {{Warning |  All GPIOs are able to drive 5 V and 3.3 V in input mode, but they are only able to generate 3 V .3V in output push-pull mode}}
    <noinclude>
    
    {{ArticleBasedOnModel | Example STM32 features/examples article}}
    {{PublicationRequestId | 23751 25447| 2022-06-1312-01 | }}
    {{PublicationRequestId | 17185 | 2020-08-26 | Alain FONTBONNE}}23751 | 2022-06-13 | }}
    [[category:Getting_started_with_STM32_system_peripherals | 15]]</noinclude>
    (90 intermediate revisions by 5 users not shown)
    Line 1: Line 1:
    This article explains what is GPIO and how to use it through examples
    +
    This article explains what GPIO is and how to use it through examples.
    ==GPIO definition==
    +
    ==What is a general purpose input output (GPIO) ==
     
    '''''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.
     
    '''''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.
      +
    * '''''Pin Mode :''''' Each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:<br>
      +
    **input or output
      +
    **analog
      +
    **alternate function (AF).
      +
    * '''''Pin characteristics :'''''
      +
    ** '''''Input''''' : no pull-up and no pull-down or pull-up or pull-down
      +
    ** '''''Output''''' : push-pull or open-drain with pull-up or pull-down capability
      +
    ** '''''Alternate function''''' : push-pull or open-drain with pull-up or pull-down capability.
      +
    [[File:GPIO Functional description graph.png|500px]]
      +
      +
    ====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 to the peripheral speed. For example, '''low''' speed is optimal for toggling GPIO at 1 Hz, while using SPI at 45 MHz requires '''''very high''''' speed setting.
      +
    [[File:GPIO_Edge.png|600px]]
      +
      +
     
    ==Configure GPIO for LED toggling==
     
    ==Configure GPIO for LED toggling==
     
    ====Objective====
     
    ====Objective====
    * Learn how to setup the pin and GPIO port in STM32CubeMX
    +
    Learn how to Toggle a pin on STM32L476 Nucleo board using Hardware Abstraction Layer (HAL) library and learn how to setup the pin and GPIO port in STM32CubeIDE.
    * Modify the code generated by STM32CubeMX and use the HAL functions
    +
     
      +
    ====Create the project in STM32CubeIDE====
      +
    *'''''File''''' > '''''New''''' > '''''STM32 Project''''' in main panel. <br>
      +
    [[File:create STM32CubeIDE project.png]]<br><br>
      +
    This example uses the NUCLEO-L476RG board.<br>
      +
    *Select '''''NUCLEO-L476RG''''' using the '''''Board Selector''''' as shown in the figure below:<br><br>
      +
    [[File:Select NUCLEO-L476RG board.png|600px]]<br>
      +
     
      +
    In case you haven't downloaded the STM32L476 Cube library, it will be downloaded automatically. This however may take some time.<br>
       
    ====How====
    +
    Save the project.<br>
    * Configure the GPIO pin in STM32CubeMX and generate the code
    +
    [[File:Setup_menu_GPIO.png|300px]]<br>
    * Add into the project the HAL_Delay function and HAL_GPIO_Toggle function
    +
    *You will then get a popup asking if you want to initialize peripherals to their default configuration.
    * Verify the correct functionality on toggling LED
    +
    *No need to configure any peripheral as only the core/Flash/SRAMs are used (default peripherals).
      +
    {{info|Existing examples for products like the STM32G0 can be found using the following path: '''''c:\Users\YourUserName\STM32Cube\Repository\STM32Cube_FW_G0_V1.3.0\Projects\NUCLEO-G071RB\Examples\GPIO\GPIO_IOToggle\GPIO_IOToggle.ioc'''''
      +
    You can open the GPIO_IOToggle.ioc file with STM32CubeIDE }}.
      +
    {{Warning |There are no ready-to-use examples for STM32L476.}}
       
    ====Create the project in STM32CubeMX====
    +
    ====Configure GPIO====
    * '''''New project''''' > '''''Access to board selector''''' on main panel or '''''Menu''''' > '''''File''''' > '''''New Project'''''
    +
    * 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).<br>
    * 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:LedPinGPIO.png|300px]]
     
    [[File:LED_UM_GPIO.png|300px]]
     
    [[File:LED_UM_GPIO.png|300px]]
     
    +
    :*'''''Yellow''''' pins are related to the power supply <br>
    Note: During the download  of a firmware package with STM32CubeMX, existing examples can be find at the following path for example:  
    +
    :*Unused pins are marked as '''''Grey'''''
    '''''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
     
    * 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.
    [[File:GPIO_config.png|600px]]
    +
    [[File:Setup_GPIO.png|600px]]
       
    =====GPIO (pin) output-speed configuration=====
    +
    ====Generate project and edit main.c====
    * Change the rising and falling edge when the pin state changes from high to low or low to high.
    +
    The easiest way to generate the code is to save your current project : '''''Ctrl + S ''''' <br>
    * A higher GPIO speed increases the EMI noise from STM32 and increases the STM32 consumption.
    +
    The code is generated so you can see it in the left side of the screen in the project explorer <br>
    * 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:File_tree_GPIO.png|200px]]<br>
    [[File:GPIO_Speed.png|600px]]
    +
    Now, open the '''''main.c''''' file which is the main source file for this application
      +
    * The LED toggling is done with a HAL function inside '''''main.c'''''
       
    =====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">
     
    <syntaxhighlight lang="c">
     
      /* USER CODE BEGIN 3 */
     
      /* USER CODE BEGIN 3 */
     
       /* Infinite loop */
     
       /* Infinite loop */
      while (1)
     
     
       {
     
       {
         HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
    +
         HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
        HAL_Delay(500);
     
       
     
        HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_RESET);
     
     
         HAL_Delay(500);
     
         HAL_Delay(500);
     
       }
     
       }
     
       /* USER CODE END 3 */
     
       /* USER CODE END 3 */
     
    </syntaxhighlight>
     
    </syntaxhighlight>
      +
    {{Info| Make sure to keep User Code when re-generating !  }}
      +
    [[File:Code_genreation_panel.png|600px]]
       
     
    ====Compile and flash====
     
    ====Compile and flash====
    *Every 500 ms the green LED state changes.
    +
    * Click on '''''Build''''' button [[File:Built.png]]<br>
    {{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}}
    +
    * Click on '''''Debug''''' button (to run step by step) [[File:Debug.png]]<br>
      +
    * Or on '''''Run''''' button (to execute) [[File:Run.png]]<br>
      +
     
      +
    Every 500 ms the green LED state changes. <br>
      +
    [[File:ST10543_NUCLEO_L476RG_top_D2Blink.png|400px]]
      +
     
      +
    {{Warning |  All GPIOs are able to drive 5 V and 3.3 V in input mode, but they are only able to generate 3.3V in output push-pull mode}}
       
     
    <noinclude>
     
    <noinclude>
     
    {{ArticleBasedOnModel | Example STM32 features/examples article}}
     
    {{ArticleBasedOnModel | Example STM32 features/examples article}}
      +
    {{PublicationRequestId | 25447| 2022-12-01 | }}
     
    {{PublicationRequestId | 23751 | 2022-06-13 | }}
     
    {{PublicationRequestId | 23751 | 2022-06-13 | }}
    {{PublicationRequestId | 17185 | 2020-08-26 | Alain FONTBONNE}}
    +
     
     
    [[category:Getting_started_with_STM32_system_peripherals | 15]]
     
    [[category:Getting_started_with_STM32_system_peripherals | 15]]
     
    </noinclude>
     
    </noinclude>