• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Interrupt Management
2
3## Basic Concepts
4
5An 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. When a hardware interrupt is triggered, the interrupt handler is located based on the interrupt ID and then executed to handle the interrupt.
6
7By 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. 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.
8
9The following describes the concepts related to interrupts:
10
11-   Interrupt ID
12
13    Identifies an interrupt request signal. The computer locates the device that sends the interrupt request based on the interrupt ID.
14
15-   Interrupt request
16
17    A process in which an electrical pulse signal is sent to the CPU, alerting the CPU to a high-priority event requiring the interruption of the current code being executed by the CPU.
18
19-   Interrupt priority
20
21    Prioritizes the sources that trigger interrupts based on the importance and urgency of interrupt events, so that the CPU can respond to and handle all interrupts in a timely manner.
22
23-   Interrupt handler
24
25    A program executed by the CPU to respond to the interrupt request from a peripheral. Each device that triggers an interrupt has its own interrupt handler.
26
27-   Interrupt triggering
28
29    The interrupt source sends an interrupt signal to the interrupt controller. The interrupt controller arbitrates all pending interrupts, determines the priority, and sends the interrupt signal to the CPU. When an interrupt source generates an interrupt signal, the interrupt trigger is set to  **1**, alerting the CPU to respond to the interrupt.
30
31-   Interrupt vector
32
33    Entry address of an interrupt handler.
34
35-   Interrupt vector table
36
37    An area for storing interrupt vectors. It stores the mapping between interrupt vectors and interrupt IDs.
38
39
40## Available APIs
41
42The following table describes APIs available for the OpenHarmony LiteOS-M interrupt module. For more details about the APIs, see the API reference.
43
44**Table  1**  APIs of the interrupt module
45
46| Category| API| Description|
47| -------- | -------- | -------- |
48| Creating or deleting an interrupt| LOS_HwiCreate | Creates an interrupt and registers the interrupt ID, triggering mode, priority, and interrupt handler. When an interrupt is triggered, the interrupt handler will be called.|
49|  | LOS_HwiDelete | Deletes an interrupt based on the specified interrupt ID.|
50| Locking or unlocking interrupts| LOS_IntUnLock | Enables the CPU to respond to all interrupt requests.|
51|  | LOS_IntLock | Disables the CPU from responding to interrupt requests.|
52|  | LOS_IntRestore | Restores the interrupt status before the **LOS_IntLock** and **LOS_IntUnLock** operations are performed.|
53| Enabling or disabling an interrupt| LOS_HwiDisable | Disables the CPU from responding to the specified interrupt by setting the register.|
54|  | LOS_HwiEnable | Enables the CPU to respond to the specified interrupt by setting the register.|
55| Setting the interrupt priority| LOS_HwiSetPriority | Sets the interrupt priority.|
56| Triggering an interrupt| LOS_HwiTrigger | Triggers an interrupt (simulate an external interrupt by writing the related register of the interrupt controller).|
57| Clearing interrupt register status| LOS_HwiClear | Clears the status bit of the interrupt register corresponding to the interrupt ID. The implementation of this API depends on the interrupt controller version. It is optional.|
58
59## How to Develop
60
611. Call **LOS_HwiCreate** to create an interrupt.
622. Call **LOS_HwiTrigger** to trigger the interrupt.
633. Call **LOS_HwiDelete** to delete the specified interrupt. Use this API based on actual requirements.
64
65>![](../public_sys-resources/icon-note.gif) **NOTE**<br/>
66>-   Configure the maximum number of interrupts supported and the number of configurable interrupt priorities based on the specific hardware.
67>-   If the interrupt handler takes long time, the CPU cannot respond to interrupt requests in a timely manner.
68>-   Functions that trigger  **LOS\_Schedule**  cannot be directly or indirectly executed during interrupt response process.
69>-   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.
70
71## Development Example
72
73This example implements the following:
74
751.  Create an interrupt.
762.  Trigger an interrupt.
773.  Delete an interrupt.
78
79The 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.
80
81```
82#include "los_interrupt.h"
83
84/* Create an interrupt. */
85#define HWI_NUM_TEST 7
86
87STATIC VOID HwiUsrIrq(VOID)
88{
89    printf("in the func HwiUsrIrq \n");
90}
91
92static UINT32 Example_Interrupt(VOID)
93{
94    UINT32 ret;
95    HWI_PRIOR_T hwiPrio = 3;
96    HWI_MODE_T mode = 0;
97    HwiIrqParam irqParam;
98    (void)memset_s(&irqParam, sizeof(HwiIrqParam), 0, sizeof(HwiIrqParam));
99    irqParam.pDevId = 0;
100
101    /* Create an interrupt. */
102    ret = LOS_HwiCreate(HWI_NUM_TEST, hwiPrio, mode, (HWI_PROC_FUNC)HwiUsrIrq, &irqParam);
103    if(ret == LOS_OK){
104        printf("Hwi create success!\n");
105    } else {
106        printf("Hwi create failed!\n");
107        return LOS_NOK;
108    }
109
110    /* Trigger the interrupt. */
111    ret = LOS_HwiTrigger(HWI_NUM_TEST);
112    if(ret == LOS_OK){
113        printf("Hwi trigger success!\n");
114    } else {
115        printf("Hwi trigger failed!\n");
116        return LOS_NOK;
117    }
118
119    /* Delete the interrupt. */
120    ret = LOS_HwiDelete(HWI_NUM_TEST);
121    if(ret == LOS_OK){
122        printf("Hwi delete success!\n");
123    } else {
124        printf("Hwi delete failed!\n");
125        return LOS_NOK;
126    }
127    return LOS_OK;
128}
129```
130
131## Verification
132
133The development is successful if the return result is as follows:
134
135```
136Hwi create success!
137Hwi delete success!
138```
139