Interrupts, ISR, Context Switching| RTOS| Embedded Systems
What is an Interrupt?
Definition and Functionality of Interrupts
- An interrupt is a signal to the processor, emitted by hardware or software, indicating that an event requires immediate attention.
- When an interrupt signal is received, the processor halts its current task to address the request from hardware or software needing assistance.
- The term "interrupt" reflects how this signal disrupts the normal operation of the processor, necessitating a shift in focus to high-priority conditions.
- Upon receiving an interrupt, the processor suspends its activities, saves its state, and executes a function known as the interrupt handler to manage the event.
- After servicing the interrupt, the processor resumes its previous activity by restoring its saved state.
Understanding Interrupt Service Routine (ISR)
- The interrupt handler is also referred to as an Interrupt Service Routine (ISR), which is specifically designed for handling interrupts.
- The ISR executes in response to interrupts and ensures that necessary actions are taken before returning control back to normal operations.
How Does an Interrupt Work?
Process Flow of Handling Interrupts
- Once an interrupt occurs, it temporarily halts ongoing processes; after addressing it, normal activities resume seamlessly.
- A diagram illustrates this process: when an interrupt arises (depicted as a yellow box), it shows how processing returns to regular tasks post-interrupt service.
Types and Sources of Interrupts
Classification of Interrupt Sources
- Interrupt sources can be categorized into two main types: hardware interrupts and software interrupts. Software interrupts are often termed exceptions.
Hardware Interrupt Sources
- Hardware interrupts can be further divided into internal and external categories:
- Internal Hardware Interrupt: Triggered by components within the system requiring attention.
Understanding Hardware and Software Interrupts
Internal Hardware Sources
- The system contains various internal hardware components, such as real-time clocks and watchdog timers, which may require servicing or attention. These are referred to as internal hardware sources.
Classification of Hardware Interrupts
- Hardware interrupts are categorized into two types: maskable interrupts and non-maskable interrupts.
- Maskable interrupts can be delayed; for instance, if a low-priority device raises an interrupt while the processor is busy, it can wait until the current task is completed before being serviced.
- Non-maskable interrupts must be addressed immediately regardless of what the processor is doing. An example includes pressing a reset button during critical operations.
Software Interrupts
- Software interrupts, also known as exceptions, occur due to specific conditions like computational errors (e.g., division by zero), illegal opcodes, or overflow situations.
- When a division by zero occurs, it triggers a software-driven interrupt that requires immediate handling by the processor.
Types of Software Interrupts
- Software interrupts can be classified into normal interrupts and exceptions. Normal interrupts are intentionally coded within software to create an interrupt condition.
- Exceptions arise from unplanned conditions (e.g., division by zero), prompting the processor to halt its current tasks to resolve these issues.
Interrupt Service Routine (ISR)
- An interrupt service routine is a small program executed by the processor in response to an interrupt request from hardware. It defines how the system should respond when an interrupt occurs.
Understanding Interrupts and Context Switching
Steps in Handling an Interrupt
- When an interrupt occurs, the processor masks further interrupts to prevent additional interruptions during processing. It saves the current context or program details onto the stack before loading the appropriate Interrupt Service Routine (ISR).
- After executing the ISR, the processor restores the saved context and resumes normal processing while enabling further interrupts.
- The initial steps include masking other interrupts, saving current routines' status, and executing the relevant ISR based on which interrupt occurred.
- Upon completion of an ISR, a return instruction is executed to restore previously saved contexts from the stack and re-enable interrupts.
Classification of ISRs: Nested vs Non-Nested
- ISRs can be classified into two categories: nested and non-nested. Nested ISRs allow higher priority interrupts to preempt currently executing ISRs.
- In a nested scenario, if ISR 1 is being processed and a higher priority interrupt (ISR 2) occurs, ISR 1 will be paused to execute ISR 2 first.
- Once ISR 2 completes execution, control returns to finish processing ISR 1 before resuming normal operations in the main routine.
Characteristics of Non-Nested ISRs
- Non-nested ISRs do not permit interruption; they complete execution of one ISR before considering any new incoming interrupts.
- This means that if an interrupt occurs while another is being processed, it will wait until the current one finishes before handling subsequent ones.
Preemption Concepts
- Two terms related to this classification are preemption (where one interrupt can preempt another during execution) and non-preemption (where no such interruption is allowed).
Context Switching Explained
- Context switching refers to saving and restoring data for a currently running program when an interrupt occurs.
- The process involves three key actions: saving all registers including program status and counter onto the stack; loading the next context for handling interrupts; and readjusting the stack pointer for new function execution.
Understanding Context Switching and Interrupts
Execution of Interrupt Service Routine (ISR)
- The execution of an interrupt involves returning to the previous process after completing the ISR. This is done by encountering a return instruction, which signifies the end of the ISR.
- Upon executing the return instruction, several steps occur in reverse: restoring registers, loading the previous program's context, and adjusting the stack pointer back to its state before the interrupt.
Steps Involved in Context Switching
- The process of context switching includes saving all necessary data when an interrupt occurs and restoring it upon completion of the ISR. This involves three main steps executed in reverse order during both transitions.
- Context switching refers to transitioning from a current program's context to an interrupt context and then back again. These operations are crucial for managing multiple processes effectively.
Memory Management During Interrupts
- Data related to interrupts is stored in stack memory. When an interrupt occurs, the processor saves the current program's status and registers onto this stack.
- The operation of inserting data into the stack is known as a push operation. Each time new data is pushed onto the stack, such as values 10 or 12, the stack pointer increments accordingly.
Retrieving Data from Stack
- After finishing with ISR servicing, retrieving stored data from the stack is performed through a pop operation. This decrements the stack pointer as data is retrieved.
- The popping operation continues until all necessary data has been restored from memory back into active use within programs.
Summary of Key Concepts Discussed