Difference between revisions of "Getting started with EXTI"

[unchecked revision] [quality revision]
m
m

This article explains the EXTI and its use, with examples.

1 External interrupt/event controller (EXTI)[edit]

The external interrupt/event controller consists of up to 23 edge detectors for generating event/interrupt requests. Each input line can be independently configured to select the type (interrupt or event) and the corresponding trigger event (rising, falling, or both).

2 Configure EXTI to turn on an LED when a user button is pressed[edit]

2.1 Objective[edit]

  • Set up an input pin with EXTI in STM32CubeMX.
  • Generate code in STM32CubeMX and use HAL functions.

2.2 How[edit]

  • Configure a GPIO and EXTI pin in STM32CubeMX and generate code.
  • Add into a project: a callback function, and a function to turn on an LED.
  • Verify the correct functionality by pressing a button that turns on an LED.

2.3 Create project in STM32CubeMX[edit]

  • Menu > File > New Project
  • Select STM32F439ZITx

Select F439.png

  • Configure an LED pin as GPIO_Output (PG14 on NucleoF439ZI). For other boards check their user manual.

LED UM.png LED PIN.png

  • Configure a button pin as GPIO_EXTIX (PA0 on NucleoF439ZI). For other boards check their user manual.

Button.png

  • GPIO configuration check
EXTI config 1.png
  • Enable the interrupt for EXTI
EXTI config 2.png
  • Save your project and generate the code for your IDE.

2.4 HAL Library workflow summary[edit]

EXTI HAL flowchart.png

2.5 Open your IDE[edit]

Create a function to handle the EXTI interrupts:

  • HAL callback function for EXTI: void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  • To turn an LED on we need to use the function: HAL_GPIO_WritePin

Put the functions into main.c

Info white.png Information
Between /* USER CODE BEGIN 4 */ and /* USER CODE END 4 */ tags
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if(GPIO_Pin == GPIO_PIN_0) {
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
  } else {
      __NOP();
  }
}
/* USER CODE END 4 */

2.6 Compile and Flash[edit]

Compile the above code, then load it to the board Flash flash memory.

  • When you press the blue button on your board the LED should must switch on.



This article explains the EXTI and its use, with examples. 
==External interrupt/event controller (EXTI)==
The external interrupt/event controller consists of up to 23 edge detectors for generating
event/interrupt requests. Each input line can be independently configured to select the type
(interrupt or event) and the corresponding trigger event (rising, falling, or both).
==Configure EXTI to turn on an LED when a user button is pressed==
====Objective====
* Set up an input pin with EXTI in STM32CubeMX.

* Generate code in STM32CubeMX and use HAL functions.
====How====
* Configure a GPIO and EXTI pin in STM32CubeMX and generate code.

* Add into a project: a callback function, and a function to turn on an LED.

* Verify the correct functionality by pressing a button that turns on an LED.
====Create project in STM32CubeMX====
* Menu > File > New Project
* Select  STM32F439ZITx

[[File:Select F439.png|600px]]

* Configure an LED pin as GPIO_Output (PG14 on NucleoF439ZI). For other boards check their user manual.
[[File:LED UM.png|300px]]
[[File:LED PIN.png|300px]]

* Configure a button pin as GPIO_EXTIX (PA0 on NucleoF439ZI). For other boards check their user manual.

[[File:Button.png|600px]]
*GPIO configuration check
[[File:EXTI config 1.png|600px|none]]
*Enable the interrupt for EXTI
[[File:EXTI config 2.png|600px|none]]
*Save your project and generate the code for your IDE.
==== HAL Library workflow summary ====
[[File:EXTI HAL flowchart.png|600px]]

====Open your IDE====

Create a function to handle the EXTI interrupts:

* HAL callback function for EXTI: void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
* To turn an LED on we need to use the function: HAL_GPIO_WritePin

Put the functions into main.c

{{Info|Between '''/* USER CODE BEGIN 4 */''' and '''/* USER CODE END 4 */''' tags}}<syntaxhighlight lang="c">

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  if(GPIO_Pin == GPIO_PIN_0) {
    HAL_GPIO_WritePin(GPIOG, GPIO_PIN_14, GPIO_PIN_SET);
  } else {
      __NOP();
  }
}
/* USER CODE END 4 */</syntaxhighlight>


====Compile and Flash====

Compile the above code, then load it to the board Flashflash memory.

*When you press the blue button on your board the LED shouldmust switch on.<noinclude>

{{ArticleBasedOnModel | Example STM32 features/examples article}}
{{PublicationRequestId | 23752 | 2022-06-13 |}}
{{PublicationRequestId | 17438 | 2020-09-21 | xxxxxxx}}
[[category:Getting_started_with_STM32_system_peripherals | 20]]</noinclude>
Line 6: Line 6:
 
==Configure EXTI to turn on an LED when a user button is pressed==
 
==Configure EXTI to turn on an LED when a user button is pressed==
 
====Objective====
 
====Objective====
* Set up an input pin with EXTI in STM32CubeMX
+
* Set up an input pin with EXTI in STM32CubeMX.
 
* Generate code in STM32CubeMX and use HAL functions.
 
* Generate code in STM32CubeMX and use HAL functions.
 
====How====
 
====How====
* Configure a GPIO and EXTI pin in STM32CubeMX and generate code
+
* Configure a GPIO and EXTI pin in STM32CubeMX and generate code.
* Add into a project: a callback function, and a function to turn on an LED
+
* Add into a project: a callback function, and a function to turn on an LED.
 
* Verify the correct functionality by pressing a button that turns on an LED.
 
* Verify the correct functionality by pressing a button that turns on an LED.
 
====Create project in STM32CubeMX====
 
====Create project in STM32CubeMX====
Line 58: Line 58:
 
====Compile and Flash====
 
====Compile and Flash====
   
Compile the above code, then load it to the board Flash memory.
+
Compile the above code, then load it to the board flash memory.
   
*When you press the blue button on your board the LED should switch on.
+
*When you press the blue button on your board the LED must switch on.
 
<noinclude>
 
<noinclude>
 
{{ArticleBasedOnModel | Example STM32 features/examples article}}
 
{{ArticleBasedOnModel | Example STM32 features/examples article}}