Vous êtes sur la page 1sur 32

Chapitre 4 : Programmation

des Interruptions Externes


(EXTI)

Platforme : STM32F4, API: HAL


Objectifs :

 Expliquer le principe de EXTI:


 Autoriser ou interdire des interruptions externes
 Configurer les fronts (montant/descendant) d’une source
externe d’interruption
 Manipulation des drapeaux relatifs au contrôleur
d’interruption

 Développer une application

1 EXTI with HAL


Registres et principe de
fonctionnement

Partie 1
Principe de Fonctionnement

 Interruption = coupure de l’exécution séquentielle de la fonction


principale ( main) pour l’exécution d’une fonction spéciale
appelée routine d’interruption.

 L’adresse de cette routine est prédéfinie par le système suivant la


nature de l’interruption

 Dans ce chapitre on traitera les interruptions de nature externe


(EXTI) qui sont survenues suite à un front détecté au niveau d’une
broche d’entrée.

3 EXTI with HAL


Principe de Fonctionnement

 Pour programmer un MCU en mode interruption, utiliser des


registres spéciaux afin de :

 Définir la source d’interruption

 Configurer la source d’interruption

 Autoriser cette source d’interruption

 Écrire le code de la routine d’interruption

4 EXTI with HAL


NVIC: Nested vectored interrupt controller

 C’est le vecteur d’interruption, il permet :


 La configuration de 82 interruptions masquables pour le
STM32F405xx/07xx

 La gestion de 16 lignes d’interruption

 La gestion de 16 niveau de priorité (4 bits de configuration)

5 EXTI with HAL


Schéma fonctionnel

Pending Request Interrupt Mask Rising Trigger Falling Trigger


Software Interrupt
Register Register Selection Register Selection Register
Event Register
(EXTI_PR) (EXTI_IMR) (EXTI_RTSR) (EXTI_FTSR)

Edge Detect

EXTI[15:0]
Circuit
To NVIC

6 EXTI with HAL


Interruption externe: EXTI
• Chaque GPIO peut être utilisé comme entrée d’interruption
• On peut gérer 23 sources d’interruption différentes Matériel/
Événement
• Déclenchement sur front montant ou descendant ou les deux en
même temps.
• Une source logicielle pourrait être configurée pour déclencher un
évènement d’interruption
• Toute interruption externe peut être masquée
• Chaque interruption est associée à un bit d’état (drapeau)
indiquant l’état de l’interruption. Ce bit doit être remis à 0 pour
autoriser une deuxième fois l’interruption( dans le pending
request register)
7 EXTI with HAL
Interruption externe: EXTI : 23 sources

adresses des vecteurs


d’interruption pour les
EXTI
8 EXTI with HAL
Interruption externe: EXTI : 23 sources

 Les 7 autres lignes d’interruption sont :


 EXTI ligne 16 est reliée à PVD output (Programmable voltage
detector)
 EXTI ligne 17 est reliée à RTC Alarm event
 EXTI ligne 18 est reliée à USB OTG FS Wakeup event
 EXTI ligne 19 est reliée à Ethernet Wakeup event
 EXTI ligne 20 est reliée à USB OTG HS Wakeup event
 EXTI ligne 21 est reliée à RTC Tamper et TimeStamp events
 EXTI ligne 22 est reliée à RTC Wakeup event

9 EXTI with HAL


Comment programmer une interruption

1. Activer l’horloge de l’AFIO: bit 0 de RCC_APB2ENR


2. Sélectionner la ligne d’interruption par configuration de
l’un des 4 registres:
a) AFIO_EXTICR1 pour connecter la source externe (pine GPIO) vers EXTI0 ou EXTI1 ou EXTI2 ou
EXTI3
b) AFIO_EXTICR2 pour connecter la source externe (pine GPIO) vers EXTI4 ou EXTI5 ou EXTI6 ou
EXTI7
c) AFIO_EXTICR3 pour connecter la source externe (pine GPIO) vers EXTI8 ou EXTI9 ou EXTI10 ou
EXTI11
d) AFIO_EXTICR4 pour connecter la source externe (pine GPIO) vers EXTI12 ou EXTI13 ou EXTI14 ou
EXTI15

3. Autoriser l’interruption par le registre EXTI_IMR


4. Choisir le/les fronts de la source par les registres
EXTI_RTSR et EXTI_FTSR
5. Connecter l’interruption au NVIC
10 EXTI with HAL
Sélection d’une ligne d’interruption

11 EXTI with HAL


Sélection d’une ligne d’interruption

12 EXTI with HAL


Sélection d’une ligne d’interruption

13 EXTI with HAL


Sélection d’une ligne d’interruption

14 EXTI with HAL


Les Registres de EXTI
Interrupt mask register (EXTI_IMR)
Address offset: 0x00h

Reset Value : 0x00000000

Bits N° Name Description

Bits [31:23] Reserved must be kept at reset value (0)

MRx: Interrupt Mask on line x 0: Interrupt request from Line x is masked


Bits[22:0]
1: Interrupt request from Line x is not masked

15 EXTI with HAL


Les Registres de EXTI
Rising trigger selection register (EXTI_RTSR)
Address offset: 0x08h

Reset Value : 0x00000000

Bits N° Name Description

Bits [31:23] Reserved must be kept at reset value (0)

TRx: Rising trigger event configuration bit 0: Rising trigger disabled for input line.
Bits[22:0]
of line x 1: Rising trigger enabled for input line.

16 EXTI with HAL


Les Registres de EXTI
Falling trigger selection register (EXTI_FTSR)
Address offset: 0x0C

