STM32 GPIO Tutorial

 STM32 GPIO Tutorial 

STM32 GPIO Tutorial

Hello friends 👋👋, we back again to STM32 tutorials, after the basics of ARM architecture and STM32 basics today we come to GPIO section in which we will explore STM32 GPIO ports architecture, hardware and software configuration and it’s working principal, then we will make our first program (simple Pin toggling project) to put on live our STM32 dev-board.

GPIO Ports Architecture:

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.

Every GPIO ports composed of configuration registers (32 bits) , data registers (32 bits), set reset register (32 bits or 16 bits)  and locking register (32 bits) as it shown in the picture below (internal structure of GPIO ports)

STM32 GPIO Port Structure

GPIO Ports Speed:

STM32 GPIO have a programmable speed according to the application (project) and the programmer needs, but we should be careful in this point specially with high-speed configuration because a high GPIO speed rate increases the MCU consumption and also conduct to a high EMI noise form STM32 MCU.

The GPIO speed rate can be resumed as: the rising and falling edge time (from low to high and high to low).

GPIO ports speed is set via the APB2 bus which is responsible for determines the input sampling speed, also for other microcontroller there is a specific speed control register called OSPEED in which the speed configuration is done:

  • 00: Low speed
  • 01: Medium speed
  • 10: High speed
  • 11: Very high speed 

The speed rates are shown in the picture below:

STM32 GPIO speed rate

GPIO Ports Modes & Configuration:

Every GPIO ports in the STM32 can be configured into two modes (input & output) , each mode has several several configuration regarding to datasheet specification

Input Configuration:

  1. Pull-up
  2. Pull-down
  3. Floating
Output Configuration;

  1. Open drain
  2. Push/Pull
Also, there is an alternate function configuration:

  1. Open drain
  2. Push/Pull

GPIO Ports Voltage & Current:

STM32 GPIO ports voltage level is by default 3.3V (for both input and output) but some of input and output ports have 5V tolerance (not all ports and pin), it should refer always to the datasheet of the microcontroller to determine theses pins in order to avoid damaging the ports and MCU.

For the current STM32 GPIO ports can handle a maximum current of 25mA for both modes (sinking and sourcing), as a result we should to be very careful in this point to don not expose the MCU to a high current level that can damage the microcontroller.

GPIO Ports Interrupt:

All ports have external interrupt capability. To use external interrupt lines, the port must be configured in input mode and also set the NVIC (Nested Vector Interrupt Controller) to enable the interrupt, this topic will take place in the upcoming tutorials.

STM32 GPIO  example (Toggling LED state):

 first of all we open STM Cube IDE and open a new project and we choose our target microcontroller (STM32F404ZET6 in our case)

Choosing Target board Cube IDE

then we give name to our project

Cube IDE project name


next a new tab will be open that contain the microcontroller in order to make configuration and setup

STM32 MCU Configuration



We choose the clock of the system as external clock (crystal oscillator) to ensure frequency stability and low noise, then we chose PF9 (GPIO port F pin9 as GPIO output to control the Led)

External clock setup STM32
STM32 GPIO Cube IDE

And finally we set the clock configuration of the microcontroller and going to save and generate the code

STM32 Clock Configuration

 


the code is as follow:
#include "main.h"
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
 
  while (1)
  {
    // LED ON
    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_RESET);
    HAL_Delay(500);
    // LED OFF
    HAL_GPIO_WritePin(GPIOF, GPIO_PIN_9, GPIO_PIN_SET);
    HAL_Delay(500);
  }
}


Note: we should always refer to the board schematics to check the Led connection (in our case the led is connected to 3.3V directly and the cathode is connected to the PF9 so to turn on the led we should set the PF9 to logic low 0V)


then we will follow the step below:

  1. Click on Build button 
  2. Click on Debug button (to run step by step) 
  3. Or on Run button (to execute) 
and our board Now is alive (code uplaoded) and the led will blink every 500 ms

we have arrive to the end of today blog , i hope that you like-it in the next blog we will use externa interrupt to interface with GPIO port and practice example vedio will be added to the blog section as demonstration (we don't make a vedio to this example because the my STlink have connection problem in the upcoming blog all examples will be with vedios)
feel free to comment and share the blog with your friends, see you guys in the next tutorial 😎😎 

Post a Comment

Previous Next

نموذج الاتصال