STM32异常与中断

本文介绍了中断的基本概念,包括中断的定义、中断源、中断类型和中断优先级。中断是处理器处理突发事件的方式,中断服务函数用于响应中断。文章还涉及了STM32的外部中断处理,如中断引脚配置和中断优先级设置。异常被视为另一种改变程序流程的事件,与中断相关,Cortex-M系列处理器的NVIC负责中断和异常的管理和优先级控制。
摘要由CSDN通过智能技术生成

一、中断的基本概念

中断的定义及中断工作方式

中断,即CPU在正常执行程序的过程中,遇到外部(例如常见的按键中断)/内部(例如定时器中断)的紧急事件需要处理,暂时中断(中止)当前程序的执行,而转去为事件服务,待服务完毕,再返回到暂停处(断点)继续执行原来的程序。

通俗的说:比如我正在写代码,老板突然给我一个任务,我暂停的写代码,转而把老板布置的任务完成之后,再继续写代码,这个过程就可以理解成中断。

从中断的定义中引出了以下几个新的概念:

1.事件的“偶然”性与“必然”性

例如,计算机键盘、鼠标的设置就为计算机系统增加了两个必然的“偶然”事件发生的机会。

通俗一点来讲,中断,意味着中途打断现在干的事情,要立即处理紧急的事件。

现实的例子:手机玩游戏的时候,突然来电话。在编程当中还常遇到实时接收数据的请求,都使用中断服务函数,示例如下:

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/1DFEE4494A174FEEBC7B9A94D212B415/144950

 2、中断的作用

  • 提高硬件响应,及时处理。

3、中断源(中断触发的硬件)

(1)中断源:介于事件与CPU之间的电路模块。

(2)中断请求信号:事件引起的,由中断源产生的,能被单片机识别的信号。

4、中断类型

中断产生来源于事件。因此根据事件来源地,将中断分为外部中断和内部中断两种类型。

外部中断是指由单片机外部事件引发的中断。

内部中断是指由单片机芯片内部事件引发的中断。

5、中断优先级

事件具有不同的轻重、缓急程度。系统工作时,我们总希望最紧急的事件优先被处理,以保证系统的实时性。这就引出了中断的优先级、中断嵌套问题。

主要用于排序多个中断的请求。

与中断控制器相连的每条线叫做中断线,要使用中断线,就得进行中断线的申请,就是IRQ,把申请一条中断线称为申请一个IRQ或者是申请一个中断号。IRQ和向量之间的映射可以通过中断控制器进行修改。

二、异常与中断

(一)概述

《Cortex M3与M4权威指南》章节4.5 P104

Exceptions are events that cause changes to program flow. When one happens, the processor suspends the current executing task and executes a part of the program called the exception handler. After the execution of the exception handler is completed, the processor then resumes normal program execution. In the ARM architecture, interrupts are one type of exception. Interrupts are usually generated from peripheral or external inputs, and in some cases they can be triggered by software. The exception handlers for interrupts are also referred to as Interrupt Service Routines (ISR)。

Each exception source has an exception number. Exception numbers 1 to 15 as system exceptions, and exceptions 16 and above are for interrupts. The design of the NVIC in the Cortex-M3 and Cortex-M4 processors can support up to 240 interrupt inputs. However, in practice the number of interrupt inputs imple mented in the design is far less, typically in the range of 16 to 100. In this way the silicon size of the design can be reduced, which also reduces power consumption。

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/WEBRESOURCE092ddf389419a31f8a160ac84746b75c/144970

Nested vectored interrupt controller (NVIC,嵌套向量中断控制器)

The NVIC is a part of the Cortex-M processor. It is programmable and its registers are located in the System Control Space (SCS) of the memory map

The NVIC handles the exceptions and interrupt configurations, prioritization, and interrupt masking. The NVIC has the following features:

• Flexible exception and interrupt management

• Nested exception/interrupt support

• Vectored exception/interrupt entry

• Interrupt masking

专业英文

priority,/praɪˈɔːrəti/,优先级

prioritization,/praɪˌɔːrətəˈzeɪʃn/,优先次序

preempt,/priˈempt/,抢占

二)异常类型

《Cortex M3与M4权威指南》章节7.2 P232

1.系统异常

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/B7A908558D574710A7F87C4502E86BD7/144952

