Embedded – what happens when ISR runs and another interrupt occurs?

What happens if ISR is running and another interrupt occurs? Is the first interrupt interrupted? Will the second interrupt be ignored? Or will it fire when the first ISR is completed?

Editor: I forgot to include it in the question (but I included it in the tag). I'm going to ask ATMEL how AVR works

Solution

Generally speaking, the interrupt service program continues until it is completed, and will not interrupt itself in most systems However, if we have a larger system, several devices may interrupt the microprocessor, and a priority problem may occur

If the interrupt enable flag is set in the current interrupt, you can allow further interrupts to have priority over the executed interrupt This "interrupt" is called a nested interrupt It handles this by stopping the execution of the original service program and storing another register sequence in the stack This is similar to nested subroutines Since the stack pointer is automatically decremented by each interrupt and then incremented by the return instruction, after the second interrupt is completed, the first interrupt service program is restored and the interrupts are processed in the correct order Interrupts can be nested to any depth, limited only by the amount of memory available on the stack

For example, in the following figure, thread a is running Interrupting irqx causes the interrupt handler intx to run, which is preempted by irqy and its handler inty Inty returns an event that causes thread B to run; Intx returns an event that causes thread C to run

Image Ref

For hardware interrupts, priority interrupt controller chip (PIC) is a hardware chip designed to make the device present its own address to the CPU The pic also evaluates the priority of the devices connected to it Modern pic can also be programmed to prevent lower than expected interrupts

Update: how nested interrupts work on ATMEL AVR

Before entering the interrupt vector, AVR hardware clears the global interrupt flag in sreg Therefore, under normal circumstances, interrupts inside the handler remain disabled until the handler exits, where the reti instruction (issued by the compiler as part of the end of the normal function of the interrupt handler) will eventually re enable further interrupts Therefore, interrupt handlers are usually not nested For most interrupt handlers, this is the required behavior, and some are even required to prevent infinite recursive interrupts, such as UART interrupts or level triggered external interrupts

In rare cases, although the global interrupt flag is re enabled as early as possible in the interrupt handler, it may be necessary to nest interrupts so as not to exceed any other interrupts absolutely required This can be done using the SEI () instruction at the beginning of the interrupt handler, but several instructions are still left in the function preamble generated by the compiler to disable global interrupt operation You can instruct the compiler to declare the handler by inserting an SEI directive at the beginning of the interrupt handler:

ISR(XXX_vect,ISR_NOBLOCK)
{
  ...
}

Including XXX_ Vect is the name of a valid interrupt vector of MCU type

In addition, take a look at this application note for more information about ATMEL AVRs interrupts

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>