How to properly enable/disable interrupts in ARM Cortex-M?

Point of this post is not how to use NVIC (Nested Vectored Interrupt Controller) in Cortex-M processors but how to disable/enable interrupts properly for your system to avoid strange behaviours in your code.

Let’s assume you have 2 functions, which do some important stuff and they have to make sure that noone interrupts these 2 functions

By calling these 2 functions from some other function, this will just work properly, interrupts will be disabled, important stuff done and interrupts enabled again. But what if you come to a situation, when important function 1 calls important function 2 from section where function 1 should not be interrupted? In case above, important function 2 would enable interrupts back and function 1 would not be safe anymore. Check example below which will fail in order to make clean and safe program:

To avoid problems like this, the idea is that before you disable interrupts in your function, first check interrupt enabled status in Cortex-M4 PRIMASK register to see if they were enabled or disabled before. Check properly working example where you don’t have to worry about nested function calls with multiple interrupt enable or disable function calls:


If you are using my TM_GENERAL library for STM32F4xx and its Enable and Disable interrupts function, then you don’t have to worry about everything listed above, because these 2 functions are written in a way to avoid strange behaviour in your system. You can use it everywhere and they will work as expected. Download available below:



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!
  • Pingback: All STM32F4 tutorials - STM32F4 Discovery()

  • Pingback: How To Enable Interrupts In Arm | Information()

  • radipta ady

    I have a project that currently using NVIC to read USART but I also need this irq method to read encoder data. Is that possible to combine this two thing( NVIC and irq interrupt) in one project, and if it is, how to manage them?
    thanks for the tutorials

    • Ok. NVIC is peripheral to handle interrupts. UART is one of many supported interrupts. You can use multiple interrupts in your program.

      • radipta ady

        Thank for the reply, if I have my NVIC priority for usart set to be 0 (zero), and I have my encoder reader with enable_irq(), which one is gonna be prioritized?

        • What? Please read NVIC thing on ARM tech site first.

  • Selso Liberado

    Instead of complety disable IRQ what about using SVC ? I am not used to SVC, but worked on ARM7TMDI and implemented SWI functions with the keil toolchain quite easily without worring about stuffs like the priMASK.
    Actually I did not find many example on how to make it work with STM32 / gcc based IDE…

  • kenneth

    Hi ,
    Sorry. I can not found files between “defines.h” and ” attributes.h ” in the tm_stm32f4_general.rar.
    can you attach them?
    thanks a lot.