0% found this document useful (0 votes)
7 views35 pages

Chapter2-STM32_GPIOdriverHAL

This document provides an overview of GPIO programming using the STM32 HAL library, detailing the APIs available for GPIO initialization, reading, writing, and toggling pins. It explains the structure and parameters of GPIO_InitTypeDef, as well as how to enable and reset GPIO clocks. Additionally, it includes example code for configuring and controlling GPIO pins to operate LEDs and buttons.

Uploaded by

Adel 159
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)
7 views35 pages

Chapter2-STM32_GPIOdriverHAL

This document provides an overview of GPIO programming using the STM32 HAL library, detailing the APIs available for GPIO initialization, reading, writing, and toggling pins. It explains the structure and parameters of GPIO_InitTypeDef, as well as how to enable and reset GPIO clocks. Additionally, it includes example code for configuring and controlling GPIO pins to operate LEDs and buttons.

Uploaded by

Adel 159
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/ 35

CHAPITRE-II

STM32
Programmation GPIO par HAL

1
PLAN

■ Introduction

■ Les APIs du GPIOs

■ Activation des GPIOs par RCC

■ Programmation

2
Introduction

3
Objectifs

■ Accéder aux registres GPIO (ou Autres)


comme prédéfinis dans la bibliothèque HAL
STM32

■ Comprendre les APIs et les utiliser pour


programmer les GPIOs

■ Générer un projet de configuration par le


logiciel STM32 Cube-MX
4
Présentation des APIs GPIAO
■ Les APIs GPIO du HAL sont les suivants:
■ HAL_GPIO_Init()
■ HAL_GPIO_DeInit()
■ HAL_GPIO_ReadPin()
■ HAL_GPIO_WritePin()
■ HAL_GPIO_TogglePin ().
■ En plus des modes standards (input, output, analog), chaque broche
(PINE) peut être configurée en mode
■ EXTI
■ EVENT.
■ Lorsque le mode EXTI est utilisé:
■ L’utilisateur doit appeler la fonction HAL_GPIO_EXTI_IRQHandler() dans le
fichier stm32f4xx_it.c
■ L’utilisateur doit implémenter la fonction HAL_GPIO_EXTI_Callback()
5
HAL_GPIO_INIT

6
Description du HAL_GPIO_Init
Function void HAL_GPIO_Init (GPIO_TypeDef * GPIOx,
Name GPIO_InitTypeDef * GPIO_Init)

Function Initializes the GPIOx peripheral according to the specified


Description parameters in the GPIO_Init

➢ GPIOx: where x can be (A..I) to select the GPIO peripheral

Parameters ➢ GPIO_Init: pointer to a GPIO_InitTypeDef structure that


contains the configuration information for the specified GPIO
peripheral.

Return none
values

7
GPIO_InitTypeDef
➢ uint32_t GPIO_InitTypeDef::Pin
Specifies the GPIO pins to be configured. This parameter can be any
value of GPIO_pins_define
typedef struct {
➢ uint32_t GPIO_InitTypeDef::Mode
uint32_t Pin; Specifies the operating mode for the selected pins. This parameter can
uint32_t Mode; be a value of GPIO_mode_define
uint32_t Pull;
➢ uint32_t GPIO_InitTypeDef::Pull
uint32_t Speed; Specifies the Pull-up or Pull-Down activation for the selected pins. This
uint32_t Alternate; parameter can be a value of GPIO_pull_define
}GPIO_InitTypeDef; ➢ uint32_t GPIO_InitTypeDef::Speed
Specifies the speed for the selected pins. This parameter can be a value
of GPIO_speed_define
➢ uint32_t GPIO_InitTypeDef::Alternate
Peripheral to be connected to the selected pins. This parameter can be a
value of GPIO_Alternate_function_selection

8
GPIO_InitTypeDef::PIN
#define GPIO_PIN_0 ((uint16_t)0x0001U)
#define GPIO_PIN_1 ((uint16_t)0x0002U)
typedef struct { #define GPIO_PIN_2 ((uint16_t)0x0004U)
uint32_t Pin; #define GPIO_PIN_3 ((uint16_t)0x0008U)
uint32_t Mode; #define GPIO_PIN_4 ((uint16_t)0x0010U)
#define GPIO_PIN_5 ((uint16_t)0x0020U)
uint32_t Pull;
#define GPIO_PIN_6 ((uint16_t)0x0040U)
uint32_t Speed;
#define GPIO_PIN_7 ((uint16_t)0x0080U)
uint32_t Alternate; #define GPIO_PIN_8 ((uint16_t)0x0100U)
}GPIO_InitTypeDef; #define GPIO_PIN_9 ((uint16_t)0x0200U)
#define GPIO_PIN_10 ((uint16_t)0x0400U)
#define GPIO_PIN_11 ((uint16_t)0x0800U)
#define GPIO_PIN_12 ((uint16_t)0x1000U)
#define GPIO_PIN_13 ((uint16_t)0x2000U)
#define GPIO_PIN_14 ((uint16_t)0x4000U)
#define GPIO_PIN_15 ((uint16_t)0x8000U)
#define GPIO_PIN_All ((uint16_t)0xFFFFU)

