1# Interrupt and Exception Handling 2 3 4## Basic Concepts<a name="section439816296117"></a> 5 6An interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor of a high-priority condition requiring interruption of the 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. 7 8Exception handling involves a series of actions taken by the OS to respond to exceptions \(chip hardware faults\) that 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. 9 10## Working Principles<a name="section2792838318"></a> 11 12Peripherals can complete certain work without the intervention of the CPU. In some cases, however, the CPU needs to perform certain work for peripherals. With 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. 13 14Exception handling interrupts the normal running process of the CPU to handle exceptions, such as, undefined instructions, 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. 15 16The 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. 17 18**Figure 1** Interrupt vector table<a name="fig1552753243714"></a> 19 20 21## Development Guidelines<a name="section15415165510110"></a> 22 23### Available APIs<a name="section57441612024"></a> 24 25Exception handling is an internal mechanism and does not provide external APIs. The following table describes APIs available for the interrupt module. 26 27<a name="table11657113333110"></a> 28<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>Function</strong></p> 29</th> 30<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> 31</th> 32<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> 33</th> 34</tr> 35</thead> 36<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> 37</td> 38<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> 39</td> 40<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> 41</td> 42</tr> 43<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> 44</td> 45<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> 46</td> 47</tr> 48<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> 49</td> 50<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> 51</td> 52<td class="cellrowborder" valign="top" width="61.67%" headers="mcps1.1.4.1.3 "><p id="p93681327171713"><a name="p93681327171713"></a><a name="p93681327171713"></a>Enables all interrupts of the current processor.</p> 53</td> 54</tr> 55<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> 56</td> 57<td class="cellrowborder" valign="top" headers="mcps1.1.4.1.2 "><p id="p1161283971712"><a name="p1161283971712"></a><a name="p1161283971712"></a>Disables all interrupts for the current processor.</p> 58</td> 59</tr> 60<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> 61</td> 62<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> 63</td> 64</tr> 65<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> 66</td> 67<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> 68</td> 69<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> 70</td> 71</tr> 72</tbody> 73</table> 74 75### How to Develop<a name="section64332181221"></a> 76 771. Call **LOS\_HwiCreate** to create an interrupt. 782. Call **LOS\_HwiDelete** to delete the specified interrupt. Use this API based on actual requirements. 79 80### Development Example<a name="section204698276478"></a> 81 82This example implements the following: 83 841. Create an interrupt. 852. Delete an interrupt. 86 87The 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. 88 89``` 90#include "los_hwi.h" 91/* Interrupt handler function*/ 92STATIC VOID HwiUsrIrq(VOID) 93{ 94 printf("in the func HwiUsrIrq \n"); 95} 96 97static UINT32 Example_Interrupt(VOID) 98{ 99 UINT32 ret; 100 HWI_HANDLE_T hwiNum = 7; 101 HWI_PRIOR_T hwiPrio = 3; 102 HWI_MODE_T mode = 0; 103 HWI_ARG_T arg = 0; 104 105/* Create an interrupt.*/ 106 ret = LOS_HwiCreate(hwiNum, hwiPrio, mode, (HWI_PROC_FUNC)HwiUsrIrq, (HwiIrqParam *)arg); 107 if(ret == LOS_OK){ 108 printf("Hwi create success!\n"); 109 } else { 110 printf("Hwi create failed!\n"); 111 return LOS_NOK; 112 } 113 114 /* Delay 50 ticks. When a hardware interrupt occurs, call the HwiUsrIrq function.*/ 115 LOS_TaskDelay(50); 116 117 /* Delete an interrupt./ 118 ret = LOS_HwiDelete(hwiNum, (HwiIrqParam *)arg); 119 if(ret == LOS_OK){ 120 printf("Hwi delete success!\n"); 121 } else { 122 printf("Hwi delete failed!\n"); 123 return LOS_NOK; 124 } 125 return LOS_OK; 126} 127``` 128 129### Verification<a name="section1466144215476"></a> 130 131The development is successful if the return result is as follows: 132 133``` 134Hwi create success! 135Hwi delete success! 136``` 137 138