2、中断

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/8C4EC0A1617D4F498AF477D113583B0F/144953

(三)中断控制

《Cortex M3与M4权威指南》章节7.3 P235

 

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/0B3757C257CB447A919ECF25BD2977EF/144954

After reset, all interrupts are disabled and given a priority-level value of 0. Before using any interrupts, you need to:

  • Set up the priority level of the required interrupt (this step is optional)
  • Enable the interrupt generation control in the peripheral that triggers the interrupt
  • Enable the interrupt in the NVIC

In most typical applications, this is all you need to do. When the interrupt triggers, the corresponding Interrupt Service Routine (ISR) will execute (you might need to clear the interrupt request from the peripheral within the handler). The name of the ISR can be found inside the vector table inside the startup code, which is also provided by the microcontroller vendor. The name of the ISR needs to match the name used in the vector table so that the linker can place the starting address of the ISR into the vector table correctly.

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/WEBRESOURCE50de7cbe9f7ca8a4ba0b249b01908507/144968

(四)向量表(Vector table)

《Cortex M3与M4权威指南》章节4.5.3 P107

When an exception event takes place and is accepted by the processor core, the corresponding exception handler is executed. To determine the starting address of the exception handler, a vector table mechanism is used. The vector table is an array of word data inside the system memory, each representing the starting address of one exception type。

三、STM32 的外部中断

1.中断引脚

多达 140 个 GPIO(STM32F405xx/07xx 和 STM32F415xx/17xx)通过以下方式连接到 16 个外部中断/事件线。

例如:PA0占用了EXTI0,其他PB0~PI0是不能使用的。

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/A7D3A237424E459FA5205593EBB923DA/144955

 引脚编号决定了对应哪个外部中断。

四、代码思路

1.8051单片机

外部中断的触发方式:低电平触发、下降沿触发 IT0=1

允许外部中断引脚申请中断请求 EX0=1

优先级的配置

中断服务函数

2.STM32

端口A硬件时钟使能

SYSCFG硬件时钟使能

配置引脚的工作模式

将引脚连接到外部中断

中断触发方式:下降沿触发、上升沿触发

允许外部中断引脚申请中断请求

优先级的配置

中断服务函数

注:

中断服务函数是不能被调用,编写格式不能随意编写,这是它特有的存在形式。不同的硬件平台,其编写方法是不一样的。

[拓展]唤醒事件管理

STM32F4xx 能够处理外部或内部事件来唤醒内核 (WFE)。唤醒事件可通过以下方式产生:

  • 在外设的控制寄存器使能一个中断,但不在 NVIC 中使能,同时使能 Cortex™-M4F 系统 控制寄存器中的 SEVONPEND 位。当 MCU 从 WFE 恢复时,需要清除相应外设的中断挂起位和外设 NVIC 中断通道挂起位(在 NVIC 中断清除挂起寄存器中)。
  • 配置一个外部或内部 EXTI 线为事件模式。当 CPU 从 WFE 恢复时,因为对应事件线的挂起位没有被置位,不必清除相应外设的中断挂起位或 NVIC 中断通道挂起位。使用外部线作为唤醒事件,请参见stm32中文参考手册中的第 10.2.4 节:功能说明。

 参考代码

EXTI_InitStructure.EXTI_Line=EXTI_Line0;//EXTI0

EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Event;//事件模式

EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;//下降沿触发,就是用于检测按键的按下

EXTI_InitStructure.EXTI_LineCmd=ENABLE;//使能EXTI0,让EXTI0工作、

EXTI_Init(&EXTI_InitStructure);

五、函数接口

1.为引脚选择使用哪个中断

/**

* @brief Selects the GPIO pin used as EXTI Line.

* @param EXTI_PortSourceGPIOx : selects the GPIO port to be used as source for

* EXTI lines where x can be (A..K) for STM32F42xxx/43xxx devices, (A..I)

*         for STM32F405xx/407xx and STM32F415xx/417xx devices or (A, B, C, D and H)

*         for STM32401xx devices.

*

* @param EXTI_PinSourcex: specifies the EXTI line to be configured.

*         This parameter can be EXTI_PinSourcex where x can be (0..15, except

*         for EXTI_PortSourceGPIOI x can be (0..11) for STM32F405xx/407xx

*         and STM32F405xx/407xx devices and for EXTI_PortSourceGPIOK x can

*         be (0..7) for STM32F42xxx/43xxx devices.

*

* @retval None

*/

