Development Guidelines

When an interrupt request is generated by a peripheral, the CPU suspends the current task and responds to the interrupt request. You need to register the interrupt handler and specify the operation to be performed by the CPU.

Available APIs

The following table describes APIs available for the OpenHarmony LiteOS-M interrupt module. For more details about the APIs, see the API reference.

Table 1 APIs of the interrupt module

Category

API

Description

Creating or deleting interrupts

HalHwiCreate

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.

HalHwiDelete

Deletes an interrupt based on the specified interrupt ID.

Enabling or disabling interrupts

LOS_IntUnLock

Enables the CPU to respond to all interrupt requests.

LOS_IntLock

Disables the CPU from responding to interrupt requests.

LOS_IntRestore

Restores the interrupt status before the LOS_IntLock and LOS_IntUnLock operations are performed.

How to Develop

  1. Create an interrupt by calling HalHwiCreate.
  2. Call TestHwiTrigger to trigger the specified interrupt. (This API is defined in the test suite. It simulates an external interrupt by writing the related register of the interrupt controller. Skip this step for common peripheral devices.)
  3. Call HalHwiDelete to delete the specified interrupt. Use this API based on actual requirements.

NOTE:

  • Configure the maximum number of interrupts supported and the number of configurable interrupt priorities based on the specific hardware.
  • If the interrupt handler takes long time, the CPU cannot respond to interrupt requests in a timely manner.
  • Functions that trigger LOS_Schedule cannot be directly or indirectly executed during interrupt response process.
  • The input parameter of LOS_IntRestore() must be the return value of LOS_IntLock(), that is, the current program status register (CPSR) value before the interrupt is disabled. Interrupts 0 to 15 in the Cortex-M series processors are for internal use. You are advised not to apply for or create interrupts 0 to 15.

Development Example

This example implements the following:

  1. Create an interrupt.
  2. Trigger an interrupt.
  3. 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_interrupt.h"

/* Create an interrupt.*/
#define HWI_NUM_TEST 7

STATIC VOID HwiUsrIrq(VOID)
{
    printf("in the func HwiUsrIrq \n"); 
}

static UINT32 Example_Interrupt(VOID)
{
    UINT32 ret;
    HWI_PRIOR_T hwiPrio = 3;
    HWI_MODE_T mode = 0;
    HWI_ARG_T arg = 0;
  
    /* Create an interrupt. */
    ret = HalHwiCreate(HWI_NUM_TEST, hwiPrio, mode, (HWI_PROC_FUNC)HwiUsrIrq, 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 = HalHwiDelete(HWI_NUM_TEST);    
    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!.