0% found this document useful (0 votes)
67 views25 pages

CH2 - STM32 Periperal - GPIO - AF

Uploaded by

Chaÿma Sãoūdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views25 pages

CH2 - STM32 Periperal - GPIO - AF

Uploaded by

Chaÿma Sãoūdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter 2 : STM32 MCU:

peripherals and software


programming

Hanene Ben Fradj


ING 2 -ISI
Mise à jour : october 2023
OUTLINE
⚫ The STM32F40x features and peripherals
⚫ STM32 peripherals :
1. General Purpose Input/Output :
⚫ GPIO

⚫ Alternate function

⚫ EXTI ( EXTernal Interrupt)

⚫ STM32 Programming

2
2
STM32F4xx Block Diagram
▪ ARM 32-bit Cortex-M 4 CPU +FPU+MPU
up to 168 MHz
CORTEX-M4 AHB2 (max 168MHz)
▪ Nested Vectored Interrupt Controller (NVIC) w/ 43 D-bus Encryption**
CPU + FPU +
maskable IT + 16 prog. priority levels MPU I-bus

Flash I/F
▪ Embedded Memories : 168 MHz 512kB- 1MB Camera Interface

ARM ® 32-bit multi-AHB bus matrix


Flash Memory
▪ FLASH: up 1Mbytes S-bus
USB 2.0 OTG FS
▪ SRAM: up 128Kbytes
128KB SRAM
▪ CRC calculation unit
JTAG/SW Debug External Memory
▪ 16 Channels DMA Power Supply
ETM Interface Reg 1.2V

Arbiter (max 150MHz)


Power Supply with internal regulator and low
POR/PDR/PVD
power modes : Nested vect IT Ctrl
USB 2.0 OTG FS/HS
▪ 2V to 3V6 supply XTAL oscillators
1 x Systic Timer 32KHz + 8~25MHz
▪ 4 Low Power Modes with Auto Wake-up Ethernet MAC
▪ Integrated Power On Reset (POR)/Power Down DMA 10/100, IEEE1588 Int. RC oscillators
16 Channels 32KHz + 16MHz
Reset (PDR) + Programmable voltage detector
(PVD) Bridge APB1 (max 42MHz) PLL
▪ Backup domain w/ 20B reg Clock Control AHB1
RTC / AWU
▪ Up to 72 MHz frequency managed & monitored by (max 168MHz)
5x 16-bit Timer
the Clock Control 51/82/114/140 I/Os
Bridge 4KB backup RAM
▪ Rich set of peripherals & IOs 2x 32-bit Timer
2x6x 16-bit PWM
▪ Embedded low power RTC with VBAT capability Synchronized AC Timer 2x DAC + 2 Timers
2x Watchdog

APB2 (max 84MHz)


▪ Dual Watchdog Architecture (independent & window)
3 x 16bit Timer 2x CAN 2.0B
▪ 17 Timers w/ advanced control features
(including Cortex SysTick) Up to 16 Ext. ITs 1x SDIO
2 x SPI / I2S
▪ Up to 140 I/Os (100 pin package) w/ 16 external
3x 12-bit ADC
interrupts/event 1 x SPI 24 channels / 2Msps 4x USART/LIN
▪ Up to 3x12-bits 2Msps ADC w/ up to 24 channels
▪ Embedded temperature sensor w/ +/-1.5° 2 x USART/LIN Temp Sensor 3x I2C
linearity with T°
STM32F4xx Block Diagram
▪ Up to 15 communication
CORTEX-M4 AHB2 (max 168MHz)
interfaces CPU + FPU +
D-bus Encryption**

– Up to 3 × I2C interfaces MPU I-bus

Flash I/F
168 MHz 512kB- 1MB Camera Interface

ARM ® 32-bit multi-AHB bus matrix


– Up to 4 USARTs/2 UARTs S-bus
Flash Memory

– Up to 3 SPIs (37.5 Mbits/s), 128KB SRAM


USB 2.0 OTG FS

– 2 × CAN interfaces (2.0B Active) JTAG/SW Debug External Memory Power Supply
– SDIO interface ETM Interface Reg 1.2V

Arbiter (max 150MHz)


