您的位置:首页 > 其它

STM32 中断优先级以及全局允许和禁止

2017-09-07 08:47 274 查看
请问例程上为什么要把串口中断优先级设置在NVIC中断分组2

NVIC: M3内核允许8bit做优先级分组设置,而STM32只有4bit可以设置。 

AIRCR寄存器的4个位的分组方式如下: 

第0组:所有4位用于指定响应优先级 

第1组:最高1位用于指定抢占式优先级,最低3位用于指定响应优先级 

第2组:最高2位用于指定抢占式优先级,最低2位用于指定响应优先级 

第3组:最高3位用于指定抢占式优先级,最低1位用于指定响应优先级 

第4组:所有4位用于指定抢占式优先级 

例:定义为第二组,抢占式优先级2bit,响应式优先级2bit,2bit共可有4种状态,也就是总共能设置4*4=16个中断优先级。 

定义为第三组,抢占式优先级3bit,响应式优先级1bit,也就是总共能设置8*2=16个中断优先级。

我觉得可能是易于程序员查看代码吧,比如把串口1、2、3分在同一组,把SPI1、2、3分在同一组,看起来比较直观。 
实际上分不分组都能实现同一种结果。

====================================

优先级可以被分为高低两个位段,分别是抢占优先级和亚优先级

Cortex-M3允许具有较少中断源时使用较少的寄存器位指定中断源的优先级,因此STM32把指定中断优先级的寄存器位减少到4位 AIRC 中 PRIGROUP 的值规定了设置和确定每个外部中断通道优先级的格式。例如,在上面将 0x05 写入了 AIRC 中 PRIGROUP,也就规定了当前系统中只能有
4 个抢先式优先级,相同的抢先式优先级下还可以有 4 个不同级别的子优先级,他们分别为: 

位[7:6]                   位[5:4]                  位[3:0] 

00         0 号抢先优先级   00        0 号子优先级    无效 

01         1 号抢先优先级   01        1 号子优先级    无效 

10         2 号抢先优先级   10        2 号子优先级    无效 

11         3 号抢先优先级   11        3 号子优先级    无效 

在Cortex—M3的优先级分配中,较低的优先级值具有较高的优先级。

数值小的优先级高, 数值大的优先级低

a 只能高抢先优先级的中断可以打断低抢先优先级的中断服务,构成中断嵌套。

b 当 2(n)个相同抢先优先级的中断出现,它们之间不能构成中断嵌套,但
STM32 首先响应子优先级高的中断。

c 当 2(n)个相同抢先优先级和相同子优先级的中断出现,STM32 首先响应中断通道所对应的中断向量地址低的那个中断。 

可以通过调用STM32的固件库中的函数NVIC_PriorityGroupConfig()选择使用哪种优先级分组方式,这个函数的参数有下列5种:

NVIC_PriorityGroup_0 => 选择第0组

NVIC_PriorityGroup_1 => 选择第1组

NVIC_PriorityGroup_2 => 选择第2组

NVIC_PriorityGroup_3 => 选择第3组

NVIC_PriorityGroup_4 => 选择第4组

第0组:所有4位用于指定响应优先级   ( 从不嵌套 )

第1组:最高1位用于指定抢占式优先级,最低3位用于指定响应优先级

第2组:最高2位用于指定抢占式优先级,最低2位用于指定响应优先级

第3组:最高3位用于指定抢占式优先级,最低1位用于指定响应优先级
第4组:所有4位用于指定抢占式优先级 ( 最大的嵌套可能性 ) ( 类似于 STR710 )

subpriority 开始的位号 :

如果从 7 号开始 那么没有 pre-emption priority, [ b7 b6 b5 b4 ] 全部为 subpriority 

如果从 6 号开始 那么 [ b7 ]为 pre-emption priority, [ b6 b5 b4 ] 为 subpriority 

如果从 3 号开始 那么 [ b7 b6 b5 b4 ]为 pre-emption priority, 没有 subpriority 

[pre-emption priority] :: [ subpriority ] ( STM32 共计 4 位 ) 所以无论如何配置做多只能有 16 级, 数值最小的具有最高的优先级

在程序中合理按排中断优先级可以达到具有高抢占式优先级的中断可以在具有低抢占式优先级的中断处理过程中被响应,即中断嵌套,或者说高抢占式优先级的中断可以嵌套低抢占式优先级的中断。

要注意的几点是:

1)如果指定的抢占式优先级别或响应优先级别超出了选定的优先级分组所限定的范围,将可能得到意想不到的结果;

