当前位置:网站首页>SCM learning notes 6 -- interrupt system (based on Baiwen STM32F103 series tutorials)
SCM learning notes 6 -- interrupt system (based on Baiwen STM32F103 series tutorials)
2022-07-24 01:21:00 【Mountains】
Chapter six interrupt
Section 1 The concept of interruption
Simply speaking , That is, he can interrupt the task currently being performed by the system , Give Way CPU Give priority to this task , When this task is over ,CPU To return to the place where I was interrupted just now and re execute the program .
Interrupts are generally divided into System exception and External interrupt . Usually , hold CPU An internal emergency is called an exception , Such as illegal instructions ( Divide by zero )、 Address access cross-border, etc ; Put the CPU An emergency event caused by an external on-chip peripheral is called an interrupt , such as GPIO Pin level change 、 Timer overflow, etc .
In the second quarter Interrupts and priorities
2.1Cortex-M3 Interrupts and priorities
Cortex-M3 Shared kernel 0-255, common 256 A break , Which number 015 It's a system exception **,**16255 It's an external interrupt . Each interrupt has its own specific trigger condition , When their trigger conditions are met , The system will be interrupted ,CPU It will jump to the corresponding interrupt service function to execute .
The name of the system exception is determined , The manufacturer cannot modify . External interrupts can define their own interrupt function names . Priority is the status of each interrupt 、 The level of importance , The more important the interruption , The higher his priority . When several interrupts occur at the same time ,CPU Will give priority to the execution of higher priority interrupts , Interrupts with relatively low priority can only be executed after the execution of interrupts with high priority . stay Cortex-M3 The kernel The lower the priority value , The higher his priority . Details can be found 2_ Official information \10.0_Cortex-M3 Authoritative guide .