■ Advanced connectivity Nested vect IT Ctrl
POR/PDR/PVD
USB 2.0 OTG FS/HS
– USB 2.0 full-speed device 1 x Systic Timer
XTAL oscillators
32KHz + 8~25MHz
– USB 2.0 high-speed/full-speed DMA
Ethernet MAC
Int. RC oscillators
10/100, IEEE1588
device 16 Channels 32KHz + 16MHz

– 10/100 Ethernet MAC■ 8- to Clock Control


Bridge APB1 (max 42MHz) PLL
AHB1
14-bit parallel camera interface up (max 168MHz)
RTC / AWU
5x 16-bit Timer
to 51/82/114/140 I/Os
4KB backup RAM
Bridge
54 Mbytes/s 2x6x 16-bit PWM
2x 32-bit Timer
Synchronized AC Timer 2x DAC + 2 Timers
2x Watchdog

APB2 (max 84MHz)


(independent & window)
3 x 16bit Timer 2x CAN 2.0B

Up to 16 Ext. ITs 1x SDIO


2 x SPI / I2S
3x 12-bit ADC
1 x SPI 24 channels / 2Msps 4x USART/LIN

2 x USART/LIN Temp Sensor 3x I2C


STM32 F4 portfolio 5

STM32F407VG :
• 1Mbyte de Flash
• 192 kB de SRAM
• 100 pins LQFP
(Quad Flat Package)
OUTLINE
⚫ The STM32F40x Features and peripherals
⚫ STM32 peripheral I/O blocs:

⚫ General Purpose Input output : GPIO

⚫ Alternate function

⚫ EXTINT
⚫ STM32 Software Programming

6
6
STM32F4xx Block Diagram 7

CORTEX-M4 AHB2 (max 168MHz)


D-bus Encryption**
CPU + FPU +
MPU I-bus

Flash I/F
168 MHz 512kB- 1MB Camera Interface

ARM ® 32-bit multi-AHB bus matrix


Flash Memory
S-bus
USB 2.0 OTG FS
128KB SRAM
JTAG/SW Debug External Memory Power Supply
- Un ensemble de GPIO ETM Interface Reg 1.2V

Arbiter (max 150MHz)


POR/PDR/PVD
- Le nombre de pin dépend Nested vect IT Ctrl
USB 2.0 OTG FS/HS
XTAL oscillators
de la référence exacte du 1 x Systic Timer 32KHz + 8~25MHz
Ethernet MAC
microcontrôleur STM32F4 DMA
16 Channels
10/100, IEEE1588 Int. RC oscillators
32KHz + 16MHz

Bridge APB1 (max 42MHz) PLL


Clock Control AHB1
(max 168MHz)
RTC / AWU
5x 16-bit Timer
51/82/114/140 I/Os
Bridge 4KB backup RAM
2x 32-bit Timer
2x6x 16-bit PWM
Synchronized AC Timer 2x DAC + 2 Timers
2x Watchdog

APB2 (max 84MHz)


(independent & window)
3 x 16bit Timer 2x CAN 2.0B

Up to 16 Ext. ITs 1x SDIO


2 x SPI / I2S
3x 12-bit ADC
1 x SPI 24 channels / 2Msps 4x USART/LIN

2 x USART/LIN Temp Sensor 3x I2C


C'est par ces broches que
transitent toutes les
communications avec
l'extérieur, sous la forme
de tension

8
GPIO : Définition et fonction
⚫ jusqu’à 9 GPIO (ports ): GPIOA …GPIOI

⚫ Un GPIO gère un ensemble de 16 broches ou pin ( PA0, PA1, ….PA15 sont les 16
broches du GPIOA)

⚫ Les broches de ces ports peuvent donc indépendamment être configurées :


⚫ en entrée
⚫ en sortie,
⚫ En mode analogique
⚫ En alternate function ( liés à des périphériques du MCU : USART, SPI, Timers,…)
⚫ avec différentes options (résistances de rappel: pull-up, pull-down,...).

⚫ La configuration ainsi que l’état logique de ces broches est obtenue par des
opérations d’écriture (write) ou de lecture (read) dans différents registres
associés à chaque port.
⚫ On trouve généralement :
⚫ Des registres de configuration: pour configurer les pin d’un GPIO en entrée ou en
sortie avec plusieurs options
⚫ Des registres de donnée recopiant les états logiques de chaque broche de port

9
STM32 GPIOs Registers 10

⚫ To use GPIOs, we should configure the following registers:


