I. Overview
STM32 currently supports a total of 84 interrupts (16 cores + 68 external), which can provide 16 levels of programmable interrupt priority settings (only using the upper 4 bits of the interrupt priority setting 8 bits) and 16 preemption priorities (because the preemption priority can be up to 4 bits).
2. Priority Judgment
There are two concepts of priority in STM32 (Cortex-M3) - preemption priority and response priority. The smaller the attribute number, the higher the priority. Some people call the response priority "sub-priority" or "sub-priority". Each interrupt source needs to be assigned these two priorities.
An interrupt with a high preemptive priority can be responded to during the processing of an interrupt with a low preemptive priority, that is, interrupt nesting, or an interrupt with a high preemptive priority can nest an interrupt with a low preemptive priority.
When the preemptive priorities of two interrupt sources are the same, there will be no nesting relationship between the two interrupts. When an interrupt arrives, if another interrupt is being processed, the later interrupt will have to wait until the previous interrupt is processed before it can be processed. If the two interrupts arrive at the same time, the interrupt controller decides which one to process first based on their response priority; if their preemptive priority and response priority are equal, it decides which one to process first based on their ranking order in the interrupt table.
3: Priority grouping
Since each interrupt source needs to be assigned these two priorities, there needs to be a corresponding register bit to record the priority of each interrupt; 8 bits are defined in Cortex-M3 to set the priority of the interrupt source. These 8 bits can be allocated in 8 ways in the interrupt priority grouping field of the NVIC Application Interrupt and Reset Control Register (AIRCR):
All 8 bits are used to specify the response priority.
The highest 1 bit is used to specify the preemptive priority, and the lowest 7 bits are used to specify the response priority.
The highest 2 bits are used to specify the preemptive priority, and the lowest 6 bits are used to specify the response priority. The
highest 3 bits are used to specify the preemptive priority, and the lowest 5 bits are used to specify the response priority. The
highest 4 bits are used to specify the preemptive priority, and the lowest 4 bits are used to specify the response priority. The
highest 5 bits are used to specify the preemptive priority, and the lowest 3 bits are used to specify the response priority. The
highest 6 bits are used to specify the preemptive priority, and the lowest 2 bits are used to specify the response priority.
The highest 7 bits are used to specify the preemptive priority, and the lowest 1 bit is used to specify the response priority.
This is the concept of priority grouping.
Cortex-M3 allows fewer interrupt sources to use fewer register bits to specify the priority of the interrupt source, so the STM32 reduces the register bits for specifying interrupt priority to 4 bits (the upper four bits of AIRCR). The grouping of these 4 register bits is as follows:
Group 0: All 4 bits are used to specify the response priority
Group 1: The highest 1 bit is used to specify the preemptive priority, and the lowest 3 bits are used to specify the response priority
Group 2: The highest 2 bits are used to specify the preemptive priority, and the lowest 2 bits are used to specify the response priority
Group 3: The highest 3 bits are used to specify the preemptive priority, and the lowest 1 bit is used to specify the response priority
Group 4: All 4 bits are used to specify the preemptive priority
You can select which priority grouping method to use by calling the function NVIC_PriorityGroupConfig() in the STM32 firmware library. This function has the following 5 parameters:
NVIC_PriorityGroup_0 => Select Group 0
NVIC_PriorityGroup_1 => Select Group 1
NVIC_PriorityGroup_2 => Select Group 2
NVIC_PriorityGroup_3 => Select Group 3
NVIC_PriorityGroup_4 => Select Group 4
The interrupt priority grouping is to allocate the number of bits occupied by each priority number in the upper four bits of the interrupt priority register to the preemptive priority and response priority. It can only be set once in a program.
4: Interrupt source priority
The next step is to specify the priority of the interrupt source. The interrupt source priority is set in the interrupt priority register. Only the upper four bits can be set. The corresponding value must be set in the register according to the number of bits set in the interrupt priority group. If you choose the third group of the interrupt priority group: the highest 3 bits are used to specify the preemptive priority, and the lowest 1 bit is used to specify the response priority, then the preemptive priority has eight data options from 000-111, that is, there are eight interrupt nests, and the response priority has two types of 0 and 1, a total of 8*2=16 priorities.
The interrupt source priority specifically sets the priority level of the interrupt source.
Multiple (up to 16) priorities can be set in one program, but only one can be set for each interrupt source.
Every interrupt priority program must include the following two functions:
1) void NVIC_PriorityGroupConfig(u32 NVIC_PriorityGroup) interrupt group setting
2) void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct) interrupt priority setting
Note: NVIC can configure 16 types of interrupt vectors, not 16. When there are more than 16 interrupt vectors in the project, there must be more than two interrupt vectors using the same interrupt type, and interrupt vectors with the same interrupt type cannot be nested.
5. Start the required GPIOn_Pin and AFIO RCC clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,RCC_APB2Periph_AFIO,ENABLE);
Note: Don't forget to enable RCC_APB2Periph_AFIO! Otherwise the pin can only be used as a normal high or low level. When using GPIO as EXTI external interrupt or using the remapping function, the AFIO clock must be enabled, but when using the default multiplexing function, the AFIO clock does not need to be enabled.
6. Examples
NVIC_InitTypeDef NVIC_InitStructure; //Define interrupt initialization type structure variable
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //Configure priority group 1 2 preemptive priorities 8 sub-priorities
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //Open external interrupt 0
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Configure preemptive priority 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Sub-priority is configured as 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable channel
NVIC_Init(&NVIC_InitStructure); //Initialize the configuration of external interrupt 0
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //Open external interrupts 5 to 9
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //Configure preemptive priority 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Sub-priority is configured as 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable channel
NVIC_Init(&NVIC_InitStructure); //Initialize the configuration of external interrupt 0
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn; //ADC1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //Configure preemptive priority 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Sub-priority is configured as 1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable channel
NVIC_Init(&NVIC_InitStructure); //Initialize the configuration of external interrupt 0
From the above configuration, external interrupt 0 has the highest priority, which can interrupt ADC and external interrupts 5 to 9, that is, they can be nested. When the ADC interrupt and external interrupts 5 to 9 occur at the same time, their preemption priority levels are the same, and their sub-priority levels are also the same. Because the ADC1 hardware is ranked higher, the ADC interrupt occurs first. If any of the two interrupts is being executed, wait until the interrupt is executed before executing the other interrupt.
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn indicates that the interrupt vectors of lines 5 to 9 of EXTI are to be configured, that is, lines EXTI5 to EXTI9 use the same interrupt vector. These writable parameters can be found in the stm32f10x.h file.
Previous article:Understanding of NVIC (Nested Vectored Interrupt Control) in STM32 (Cortex-M3)
Next article:stm32F429 interrupt priority related knowledge
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Single package, small size drive motor controller is here
- How to debug multiple DSPs?
- [RVB2601 Creative Application Development] Bring up the development board and light up the LED
- TI C6000 DSP Notes
- [Chuanglong Technology Allwinner A40i development board] Unboxing experience
- Can OTP be written via communication?
- STM32L0 LL library IIC configuration and usage issues, can't send the data I want. Ask for advice
- Keil C51 cracking problem
- AT32F425-Evaluation Report-CAN Communication Experiment 03
- [N32L43x Review] 1. Unboxing the N32L43XRL-STB Development Board