How to configure NVIC ¶
Configuring the Nested Vectored Interrupt controller (NVIC) in STM32CubeMX2 is essential for setting interrupt priorities and enabling interrupts on STM32 microcontrollers. Here’s a detailed guide to identify and configure common NVIC scenarios.
Typical Scenarios for NVIC Configuration in STM32CubeMX2 ¶
How to access NVIC settings in CubeMX ¶
Open your project in CubeMX.
Go to System > NVIC tab.
Here you can: - Configure priorities for each enabled interrupt. - For settable system Interruption (exceptions) you can enable/disable the interrupt.
You find also NVIC settings in the peripheral configuration tabs when you enable interrupts for specific peripherals.
To configure NVIC settings for Cortex, go to Cortex > NVIC tab.
Scenario A: Configuring Interrupt Priority Grouping ¶
Priority grouping defines how many bits are allocated to preemption priority vs subpriority.
Use case: When multiple interrupts with different urgency levels exist.
Steps:
In STM32CubeMX2, navigate to NVIC settings.
Set the priority grouping (for example, 4-bit preemption priority, 0 subpriority).
This affects how priorities are interpreted and nested.
Scenario B: Enabling interrupt for a peripheral ¶
Example: Enable USART interrupt for receiving data.
Steps:
Enable the peripheral (e.g., USART1) in STM32CubeMX2.
In the Configuration tab, enable the interrupt (e.g., USART1 global interrupt).
STM32CubeMX2 automatically adds the NVIC interrupt global configuration for this peripheral.
Optionally, configure the interrupt priority in the NVIC settings.
Scenario C: Order Interrupts ¶
Example: Assign higher priority to an I2C interrupt than to a UART interrupt.
Steps:
Enable interrupts for both peripherals.
In NVIC settings, set the Preemption Priority and Subpriority for each interrupt.
Lower numerical value means higher priority (e.g., priority 0 is higher than 1).
Ensure priority grouping supports your priority scheme.
Scenario D: Disabling an Interrupt ¶
Use case: Temporarily disable an interrupt without disabling the peripheral.
Steps:
In STM32CubeMX2, uncheck the interrupt enable box from the peripheral config tab.
This disables the NVIC interrupt but keeps the peripheral functional.
Scenario E: Reset Interrupts ¶
After modifying interrupts configuration, you can cancel all changes using the Reset all interrupts button.
Important Tips ¶
Interrupt priorities: Lower number = higher priority.
Priority grouping: Determines the split between preemption priority and subpriority.
Avoid priority conflicts: Make sure critical interrupts have higher priority.
Check generated code: STM32CubeMX2 generates stm32xx_it.c and NVIC configuration in stm32xx_hal_msp.c or main.c.