Interrupt and Exception Handling

Basic Concepts

An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code being executed by the processor. In this way, the CPU does not need to spend a lot of time in waiting and querying the peripheral status, which effectively improves the real-time performance and execution efficiency of the system.

Exception handling involves a series of actions taken by the OS to respond to exceptions (chip hardware faults) occurred during the OS running, for example, printing the call stack information of the current function, CPU information, and call stack information of tasks when the virtual memory page is missing.

Working Principles

Peripherals can complete certain work without the intervention of the CPU. In some cases, however, the CPU needs to perform certain work for peripherals. By using the interrupt mechanism, the CPU responds to the interrupt request from a peripheral only when required, and execute other tasks when the peripherals do not require the CPU. The interrupt controller receives the input of other peripheral interrupt pins and sends interrupt signals to the CPU. You can enable or disable the interrupt source and set the priority and trigger mode of the interrupt source by programming the interrupt controller. Common interrupt controllers include vector interrupt controllers (VICs) and general interrupt controllers (GICs). The ARM Cortex-A7 uses GICs. After receiving an interrupt signal sent by the interrupt controller, the CPU interrupts the current task to respond to the interrupt request.

Exception handling interrupts the normal running process of the CPU to handle exceptions, such as, undefined instruction exception, an attempt to modify read-only data, and unaligned address access. When an exception occurs, the CPU suspends the current program, handles the exception, and then continues to execute the program interrupted by the exception.

The following uses the ARMv7-a architecture as an example. The interrupt vector table is the entry for interrupt and exception handling. The interrupt vector table contains the entry function for each interrupt and exception handling.

Figure 1 Interrupt vector table

Development Guidelines

Available APIs

Exception handling is an internal mechanism and does not provide external APIs. The following table describes APIs available for the interrupt module.

Category

API

Description

Creating or deleting interrupts

LOS_HwiCreate

Creates an interrupt and registers the interrupt ID, interrupt triggering mode, interrupt priority, and interrupt handler. When an interrupt is triggered, the interrupt handler will be called.

LOS_HwiDelete

Deletes an interrupt.

Enabling and disabling all interrupts

LOS_IntUnLock

Enables all interrupts of the current processor.

LOS_IntLock

Disables all interrupts for the current processor.

LOS_IntRestore

Restores to the status before all interrupts are disabled by using LOS_IntLock.

Obtaining the maximum number of interrupts supported

LOS_GetSystemHwiMaximum

Obtains the maximum number of interrupts supported by the system.

How to Develop

  1. Call LOS_HwiCreate to create an interrupt.
  2. Call LOS_HwiDelete to delete the specified interrupt. Use this API based on actual requirements.

Development Example

This example implements the following:

  1. Create an interrupt.
  2. Delete an interrupt.

The following sample code shows how to create and delete an interrupt. When the interrupt HWI_NUM_TEST is generated, the interrupt handler function will be called.

#include "los_hwi.h"
/* Interrupt handler function*/
STATIC VOID HwiUsrIrq(VOID)
{
    printf("in the func HwiUsrIrq \n"); 
}

static UINT32 Example_Interrupt(VOID)
{
    UINT32 ret;
    HWI_HANDLE_T hwiNum = 7;
    HWI_PRIOR_T hwiPrio = 3;
    HWI_MODE_T mode = 0;
    HWI_ARG_T arg = 0;

/* Create an interrupt.*/
    ret = LOS_HwiCreate(hwiNum, hwiPrio, mode, (HWI_PROC_FUNC)HwiUsrIrq, (HwiIrqParam *)arg);
    if(ret == LOS_OK){
        printf("Hwi create success!\n");
    } else {
        printf("Hwi create failed!\n");
        return LOS_NOK;
    }

    /* Delay 50 ticks. When a hardware interrupt occurs, the HwiUsrIrq function will be called.*/
    LOS_TaskDelay(50);

    /* Delete an interrupt./
    ret = LOS_HwiDelete(hwiNum, (HwiIrqParam *)arg);    
    if(ret == LOS_OK){
        printf("Hwi delete success!\n");
    } else {
        printf("Hwi delete failed!\n");
        return LOS_NOK;
    }
    return LOS_OK;
}

Verification

The development is successful if the return result is as follows:

Hwi create success!
Hwi delete success!