Library 38- External interrupts for STM32F4

A new library is here. 38th are external interrupts. This library allows you to very easly use external interrupts for your needs with just one function and function handler. I have written everything you need to start.

For more informations on how external interrupts works on STM32F4 with pins and lines and so on, you should look at my external interrupts tutorial. I tried explain there everything as best as I can. I think it’s quite understandable.

I will just say here, that you can connect only one pin to one line at a time. For more, look at tutorial.

So, my library is below.



  • Attach external interrupt on specific pin
  • Detach external interrupt on specific pin
  • Selectable trigger for interrupt
    • Rising edge
    • Falling edge
    • Both edges


    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx EXTI
    • STM32F4xx SYSCFG
    • misc.h
  • TM
    • TM GPIO
    • defines.h
    • attributes.h
Line 0 PA0 PB0 PC0 PD0 PE0 PF0 PG0 PH0 PI0 PJ0 PK0
Line 1 PA1 PB1 PC1 PD1 PE1 PF1 PG1 PH1 PI1 PJ1 PK1
Line 2 PA2 PB2 PC2 PD2 PE2 PF2 PG2 PH2 PI2 PJ2 PK2
Line 3 PA3 PB3 PC3 PD3 PE3 PF3 PG3 PH3 PI3 PJ3 PK3
Line 4 PA4 PB4 PC4 PD4 PE4 PF4 PG4 PH4 PI4 PJ4 PK4
Line 5 PA5 PB5 PC5 PD5 PE5 PF5 PG5 PH5 PI5 PJ5 PK5
Line 6 PA6 PB6 PC6 PD6 PE6 PF6 PG6 PH6 PI6 PJ6 PK6
Line 7 PA7 PB7 PC7 PD7 PE7 PF7 PG7 PH7 PI7 PJ7 PK7
Line 8 PA8 PB8 PC8 PD8 PE8 PF8 PG8 PH8 PI8 PJ8 PK8
Line 9 PA9 PB9 PC9 PD9 PE9 PF9 PG9 PH9 PI9 PJ9 PK9
Line 10 PA10 PB10 PC10 PD10 PE10 PF10 PG10 PH10 PI10 PJ10 PK10
Line 11 PA11 PB11 PC11 PD11 PE11 PF11 PG11 PH11 PI11 PJ11 PK11
Line 12 PA12 PB12 PC12 PD12 PE12 PF12 PG12 PH12 PI12 PJ12 PK12
Line 13 PA13 PB13 PC13 PD13 PE13 PF13 PG13 PH13 PI13 PJ13 PK13
Line 14 PA14 PB14 PC14 PD14 PE14 PF14 PG14 PH14 PI14 PJ14 PK14
Line 15 PA15 PB15 PC15 PD15 PE15 PF15 PG15 PH15 PI15 PJ15 PK15

Yeah, you are right. There are many GPIOxs available on STM32F4 devices. So, this table describes you, which pins are used for specific line. So let’s say Line 11 has pins PA11 to PK11.

I had to implement default interrupt handlers if I want to use interrupts. So 7 functions with default names in my library. If you need any of this functions (names below) then you can disable this functionality in library by adding some defines to defines.h file.

By default, external interrupts have priority in NVIC set to 0x0A. If you want higher priority, add line below in defines.h file and edit it. Lower number means higher priority. Sub priorities are set like pins. Px0 has highest priority, Px15 lowest.

Functions and enumerations


Example is set to work with both, discovery boards and nucleo boards.

  • Discovery boards
    • Button connected to PA0
    • Rising edge when you press button
    • Line0 selected
  • Nucleo boards
    • Button connected to PC13
    • Falling edge when you press button
    • Line13 selected
  • Every click on button will toggle led
    • After 10 clicks on button, interrupt for specific line is detached

Project is available on Github, download library here.

