专注电子技术学习与研究
当前位置:单片机教程网 >> STM32 >> 浏览文章

SMT32 systick中断优先级分析

作者:huqin   来源:本站原创   点击数:  更新时间:2014年05月05日   【字体:

 1.知识点说明:

1)中断异常向量表中的优先级应该是指的中断、异常的硬件优先级,如果两个或更多的中断指定了相同的优先级,则由它们的硬件优先级来决定处理器对它们进行处理时的顺序。(源于cortex-M3技术参考手册)

2)Systick属于内核中断,“handled by system handlers”,优先级由Systemhandler priority registers (SHPRx)来设置;(摘自cortex-M3内核编程手册)

3)timer、串口等属于外设中断,“handled by ISRsInterrupt ServiceRoutines(ISRs)”优先级由Interruptpriority registers (NVIC_IPRx)来设置;(源于cortex-M3内核编程手册)

4)STM32有16个可编程的优先等级(使用了4位中断优先级),(摘自STM3210X参考手册)。此处的4位就是“STM3210x.h”中的__NVIC_PRIO_BITS,是4);

5Systemhandler priority registers (SHPRx)说明:The SHPR1-SHPR3 registers set the priority level, 0 to 15 of the exceptionhandlers that have configurable priority.即代表内核中断优先级可以在这16个优先等级中选择,那么就和外设中断一样,可以统一设定优先等级。(摘自cortex-M3内核编程手册)

2.分析:

有了以上知识点下面来分析systick的初始化,程序中对其优先级进行重新设定。

systick配置函数中包含中断优先级的设置,如下:

static __INLINE uint32_tSysTick_Config(uint32_t ticks)

{

…………

NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1);  

…………

}

由于__NVIC_PRIO_BITS定义为4,上式中(1<<__NVIC_PRIO_BITS)- 1)=15,所以systick的中断优先级是最低的。


3.
问题:
在对外设中断优先级设定时用此函数:    NVIC_Init(&NVIC_InitStructure);
如果外设设置了主次优先级,以下三种情况中任一个:
      @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

那么优先级就没有15这一级了,该如何理解systick的优先级呢。

答案是:

不管 PriorityGroup  是多少,4bit 就意味着 priority 的范围从 0~15
PriorityGroup 位就是抢占优先级,低 4 - PriorityGroup  位就是亚优先级

假设是下列情况:
      @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
                                 3 bits for subpriority
优先级15 == 主优先级为1,亚优先级为7

关闭窗口

相关文章