STM32 NVIC interrupt priority management

Publisher:HarmoniousDreamLatest update time:2016-07-30 Source: eefocusKeywords:STM32  NVIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The CM3 core supports 256 interrupts, including 16 core interrupts and 240 external interrupts, and has 256 levels of programmable interrupt settings. However, the STM32 does not use all of the CM3 core, but only uses part of it. The STM32 has 84 interrupts, including 16 core interrupts and 68 maskable interrupts, with 16 levels of programmable interrupt priority. We often use these 68 maskable interrupts, but the STM32 has only 60 of the 68 maskable interrupts on the STM32F103 series (68 on the 107 series).

In MDK, MDK defines the following structure for registers related to NVIC:

  1.   typedef struct
  2.   {
  3.   vu32 ISER[2];
  4.   u32 RESERVED0[30];
  5.   vu32 ICER[2];
  6.   u32 RSERVED1[30];
  7.   vu32 ISPR[2];
  8.   u32 RESERVED2[30];
  9.   vu32 ICPR[2];
  10.   u32 RESERVED3[30];
  11.   vu32 IABR[2];
  12.   u32 RESERVED4[62];
  13.   vu32 IPR[15];
  14.   } NVIC_TypeDef;

The interrupts of STM32 are executed in order under the control of these registers. Only by understanding these interrupt registers can we understand the interrupts of STM32. The following is a brief introduction to these registers:

ISER[2]: ISER stands for Interrupt Set-Enable Registers, which is an interrupt enable register group. As mentioned above, the STM32F103 has only 60 maskable interrupts. Here, two 32-bit registers are used, which can represent a total of 64 interrupts. However, the STM32F103 only uses the first 60 bits. Bits 0 to 31 of ISER[0] correspond to interrupts 0 to 31 respectively. Bits 0 to 27 of ISER[1] correspond to interrupts 32 to 59; in this way, a total of 60 interrupts are corresponding. If you want to enable an interrupt, you must set the corresponding ISER bit to 1 to enable the interrupt (this is just enabling, and it must be combined with interrupt grouping, masking, IO port mapping and other settings to be considered a complete interrupt setting). For specific interrupts corresponding to each bit, please refer to line 36 in stm32f10x_nvic..h.

ICER[2]: The full name is Interrupt Clear-Enable Registers, which is an interrupt disable register group. This register group has the opposite function to ISER, and is used to clear the enable of a certain interrupt. The function of its corresponding bit is the same as ICER. Here, we need to set a special ICER to clear the interrupt bit instead of writing 0 to ISER, because these registers of NVIC are valid when writing 1, and invalid when writing 0.

ISPR[2]: The full name is Interrupt Set-Pending Registers, which is an interrupt pending control register group. The interrupt corresponding to each bit is the same as ISER. By setting 1, the ongoing interrupt can be suspended and the interrupt of the same level or higher level can be executed. Writing 0 is invalid.

ICPR[2]: The full name is Interrupt Clear-Pending Registers, which is an interrupt clear control register group. Its function is opposite to ISPR, and the corresponding bits are the same as ISER. By setting 1, the pending interrupt can be cleared. Writing 0 has no effect.

IABR[2]: The full name is Interrupt Active Bit Registers, which is an interrupt activation flag register group. This is a read-only register that can be used to determine which interrupt is currently being executed. It is automatically cleared by hardware after the interrupt is executed. The interrupt represented by the corresponding bit is the same as ISER. If it is 1, it means that the interrupt corresponding to the bit is being executed.

IPR[15]: The full name is Interrupt Priority Registers, which is a register group for interrupt priority control. This register group is very important! The interrupt grouping of STM32 is closely related to this register group. Since STM32 has more than 60 interrupts, STM32 uses interrupt grouping to determine the interrupt priority. The IPR register group consists of 15 32-bit registers, and each maskable interrupt occupies 8 bits, so a total of 15*4=60 maskable interrupts can be represented. This is exactly the same as the number of maskable interrupts of STM32. IPR[0]'s [31~24], [23~16], [15~8], and [7~0] correspond to interrupts 3~0, and so on, corresponding to a total of 60 external interrupts. However, not all of the 8 bits occupied by each maskable interrupt are used, but only the upper 4 bits are used. These 4 bits are divided into preemption priority and sub-priority. The preemption priority is in front, and the sub-priority is in the back. The number of bits each of these two priorities occupies is determined by the interrupt grouping settings in SCB->AIRCR.

Here is a brief introduction to the interrupt grouping of STM32: STM32 divides interrupts into 5 groups, group 0 to 4. The setting of this grouping is defined by bits 10 to 8 of the SCB->AIRCR register. The specific allocation relationship is shown in the table


Through this table, we can clearly see the configuration relationship corresponding to groups 0~4. For example, if group is set to 3, then for all 60 interrupts, the highest 3 bits of the upper 4 bits of the interrupt priority register of each interrupt are the preemption priority, and the lower 1 bit is the response priority. For each interrupt, you can set the preemption priority to 0~7 and the response priority to 1 or 0. The preemption priority is higher than the response priority. The smaller the value, the higher the priority.

