cm4 NVIC完全解析

NVIC(Nested Vectored Interrupt Controller)属于内核外设,所以在学习这一部分的内容的时候主要借助的参考资料就是《Cortex™-M4 Devices Generic User Guide》了,在手册的4.2章节,详细介绍了NVIC的寄存器的功能。


NVIC部分共有这个几个寄存器,如下图所示


下面来看看这几个寄存器都是干嘛的

1.ISER(Interrupt Set-enable Registers)

官方已经说的很明白了:The NVIC_ISER0-NVIC_ISER7 registers enable interrupts, and show which interrupts are enabled.

该寄存器可读可写,向某一位写1的时候就把对应的中断使能。

Write:
0 = no effect
1 = enable interrupt.
Read:
0 = interrupt disabled
1 = interrupt enabled.

CM4内核的NVIC一共有8个ISER寄存器,每个寄存器有32个位,所以理论上可以管理32*8个中断源。具体多少了中断源跟单片机的具体型号有关


2.ICER(Interrupt Clear-enable Registers)

The NVIC_ICER0-NVIC_ICER7 registers disable interrupts, and show which interrupts are enabled.

和ISER的左营相反,这个寄存器的作用是禁止中断的,当向某一位写1的时候,就把这一位对应的中断禁止了

Write:
0 = no effect
1 = disable interrupt.
Read:
0 = interrupt disabled
1 = interrupt enabled.


3.ISPR(Interrupt Set-pending Registers)

来看看官方的解释:The NVIC_ISPR0-NVIC_ISPR7 registers force interrupts into the pending state, and show which interrupts are pending.

这个寄存器用来挂起中断,通过置1,可以将正在进行的中断挂起,写0是无效的。

当高优先级的中断正在执行中,如果发生了低优先级的中断,CPU自然会将低优先级中断挂起,当高优先级中断执行完毕后,再去处理低优先级中断

Write:
0 = no effect
1 = changes interrupt state to pending.
Read:
0 = interrupt is not pending
1 = interrupt is pending.

A interrupt can enter pending state even if it is disabled. Disabling an interrupt only prevents the processor from taking that interrupt.

即使中断被禁止,还是有可能进入挂起态。禁止中断仅仅禁止CPU去响应中断


4.ICPR(Interrupt Clear-pending Registers)

The NVIC_ICPR0-NCVIC_ICPR7 registers remove the pending state from interrupts, and show which interrupts are pending.

其作用与ISPR相反,对应位也和ISER是一样的。通过设置1,可以将挂起的中断解挂

Write:
0 = no effect
1 = removes pending state from an interrupt.
Read:
0 = interrupt is not pending
1 = interrupt is pending.

NOTE:

ISPR和ISPR并不常用,从官方的说明也可以看出来,要想知道挂起或者解挂的状态,读这两个寄存器中的任意一个即可


5.IABR(Interrupt Active Bit Registers)

The NVIC_IABR0-NVIC_IABR7 registers indicate which interrupts are active.

A bit reads as one if the status of the corresponding interrupt is active or active and pending.

该寄存器只读,如果为1,则表示该位所对应的中断正在被执行或者执行期间被挂起。在中断执行完了由硬件自动清零。

Interrupt active flags:
0 = interrupt not active
1 = interrupt active.


6.IPR(Interrupt Priority Registers)

The NVIC_IPR0-NVIC_IPR59 registers provide an 8-bit priority field for each interrupt and each register holds four priority fields. These registers are byte-accessible.

中断优先级寄存器,该寄存器只能以字节访问。每个中断占用8位,所以每个寄存器可以表示4个中断优先级,如下图所示


虽然CM4内核提供了8位作为优先级的分配,但是好多采用此内核的单片机仅仅用到了其中的四位(例如STM32F4和K60系列)。这4位,又分为抢占优先级(组优先级)和响应优先级。抢占优先级在前,响应优先级在后。而这两个优先级各占几个位又要根据SCB->AIRCR中的中断分组设置来决定。

The lower the value, the greater the priority of the corresponding interrupt.数值越低,代表的中断优先级越高。


7.STIR(Software Trigger Interrupt Register)

Write to the STIR to generate an interrupt from software.

软件触发中断定时器,只有低八位可用。举个例子,当向该寄存器写0X03的时候,3号中断就被软件触发,如下图所示:



前面讲到抢占优先级(组优先级)和响应优先级(子优先级),下面详细地聊一聊这个优先级分组在中断控制中的作用。参考资料同样是《Cortex™-M4 Devices Generic User Guide》。在2.3.6章节(Interrupt priority grouping)讲解了这两个优先级的作用。原文如下:

To increase priority control in systems with interrupts, the NVIC supports priority grouping.
This divides each interrupt priority register entry into two fields:
• an upper field that defines the group priority(组优先级)
• a lower field that defines a subpriority within the group.(子优先级)

那么组优先级和子优先级是怎么确定中断执行的顺序的呢?原文如下:

Only the group priority determines preemption of interrupt exceptions. 

高优先级的抢占优先级是可以打断正在进行的低抢占优先级中断的。


When the processor is executing an interrupt exception handler, another interrupt with the same group priority as the
interrupt being handled does not preempt the handler

抢占优先级相同的中断,高响应优先级不可以打断低响应优先级的中断。


If multiple pending interrupts have the same group priority, the subpriority field determines the order in which they are processed. 

抢占优先级相同的中断,当两个中断同时发生的情况下,哪个响应优先级高,哪个先执行。


If multiple pending interrupts have the same group priority and subpriority, the interrupt with the lowest IRQ number is processed first.

如果两个中断的抢占优先级和响应优先级都是一样的中断同时发生的话,中断号低的中断先执行



中断优先级分组是由AIRCR寄存器的10:8位决定的,如下所示



PRIGROUP这三位决定了IPR(Interrupt Priority Registers)中的八位是怎么进行分组的,如下所示


当这三位设置为000的时候,IPR中的7位分配为组优先级,可以有128/种,第0位分配为子优先级,有2种

但是单片机厂家并不是IPR中的8位都用到,大多数仅仅用到了其中的四位,所以上表只能配置为一下几种 011 100 101  110 111。拿STM32举例,仅仅有5种分组方法


举个简单的例子,当设为NVIC_PriorityGroup_1,那么抢占式优先级便占一位,也就是说可以有2^1个级别,可以设置为0和1,而响应优先级则占3位,也就是说可以有2^3个选择,可以设置为0~7;总共来说就可以区别>16种优先级了,这就是优先级分组的好处。


最后,举个简单的例子供大家加深理解:

假定设置中断优先级组为2,然后设置

中断3的抢占优先级为2,响应优先级为1。 

中断6的抢占优先级为3,响应优先级为0。

中断7的抢占优先级为2,响应优先级为0。


假设中断3.7同时发生,那么中断7先执行

假设中断7已经再执行,这个时候中断3发生了,那么中断3不会打断中断7的执行

如果中断6正在执行,这个时候中断3发生了,则中断3优先处理,中断6被嵌套并挂起




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值