• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# GPIO<a name="EN-US_TOPIC_0000001206171135"></a>
2
3
4## Overview<a name="section1635911016188"></a>
5
6Generally, a general-purpose input/output \(GPIO\) controller manages all GPIO pins by group. Each group of GPIO pins is associated with one or more registers. The GPIO pins are operated by reading data from and writing data to the registers.
7
8The GPIO APIs define a set of standard functions for performing operations on GPIO pins, including:
9
10-   Setting the pin direction, which can be input or output \(high impedance is not supported currently\)
11
12-   Reading and writing level values, which can be low or high
13-   Setting an interrupt service routine \(ISR\) function and interrupt trigger mode for a pin
14-   Enabling or disabling interrupts for a pin
15
16## Available APIs<a name="section589913442203"></a>
17
18**Table  1** APIs available for the GPIO driver
19
20<a name="table89681075215"></a>
21<table><thead align="left"><tr id="row996807162115"><th class="cellrowborder" valign="top" width="19.74%" id="mcps1.2.4.1.1"><p id="p296817716212"><a name="p296817716212"></a><a name="p296817716212"></a>Capability</p>
22</th>
23<th class="cellrowborder" valign="top" width="32.36%" id="mcps1.2.4.1.2"><p id="p596897172119"><a name="p596897172119"></a><a name="p596897172119"></a>Function</p>
24</th>
25<th class="cellrowborder" valign="top" width="47.9%" id="mcps1.2.4.1.3"><p id="p39681677213"><a name="p39681677213"></a><a name="p39681677213"></a>Description</p>
26</th>
27</tr>
28</thead>
29<tbody><tr id="row896847202113"><td class="cellrowborder" rowspan="2" valign="top" width="19.74%" headers="mcps1.2.4.1.1 "><p id="p1796814719210"><a name="p1796814719210"></a><a name="p1796814719210"></a>GPIO read/write</p>
30</td>
31<td class="cellrowborder" valign="top" width="32.36%" headers="mcps1.2.4.1.2 "><p id="p39683732112"><a name="p39683732112"></a><a name="p39683732112"></a>GpioRead</p>
32</td>
33<td class="cellrowborder" valign="top" width="47.9%" headers="mcps1.2.4.1.3 "><p id="p59687710219"><a name="p59687710219"></a><a name="p59687710219"></a>Reads the level value of a GPIO pin.</p>
34</td>
35</tr>
36<tr id="row17968872212"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p396812722116"><a name="p396812722116"></a><a name="p396812722116"></a>GpioWrite</p>
37</td>
38<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p396814715219"><a name="p396814715219"></a><a name="p396814715219"></a>Writes the level value of a GPIO pin.</p>
39</td>
40</tr>
41<tr id="row129681576218"><td class="cellrowborder" rowspan="2" valign="top" width="19.74%" headers="mcps1.2.4.1.1 "><p id="p1496813782116"><a name="p1496813782116"></a><a name="p1496813782116"></a>GPIO settings</p>
42</td>
43<td class="cellrowborder" valign="top" width="32.36%" headers="mcps1.2.4.1.2 "><p id="p29688742119"><a name="p29688742119"></a><a name="p29688742119"></a>GpioSetDir</p>
44</td>
45<td class="cellrowborder" valign="top" width="47.9%" headers="mcps1.2.4.1.3 "><p id="p179682792111"><a name="p179682792111"></a><a name="p179682792111"></a>Sets the direction for a GPIO pin.</p>
46</td>
47</tr>
48<tr id="row1196817715217"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p896827182120"><a name="p896827182120"></a><a name="p896827182120"></a>GpioGetDir</p>
49</td>
50<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p39689792111"><a name="p39689792111"></a><a name="p39689792111"></a>Obtains the direction for a GPIO pin.</p>
51</td>
52</tr>
53<tr id="row69682071217"><td class="cellrowborder" rowspan="4" valign="top" width="19.74%" headers="mcps1.2.4.1.1 "><p id="p296818714213"><a name="p296818714213"></a><a name="p296818714213"></a>GPIO interrupt settings</p>
54</td>
55<td class="cellrowborder" valign="top" width="32.36%" headers="mcps1.2.4.1.2 "><p id="p1396916710216"><a name="p1396916710216"></a><a name="p1396916710216"></a>GpioSetIrq</p>
56</td>
57<td class="cellrowborder" valign="top" width="47.9%" headers="mcps1.2.4.1.3 "><p id="p99693712113"><a name="p99693712113"></a><a name="p99693712113"></a>Sets the ISR function for a GPIO pin.</p>
58</td>
59</tr>
60<tr id="row4969117172110"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p119692079215"><a name="p119692079215"></a><a name="p119692079215"></a>GpioUnSetIrq</p>
61</td>
62<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p1996916792114"><a name="p1996916792114"></a><a name="p1996916792114"></a>Cancels the setting of the ISR function for a GPIO pin.</p>
63</td>
64</tr>
65<tr id="row396907112117"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p109694717216"><a name="p109694717216"></a><a name="p109694717216"></a>GpioEnableIrq</p>
66</td>
67<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p2969473216"><a name="p2969473216"></a><a name="p2969473216"></a>Enables GPIO interrupts for a pin.</p>
68</td>
69</tr>
70<tr id="row14969117152113"><td class="cellrowborder" valign="top" headers="mcps1.2.4.1.1 "><p id="p18969157182116"><a name="p18969157182116"></a><a name="p18969157182116"></a>GpioDisableIrq</p>
71</td>
72<td class="cellrowborder" valign="top" headers="mcps1.2.4.1.2 "><p id="p19690710214"><a name="p19690710214"></a><a name="p19690710214"></a>Disables GPIO interrupts for a pin.</p>
73</td>
74</tr>
75</tbody>
76</table>
77
78
79>![](../public_sys-resources/icon-note.gif) **NOTE**<br>
80>All functions provided in this document can be called only in kernel mode.
81
82## Usage Guidelines<a name="section259614242196"></a>
83
84### How to Use<a name="section103477714216"></a>
85
86The GPIO APIs use the GPIO pin number to specify a pin. The figure below illustrates how to use the APIs.
87
88**Figure  1** Using GPIO driver APIs
89
90![](figures/using-gpio-process.png "using-gpio-process.png")
91
92### Determining the GPIO Pin Number<a name="section370083272117"></a>
93
94The method for converting GPIO pin numbers varies depending on the GPIO controller model, parameters, and controller driver of different system on chips \(SoCs\).
95
96- Hi3516DV300
97
98  A controller manages 12 groups of GPIO pins. Each group contains 8 GPIO pins.
99
100  GPIO pin number = GPIO group index x Number of GPIO pins in each group + Offset in the group
101
102  The group index ranges from 0 to 11.
103
104  Example:
105
106  GPIO pin number of GPIO10\_3 = 10 x 8 + 3 = 83
107
108-   Hi3518EV300
109
110    A controller manages 10 groups of GPIO pins. Each group contains 10 GPIO pins.
111
112    GPIO pin number = GPIO group index \(0–9\) x Number of GPIO pins in each group \(10\) + Offset in the group
113
114    The group index ranges from 0 to 9.
115
116    Example:
117
118    GPIO pin number of GPIO7\_3 = 7 x 10 + 3 = 73
119
120
121### Using APIs to Operate GPIO Pins<a name="section13604050132118"></a>
122
123-   Set the direction for a GPIO pin.
124
125    Before performing read/write operations on a GPIO pin, call **GpioSetDir**() to set the direction.
126
127    int32\_t GpioSetDir\(uint16\_t gpio, uint16\_t dir\);
128
129   **Table  2** Description of GpioSetDir
130
131    <a name="table63111557616"></a>
132    <table><tbody><tr id="row17311165469"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p53110515616"><a name="p53110515616"></a><a name="p53110515616"></a><strong id="b1142022718160"><a name="b1142022718160"></a><a name="b1142022718160"></a>Parameter</strong></p>
133    </td>
134    <td class="cellrowborder" valign="top" width="51.88%"><p id="p5311454616"><a name="p5311454616"></a><a name="p5311454616"></a><strong id="b93161228141614"><a name="b93161228141614"></a><a name="b93161228141614"></a>Description</strong></p>
135    </td>
136    </tr>
137    <tr id="row0312151666"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p1431265763"><a name="p1431265763"></a><a name="p1431265763"></a>gpio</p>
138    </td>
139    <td class="cellrowborder" valign="top" width="51.88%"><p id="p83121553613"><a name="p83121553613"></a><a name="p83121553613"></a>GPIO pin number.</p>
140    </td>
141    </tr>
142    <tr id="row11312151619"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p173121451664"><a name="p173121451664"></a><a name="p173121451664"></a>dir</p>
143    </td>
144    <td class="cellrowborder" valign="top" width="51.88%"><p id="p153122520615"><a name="p153122520615"></a><a name="p153122520615"></a>Direction to set.</p>
145    </td>
146    </tr>
147    <tr id="row165937126386"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p83111453613"><a name="p83111453613"></a><a name="p83111453613"></a><strong id="b42491732121614"><a name="b42491732121614"></a><a name="b42491732121614"></a>Return Value</strong></p>
148    </td>
149    <td class="cellrowborder" valign="top" width="51.88%"><p id="p83111151165"><a name="p83111151165"></a><a name="p83111151165"></a><strong id="b1116336166"><a name="b1116336166"></a><a name="b1116336166"></a>Description</strong></p>
150    </td>
151    </tr>
152    <tr id="row205931212123817"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p18312151463"><a name="p18312151463"></a><a name="p18312151463"></a>0</p>
153    </td>
154    <td class="cellrowborder" valign="top" width="51.88%"><p id="p103124517618"><a name="p103124517618"></a><a name="p103124517618"></a>The operation is successful.</p>
155    </td>
156    </tr>
157    <tr id="row75931212153818"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p23121951261"><a name="p23121951261"></a><a name="p23121951261"></a>Negative value</p>
158    </td>
159    <td class="cellrowborder" valign="top" width="51.88%"><p id="p153121553610"><a name="p153121553610"></a><a name="p153121553610"></a>The operation failed.</p>
160    </td>
161    </tr>
162    </tbody>
163    </table>
164
165
166-   Read or write the level value for a GPIO pin.
167
168    Call **GpioRead**() to read the level value of a GPIO pin.
169
170    int32\_t GpioRead\(uint16\_t gpio, uint16\_t \*val\);
171
172   **Table  3** Description of GpioRead
173
174    <a name="table20347743174816"></a>
175    <table><tbody><tr id="row17348144394816"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p19348164313481"><a name="p19348164313481"></a><a name="p19348164313481"></a><strong id="b4862227121610"><a name="b4862227121610"></a><a name="b4862227121610"></a>Parameter</strong></p>
176    </td>
177    <td class="cellrowborder" valign="top" width="51.88%"><p id="p134810432488"><a name="p134810432488"></a><a name="p134810432488"></a><strong id="b677332841611"><a name="b677332841611"></a><a name="b677332841611"></a>Description</strong></p>
178    </td>
179    </tr>
180    <tr id="row134874324814"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p183481437485"><a name="p183481437485"></a><a name="p183481437485"></a>gpio</p>
181    </td>
182    <td class="cellrowborder" valign="top" width="51.88%"><p id="p43481043194819"><a name="p43481043194819"></a><a name="p43481043194819"></a>GPIO pin number.</p>
183    </td>
184    </tr>
185    <tr id="row20348343144815"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p1534864310480"><a name="p1534864310480"></a><a name="p1534864310480"></a>val</p>
186    </td>
187    <td class="cellrowborder" valign="top" width="51.88%"><p id="p13689159154815"><a name="p13689159154815"></a><a name="p13689159154815"></a>Pointer to the level value.</p>
188    </td>
189    </tr>
190    <tr id="row19348043154813"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p1234812431480"><a name="p1234812431480"></a><a name="p1234812431480"></a><strong id="b10599632101619"><a name="b10599632101619"></a><a name="b10599632101619"></a>Return Value</strong></p>
191    </td>
192    <td class="cellrowborder" valign="top" width="51.88%"><p id="p434894334814"><a name="p434894334814"></a><a name="p434894334814"></a><strong id="b236513361617"><a name="b236513361617"></a><a name="b236513361617"></a>Description</strong></p>
193    </td>
194    </tr>
195    <tr id="row3348184311486"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p1934854315487"><a name="p1934854315487"></a><a name="p1934854315487"></a>0</p>
196    </td>
197    <td class="cellrowborder" valign="top" width="51.88%"><p id="p103481943114814"><a name="p103481943114814"></a><a name="p103481943114814"></a>The operation is successful.</p>
198    </td>
199    </tr>
200    <tr id="row23485436482"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p1134834310486"><a name="p1134834310486"></a><a name="p1134834310486"></a>Negative value</p>
201    </td>
202    <td class="cellrowborder" valign="top" width="51.88%"><p id="p93491343144815"><a name="p93491343144815"></a><a name="p93491343144815"></a>The operation failed.</p>
203    </td>
204    </tr>
205    </tbody>
206    </table>
207
208
209    Call **GpioWrite()** to write the level value for a GPIO pin.
210
211    int32\_t GpioWrite\(uint16\_t gpio, uint16\_t val\);
212
213   **Table  4** Description of GpioWrite
214
215    <a name="table1214911207520"></a>
216    <table><tbody><tr id="row6149720175218"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p18149132005216"><a name="p18149132005216"></a><a name="p18149132005216"></a><strong id="b19864427181615"><a name="b19864427181615"></a><a name="b19864427181615"></a>Parameter</strong></p>
217    </td>
218    <td class="cellrowborder" valign="top" width="51.88%"><p id="p16149220145216"><a name="p16149220145216"></a><a name="p16149220145216"></a><strong id="b4774132861616"><a name="b4774132861616"></a><a name="b4774132861616"></a>Description</strong></p>
219    </td>
220    </tr>
221    <tr id="row16149102014526"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p31495206527"><a name="p31495206527"></a><a name="p31495206527"></a>gpio</p>
222    </td>
223    <td class="cellrowborder" valign="top" width="51.88%"><p id="p1014972085212"><a name="p1014972085212"></a><a name="p1014972085212"></a>GPIO pin number.</p>
224    </td>
225    </tr>
226    <tr id="row3149112095214"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p1815072011528"><a name="p1815072011528"></a><a name="p1815072011528"></a>val</p>
227    </td>
228    <td class="cellrowborder" valign="top" width="51.88%"><p id="p1931618337524"><a name="p1931618337524"></a><a name="p1931618337524"></a>Level value to write.</p>
229    </td>
230    </tr>
231    <tr id="row1115062015220"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p10150172015218"><a name="p10150172015218"></a><a name="p10150172015218"></a><strong id="b760183221611"><a name="b760183221611"></a><a name="b760183221611"></a>Return Value</strong></p>
232    </td>
233    <td class="cellrowborder" valign="top" width="51.88%"><p id="p1150192015527"><a name="p1150192015527"></a><a name="p1150192015527"></a><strong id="b14366123310160"><a name="b14366123310160"></a><a name="b14366123310160"></a>Description</strong></p>
234    </td>
235    </tr>
236    <tr id="row111503202526"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p171501320205216"><a name="p171501320205216"></a><a name="p171501320205216"></a>0</p>
237    </td>
238    <td class="cellrowborder" valign="top" width="51.88%"><p id="p15150102017522"><a name="p15150102017522"></a><a name="p15150102017522"></a>The operation is successful.</p>
239    </td>
240    </tr>
241    <tr id="row1615002018528"><td class="cellrowborder" valign="top" width="48.120000000000005%"><p id="p15150182045212"><a name="p15150182045212"></a><a name="p15150182045212"></a>Negative value</p>
242    </td>
243    <td class="cellrowborder" valign="top" width="51.88%"><p id="p13150320105212"><a name="p13150320105212"></a><a name="p13150320105212"></a>The operation failed.</p>
244    </td>
245    </tr>
246    </tbody>
247    </table>
248
249    Example:
250
251    ```
252    int32_t ret;
253    uint16_t val;
254    /* Set the output direction for GPIO3. */
255    ret = GpioSetDir(3, GPIO_DIR_OUT);
256    if (ret != 0) {
257        HDF_LOGE("GpioSerDir: failed, ret %d\n", ret);
258        return;
259    }
260    /* Write the low level GPIO_VAL_LOW for GPIO3. */
261    ret = GpioWrite(3, GPIO_VAL_LOW);
262    if (ret != 0) {
263        HDF_LOGE("GpioWrite: failed, ret %d\n", ret);
264        return;
265    }
266    /* Set the input direction for GPIO6. */
267    ret = GpioSetDir(6, GPIO_DIR_IN);
268    if (ret != 0) {
269        HDF_LOGE("GpioSetDir: failed, ret %d\n", ret);
270        return;
271    }
272    /* Read the level value of GPIO6. */
273    ret = GpioRead(6, &val);
274    ```
275
276
277-   Set the ISR function for a GPIO pin.
278
279    Call **GpioSetIrq()** to set the ISR function for a GPIO pin.
280
281    int32\_t GpioSetIrq\(uint16\_t gpio, uint16\_t mode, GpioIrqFunc func, void \*arg\);
282
283   **Table  5** Description of GpioSetIrq
284
285    <a name="table16804111812466"></a>
286    <table><tbody><tr id="row880401834615"><td class="cellrowborder" valign="top" width="48.54%"><p id="p380491819469"><a name="p380491819469"></a><a name="p380491819469"></a><strong id="b0865192761614"><a name="b0865192761614"></a><a name="b0865192761614"></a>Parameter</strong></p>
287    </td>
288    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p48041318114619"><a name="p48041318114619"></a><a name="p48041318114619"></a><strong id="b1477520282162"><a name="b1477520282162"></a><a name="b1477520282162"></a>Description</strong></p>
289    </td>
290    </tr>
291    <tr id="row19805181812465"><td class="cellrowborder" valign="top" width="48.54%"><p id="p11805101874611"><a name="p11805101874611"></a><a name="p11805101874611"></a>gpio</p>
292    </td>
293    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p6805181818461"><a name="p6805181818461"></a><a name="p6805181818461"></a>GPIO pin number.</p>
294    </td>
295    </tr>
296    <tr id="row1080541817469"><td class="cellrowborder" valign="top" width="48.54%"><p id="p580541864611"><a name="p580541864611"></a><a name="p580541864611"></a>mode</p>
297    </td>
298    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p380511180463"><a name="p380511180463"></a><a name="p380511180463"></a>Interrupt trigger mode.</p>
299    </td>
300    </tr>
301    <tr id="row83541951134617"><td class="cellrowborder" valign="top" width="48.54%"><p id="p5355351104610"><a name="p5355351104610"></a><a name="p5355351104610"></a>func</p>
302    </td>
303    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p11355551174619"><a name="p11355551174619"></a><a name="p11355551174619"></a>ISR function to set.</p>
304    </td>
305    </tr>
306    <tr id="row6593577469"><td class="cellrowborder" valign="top" width="48.54%"><p id="p165985724619"><a name="p165985724619"></a><a name="p165985724619"></a>arg</p>
307    </td>
308    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p559185784619"><a name="p559185784619"></a><a name="p559185784619"></a>Pointer to the parameters passed to the ISR function.</p>
309    </td>
310    </tr>
311    <tr id="row16299193210587"><td class="cellrowborder" valign="top" width="48.54%"><p id="p7804101884614"><a name="p7804101884614"></a><a name="p7804101884614"></a><strong id="b126021932131615"><a name="b126021932131615"></a><a name="b126021932131615"></a>Return Value</strong></p>
312    </td>
313    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p680441818466"><a name="p680441818466"></a><a name="p680441818466"></a><strong id="b836716339162"><a name="b836716339162"></a><a name="b836716339162"></a>Description</strong></p>
314    </td>
315    </tr>
316    <tr id="row12299632125817"><td class="cellrowborder" valign="top" width="48.54%"><p id="p1180511189465"><a name="p1180511189465"></a><a name="p1180511189465"></a>0</p>
317    </td>
318    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p180521812465"><a name="p180521812465"></a><a name="p180521812465"></a>The operation is successful.</p>
319    </td>
320    </tr>
321    <tr id="row029833235815"><td class="cellrowborder" valign="top" width="48.54%"><p id="p1080591814468"><a name="p1080591814468"></a><a name="p1080591814468"></a>Negative value</p>
322    </td>
323    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p18805141884611"><a name="p18805141884611"></a><a name="p18805141884611"></a>The operation failed.</p>
324    </td>
325    </tr>
326    </tbody>
327    </table>
328
329    >![](../public_sys-resources/icon-caution.gif) **CAUTION**<br/>
330    >Only one ISR function can be set for a GPIO pin at a time. If **GpioSetIrq** is called repeatedly, the previous IRS function will be replaced.
331
332    If the ISR function is no longer required, call **GpioUnSetIrq()** to cancel the setting.
333
334    int32\_t GpioUnSetIrq\(uint16\_t gpio\);
335
336   **Table  6** Description of GpioUnSetIrq
337
338    <a name="table1157224664316"></a>
339    <table><tbody><tr id="row175721546174317"><td class="cellrowborder" valign="top" width="48.54%"><p id="p16572144694311"><a name="p16572144694311"></a><a name="p16572144694311"></a><strong id="b16866132761617"><a name="b16866132761617"></a><a name="b16866132761617"></a>Parameter</strong></p>
340    </td>
341    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p185721461435"><a name="p185721461435"></a><a name="p185721461435"></a><strong id="b1377613282167"><a name="b1377613282167"></a><a name="b1377613282167"></a>Description</strong></p>
342    </td>
343    </tr>
344    <tr id="row1257284664318"><td class="cellrowborder" valign="top" width="48.54%"><p id="p95721946144317"><a name="p95721946144317"></a><a name="p95721946144317"></a>gpio</p>
345    </td>
346    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p1557313464439"><a name="p1557313464439"></a><a name="p1557313464439"></a>GPIO pin number.</p>
347    </td>
348    </tr>
349    <tr id="row1857324618435"><td class="cellrowborder" valign="top" width="48.54%"><p id="p1257344624314"><a name="p1257344624314"></a><a name="p1257344624314"></a><strong id="b1160353241610"><a name="b1160353241610"></a><a name="b1160353241610"></a>Return Value</strong></p>
350    </td>
351    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p457384611439"><a name="p457384611439"></a><a name="p457384611439"></a><strong id="b133681833151617"><a name="b133681833151617"></a><a name="b133681833151617"></a>Description</strong></p>
352    </td>
353    </tr>
354    <tr id="row357318466439"><td class="cellrowborder" valign="top" width="48.54%"><p id="p1573164616438"><a name="p1573164616438"></a><a name="p1573164616438"></a>0</p>
355    </td>
356    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p857384614319"><a name="p857384614319"></a><a name="p857384614319"></a>The operation is successful.</p>
357    </td>
358    </tr>
359    <tr id="row18573124610433"><td class="cellrowborder" valign="top" width="48.54%"><p id="p165731146134311"><a name="p165731146134311"></a><a name="p165731146134311"></a>Negative value</p>
360    </td>
361    <td class="cellrowborder" valign="top" width="51.459999999999994%"><p id="p6573164613437"><a name="p6573164613437"></a><a name="p6573164613437"></a>The operation failed.</p>
362    </td>
363    </tr>
364    </tbody>
365    </table>
366
367    After the ISR function is set, call **GpioEnableIrq()** to enable interrupts.
368
369    int32\_t GpioEnableIrq\(uint16\_t gpio\);
370
371   **Table  7** Description of GpioEnableIrq
372
373    <a name="table26659291568"></a>
374    <table><tbody><tr id="row866632919566"><td class="cellrowborder" valign="top" width="50%"><p id="p066642985615"><a name="p066642985615"></a><a name="p066642985615"></a><strong id="b108661927171614"><a name="b108661927171614"></a><a name="b108661927171614"></a>Parameter</strong></p>
375    </td>
376    <td class="cellrowborder" valign="top" width="50%"><p id="p566613293568"><a name="p566613293568"></a><a name="p566613293568"></a><strong id="b10777228121617"><a name="b10777228121617"></a><a name="b10777228121617"></a>Description</strong></p>
377    </td>
378    </tr>
379    <tr id="row19666029165620"><td class="cellrowborder" valign="top" width="50%"><p id="p16660295566"><a name="p16660295566"></a><a name="p16660295566"></a>gpio</p>
380    </td>
381    <td class="cellrowborder" valign="top" width="50%"><p id="p1566632916566"><a name="p1566632916566"></a><a name="p1566632916566"></a>GPIO pin number.</p>
382    </td>
383    </tr>
384    <tr id="row84182176010"><td class="cellrowborder" valign="top" width="50%"><p id="p1566652915566"><a name="p1566652915566"></a><a name="p1566652915566"></a><strong id="b160443201612"><a name="b160443201612"></a><a name="b160443201612"></a>Return Value</strong></p>
385    </td>
386    <td class="cellrowborder" valign="top" width="50%"><p id="p966642917562"><a name="p966642917562"></a><a name="p966642917562"></a><strong id="b12369193319162"><a name="b12369193319162"></a><a name="b12369193319162"></a>Description</strong></p>
387    </td>
388    </tr>
389    <tr id="row154188171403"><td class="cellrowborder" valign="top" width="50%"><p id="p1866610292563"><a name="p1866610292563"></a><a name="p1866610292563"></a>0</p>
390    </td>
391    <td class="cellrowborder" valign="top" width="50%"><p id="p13666182975613"><a name="p13666182975613"></a><a name="p13666182975613"></a>The operation is successful.</p>
392    </td>
393    </tr>
394    <tr id="row1041891720012"><td class="cellrowborder" valign="top" width="50%"><p id="p766642911562"><a name="p766642911562"></a><a name="p766642911562"></a>Negative value</p>
395    </td>
396    <td class="cellrowborder" valign="top" width="50%"><p id="p1566652995613"><a name="p1566652995613"></a><a name="p1566652995613"></a>The operation failed.</p>
397    </td>
398    </tr>
399    </tbody>
400    </table>
401
402    >![](../public_sys-resources/icon-caution.gif) **CAUTION**<br/>
403    >The configured ISR function can be responded only after the GPIO interrupt is enabled.
404
405    Call **GpioDisableIrq()** to disable interrupts.
406
407    int32\_t GpioDisableIrq\(uint16\_t gpio\);
408
409   **Table  8** Description of GpioDisableIrq
410
411    <a name="table186682041918"></a>
412    <table><tbody><tr id="row186684413116"><td class="cellrowborder" valign="top" width="50%"><p id="p866844916"><a name="p866844916"></a><a name="p866844916"></a><strong id="b88678273169"><a name="b88678273169"></a><a name="b88678273169"></a>Parameter</strong></p>
413    </td>
414    <td class="cellrowborder" valign="top" width="50%"><p id="p46681413119"><a name="p46681413119"></a><a name="p46681413119"></a><strong id="b2077772812169"><a name="b2077772812169"></a><a name="b2077772812169"></a>Description</strong></p>
415    </td>
416    </tr>
417    <tr id="row4668243113"><td class="cellrowborder" valign="top" width="50%"><p id="p46681141919"><a name="p46681141919"></a><a name="p46681141919"></a>gpio</p>
418    </td>
419    <td class="cellrowborder" valign="top" width="50%"><p id="p136681241311"><a name="p136681241311"></a><a name="p136681241311"></a>GPIO pin number.</p>
420    </td>
421    </tr>
422    <tr id="row066884412"><td class="cellrowborder" valign="top" width="50%"><p id="p566824015"><a name="p566824015"></a><a name="p566824015"></a><strong id="b16605123251612"><a name="b16605123251612"></a><a name="b16605123251612"></a>Return Value</strong></p>
423    </td>
424    <td class="cellrowborder" valign="top" width="50%"><p id="p1766974515"><a name="p1766974515"></a><a name="p1766974515"></a><strong id="b1237063341617"><a name="b1237063341617"></a><a name="b1237063341617"></a>Description</strong></p>
425    </td>
426    </tr>
427    <tr id="row156694410112"><td class="cellrowborder" valign="top" width="50%"><p id="p14669141214"><a name="p14669141214"></a><a name="p14669141214"></a>0</p>
428    </td>
429    <td class="cellrowborder" valign="top" width="50%"><p id="p1266934818"><a name="p1266934818"></a><a name="p1266934818"></a>The operation is successful.</p>
430    </td>
431    </tr>
432    <tr id="row176691543117"><td class="cellrowborder" valign="top" width="50%"><p id="p7669941716"><a name="p7669941716"></a><a name="p7669941716"></a>Negative value</p>
433    </td>
434    <td class="cellrowborder" valign="top" width="50%"><p id="p4669164219"><a name="p4669164219"></a><a name="p4669164219"></a>The operation failed.</p>
435    </td>
436    </tr>
437    </tbody>
438    </table>
439
440    Example:
441
442    ```
443    /* ISR function */
444    */
445    int32_t MyCallBackFunc(uint16_t gpio, void *data)
446    {
447        HDF_LOGI("%s: gpio:%u interrupt service in! data=%p\n", __func__, gpio, data);
448        return 0;
449    }
450
451    int32_t ret;
452    /* Set the ISR function to MyCallBackFunc, the parameter to NULL, and the interrupt trigger mode to rising edge. */
453    ret = GpioSetIrq(3, OSAL_IRQF_TRIGGER_RISING, MyCallBackFunc, NULL);
454    if (ret != 0) {
455        HDF_LOGE("GpioSetIrq: failed, ret %d\n", ret);
456        return;
457    }
458
459    /* Enable an interrupt for GPIO3. */
460    ret = GpioEnableIrq(3);
461    if (ret != 0) {
462        HDF_LOGE("GpioEnableIrq: failed, ret %d\n", ret);
463        return;
464    }
465
466    /* Disable the interrupt for GPIO3. */
467    ret = GpioDisableIrq(3);
468    if (ret != 0) {
469        HDF_LOGE("GpioDisableIrq: failed, ret %d\n", ret);
470        return;
471    }
472
473    /* Cancel the ISR function for GPIO3. */
474    ret = GpioUnSetIrq(3);
475    if (ret != 0) {
476        HDF_LOGE("GpioUnSetIrq: failed, ret %d\n", ret);
477        return;
478    }
479    ```
480
481
482## Usage Example<a name="section25941262111"></a>
483
484The procedure is as follows:
485
4861. Select an idle GPIO pin.
487
488   This example uses pin GPIO10\_3 on a Hi3516D V300 development board as an example. The pin number is 83. You can select an idle GPIO pin as required.
489
4902. Set the ISR function for the pin, with the trigger mode of rising edge and failing edge.
491
4923. Write high and low levels to the pin alternately, and observe the execution of the ISR function.
493
494```
495#include "gpio_if.h"
496#include "hdf_log.h"
497#include "osal_irq.h"
498#include "osal_time.h"
499
500static uint32_t g_irqCnt;
501
502/* ISR function */
503static int32_t TestCaseGpioIrqHandler(uint16_t gpio, void *data)
504{
505    HDF_LOGE("%s: irq triggered! on gpio:%u, data=%p", __func__, gpio, data);
506    g_irqCnt++; /* If the ISR function is triggered, the number of global interrupts is incremented by 1. */
507    return GpioDisableIrq(gpio);
508}
509
510/* Test case function */
511static int32_t TestCaseGpioIrqEdge(void)
512{
513    int32_t ret;
514    uint16_t valRead;
515    uint16_t mode;
516    uint16_t gpio = 83; /* Number of the GPIO pin to test */
517    uint32_t timeout;
518
519    /* Set the output direction for the pin. */
520    ret = GpioSetDir(gpio, GPIO_DIR_OUT);
521    if (ret != HDF_SUCCESS) {
522        HDF_LOGE("%s: set dir fail! ret:%d\n", __func__, ret);
523        return ret;
524    }
525
526    /* Disable the interrupt of the pin. */
527    ret = GpioDisableIrq(gpio);
528    if (ret != HDF_SUCCESS) {
529        HDF_LOGE("%s: disable irq fail! ret:%d\n", __func__, ret);
530        return ret;
531    }
532
533    /* Set the ISR function for the pin. The trigger mode is both rising edge and falling edge. */
534    mode = OSAL_IRQF_TRIGGER_RISING | OSAL_IRQF_TRIGGER_FALLING;
535    HDF_LOGE("%s: mode:%0x\n", __func__, mode);
536    ret = GpioSetIrq(gpio, mode, TestCaseGpioIrqHandler, NULL);
537    if (ret != HDF_SUCCESS) {
538        HDF_LOGE("%s: set irq fail! ret:%d\n", __func__, ret);
539        return ret;
540    }
541
542    /* Enable the interrupt for this pin. */
543    ret = GpioEnableIrq(gpio);
544    if (ret != HDF_SUCCESS) {
545        HDF_LOGE("%s: enable irq fail! ret:%d\n", __func__, ret);
546        (void)GpioUnSetIrq(gpio);
547        return ret;
548    }
549
550    g_irqCnt = 0; /* Reset the global counter. */
551    timeout = 0;  /* Reset the waiting time. */
552    /* Wait for the ISR function of this pin to trigger. The timeout duration is 1000 ms. */
553    while (g_irqCnt <= 0 && timeout < 1000) {
554        (void)GpioRead(gpio, &valRead);
555        (void)GpioWrite(gpio, (valRead == GPIO_VAL_LOW) ? GPIO_VAL_HIGH : GPIO_VAL_LOW);
556        HDF_LOGE("%s: wait irq timeout:%u\n", __func__, timeout);
557        OsalMDelay(200); /* wait for irq trigger */
558        timeout += 200;
559    }
560    (void)GpioUnSetIrq(gpio);
561    return (g_irqCnt > 0) ? HDF_SUCCESS : HDF_FAILURE;
562}
563```