2)抢占式优先级别相同的中断源之间没有嵌套关系;

3)如果某个中断源被指定为某个抢占式优先级别,又没有其它中断源处于同一个抢占式优先级别,则可以为这个中断源指定任意有效的响应优先级别。

#define NVIC_PriorityGroup_0         ((uint32_t)0x700) /*!< 0 bits for pre-emption priority

                                                            4 bits for subpriority */

#define NVIC_PriorityGroup_1         ((uint32_t)0x600) /*!< 1 bits for pre-emption priority

                                                            3 bits for subpriority */

#define NVIC_PriorityGroup_2         ((uint32_t)0x500) /*!< 2 bits for pre-emption priority

                                                            2 bits for subpriority */

#define NVIC_PriorityGroup_3         ((uint32_t)0x400) /*!< 3 bits for pre-emption priority

                                                            1 bits for subpriority */

#define NVIC_PriorityGroup_4         ((uint32_t)0x300) /*!< 4 bits for pre-emption priority

                                                            0 bits for subpriority */

/**

  * @brief  Configures the priority grouping: pre-emption priority and subpriority.

  * @param  NVIC_PriorityGroup: specifies the priority grouping bits length. 

  *   This parameter can be one of the following values:

  *     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority

  *                                4 bits for subpriority

  *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority

  *                                3 bits for subpriority

  *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority

  *                                2 bits for subpriority

  *     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority

  *                                1 bits for subpriority

  *     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority

  *                                0 bits for subpriority

  * @retval None

  */
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
{

  /* Check the parameters */

  assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));

  

  /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */

  SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;



/**

  * @brief  Initializes the NVIC peripheral according to the specified

  *   parameters in the NVIC_InitStruct.

  * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains

  *   the configuration information for the specified NVIC peripheral.

  * @retval None

  */
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)

{

  uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;

  

  /* Check the parameters */

  assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));

  assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));  

  assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));

    
  if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)

  {

    /* Compute the Corresponding IRQ Priority --------------------------------*/    

    tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;

    tmppre = (0x4 - tmppriority);

    tmpsub = tmpsub >> tmppriority;

    tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;

    tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;

    tmppriority = tmppriority << 0x04;

        

    NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;

    

    /* Enable the Selected IRQ Channels --------------------------------------*/

    NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =

      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);

  }

  else // if (NVIC_InitStruct->NVIC_IRQChannelCmd == DISABLE)
  {

    /* Disable the Selected IRQ Channels -------------------------------------*/

    NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =

      (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);

  }

}

stm32如何开关中断

在STM32/Cortex-M3中是通过改变CPU的当前优先级来允许或禁止中断。
PRIMASK位:  只允许NMI和hard fault异常,其他中断/异常都被屏蔽(当前CPU优先级=0)。
FAULTMASK位:只允许NMI,其他所有中断/异常都被屏蔽(当前CPU优先级=-1)。

   在STM32固件库中(stm32f10x_nvic.c和stm32f10x_nvic.h) 定义了四个函数操作PRIMASK位和FAULTMASK位,改变CPU的当前优先级,从而达到控制所有中断的目的。

下面两个函数等效于关闭总中断:
void NVIC_SETPRIMASK(void);
void NVIC_SETFAULTMASK(void);

下面两个函数等效于开放总中断:
void NVIC_RESETPRIMASK(void);
void NVIC_RESETFAULTMASK(void);

上面两组函数要成对使用,不能交叉使用。

例如:

第一种方法:
NVIC_SETPRIMASK();   //关闭总中断
NVIC_RESETPRIMASK(); //开放总中断

第二种方法:
NVIC_SETFAULTMASK();   //关闭总中断
NVIC_RESETFAULTMASK(); //开放总中断
常常使用
NVIC_SETPRIMASK();                    // Disable Interrupts
NVIC_RESETPRIMASK();                  // Enable Interrupts

个人备注:

在3.5固件库头文件core_cmfunc.h中存在如下代码:

#define __enable_fault_irq                __enable_fiq

#define __disable_fault_irq               __disable_fiq

__attribute__( ( always_inline ) ) __STATIC_INLINE void __enable_fault_irq(void)

{

  __ASM volatile ("cpsie f" : : : "memory");

}

__attribute__( ( always_inline ) ) __STATIC_INLINE void __disable_fault_irq(void)

{

  __ASM volatile ("cpsid f" : : : "memory");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: