1# Interrupt and Exception Handling<a name="EN-US_TOPIC_0000001123638623"></a> 2 3- [Basic Concepts](#section439816296117) 4- [Working Principles](#section2792838318) 5- [Development Guidelines](#section15415165510110) 6 - [Available APIs](#section57441612024) 7 - [How to Develop](#section64332181221) 8 - [Development Example](#section204698276478) 9 - [Verification](#section1466144215476) 10 11 12## Basic Concepts<a name="section439816296117"></a> 13 14An 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. 15 16Exception 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. 17 18## Working Principles<a name="section2792838318"></a> 19 20Peripherals 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. 21 22Exception 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. 23 24The 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. 25 26**Figure 1** Interrupt vector table<a name="fig105771014134715"></a> 27 28 29 30 31## Development Guidelines<a name="section15415165510110"></a> 32 33### Available APIs<a name="section57441612024"></a> 34 35Exception handling is an internal mechanism and does not provide external APIs. The following table describes APIs available for the interrupt module. 36 37<a name="table11657113333110"></a> 38<table><thead align="left"><tr id="row1170612337312"><th class="cellrowborder" valign="top" width="19.900000000000002%" id="mcps1.1.4.1.1"><p id="p4706133373112"><a name="p4706133373112"></a><a name="p4706133373112"></a><strong id="b7792162213202"><a name="b7792162213202"></a><a name="b7792162213202"></a>Category</strong></p> 39</th> 40<th class="cellrowborder" valign="top" width="18.43%" id="mcps1.1.4.1.2"><p id="p1070653343117"><a name="p1070653343117"></a><a name="p1070653343117"></a><strong id="b19958356201"><a name="b19958356201"></a><a name="b19958356201"></a>API</strong></p> 41</th> 42<th class="cellrowborder" valign="top" width="61.67%" id="mcps1.1.4.1.3"><p id="p370613330311"><a name="p370613330311"></a><a name="p370613330311"></a><strong id="b1551072610204"><a name="b1551072610204"></a><a name="b1551072610204"></a>Description</strong></p> 43</th> 44</tr> 45</thead> 46<tbody><tr id="row8706123317311"><td class="cellrowborder" rowspan="2" valign="top" width="19.900000000000002%" headers="mcps1.1.4.1.1 "><p id="p4706193319318"><a name="p4706193319318"></a><a name="p4706193319318"></a>Creating or deleting interrupts</p> 47</td> 48<td class="cellrowborder" valign="top" width="18.43%" headers="mcps1.1.4.1.2 "><p id="p170683310317"><a name="p170683310317"></a><a name="p170683310317"></a>LOS_HwiCreate</p> 49</td> 50<td class="cellrowborder" valign="top" width="61.67%" headers="mcps1.1.4.1.3 "><p id="p15706833163110"><a name="p15706833163110"></a><a name="p15706833163110"></a>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.</p> 51</td> 52</tr> 53<tr id="row18706153318316"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p1870615332312"><a name="p1870615332312"></a><a name="p1870615332312"></a>LOS_HwiDelete</p> 54</td> 55<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p770616333313"><a name="p770616333313"></a><a name="p770616333313"></a>Deletes an interrupt.</p> 56</td> 57</tr> 58<tr id="row1370633316316"><td class="cellrowborder" rowspan="3" valign="top" width="19.900000000000002%" headers="mcps1.1.4.1.1 "><p id="p970611333318"><a name="p970611333318"></a><a name="p970611333318"></a>Enabling and disabling all interrupts</p> 59</td> 60<td class="cellrowborder" valign="top" width="18.43%" headers="mcps1.1.4.1.2 "><p id="p147061033103117"><a name="p147061033103117"></a><a name="p147061033103117"></a>LOS_IntUnLock</p> 61</td> 62<td class="cellrowborder" valign="top" width="61.67%" headers="mcps1.1.4.1.3 "><p id="p167061333193114"><a name="p167061333193114"></a><a name="p167061333193114"></a>Enables all interrupts of the current processor.</p> 63</td> 64</tr> 65<tr id="row1270603314312"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p1970623343114"><a name="p1970623343114"></a><a name="p1970623343114"></a>LOS_IntLock</p> 66</td> 67<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p1370623373115"><a name="p1370623373115"></a><a name="p1370623373115"></a>Disables all interrupts for the current processor.</p> 68</td> 69</tr> 70<tr id="row8706233173113"><td class="cellrowborder" valign="top" headers="mcps1.1.4.1.1 "><p id="p1770620337313"><a name="p1770620337313"></a><a name="p1770620337313"></a>LOS_IntRestore</p> 71</td> 72<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p1470643323112"><a name="p1470643323112"></a><a name="p1470643323112"></a>Restores to the status before all interrupts are disabled by using <strong id="b354311504226"><a name="b354311504226"></a><a name="b354311504226"></a>LOS_IntLock</strong>.</p> 73</td> 74</tr> 75<tr id="row870793320317"><td class="cellrowborder" valign="top" width="19.900000000000002%" headers="mcps1.1.4.1.1 "><p id="p1970763318316"><a name="p1970763318316"></a><a name="p1970763318316"></a>Obtaining the maximum number of interrupts supported</p> 76</td> 77<td class="cellrowborder" valign="top" width="18.43%" headers="mcps1.1.4.1.2 "><p id="p1707333123115"><a name="p1707333123115"></a><a name="p1707333123115"></a>LOS_GetSystemHwiMaximum</p> 78</td> 79<td class="cellrowborder" valign="top" width="61.67%" headers="mcps1.1.4.1.3 "><p id="p4707173323111"><a name="p4707173323111"></a><a name="p4707173323111"></a>Obtains the maximum number of interrupts supported by the system.</p> 80</td> 81</tr> 82</tbody> 83</table> 84 85### How to Develop<a name="section64332181221"></a> 86 871. Call **LOS\_HwiCreate** to create an interrupt. 882. Call **LOS\_HwiDelete** to delete the specified interrupt. Use this API based on actual requirements. 89 90### Development Example<a name="section204698276478"></a> 91 92This example implements the following: 93 941. Create an interrupt. 952. Delete an interrupt. 96 97The 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. 98 99``` 100#include "los_hwi.h" 101/* Interrupt handler function*/ 102STATIC VOID HwiUsrIrq(VOID) 103{ 104 printf("in the func HwiUsrIrq \n"); 105} 106 107static UINT32 Example_Interrupt(VOID) 108{ 109 UINT32 ret; 110 HWI_HANDLE_T hwiNum = 7; 111 HWI_PRIOR_T hwiPrio = 3; 112 HWI_MODE_T mode = 0; 113 HWI_ARG_T arg = 0; 114 115/* Create an interrupt.*/ 116 ret = LOS_HwiCreate(hwiNum, hwiPrio, mode, (HWI_PROC_FUNC)HwiUsrIrq, (HwiIrqParam *)arg); 117 if(ret == LOS_OK){ 118 printf("Hwi create success!\n"); 119 } else { 120 printf("Hwi create failed!\n"); 121 return LOS_NOK; 122 } 123 124 /* Delay 50 ticks. When a hardware interrupt occurs, the HwiUsrIrq function will be called.*/ 125 LOS_TaskDelay(50); 126 127 /* Delete an interrupt./ 128 ret = LOS_HwiDelete(hwiNum, (HwiIrqParam *)arg); 129 if(ret == LOS_OK){ 130 printf("Hwi delete success!\n"); 131 } else { 132 printf("Hwi delete failed!\n"); 133 return LOS_NOK; 134 } 135 return LOS_OK; 136} 137``` 138 139### Verification<a name="section1466144215476"></a> 140 141The development is successful if the return result is as follows: 142 143``` 144Hwi create success! 145Hwi delete success! 146``` 147 148