How to disable all interrupt on STM32?

 Here are the practical ways to “disable all interrupts” on STM32, depending on what you mean (mask CPU interrupts vs. stop peripherals from generating them).




1) Mask (disable) all CPU interrupts globally

This is the most common meaning: prevent any interrupt handler from running on the Cortex-M core.

CMSIS (portable) way

#include "cmsis_gcc.h" // or core_cm*.h via your device header __disable_irq(); // masks IRQs (PRIMASK = 1) // critical section... __enable_irq(); // unmasks IRQs

Direct register (same thing)

__set_PRIMASK(1); // disable IRQ // ... __set_PRIMASK(0); // enable IRQ

Notes

  • This masks all “normal” interrupts (IRQ).

  • NMI (Non-Maskable Interrupt) and HardFault still happen.


2) Also stop Fault exceptions (more “everything”)

If you also want to block most fault exceptions (MemManage/BusFault/UsageFault), use FAULTMASK.

__disable_fault_irq(); // sets FAULTMASK = 1 // ... __enable_fault_irq(); // clears FAULTMASK

Notes

  • This masks all exceptions except NMI.

  • Use carefully: you can hide real bugs and make debugging harder.


3) Disable all interrupts in the NVIC (per-IRQ)

If you want to disable every individual IRQ line in the NVIC (but not exceptions), you can clear the enable bits.

CMSIS-style loop (example)

#include "stm32xxxx.h" void NVIC_DisableAllIRQs(void) { for (uint32_t i = 0; i < (sizeof(NVIC->ICER) / sizeof(NVIC->ICER[0])); i++) { NVIC->ICER[i] = 0xFFFFFFFFu; // disable all IRQs in that register block NVIC->ICPR[i] = 0xFFFFFFFFu; // clear any pending IRQs } __DSB(); __ISB(); }

Notes

  • This disables NVIC external interrupts, but doesn’t stop peripherals from setting their interrupt pending flags internally.

  • If you later re-enable, pending flags may immediately fire unless you cleared peripheral flags too.


4) Disable interrupts from specific peripherals (best practice)

Often the correct approach is: disable the interrupt source at the peripheral.

Examples:

  • TIMx: clear DIER bits (UIE, CCxIE, etc.)

  • USART: clear RXNEIE, TXEIE, TCIE

  • EXTI: clear IMR bit for that line + clear pending in PR

  • DMA: clear TCIE/HTIE/TEIE and clear flags in DMA IFCR

This is safer than global masking if you just need to stop one subsystem.


5) “Critical section” pattern (recommended)

When you must briefly block interrupts, keep it short and restore state properly.

uint32_t primask = __get_PRIMASK(); __disable_irq(); // critical section... __set_PRIMASK(primask); // restore previous state

Common gotchas

  • RTOS: If you’re using FreeRTOS, prefer its APIs (taskENTER_CRITICAL() / taskEXIT_CRITICAL()) because it manages nesting and scheduler rules.

  • Timing/peripherals: Disabling interrupts doesn’t stop timers or DMA running; it just stops handlers from executing.

  • Debugging: Long global interrupt disables can break SysTick timing, UART reception, watchdog servicing, etc.

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How To Connect Stm32 To PC?

How do you set up ADC (Analog-to-Digital Converter) in STM32?