嵌入式开发中断全解(1)


一、中断的基本概念

中断的定义及中断工作方式
中断,即CPU在正常执行程序的过程中,遇到外部/内部的紧急事件需要处理,暂时中断(中止)当前程序的执行,而转去为事件服务,待服务完毕,再返回到暂停处(断点)继续执行原来的程序。
通俗的说:比如我正在写代码,老板突然给我一个任务,我暂停写代码,转而把老板布置的任务完成之后,再继续写代码,这个过程就可以理解成中断。
从中断的定义中引出了以下几个新的概念:

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

例如,计算机键盘、鼠标的设置就为计算机系统增加了两个必然的“偶然”事件发生的机会。
在这里插入图片描述
通俗一点来讲,中断,意味着中途打断现在干的事情,要立即处理紧急的事件。
现实的例子:手机玩游戏的时候,突然来电话。在编程当中还常遇到实时接收数据的请求,都使用中断服务函数,示例如下:

在这里插入图片描述

2.中断的作用

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

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

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

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

4.中断类型

中断产生来源于事件。因此根据事件来源地,将中断分为外部中断和内部中断两种类型。
外部中断是指由单片机外部事件(外部设备)引发的中断。例如:IO口输入为高、IO口输入为低、IO口输入由高变为低、IO口输入由低变为高、IO口输入由高变低或者由低变高。
内部中断是指由单片机芯片内部事件引发的中断。

5.中断优先级

事件具有不同的轻重、缓急程度。系统工作时,我们总希望最紧急的事件优先被处理,以保证系统的实时性。这就引出了中断的优先级、中断嵌套问题。
例如:中断优先级:触摸屏事件 得到数据 小于网络数据包事件。
不懂的看看这篇,我觉得写的挺不错的!!!
gd32f303 设计中断优先级_不懂中断,你就别玩单片机了!

二、异常与中断

(一)概述

《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。
异常是导致程序流更改的事件。当发生异常时,处理器暂停当前正在执行的任务,并执行程序中称为异常处理程序的一部分。异常处理程序的执行完成后,处理器将恢复正常的程序执行。在ARM体系结构中,中断是一种异常类型。中断通常由外设或外部输入产生,在某些情况下,它们可以由软件触发。中断的异常处理程序也称为中断服务例程(ISR)
每个异常源都有一个异常编号。异常编号1至15为系统异常,异常编号16及以上为中断。Cortex-M3和Cortex-M4处理器中的NVIC设计可支持多达240个中断输入。然而,实际上,在设计中实现的中断输入数量要少得多,通常在16到100之间。这样可以减小设计中的硅尺寸,从而降低功耗。(看着肯定很懵逼~)
在这里插入图片描述
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
异常包含中断。中断时异常的一部分,中断通常有外围设备:定时器、看门狗、ADC、spi等 ,也可以由引脚来触发。
(有时候会问到是芯片内部外设或者Cotex-M的外设,芯片内部外设例如:SPI、I2C、串口、内部FLASH、ADC、RTC等,Cotex-M核心架构外外设就是上图:SysTick timer系统滴答时钟、NVIC)
软件中断
以上可称为中断服务函数。

(二)异常类型

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

1.系统异常

在这里插入图片描述

2.中断

在这里插入图片描述

(三)中断控制

在这里插入图片描述
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.

(四)向量表(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是不能使用的。
在这里插入图片描述
在这里插入图片描述
引脚编号决定了对应哪个外部中断

四、代码思路

1.8051单片机
外部中断的触发方式:低电平触发、下降沿触发 IT0=1
允许外部中断引脚申请中断请求 EX0=1
优先级的配置
中断服务函数

2.STM32
端口A硬件时钟使能
SYSCFG硬件时钟使能
配置引脚的工作模式
将引脚连接到外部中断
中断触发方式:电平触发、边沿触发
允许外部中断引脚申请中断请求
优先级的配置
中断服务函数

注:
中断服务函数是不能被调用,编写格式不能随意编写,这是它特有的存在形式。不同的硬件平台,其编写方法是不一样的。
例如:基本都是void 中断名(void) 注意:中断服务函数是没有传参数的,所以括号里面都是(void)

五、函数接口

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级响应优先级
  *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority    //支持2级抢占优先级
  *                                3 bits for subpriority             //支持8级响应优先级
  *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority    //支持4级抢占优先级
  *                                2 bits for subpriority             //支持4级响应优先级
  *     @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 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)