TM STM32F4 EXTI Library


Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • محــمد مهــدی سلطانی

    dear Majele
    can we have an interrupt routine for rising edge of a pin and also another routine for the falling edge of the same pin?

    • It’s not possible. You can set pin to trigger interrupt on falling or rising edge but you have the same handler.
      To implement this you have to check pin input data to see if trigger was falling or rising.

      • محــمد مهــدی سلطانی

        I had that check
        but the problem started when I need to done one of the edge triggers with higher priority, instead of that another one is running…
        I think the solution is to connect on signal to more than one pin! I mean that to connect it to 2 pin with different Exti and different priority…

  • Miki

    Hi Majerle, thank you for sharing all those libs. I’m using several libs (SPI, ADC, DAC, PWM, HD44780, rotary) and all works correct. But this one for EXTI for some reason do not work on STM32F4. It pass the initialization but never enter into handler function. I have tried PA1, PE5, PE6 and same things, not entering into handler. Also tried standalone demo code provided here, no lucky. What can be wrong here?

    • Hi. It’s hard to say right now. I’ve recompile again for my F4 and F429 boards. Buttons, in both cases connected to PA0, works just fine.

      Can you show me your code?

  • Nguyen Master

    Hi Majerle, I got a problem like this, an error appear “identifier “EXTI9_5_IRQn” is undefined”, although I had “include “stm32f4xx.h” already.

    Thank you so much.

    • And this irqn is defined for sure? Because if other irqns are working ok i dont see an option why this wouldnt. Can you show the project?

      • Nguyen Master

        I just import 2 file “tm_stm32f4_exti.c” and “tm_stm32f4_exti.h” of yours. I see “EXTI9_5_IRQn” is already define in stm32f4xx.h but i don’t know why this error appear?

        #if defined (STM32F40_41xxx)
        CAN1_TX_IRQn = 19, /*!< CAN1 TX Interrupt */
        CAN1_RX0_IRQn = 20, /*!< CAN1 RX0 Interrupt */
        CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */
        CAN1_SCE_IRQn = 22, /*!< CAN1 SCE Interrupt */
        EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */

        • Ok, open “Options for Target”, go to C/C++ tab and under “defines” make sure that you have this at least:

          If not, add and try again.

          • Nguyen Master

            I solved it. Thank you Tilen 🙂

  • Tim

    Do you think I can drive your 4×4 keypad code (in your other post) with interrupt using this tutorial? And would that be more efficient? Thanks

    • With a lot of modification maybe yes.

      • Tim

        Hi Tilen,
        I’m trying to get the 4×4 keypad to work using your interrupt tutorial. I wired them the same way you did for your polling method (which I tried and was working fine), but it would not work (I set up the rows as interrupt input just the way you did in your interrupt tutorial, and the columns as regular GPIO outputs). What do you think might be the problem? Thanks in advance Tilen.

        • First make sure that you know how to properly read matrix keypad.
          Theb, explain to me so I know if you are correct. After that you will see that interrupts are bad way here.

          • Tim

            Thanks for the prompt reply. I have had experience doing 4×4 keypad before (on MSP430). Here is how I think it works:
            1. Set rows as inputs and columns as outputs
            2. Enable interrupt for row inputs
            3. Initialize all columns as high, then force each one of them low and look at the row input. If the row input is also low at that forced low column, return the key pressed.
            As of now, I am just testing the interrupt to make sure that my row input (interrupt driven) works.

  • Khoa Tran

    Can I use pin PA12 as USB DP and interrupt as same time?

  • Pingback: All STM32F4 libraries - STM32F4 Discovery()

  • marie

    Hey Tilen,
    once again my stupid question, I am sorry! How do I need to connect external buttons electronically, just one side to GND and the other to the pin or do I need to connect it to VDD somehow as well? I know that I need a condensator in parallel to the pushbutton, I just don´t get how the internal circuit works so I don´t know how to connect the external part.
    Thanks a lot for your help!

  • Pingback: STM32F4 External interrupts tutorial - STM32F4 Discovery()

  • kuds

    When using SPI (not your lib, because DMA+SPI as slave), I need to detect the falling and rising edge of Slave Select. Is there a way I can distinguish between them when using EXTI_Trigger_Rising_Falling?

    • Check GPIO PIN VALUE using GPIO_ReadInputDataBit function if you use STD drivers.

  • محــمد مهــدی سلطانی

    Hi Tilen
    I need to Connect both of PA0 & PB0 to EXTI with different functions!
    how to check that which one Interrupted my system in the EXTI Interrupt Handler?

    • This is not possible. You can’t have 2 pins with the same number to EXTI connected. Read my EXTI tutorial about that.

      • محــمد مهــدی سلطانی

        So the only way is to check one off them in a period? some thing like: TM_DELAY_1msHandler

        • Yes.

          • محــمد مهــدی سلطانی


  • G.H.ornil

    I am trying to compile this example code. i included all the files header and source files.I got below errors.
    Build target ‘Target 1’
    .stbth1.axf: Error: L6218E: Undefined symbol TM_EXTI_Attach (referred from main.o).
    .stbth1.axf: Error: L6218E: Undefined symbol TM_EXTI_Detach (referred from main.o).
    .stbth1.axf: Error: L6218E: Undefined symbol assert_param (referred from stm32f4xx_rcc.o).
    .stbth1.axf: Error: L6218E: Undefined symbol TM_GPIO_Init (referred from tm_stm32f4_disco.o).
    “.stbth1.axf” – 4 Errors, 0 Warning(s).
    Target not created