void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex)

2.配置外部中断

/**

* @brief Initializes the EXTI peripheral according to the specified

*         parameters in the EXTI_InitStruct.

* @param EXTI_InitStruct: pointer to a EXTI_InitTypeDef structure

*         that contains the configuration information for the EXTI peripheral.

* @retval None

*/

void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct)

3.中断优先级配置

/**

* @brief Initializes the NVIC peripheral according to the specified

* parameters in the NVIC_InitStruct.

* @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()

* function should be called before.

* @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)

4、获取外部中断状态

/**

* @brief Checks whether the specified EXTI line is asserted or not.

* @param EXTI_Line: specifies the EXTI line to check.

*         This parameter can be EXTI_Linex where x can be(0..22)

* @retval The new state of EXTI_Line (SET or RESET).

*/

ITStatus EXTI_GetITStatus(uint32_t EXTI_Line)

5.清空外部中断标志位

/**

* @brief Clears the EXTI's line pending bits.

* @param EXTI_Line: specifies the EXTI lines to clear.

*         This parameter can be any combination of EXTI_Linex where x can be (0..22)

* @retval None

*/

void EXTI_ClearITPendingBit(uint32_t EXTI_Line)

六、中断优先级

        中断优先级的一个意义:出现多个中断同时触发,但是不能同时处理,所以先后顺序之分,要根据实际上的运行环境优先处理重要的中断。

1.概述

        STM32 对中断优先级进行分组,共 5 组,组 0~4,这些分组是用于指定当前M4支持多少级抢占优先级和多少个响应优先级。同时,对每个中断设置一个抢占优先级和一个响应优先级。中断优先判断抢占优先级,若抢占优先级相同,则判断响应优先级。若抢占优先级和响应优先级都一样,则判断硬件的优先级。函数原型如下:

/**

* @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 //支持16级响应优先级(0 ~ 15)

*         @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority //支持2级抢占优先级(0 ~ 1)

*                                                         3 bits for subpriority //支持8级响应优先级(0 ~ 7)

*         @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority //支持4级抢占优先级(数值0~3)

*                                                         2 bits for subpriority //支持4级响应优先级(数值0~3)

*         @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority //支持8级抢占优先级

*                                                         1 bits for subpriority //支持2级响应优先级

*         @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority //支持16级抢占优先级(数值0~15)

*                                                         0 bits for subpriority //不支持响应优先级

* @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible. *                        The pending IRQ priority will be managed only by the subpriority.

* @retval None

*/

void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)

只要开机初始化一次就可以了。

如果使用FreeRTOS操作系统,它只支持第四组,也就是只支持抢占优先级。

2.中断嵌套

所谓中断嵌套,就是中断抢占机制,允许高优先级中断源抢占正在执行的低优先级中断。

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/WEBRESOURCE9259f389a40475433cbe2cad44e5a8b7/150205

3.抢占优先级与响应优先级区别

1)高抢占优先级是可以打断正在进行的低抢占优先级的中断。抢占优先级若相同,则不会出现抢占的过程。

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/775CF73D47A94D5BA8ED00B65BF05858/144956

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

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/D8AC0DAE11C24ED8803546F365351468/144957

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

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/8A6AF218F3974E1F92056784715A985B/144958

 4)抢占优先级相同且响应优先级相同的中断,假如同时发生,会按照硬件内部固定的优先级执行,如下图。

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/6FF424CCC8EC485A8C4819CAAD8FB0E7/144959

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/7BF5498F993D4FED964A600A1693BBBE/144960

 5)无论是抢占优先级、响应优先级还是硬件内部的优先级,优先级数值越小,就代表优先级越高。

七、中断服务函数

中断服务函数要简单、高效完成,以下的delay函数是为了方便观察中断现象,在实际项目开发过程,是不会这么做的。

https://note.youdao.com/yws/public/resource/b994246549f84052c3cad3ea416ccf64/xmlnote/BC782AC1EDD14321B99FEE73C320796F/3A6A7975A1754784A1A7B7E36E3C9373/144961

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿文的储物间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值