i.MX6ULL是NXP(原Freescale)推出的一款基于ARM Cortex-A7内核的微处理器,广泛应用于嵌入式系统。在i.MX6ULL中,中断(Interrupt)是一种重要的机制,用于处理外部或内部事件,允许微处理器在执行当前任务的同时,能够及时响应其他紧急任务或外部设备的请求。
中断的概念:
中断是计算机系统中的一种关键机制,它允许在程序执行过程中,当特定事件或条件发生时,处理器能够暂停当前正在运行的程序,转而去处理这些紧急或高优先级的事件。处理完这些事件后,处理器会返回到被中断的程序继续执行。
中断机制是现代计算机系统的基础,它使得系统能够及时响应外部事件,如用户输入、设备状态变化等,同时也支持多任务操作系统中的进程调度和系统稳定性。
计算机系统中两种基本的事件处理机制
轮询和中断
轮询方式(Polling)
轮询是一种被动的事件处理机制,CPU不断地检查某个事件是否发生,如果发生了就进行处理。
优点:
- 实现简单,不需要操作系统或硬件的额外支持。
缺点:
- 效率低下,因为CPU在等待事件的过程中会做很多无用功。
- 响应时间可能较长,因为CPU需要不断地检查,而不是在事件发生时立即响应。
中断方式(Interrupt)
中断是一种主动的事件处理机制,当某个设备或进程需要CPU注意时,它会发送一个信号(中断信号),CPU在接收到信号后会暂停当前的工作,转而去处理这个信号,处理完毕后再回到原来的地方继续执行。
优点:
- 效率更高,CPU不需要不断地检查事件是否发生。
- 响应时间快,因为CPU可以在事件发生的瞬间立即响应。
缺点:
- 实现复杂,需要操作系统和硬件的支持。
- 可能会影响系统的稳定性,如果中断处理不当,可能会导致系统崩溃。
中断处理需要注意的事项
快速响应:中断处理程序应该尽可能快地执行,以减少对其他程序执行的干扰。
保存和恢复现场:在进入中断处理程序之前,需要保存当前CPU的状态(包括程序计数器、寄存器等),这称为“保存现场”。中断处理完毕后,需要恢复这些状态,以便CPU能够回到中断发生前的状态继续执行,这称为“恢复现场”。
中断优先级:多个中断可能同时发生,需要根据中断的优先级来决定处理的顺序。
中断嵌套:如果一个中断处理程序正在执行时又发生了另一个中断,需要正确处理这种嵌套中断的情况。
资源共享:如果多个中断共享相同的资源,需要确保访问的同步,避免竞态条件。
错误处理:中断处理程序应该能够处理错误情况,确保系统稳定运行。
ARM核中断处理过程
.global _start_start:b resetldr pc,_undefined_instructionldr pc,_software_interrupt @B software_interruptldr pc,_prefetch_abortldr pc,_data_abortldr pc,_not_usedldr pc,_irqldr pc,_fiq
_undefined_instruction:.word _undefined_instruction
_software_interrupt:.word software_interrupt
_prefetch_abort:.word _prefetch_abort
_data_abort:.word _data_abort
_not_used:.word _not_used
_irq:.word irq
_fiq:.word _fiqreset:@告诉ARM核异常向量表所在的基地址adr r0,_start @获得异常向量表所在的地址mcr p15,0,r0,c12, c0, 0 @将异常向量表的基基地址写入cp15.c12ldr sp,=0x87800000bl mainstop:b stopsoftware_interrupt:ldr sp,=0x81000000stmfd sp!,{r0-r12,lr}ldr r0,[lr,#-4]mov r1,#0xffbic r0,r0,r1,lsl #24ldmfd sp!,{r0-r12,pc}^irq:ldr sp,=0x82000000sub lr,lr,#4stmfd sp!,{r0-r12,lr}bl do_irqldmfd sp!,{r0-r12,pc}^
中断的基本概念:
中断源
中断源是指能够触发中断的事件或信号的来源。这些可以是硬件设备(如键盘、鼠标、网络接口卡等),也可以是软件事件(如程序执行特定的系统调用)。中断源是产生中断的“源头”。
中断号
中断号是系统对中断源的唯一标识符。在SOC(System on Chip,系统级芯片)中,芯片厂家会为内部的中断源分配一个编号,这个编号用于识别和区分不同的中断。当中断发生时,处理器会根据中断号来确定是哪一个中断源触发了中断,并调用相应的中断处理程序。
中断处理函数
中断处理函数(Interrupt Service Routine, ISR)是在中断发生后被调用的函数,用于处理中断。这个函数需要快速执行,以减少对系统其他部分的影响。
中断处理函数通常会完成以下任务:
- 保存当前任务的状态。
- 处理中断事件。
- 恢复任务的状态。
中断控制器
中断控制器是硬件设备,用于管理中断信号。它负责以下任务:
- 控制中断的优先级,决定哪些中断可以被立即处理,哪些需要等待。
- 决定哪些中断是允许的,哪些是禁止的。
- 处理多个中断源的中断请求,并将它们传递给处理器。
中断的两种基本类型:
内部中断
内部中断是由系统内部的事件或条件触发的中断。这些中断通常与系统的硬件或软件状态有关,不依赖于外部的物理信号。
- GPT定时器:通用目的定时器(General-Purpose Timer)溢出时产生的中断,用于时间管理。
- 看门狗定时器:当系统没有在规定时间内“喂狗”(即重置定时器),看门狗定时器会溢出并触发中断,用于系统监控。
- 软件生成的中断:通过软件指令(如SWI或SVC)触发的中断,用于系统调用或异常处理。
外部中断
外部中断是由系统外部的物理信号触发的中断。这些中断依赖于外部设备或环境的变化,如按钮按下、传感器信号等。
- 高电平触发:当外部管脚的电平达到高电平时触发中断。
- 低电平触发:当外部管脚的电平达到低电平时触发中断。
- 上升沿触发:当外部管脚的电平从低变高时触发中断。
- 下降沿触发:当外部管脚的电平从高变低时触发中断。
- 双边沿触发:无论电平是从低变高还是从高变低,都会触发中断。
这些触发方式可以根据具体的应用需求进行配置。例如,一个按钮可能被配置为在按下时(电平变化)触发中断,而一个通信接口可能在数据帧的开始(电平上升沿)触发中断。
外部中断通常需要外部中断控制器来管理,这个控制器会根据中断信号的优先级和状态,将中断请求发送给SOC的中断控制器,然后由中断控制器传递给处理器。
GIC通用中断控制器
GIC 全称是 Generic Interrupt Controller是 ARM 公司给 Cortex-A/R 内核提供的一个中断控制器,目前 GIC 有 4 个版本:V1~V4,V1 是最老的版本,已经被废弃了。GIC V2 是给 ARMv7-A 架构使用的,比如 Cortex-A7、Cortex-A9、Cortex-A15 等,V3 和 V4 是给 ARMv8-A/R 架构使用的,也就是 64 位芯 片使用的。
在Cortex-M 内核中,ARM设计了NVIC(Nested Vectored Interrupt Controller:内嵌向量中断控制器)中断控制器.
The Generic Interrupt Controller (GIC) architecture defines:(通用中断控制器(GIC)架构定义)
• registers for managing interrupt sources, interrupt behavior, and interrupt routing to one or more processors用于管理中断源、中断行为和中断路由到一个或多个处理器的寄存器• support for:— the ARM architecture Security ExtensionsARM架构安全扩展— the ARM architecture Virtualization ExtensionsARM架构虚拟化扩展— enabling, disabling, and generating processor interrupts from hardware (peripheral) interrupt sources从硬件(外设)中断源启用、禁用和生成处理器中断— Software-generated Interrupts (SGIs)软件生成中断(SGIs)— interrupt masking and prioritization中断屏蔽和优先级— uniprocessor and multiprocessor environments单处理器和多处理器环境— wakeup events in power-management environments.电源管理环境中的唤醒事件
The GIC includes interrupt grouping functionality that supports:GIC包括支持的中断分组功能:
• configuring each interrupt as either Group 0 or Group 1将每个中断配置为组0或组1• signaling Group 0 interrupts to the target processor using either the IRQ or the FIQ exception request信令组0中断到目标处理器使用IRQ或FIQ异常请求
• signaling Group 1 interrupts to the target processor using the IRQ exception request only信令组1只使用IRQ异常请求中断到目标处理器• a unified scheme for handling the priority of Group 0 and Group 1 interrupts处理组0和组1中断优先级的统一方案• optional lockdown of the configuration of some Group 0 interrupts.可选锁定某些组0中断的配置
GIC的构成
GIC支持的中断类型
Peripheral interrupt This is an interrupt asserted by a signal to the GIC. The GIC architecture defines the following types of peripheral interrupt:这是由信号向通用中断控制器(GIC)发起的中断请求。 GIC体系结构定义了下列外设中断类型。Private Peripheral Interrupt (PPI) 私有外设中断This is a peripheral interrupt that is specific to a single processor.指仅能被单个CPU核心识别和处理的中断类型,通常用于处理核心私有的或高优先级的中断。Shared Peripheral Interrupt (SPI) 共享外设中断This is a peripheral interrupt that the Distributor can route to any of a specified combination of processors.指可以被多个CPU核心识别和处理的中断类型,允许中断分发器根据配置将中断路由到一个或多个CPU核心。Each peripheral interrupt is either:每个外设中断可以是以下两种类型之一边沿触发(Edge-triggered):This is an interrupt that is asserted on detection of a rising edge of an interrupt signal and then, regardless of the state of the signal, remains asserted until it is cleared by the conditions defined by this specification.指的是中断信号的边沿变化(如从低到高)触发中断,一旦触发,即使信号继续维持在高电平,中断状态也会保持,直到通过特定的清除条件来重置。电平敏感(Level-sensitive):This is an interrupt that is asserted whenever the interrupt signal level is active, and deasserted whenever the level is not active.指的是中断信号的电平状态触发中断,如果信号电平保持在触发电平,中断就会持续有效,直到信号电平改变,不再满足触发条件。tips:当电平敏感型中断被断言时,其在GIC(通用中断控制器)中的状态为挂起(pending),或者活动且挂起。如果外设由于任何原因取消断言(deasserts)中断信号,GIC将从中断中移除挂起状态。
Software-generated interrupt (SGI) 软件生成中断
This is an interrupt generated by software writing to a GICD_SGIR register in the GIC. The
system uses SGIs for interprocessor communication.
An SGI has edge-triggered properties. The software triggering of the interrupt is equivalent
to the edge transition of the interrupt request signal.When an SGI occurs in a multiprocessor implementation, the CPUID field in the InterruptAcknowledge Register, GICC_IAR , or the Aliased Interrupt Acknowledge Register,GICC_AIAR , identifies the processor that requested the interrupt.In an implementation that includes the GIC Virtualization Extensions:
• when an SGI occurs, management registers in the GIC virtualization Extensions enable the requesting processor to be reported to the Guest OS, as required by the GIC specifications
• by writing to the management registers in the GIC Virtualization Extensions, a hypervisor can generate a virtual interrupt that appears to a virtual machine as an SGI.
See Software-generated interrupts on page 5-165 and List Registers, GICH_LRn on page 5-176 for more information.
- 这是通过软件向GIC的GICD_SGIR寄存器写入数据来生成的中断。系统使用SGI进行处理器间通信。
- SGI具有边沿触发特性。软件触发中断相当于中断请求信号的边沿转换。
- 在多处理器实现中,当发生SGI时,中断确认寄存器(Interrupt Acknowledge Register,GICC_IAR)或别名中断确认寄存器(Aliased Interrupt Acknowledge Register,GICC_AIAR)中的CPUID字段标识请求中断的处理器。
- 在包含GIC虚拟化扩展的实现中:
- 当发生SGI时,GIC虚拟化扩展的管理寄存器允许根据GIC规范向客户操作系统(Guest OS)报告请求处理器。
- 通过向GIC虚拟化扩展的管理寄存器写入数据,虚拟机监控器(hypervisor)可以生成一个虚拟中断,该中断对虚拟机表现为SGI。
Virtual interrupt:虚拟中断:
In a GIC that implements the GIC Virtualization Extensions, an interrupt that targets a
virtual machine running on a processor, and is typically signaled to the processor by the
connected virtual CPU interface.在实现了GIC虚拟化扩展的GIC(通用中断控制器)中,针对在处理器上运行的虚拟机的中断,通常由连接的虚拟CPU接口向处理器发出信号。这种中断被称为虚拟中断。
Maintenance interrupt :维护中断:
In a GIC that implements the GIC Virtualization Extensions, a level-sensitive interrupt that
is used to signal key events, such as a particular group of interrupts becoming enabled or
disabled.在实现了GIC虚拟化扩展的GIC(通用中断控制器)中,一种电平敏感的中断被用来信号关键事件,例如特定组的中断被启用或禁用。
GIC支持的中断源个数
The GIC assigns interrupt ID numbers ID0-ID1019 as follows:GIC分配的中断ID号ID0-ID1019如下所示
• Interrupt numbers ID32-ID1019 are used for SPIs.中断编号ID32至ID1019用于SPI(Shared Peripheral Interrupts,共享外设中断)
• Interrupt numbers ID0-ID31 are used for interrupts that are private to a CPU interface. These interrupts are banked in the Distributor.中断编号ID0至ID31用于特定于CPU接口的私有中断。这些中断在中断分发器(Distributor)中是分组的(banked)。
A banked interrupt is one where the Distributor can have multiple interrupts with the same ID. A banked interrupt is identified uniquely by its ID number and its associated CPU interface number.分组中断(Banked Interrupt)是指中断分发器可以拥有多个具有相同ID的中断。分组中断通过其ID编号和相关的CPU接口编号唯一标识。
Of the banked interrupt IDs:— ID0-ID15 are used for SGIsID0至ID15用于SGI(Software Generated Interrupts,软件生成中断)
— ID16-ID31 are used for PPIsID16至ID31用于PPI(Private Peripheral Interrupts,私有外设中断)
Distributor(中断分发器)
- 优先级掩蔽(Priority Masking):一种机制,允许CPU忽略低于特定优先级阈值的中断,从而确保高优先级任务能够获得及时处理。
- 抢占处理(Preemption Handling):当一个高优先级的中断到来时,如果CPU正在处理一个低优先级的中断或任务,系统会暂停当前任务,转而处理高优先级中断,处理完成后再返回执行被暂停的任务。
The Distributor provides a programming interface for:
分发器为以下方面提供了编程接口:
• Globally enabling the forwarding of interrupts to the CPU interfaces.全局使能将中断转发到CPU接口• Enabling or disabling each interrupt.启用或禁用每个中断• Setting the priority level of each interrupt.设置每个中断的优先级• Setting the target processor list of each interrupt.•设置每个中断的目标处理器列表。• Setting each peripheral interrupt to be level-sensitive or edge-triggered.将每个外设中断设置为电平敏感或边缘触发• Setting each interrupt as either Group 0 or Group 1.设置组0或组1
CPU interface block(CPU接口单元)
• enabling the signaling of interrupt requests to the processor允许中断请求信号被发送到处理器• acknowledging an interrupt确认中断• indicating completion of the processing of an interrupt指示中断处理完成处理器完成中断服务程序(ISR)的执行后,通过发送结束中断(End of Interrupt,EOI)信号来指示中断处理已经完成。• setting an interrupt priority mask for the processor设置处理器的中断优先级掩蔽为处理器设置一个优先级阈值,只有优先级高于或等于这个阈值的中断才会被处理器识别和处理。• defining the preemption policy for the processor定义处理器的抢占策略设置处理器如何处理高优先级中断与当前正在处理的低优先级中断之间的关系,即决定是否允许高优先级中断打断低优先级中断的处理。• determining the highest priority pending interrupt for the processor.确定处理器最高优先级的挂起中断识别和确定当前挂起(待处理)的中断中,优先级最高的中断,以便处理器可以优先处理。
中断分组(Interrupt grouping)
• configuring each interrupt as either Group 0 or Group 1配置每个中断为组0(Group 0)或组1(Group 1)• signaling Group 0 interrupts to the target processor using either the IRQ or the FIQ exception request使用IRQ或FIQ异常请求向目标处理器发送组0中断• signaling Group 1 interrupts to the target processor using the IRQ exception request only仅使用IRQ异常请求向目标处理器发送组1中断• a unified scheme for handling the priority of Group 0 and Group 1 interrupts统一的方案来处理组0和组1中断的优先级• optional lockdown of the configuration of some Group 0 interrupts.可选的锁定一些组0中断的配置— Group 0 interrupts are Secure interrupts (第0组的中断都是安全中断)— Group 1 interrupts are Non-secure interrupts (第1组的中断都是非安全中断)By default, all interrupts are Group 0 interrupts, and are signaled to a connected processor using the IRQ interrupt request。(默认情况下所有中断都在第0组,产生的中断的信号使用IRQ向处理器发出中断请求)each interrupt can be configured as Group 1 interrupt, or as a Group 0 interrupt(每个中断都可以配置为第1组中断或第0组中断)a CPU interface can be configured to signal Group 0 interrupts to a connected processorusing the FIQ interrupt request.(CPU接口可以配置为使用FIQ中断请求向连接的处理器发出组0中断信号
中断优先级(Interrupt prioritization)
Software configures interrupt prioritization in the GIC by assigning a priority value to eachinterrupt source. Priority values are 8-bit unsigned binary. A GIC supports a minimum of 16 and a maximum of 256 priority levels.(可以通过软件为每个中断源分配优先级值来配置GIC中的中断优先级。优先级值是8位无符号二进制。GIC支持最小16个和最大256个优先级。)In the GIC prioritization scheme, lower numbers have higher priority, that is, the lower theassigned priority value the higher the priority of the interrupt . Priority field value 0 alwaysindicates the highest possible interrupt priority, and the lowest priority value depends on thenumber of implemented priority levels。(在GIC优先级设计方案中, 较低的数字具有较高的优先级,即分配的优先级值越低,中断的优先级越高。优先级字段值0总是表示可能的最高中断优先级,而最低优先级值取决于实现的优先级级别的数量)Priority masking (优先级屏蔽)The GICC_PMR for a CPU interface defines a priority threshold for the target processor. The GIC only signals pending interrupts with a higher priority than this threshold value to the target processor . A value of zero, the register reset value, masks all interrupts from being signaled to the associated processor. (CPU接口单元的GICC_PMR定义了目标处理器的优先级阈(yu)值。GIC只向目标处理器发出优先级高于该阈值的挂起中断的信号。零值,即寄存器重置值,屏蔽了所有被发信号通知相关处理器的中断)
中断状态(interrupt states)
Inactive(不活动)
An interrupt that is not active (非活动的中断)
Pending(待定)
An interrupt from a source to the GIC that is recognized as asserted in hardware, or generated by software, and is waiting to be serviced by a target processor.
(硬件设备产生 或 由软件生成一个中断到达GIC,并等待由目标处理器处理)
Active (活跃)
An interrupt from a source to the GIC that has been acknowledged by a processor, and is
being serviced but has not completed.
(一个中断到达GIC,已被处理器确认,并且是正在处理但尚未完成)
Active and pending(活动和待定)
A processor is servicing the interrupt and the GIC has a pending interrupt from the
same source.(处理器正在处理中断这个中断源,并且来了一个同类的中断源处于待定状态)