There are two points to note here: first, if the preemption priority and response priority of two interrupts are the same, the interrupt that occurs first will be executed first; second, the preemption priority with a higher priority can interrupt the ongoing interrupt with a lower preemption priority. For interrupts with the same preemption priority, the response priority with a higher priority cannot interrupt the interrupt with a lower response priority.

 

Using library functions to implement the above interrupt grouping settings and interrupt priority management simplifies our future interrupt settings. The NVIC interrupt management function is mainly in the misc.c file.

The first is the interrupt priority grouping function NVIC_PriorityGroupConfig. This function is used to group the interrupt priorities. This function can only be called once in the system. Once the grouping is determined, it is best not to change it.

For example, if we set the interrupt priority group value of the entire system to 2 (2-bit preemption priority, 2-bit response priority), the method is: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

After setting up the system interrupt grouping, how do we determine the preemption priority and response priority for each interrupt? Let's look at an important function, the interrupt initialization function NVIC_Init, whose function declaration is:

void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)

NVIC_InitTypeDef is a structure. You can look at the member variables of the structure:

  1.    typedef struct
  2.    {
  3.      uint8_t NVIC_IRQChannel; //Define which interrupt is initialized. We can find the corresponding name of each interrupt in stm32f10x.h, such as USART1_IRQn.
  4.      uint8_t NVIC_IRQChannelPreemptionPriority; //Define the preemption priority level of this interrupt.
  5.      uint8_t NVIC_IRQChannelSubPriority; //Define the sub-priority level of this interrupt.
  6.      FunctionalState NVIC_IRQChannelCmd; //Whether the interrupt is enabled.
  7.    } NVIC_InitTypeDef;

For example, if we want to enable the interrupt of serial port 1, and set the preemption priority to 1 and the sub-priority to 2, the initialization method is:

  1.    USART_InitTypeDef USART_InitStructure;
  2.    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //Serial port 1 interrupt
  3.    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ; // preemption priority is 1
  4.    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; // Subpriority bit 2
  5.    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel enable
  6.    NVIC_Init(&NVIC_InitStructure);

Keywords:STM32  NVIC Reference address:STM32 NVIC interrupt priority management

Previous article:STM32's standby wake-up function
Next article:stm32_file organization structure

Recommended ReadingLatest update time:2024-11-17 00:04

STM32 software programming method
STM32 software programming method 1 ST-LINK programming 1.1 SWD mode of ST-LINK programming The SWD mode of ST-LINK programming is a way of ST-LINK programming, which only requires 4 wires. They are VCC, GND, SWCLK, SWDIO; 1.2 JTAG mode of ST-LINK programming During the program burning process in JTAG mode,
[Microcontroller]
STM32 software programming method
STM32 - Issues with redirecting printf to the serial port
Simply put: if you want to use printf in mdk, you need to redefine the fputc function and avoid using semihosting (semihosting mode).  The default output device of the standard library function is the display. To output to the serial port or LCD, you must redefine the functions related to the output device called in t
[Microcontroller]
STM32 study notes---EXTI external key interrupt experiment based on UCOSII
After doing the IWDG independent watchdog experiment based on UCOSII, we continued with the 25th experiment - the EXTI external key interrupt experiment based on UCOSII. This experiment adds EXTI to the IWDG experiment based on UCOSII. Three keys are pressed separately to output key information in the serial port! IWD
[Microcontroller]
STM32 study notes---EXTI external key interrupt experiment based on UCOSII
Transplanting fatfs file system based on stm32cubemx
What I want to share with you today is how to use the FatFs library to read and write files on the SD card. The initialization function of the project and FatFs are generated by STM32CubeMX configuration, so we don't need to add the library manually.  What I share today is related to my last post,  SDIO reads SD card
[Microcontroller]
The Delay_ms() function is stuck during STM32 hardware debugging
The program got stuck during LCD initialization. Through hardware debugging, it was found that it was stuck in the Delay_ms() function. Going to the definition, it was found to be a macro definition function of Delay_us: #define Delay_ms(x) Delay_us(1000*x)//单位ms   That is, delay x in ms, go to the Delay_us() functi
[Microcontroller]
STM32 interrupt management
 Regarding the interrupt of STM32, I believe that friends who have played with microcontrollers know that interrupt is a major advantage of MCU, which enables MUC work to distinguish the "priorities" of things, so as to handle things in an orderly manner;  Today I will explain the interrupt management mechanism of t
[Microcontroller]
Application of STM32 external (IO) interrupt EXTI
a)                Purpose: Similar to serial port input, IO input without interrupts is also inefficient, and button events can be inserted through EXTI. This section is related to EXTI interrupts. b)                Initialization function definition: void  EXTI_Configuration(void);  //Define IO interrupt initial
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号