⚫ Configuration registers:
⚫ GPIOx_MODER: port mode register :
⚫ GPIOx_OTYPER: port output type register
Written by software
⚫ GPIOx_OSPEEDR: port output speed register
⚫ GPIOx_PUPDR: port pull-up/pull-down register

⚫ Data register :
⚫ GPIOx_IDR: Port input data register : captures the data present on the I/O pin at every
AHB clock cycle.
⚫ GPIOx_ODR: Port output data register

⚫ GPIOx_BSRR : Bit Set/ Reset Register


⚫ GPIOx_BRR : Bit Reset Register

⚫ Locking register:
⚫ GPIOx_LCKR Port configuration locked :Locking mechanism to avoid spurious write in
the IO registers: When the LOCK sequence has been applied on a port bit, it is no
longer possible to modify the configuration of the port bit until the next reset
MODER register: GPIOD🡪MODER

2 bits in the register define


The mode of Pin0 of GPIOD

11
STM32 GPIO Configuration Modes 12

Analog

MODER( OTYPER(i PUPDR(i


i) ) ) I/O configuration
[1:0] [1:0] [1:0] Alternate Function Input
To On-chip Peripherals

0 0 Output Push Pull


0 0 1 Output Push Pull with Pull-up
1 0 Output Push Pull with Pull-down
On Off

Input Data
01

Register
Read 0 VDD VDD or VDD_FT(1)
0 0 Output Open Drain
1 0 1 Output Open Drain with Pull-up
On/Off
1 0 Output Open Drain with Pull-down
Schmitt

Pull - Up
Trigger Input Driver
0 0 Alternate Function Push Pull
0 0 1 Alternate Function PP Pull-up

I/O pin
1 0 Alternate Function PP Pull-down

10 VDD

Pull - Down
0 0 Alternate Function Open Drain
1 0 1 Alternate Function OD Pull-up

Bit Set/Reset

Output Data
1 0 Alternate Function OD Pull-down On/Off
OUTPU

Register
Register
Write T
VSS VSS
0 0 Input floating CONTR
00 x 0 1 Input with Pull-up OL
1 0 Input with Pull-down

Read /
Write VSS Push-Pull
11 x x Analog mode From On-chip Peripherals Output Open Drain
Alternate Function Output Driver

Analog
* In output mode, the I/O speed is configurable through OSPEEDR register: 2MHz,
25MHz, 50MHz or 100 MHz (1) VDD_FT is a potential specific to five-volt tolerant I/Os and different from VDD.
GPIO Configuration: Output type (1/2)

•les 2 transistors PMOS et NMOS sont • Seul le transistor NMOS est commandé,
activés l'autre est maintenu ouvert , la broche ne
• La broche portée au potentiel 0V ou Vdd. peut être portée par le GPIO qu’a Vss.
• Le GPIO impose le potentiel de la broche • si K1 est ouvert 🡪 c’est au circuit extérieur
de fixer le potentiel de la broche 13
GPIO Configuration :Output type (2/2)
⚫ Type Push-Pull :
1. Le processeur fait un write dans le registre ‘Output Data Register’
pour spécifier la valeur à envoyer ‘0’ ou ‘1’.
2. Le GPIO utilisera les 2 transistors pour relier la broche soit:
⚫ à VDD, si le processeur veut envoyer ‘1’: fermer le transistor PMOS et ouvrir le
transistor NMOS)
⚫ à Vss ou la masse, si le processeur veut envoyer ‘0’: ouvrir le transistor PMOS et
fermer le transistor NMOS)
⚫ Type Open Drain :
⚫ Drain laissé ouvert, Drain est le nom du bras du transistor NMOS reliée
à la broche puisque le transistor PMOS est toujours ouvert.
⚫ le GPIO ne peut imposer que le niveau logique ‘0’ par la fermeture du
transistor NMOS.
⚫ Le niveau '1' est donc fixé par le circuit extérieur
⚫ Le GPIO n'est donc pas le seul maitre du potentiel sur la broche. Le
circuit extérieur l'est aussi
⚫ Utilité : connecter plusieurs composants extérieurs sur le même bus (
exemple Bus I2C)

14
Examples : mode: Output /type: Push Pull
Pour allumer la LED, le GPIO a
fermé le transistor PMOS donc
il a lié la LED à VDD et non à la
masse parceque le GPIO a
ouvert aussi le transistor
NMOS