As mentioned above, when multiple interrupts occur at the same time , The system will give priority to interrupts with higher priority , So how does the system manage and judge the priority of each interrupt .
Cortex-M3 The kernel has a peripheral dedicated to managing interrupts NVIC (Nested Vectored Interrupt Controller, Nested vector interrupt controller ) It is used to control the priority of various interrupts to realize the nesting and scheduling of interrupts .
NVIC Is a total interrupt controller , Whether you come or not The exception of the kernel is also the external interrupt of the peripheral , All by NVIC Unified management .
except Reset( Reset )、NMI(Non Maskable Interrupt, Do not mask interrupts )、HardFault( Hardware exception 〉, Their priorities are fixed , And the priority is negative , The highest. . The remaining exceptions or interrupts , Can be modified NVIC Register adjustment priority ( but Cannot be set to a negative number , The minimum is 0).
At the same time Cortex-M3 in , Split the priority into preemption (Preempt Priority) and Sub priority (Subpriority) , Each interrupt needs to specify these two levels .
preemption It's a decision preemption Of . This preemption is for interrupts , Interrupts with high preemption priority can CPU Grab it . If CPU Executing an interrupt function , Preempting interrupts with higher priority can interrupt running interrupts , Give Way CPU Run interrupts with higher priority .
Sub priority It determines the order of operation , He There is no preemption function , When the preemption priority of two interrupts is the same , Interrupts with higher sub priority run first , But if the interrupt with low sub priority is executing , Interrupts with high sub priority can only be executed after the execution of interrupts with low sub priority , Because the sub priority has no preemption function .
if The two priorities are the same, and the position of interrupts in the interrupt exception table , The more forward, the more responsive . For example, the number in the figure above 16 Of IRQ_0 And number 255 Of IRQ_239 At the same time , Number 16 Of IRQ_0 priority .
Through these two priorities , The system can realize Break nesting .
Both priorities pass 8 individual Bit To control . High location is the priority , Low location is sub priority .

For example, choose PRIGROUP = 4; Then preempt priority 7-5 position , common 2^3 = 8 Priority ; Sub priority accounts for 4-0 position , common 2^5 = 32 Priority .

It should be noted that , For priority grouping , in the majority of cases , Once the grouping is set , There is no need to reset this register before resetting . That is to say, priority groups should be set at the beginning of the program , Don't change in the future .
2.2STM32 Interrupts and priorities
1、 Break the introduction
STM32 Is based on Cortex-M3 Kernel , therefore STM32 The interrupt 、 Priority settings and their features and Cortex-M3 The kernel is the same , It's just STM32 Cut it . because Cortex-M3 The design has 256 Kind of interrupt , But most MCU It doesn't take so many interrupts , such as STM32F103 The series only has 70 There are exceptions and interrupts , The top 15 It's an exception ( Yes 5 Reserved ), Back 60 One is an external interrupt .

STM32 The interruption of has not changed much , Just remove some external interrupts that can be changed , At the same time, the interrupt numbers of all interrupts are rearranged .STM32 Medium Reset and NMI It belongs to the interrupt when the system starts , So the interrupt number is not set , And then from HardFault The starting interrupt number is from -13 Start increasing up . The external interrupt number starts from 0 It's starting to increase .
2、 Priority introduction
STM32F103 Inherited Cortex-M3 Interrupt priority rule , It also uses preemptive priority and sub priority to control interrupt priority . But because STM32F103 There are many fewer interruptions , So interrupt priority can't be used that much , Use only the 8 individual Bit Medium 4 To set .

In the third quarter Use HAL Library configuration interrupted
3.1 Correlation function
stay HAL In the library , There are several functions about interrupts .

HAL_NVIC_SetPriorityGrouping: Set interrupt priority grouping , Determine the range of preemption priority and sub priority .
HAL_NVIC_SetPriority: Set the preemptive priority and sub priority of a specific interrupt .
HAL_NVIC_EnableIRQ: Enable a specific interrupt .
HAL_NVIC_DisableIRQ: Close a specific interrupt .
void XXX_IROHandler(void): A specific interrupt service function .
HAL_XXX_IROHandler(type para):HAL Interrupt service function in Library , stay XXX_IROHandler In the called , What is executed inside is to clear the specific interrupt flag and obtain some states , A callback function for the user will also be called HAL_XXX_Callback.
HAL_XXX_Callback:HAL The callback function called in the interrupt service function in the Library , Users can handle some additional tasks here .
3.2 Usage flow
The above analysis shows that , If you want to use interrupts to achieve your own tasks , Just three steps :
1、 System interrupt service function relocation ,XXX_IROHandler(void) It is the interrupt function set by the system kernel , This function will be called when an interrupt occurs .
You can see that they all have weak modification ,weak The function is simply when you write a function with the same name as this function , The system will give priority to calling the functions written by yourself ; If you don't rewrite this function , Then the system calls this function by default .

2、 In the relocated system interrupt processing function call HAL The corresponding interrupt handling service function of the Library . So when an interrupt occurs , Actually, the function executed is HAL Functions in the Library .

3、 stay HAL Called in the interrupt service function of the Library Write your own code in the callback function , Achieve the task . You can see HAL The interrupt function in the library will call a callback function after internally clearing the interrupt flag , This callback function is HAL The library is prepared for users . Users write their own code in it , Realize the required functions .
So when an interrupt occurs , The flow of interrupt function is to call back and forth , System function call HAL Library function ,HAL The library function calls the callback function .
Be careful HAL_XXX_Callback: This callback function also uses weak Modified virtual function , So you can write this function directly , You can also find the specific location of this function in the file , Add specific code inside .

边栏推荐
- Kotlin基础从入门到进阶系列讲解(基础篇)关键字:suspend
- RIP(第二天笔记)
- Sublime text 3 汉化+添加常用插件
- Skywalking distributed system application performance monitoring tool - upper
- Use and understanding of string functions (2)
- [reply] about the fact that I chose the wrong technology at the wrong time
- Kotlin foundation from introduction to advanced series explanation (basic chapter) keyword: suspend
- Basic exercises of C language for beginners
- 复制可读路径不好使
- Idea setting automatic package import and useless package deletion
猜你喜欢

Idea setting automatic package import and useless package deletion

C language database: detailed description. Use the student management system to understand the operation of the database, which is simple and easy to understand.

Deep understanding of collaborative process

Collation of key knowledge of high voltage technology

LVS load balancing scheduling principle and configuration method

Skywalking distributed system application performance monitoring tool - upper

爬虫请求库的使用2
出于数据安全考虑 荷兰教育部要求学校暂停使用Chrome浏览器

HCIP第二天笔记

HCIP第六天笔记
随机推荐
HCIA知识点总结
国产MCU和SOC崛起但特殊领域仍落后
数字化转型时代的企业数据新基建 | 爱分析报告
URL query parameter encoding problem (golang)
B tree and b+ tree
【複盤】關於我在錯誤的時間選錯了技術這件事
OSPF(第六天笔记)
Understanding of flexible array in C language
RIP(第二天笔记)
128. Longest continuous sequence
HCIP实验
C语言力扣第53题之最大子数组和。动态规划与分治
Solve the problem that the double click of Google Chrome browser doesn't respond and can't be started (friendly testing)
SkyWalking分布式系统应用程序性能监控工具-上
2022全球开发者薪资曝光:中国排第19名,平均年薪23,790美元
IDEA 编译器 设置方法之间分割线隔开
Use and understanding of string functions (1)
Talk about moment of inertia
好大夫问诊-俞驰-口腔信息
Concurrent programming 1-2