An interrupt is a signal that stops the processor from doing what it’s doing and runs other code for a brief period.
The actual code that is run is written into a function, but this function is not necessarily called by the user. The chip calls the function itself once the conditions for the interrupt are met.
The signals that trigger an interrupt can be external, software-generated, or internal. The functions are called “Interrupt Service Routines” (ISRs), “Interrupt Handlers” (never abbreviated), or sometimes “Interrupt Service Requests” (frustratingly, IRQ, since ISR is already taken).
External Interrupts
An external interrupt is usually a voltage delivered via wire due to some hardware change. For example:
- GPIO: Some GPIO pins can be configured as interrupts. When the GPIO pin’s state changes to whatever triggers the interrupt, the signal is sent.
- Communications: UART, SPI, I2C and others all communicate via interrupts
External interrupts usually indicate that some peripheral needs attention. They are often not crucial to the safe operation of the CPU. For instance, if the CPU misses a UART command because it is dealing with a power problem that’s not crucial. External interrupts are also programmed as part of the application, since they depend on what hardware is used.
Software Interrupts
It is possible to trigger an interrupt via software. Most commonly done via a robust system call framework – the API exposes a single function that the user does call, and that function is then responsible for triggering the software interrupt itself.
Keyboard Input Example
This is how keyboard input works on your laptop:
- If you write a program that waits on keyboard input (using, for instance,
cin
if writing in C++), the function that is called is not an interrupt function yet.- It then makes a request to the operating system via a system call that allows a user program to interact with the kernel. This system call is an interrupt, and its job is to tell the kernel what to do.
- In this case, it tells the kernel “Hey, whenever keyboard input is available, send it over to this process”.
- In this case, it tells the kernel “Hey, whenever keyboard input is available, send it over to this process”.
- The kernel then pays attention to a second interrupt, this time an external interrupt looking for keyboard input.
- Once keyboard input is available the kernel and other processes get interrupted, and the kernel passes the input along to the process that was waiting.
Internal Interrupts
Some signals inside of the chip itself cause interrupts. These are almost always fault conditions on the Cortex M4, but sometimes internal interrupts just indicate that something needs attention.
Hard Fault: In the Cortex M4 there is a condition known as a Hard Fault, which means that the processor does not understand the instruction it is supposed to run. This is generated by the processor itself, hence it is internal, and it usually means that something is so wrong with the code that there is no obvious way to recover.
Page fault: The x86-64 family of chips have many recoverable faults. For instance, a “page fault”. This is a misnomer – it isn’t necessarily a fault. It just means that a process has asked for memory that isn’t currently loaded. It might be an actual fault, as it would be if the process tried to access memory that it isn’t permitted to access, or it might just be an indication that the process needs more memory. This would be more accurately called an “exception”.