Dans ce montage, pour allumer


la LED, le GPIO a fermé le
transistor NMOS pour relier la
LED à Vss et a ouvert le
transistor PMOS

15
Example: Mode: Output /type: OpenDrain

Wired and

16
GPIO configuration : input mode
Schmitt-Trigger
Schmitt-trigger type of input is designed to accept slow-change signals and
produce an oscillation-free output

5V

0V

VT- VT+ A

▪ (a) If input transition times are too long, a standard logic device-output might oscillate or change
erratically;
▪ (b) a logic device with a Schmitt-trigger type of input will produce clean, fast output transitions.
17
GPIO : floating/ Pull up/ pull down

• c'est le circuit extérieur qui est


totalement maître du potentiel de la • La broche est reliée au VCC par l'intermédiaire
broche. d'une résistance (dite de rappel ou Pull UP).
🡪 si le circuit extérieur est + si le circuit extérieur est déconnecté, le
déconnecté, la broche possède un potentiel de la broche se retrouve à VCC grâce à la
potentiel inconnu (est laissée libre, résistance de rappel ( Pull up).
flottante) 🡪 le circuit extérieur, pour imposer un potentiel,
🡪 proscrire car favorise le captage de doit avoir une résistance de sortie faible devant R,
parasites. sinon, la tension chute. Elle est faussée.
18
Example : résistance de rappel : PU or PD

By choosing Pull Down resistance,


the circuit will read LOW i.e. “0” all the
times and when the switch is pressed
it will start reading HIGH “1”.

VDD

19
STM32 GPIO features 20

⚫ Up to 140 multifunction bi-directional I/O :


⚫ available on biggest package up to 176 pins :
⚫ 9 * 16 –pin port
⚫ GPIOA..GPIOI

⚫ Almost standard I/Os are 5V tolerant:


⚫ Vdd 1.8 à 3.6 V
⚫ Output current of 25 mA
⚫ GPIO connected to AHB bus (168MHz) : max toggling frequency 84 MHz
⚫ Configurable Output Speed up to 100 MHz
⚫ Up to 140 GPIOs can be set-up as external interrupt (up to 16 lines at time)
able to wake-up the MCU from low power modes
⚫ Each pin can work as :
⚫ GPIO
⚫ Alternate fonction
⚫ External Interrupt
Alternate Functions features 21

⚫ Most of the peripherals shares the same pin (like USARTx_Tx, TIMx_CH2,
I2Cx_SCL, SPIx_MISO …)
⚫ Alternate functions multiplexers prevent to have several peripheral’s function
pin to be connected to a specific I/O at a time.
⚫ Some Alternate function pins are remapped to give the possibility to optimize
the number of peripherals used in parallel.
⚫ To optimize the number of peripherals available for the 64-pin or the 100-pin
,144 pin or the 176-pin package, it is possible to remap some alternate functions
to some other pins.
⚫ This is achieved by software, In this case, the alternate functions are no longer
mapped to their original assignations.

AF0 (system)
AF1 (TIM1/2)
AF2 (TIM3..5)

Pin x (0…15)

AF15 (USARTx_Tx)
Alternate Functions features 22
AlternateFunction
⚫ Consulter le fichier
STM32F407VGT6, Table 7 :
alternate function mapping
⚫ Avant configuration de la broche
vérifier si le pin n’est pas déjà
utilisé dans la carte discovery ou
autres. Consulter le fichier le
fichier UM1472.pdf table 5.

23
Alternate Functions features 24

NB: ADC et DAC periph


pins are configured as
analog input mode
Alternate Function: SoftwareProg
/* Configure Pin2 and Pin3 as alternate function */
GPIO_InitStructure.Pin=GPIO_PIN_2|GPIO_PIN_3
GPIO_InitStructure.Mode= GPIO_MODE_AF_PP; // GPIO_MODE_AF_OD
GPIO_InitStructure .Pull= GPIO_PULLDOWN;
GPIO_InitStructure.Speed=GPIO_SPEED_FREQ_MEDIUM;
GPIO_InitStructure.Alternate = GPIO_AF7_USART2 ; // GPIO_AF1_TIM1
HAL_GPIO_Init(GPIOA,& GPIO_InitStructure);

25

You might also like