Reset Value : 0x00000000

Bits N° Name Description

Bits [31:23] Reserved ,must be kept at reset value (0)

TRx: Falling trigger event configuration bit 0: Falling trigger disabled for input line.
Bits[22:0]
of line x 1: Falling trigger enabled for input line.

17 EXTI with HAL


Les Registres de EXTI
Pending register (EXTI_PR): Etats des interruptions en cours, l’utilisateur
doit effacer le drapeu correspondant après chaque interruption
Address offset: 0x14h

Reset Value : undefined


Bits N° Name Description

Bits [31:18] Reserved ,must be kept at reset value (0)

0: No trigger request occurred


1: selected trigger request occurred
Bits[17:0] PRx: Pending bit This bit is set when the selected edge event arrives on the external
interrupt line. This bit is cleared by writing a 1 into the bit or by changing
the sensitivity of the edge detector.

18 EXTI with HAL


Les Registres de EXTI

19 EXTI with HAL


Le APIs de l’EXTI

Source files : stm32f4xx_hal_gpio.c


stm32f4xx_it.c
stm32f4xx_hal_msp.c
stm32f4xx_hal_cortex.c

20 EXTI with HAL


Logique de fonctionnement par HAL

1) Configurer la broche en mode EXTI dans la function


HAL_GPIO_Init
2) Relier la ligne d’interruption à NVIC par les deux fonctions
a. HAL_NVIC_SetPriority
b. HAL_NVIC_EnableIRQ
3) Implémenter la routine d’interruption dans le fichier main.c
HAL_GPIO_EXTI_Callback(). Cette fonction est
imlplémentée en weak dans le fichier stm32f4xx_hal_gpio.c:
__weak void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
4) On peut ne pas toucher à cette fonction et implémenter la
routine d’interruption dans le fichier stm32f4xx_it.c

21 EXTI with HAL


1) HAL_GPIO_Init et Mode exti

 Même fonction que celle du chapitre GPIO


 Pour configurer une pine en entrée EXTI (exemple PA0)
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 Vérifier le traitement des registres dans le fichier


stm32f4xx_hal_gpio.c

22 EXTI with HAL


2) HAL_NVIC_SetPriority

 void HAL_NVIC_SetPriority(IRQn_Type IRQn,


uint32_t PreemptPriority,
uint32_t SubPriority)
 IRQn: External interrupt number.
 * This parameter can be an enumerator of IRQn_Type enumeration
 * (For the complete STM32 Devices IRQ Channels list, please refer to stm32f4xxxx.h
 PreemptPriority: The preemption priority for the IRQn channel.
 * This parameter can be a value between 0 and 15
 * A lower priority value indicates a higher priority
 SubPriority: the subpriority level for the IRQ channel.
 * This parameter can be a value between 0 and 15
 * A lower priority value indicates a higher priority.

23 EXTI with HAL


void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)

 void HAL_NVIC_EnableIRQ(IRQn_Type IRQn)


 Enables a device specific interrupt in the NVIC interrupt controller.
 IRQn: External interrupt number. This parameter can be an
enumerator of IRQn_Type enumeration
 Remarque: To configure interrupts priority correctly, the
HAL_NVIC_SetPriorityGrouping() function should be called
before.
 Le HAL configure le groupe par la fonction HAL_Init() définit dans
stm32f4xx_hal.c qui à son tour appelle la fonction HAL_MspInit.
 La fonction HAL_MspInit appelle la fonction
HAL_NVIC_SetPriorityGrouping qui fixe le groupe par défaut à 4.

24 EXTI with HAL


Interrupt priority grouping

 To increase priority control in systems with interrupts, the


NVIC supports priority grouping. This divides each interrupt
priority register entry into two fields:
 Only the group priority determines preemption of interrupt exceptions.
When the processor is executing an interrupt exception handler,
another interrupt with the same group priority as the interrupt being
handled does not preempt the handler,
 If multiple pending interrupts have the same group priority, the
subpriority field determines the order in which they are processed. If
multiple pending interrupts have the same group priority and subpriority,
the interrupt with the lowest IRQ number is processed first.
 Pour le deuxième cas penser à utiliser le groupe 4:
 HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_4);

25 EXTI with HAL


void EXTI0_IRQHandler(void)

 Cette fonction doit être définit dans le fichier stm32f4xx_it.c


 void EXTI0_IRQHandler(void)
 {
 HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
 }

 Le traitement de l’interruption peut être définit dans cette


fonction ou dans la fonction HAL_GPIO_EXTI_Callback qui est
appelée par la fonction HAL_GPIO_EXTI_IRQHandler

26 EXTI with HAL


Exemple

PA0 source d’interruption


Programme principal clignote la diode LED verte (PD12)
A chaque interruption, les autres LED changent d’état

27 EXTI with HAL


Exemple complet: PA0 entrée EXTI

void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_NVIC_Init(void);
int main(void) {
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_NVIC_Init();
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_12, GPIO_PIN_SET);
while (1) {
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_12);
HAL_Delay(500);
}
}
28 EXTI with HAL
Suite … static void MX_NVIC_Init(void)

static void MX_NVIC_Init(void)


{
/* EXTI0_IRQn interrupt configuration */
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
}

29 EXTI with HAL


Suite … void MX_GPIO_Init(void){
static void MX_GPIO_Init(void){
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin : PA0 */
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PA12 … */
GPIO_InitStruct.Pin = PIO_PIN_12|GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
30 EXTI with HAL
Call back function

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)


{
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_14);
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15);
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_13);
}

31 EXTI with HAL

Vous aimerez peut-être aussi