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
Direct register (same thing)
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.
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)
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
DIERbits (UIE,CCxIE, etc.) -
USART: clear
RXNEIE,TXEIE,TCIE -
EXTI: clear
IMRbit for that line + clear pending inPR -
DMA: clear
TCIE/HTIE/TEIEand 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.
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.
.jpg)
评论
发表评论