只要开机初始化一次就可以了
2.抢占优先级与响应优先级区别
数字越小优先级越高
1)高抢占优先级是可以打断正在进行的低抢占优先级的中断。抢占优先级若相同,则不会出现抢占的过程。
在这里插入图片描述
2)抢占优先级相同的中断,高响应优先级不可以打断低响应优先级的中断。
在这里插入图片描述
3)抢占优先级相同的中断,当两个中断同时发生的情况下,哪个响应优先级高,哪个先执行。

在这里插入图片描述
4)抢占优先级和响应优先级完全相同的中断,假如同时发生,会按照硬件内部固定的优先级执行,如下图。
在这里插入图片描述
在这里插入图片描述
5)无论是抢占优先级、响应优先级还是硬件内部的优先级,优先级数值越小,就代表优先级越高。

地址存储,向量表
七、中断服务函数

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

总结

以上先知道有这么多东西,中断其实就时感觉自己好像懂了这么回事,就是代码不会写,就记住几个重要点,初始化(时钟、引脚配置或者有的可以直接引用)、中断函数、中断函数里面有清除标志位,这点很重要,还有就是优先级的问题。感觉还有很多需要去深刻了解的,比如向量表、独立中断表等,往后继续一起探讨,后面也会列举一些列子和自己学的时候遇到的问题。

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
嵌入式软件的开发流程。第一步:工程建立和配置。第二步:编辑源文件。第三步:工程编译和链接。第四步:软件的调试。第五步:执行文件的固化。   在整个流程中,用户首先需要建立工程并对工程做初步的配置,包括配置处理器和配置调试设备。编辑工程文件,包括自己编写的汇编和C语言源程序,还有工程编译时需要编写的链接脚本文件,调试过程中需要编写存储区映像文件和命令脚本文件,以及上电复位时的程序运行入口的启动程序文件。   对后四种文件的理解很重要,其作用解释如下: (1) 链接脚本文件:在程序编译时起作用。该文件描述代码链接定位的有关信息,包括代码段,数据段,地址段等,链接器必须使用该文件对整个系统的代码做正确的定位。在SDRAM中调试程序、在FLASH中调试或固化后运行的链接脚本文件应加以区分。(在IDE开发环境中使用扩展名*.ld) (2)命令脚本文件:在SDRAM中调试程序时起作用。在集成环境与目标连接时、软件调试过程中以及目标板复位后,有时需要集成环境自动完成一些特定的操作,比如复位目标板、清除看门狗、屏蔽中断寄存器、存储区映射等。这些操作可以通过执行一组命令序列来完成,保存一组命令序列的文本文件称为命令脚本文件(在 IDE开发环境中使用扩展名*.cs)。 (3)存储区映像文件:在SDRAM中调试程序时起作用。在软件调试过程中访问非法存储区在部分处理器和目标板上会产生异常,如果异常没有处理,则会导致软件调试过程无法继续,为了防止以上问题并调整仿真器访问速度以达到最合适的水平,提供这样一种用于描述各个存储区性质的文件叫存储区映像文件(在IDE开发环境中使用扩展名*.map)。 在程序的调试过程中可以选择使用存储区映像文件*.map和命令脚本文件*. cs配合程序的调试。 (4) 启动文件:它主要是完成一些和硬件相关的初始化的工作,为应用程序做准备。一般,启动代码的第一步是设置中断和异常向量;第二步是完成系统启动所必须的寄存器配置;第三步设置看门狗及用户设计的部分外围电路;第四步是配置系统所使用的存储区分配地址空间; 第五步是变量初始化;第六步是为处理器的每个工作模式设置栈指针;最后一步是进入高级语言入口函数(Main函数)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Yank_k

点个关注加分享,一起探讨学习!

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

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

打赏作者

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

抵扣说明:

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

余额充值