cm4 NVIC完全解析

原文鏈接:https://blog.csdn.net/wwwlyj123321/article/details/78662060

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被嵌套並掛起




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章