9
GPIO_InitTypeDef::MODE typedef struct {
uint32_t Pin;
uint32_t Mode;
uint32_t Pull;
uint32_t Speed;
uint32_t Alternate;
}GPIO_InitTypeDef;

#define GPIO_MODE_INPUT ((uint32_t)0x00000000U)


#define GPIO_MODE_OUTPUT_PP ((uint32_t)0x00000001U)
#define GPIO_MODE_OUTPUT_OD ((uint32_t)0x00000011U)
#define GPIO_MODE_AF_PP ((uint32_t)0x00000002U)
#define GPIO_MODE_AF_OD ((uint32_t)0x00000012U)
#define GPIO_MODE_ANALOG ((uint32_t)0x00000003U)
#define GPIO_MODE_IT_RISING ((uint32_t)0x10110000U)
#define GPIO_MODE_IT_FALLING ((uint32_t)0x10210000U)
#define GPIO_MODE_IT_RISING_FALLING ((uint32_t)0x10310000U)
#define GPIO_MODE_EVT_RISING ((uint32_t)0x10120000U)
#define GPIO_MODE_EVT_FALLING ((uint32_t)0x10220000U)
#define GPIO_MODE_EVT_RISING_FALLING ((uint32_t)0x10320000U)
10
GPIO_InitTypeDef::Pull
typedef struct {
uint32_t Pin;
uint32_t Mode;
uint32_t Pull;
uint32_t Speed;
uint32_t Alternate;
}GPIO_InitTypeDef;

/* No Pull-up or Pull-down */
#define GPIO_NOPULL ((uint32_t)0x00000000U )
/* Pull-up activation */
#define GPIO_PULLUP ((uint32_t)0x00000001U )
/*Pull-down activation */
#define GPIO_PULLDOWN ((uint32_t)0x00000002U)

11
GPIO_InitTypeDef::SPEED
typedef struct {
uint32_t Pin;
uint32_t Mode;
uint32_t Pull;
uint32_t Speed;
uint32_t Alternate;
}GPIO_InitTypeDef;

/*1- IO works at 2 MHz */


#define GPIO_SPEED_FREQ_LOW ((uint32_t)0x00000000U)

/*2- range 12,5 MHz to 50 MHz,→ F407= 25Mhz */


#define GPIO_SPEED_FREQ_MEDIUM ((uint32_t)0x00000001U)

/*3- range 25 MHz to 100 MHz , → F407= 50Mhz */


#define GPIO_SPEED_FREQ_HIGH ((uint32_t)0x00000002U)

/*4- range 50 MHz to 200 MHz, → F407= 100Mhz */ */


#define GPIO_SPEED_FREQ_VERY_HIGH ((uint32_t)0x00000003U)
12
GPIO_InitTypeDef:: Alternate
GPIO_AF0_RTC_50Hz
typedef struct {
GPIO_AF0_MCO
uint32_t Pin;
GPIO_AF0_TAMPER
uint32_t Mode;
GPIO_AF0_SWJ
uint32_t Pull;
GPIO_AF0_TRACE
uint32_t Speed;
GPIO_AF1_TIM1
uint32_t Alternate; ……
}GPIO_InitTypeDef; GPIO_AF3_TIM11
GPIO_AF4_I2C1
….
GPIO_AF4_I2C3
GPIO_AF5_SPI1
✓Peripheral to be connected to the selected pins. GPIO_AF5_SPI2
✓ Possible values: GPIO_AFx_PPP, where ……

✓ AFx: is the alternate function index


✓ PPP: is the peripheral instance
✓Example: use GPIO_AF1_TIM1 to connect TIM1 IOs on AF1.
These values are defined in the GPIO extended driver, since the AF mapping
may change between product lines.
13
Exemples d’utilisation de HAL_GPIO_Init
1) Configurer GPIOs en output push-pull pour commander les LEDs connectées
aux broches 12, 13,14 et 15
GPIO_InitTypeDef x;
x.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
x.Mode = GPIO_MODE_OUTPUT_PP;
x.Pull = GPIO_NOPULL;
x.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &x);
2) Configurer PA0 en entrée reliée au bouton user
x.Mode = GPIO_MODE_INPUT;
x.Pull = GPIO_PULLDOWN;
x.Pin = GPIO_PIN_0;
HAL_GPIO_Init(GPIOA, &x);
3) Configurer USART3 Tx (PC10, mapped on AF7) en alternate function:
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
14
HAL_GPIO_DeInit()

15
HAL_GPIO_DeInit

▪ Cette fonction permet de remettre la broche


GPIO_Pin passée en paramètre du port GPIOx à
l’état de reset.

void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx,


uint32_t GPIO_Pin)

* Paramètre1: GPIOx: where x can be (A..I)


* Paramètre2: GPIO_Pin: specifies the port bit to read.
GPIO_PIN = GPIO_PIN_x where x can be (0..15).

16
HAL_GPIO_ReadPin()

