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)
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:
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:
- Pull-up
- Pull-down
- Floating
- Open drain
- Push/Pull
- Open drain
- 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)
then we give name
to our project
next a new tab will
be open that contain the microcontroller in order to make configuration and
setup
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)
And finally we set the clock configuration of the microcontroller and going to save and generate the code
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:
and our board Now is alive (code uplaoded) and the led will blink every 500 ms