17
HAL_GPIO_ReadPin
GPIO_PinState HAL_GPIO_ReadPin( GPIO_TypeDef*
GPIOx,
uint16_t GPIO_Pin)

typedef enum
{
GPIO_PIN_RESET = 0,
GPIO_PIN_SET
}GPIO_PinState;

/**
* Description: lit l’état de broche configurée en entrée.
* Paramètres1: GPIOx: where x can be (A..I)
* Paramètres2: GPIO_Pin: specifies the port bit to read.
GPIO_PIN = GPIO_PIN_x where x can be (0..15).
* Retour : GPIO_PinState = The input port pin value.
*/

18
HAL_GPIO_WritePin()

19
HAL_GPIO_WritePin
void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx,
uint16_t GPIO_Pin,
GPIO_PinState PinState)

/**
Description: Sets or clears the selected data port bit.
* @param1: GPIOx: where x can be (A..I)
* @param2: GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @param3: PinState: specifies the value to be written to the selected bit.
* This parameter can be one of the GPIO_PinState enum values:
* @arg GPIO_PIN_RESET: to clear the port pin
* @arg GPIO_PIN_SET: to set the port pin
* @retval None
*/

20
Code de la fonction
Remarque: This function uses GPIOx_BSRR register to allow atomic
read/modify accesses. In this way, there is no risk of an IRQ occurring
between the read and the modify access.

void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t


GPIO_Pin, GPIO_PinState PinState)
{

if(PinState != GPIO_PIN_RESET)
{
GPIOx->BSRR = GPIO_Pin;
}
else
{
GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
}
}

21
HAL_GPIO_TogglePin()

22
HAL_GPIO_TogglePin

void HAL_GPIO_TogglePin( GPIO_TypeDef* GPIOx,


uint16_t GPIO_Pin)
{

GPIOx->ODR ^= GPIO_Pin;
}

/**
Description: Toggles the specified GPIO pins..
* @param1: GPIOx: where x can be (A..I)
* @param2: GPIO_Pin: specifies the port bit to be written.
* This parameter can be one of GPIO_PIN_x where x can be (0..15).
* @retval None
*/

23
Activation des horloges des GPIO

24
Les Macros d’activation/reset

➢ Un ensemble de macros est défini dans le fichier


stm32f4xx_hal_rcc.h. Ces macros permettent d’exécuter des
opérations élémentaires dans les registres de RCC.

▪ __PPP_CLK_ENABLE: enable the peripheral(PPP) clock


▪ __PPP_CLK_DISABLE: disable the peripheral PPP clock
▪ __PPP_FORCE_RESET: force peripheral reset
▪ __PPP_RELEASE_RESET: release peripheral reset

➢ Les registres utilisés par ces macros sont AHB1RSTR et


AHB1ENR pour les GPIO

25
__PPP_CLK_ENABLE()/
__PPP_CLK_DISABLE
#define __HAL_RCC_GPIOA_CLK_ENABLE() do { \
SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOAEN); \
}while(0)

#define __HAL_RCC_GPIOA_CLK_DISABLE() \
(RCC->AHB1ENR &= ~(RCC_AHB1ENR_GPIOAEN))

Pour chaque GPIO ces deux macros sont aussi définies.

26
__PPP_FORCE_RESET() et
__PPP_RELEASE_RESET()
#define __HAL_RCC_GPIOB_FORCE_RESET() \
(RCC->AHB1RSTR |= (RCC_AHB1RSTR_GPIOBRST))

#define __HAL_RCC_GPIOH_RELEASE_RESET() \
(RCC->AHB1RSTR &= ~(RCC_AHB1RSTR_GPIOHRST))

Ces deux macros sont aussi définies pour tous les GPIOs

27
PROGRAMMATION AVEC LES APIS

28
Exemple

■ Clignoter la diode LED connectée à PD12 en


utilisant les registres prédéfinis dans le HAL

29
Exemple …code
int main(void) {

GPIO_InitTypeDef ma_structure;

__HAL_RCC_GPIOD_FORCE_RESET();
__HAL_RCC_GPIOD_RELEASE_RESET();
__HAL_RCC_GPIOD_CLK_ENABLE();

ma_structure.Pin = GPIO_Pin_12;
ma_structure.Mode = GPIO_MODE_OUTPUT_PP;
ma_structure.Pull = GPIO_NOPULL;
ma_structure.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOD, &ma_structure);

30
Suite ..
int main(void) {
GPIO_InitTypeDef ma_structure;
…………
…………
HAL_GPIO_Init(GPIOD, &ma_structure);

HAL_GPIO_WritePin( GPIOD, GPIO_Pin_12, GPIO_PIN_SET);


HAL_Delay(500);
for(;;){
HAL_GPIO_TogglePin(GPIOD, GPIO_Pin_12);
HAL_Delay(300);
}
}

31
Clignotez les 4 LEDS en même
temps en utilisant le HAL ?
Défiler les 4 leds en utilisant le
HAL ?
En utilisant le HAL, allumer la led
PD12 tant que le bouton user est
appuyé?
En utilisant le HAL, inverser les
leds deux par deux à chaque
appuie sur le bouton user?

You might also like