1/*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16/**
17 * @addtogroup IotHardware
18 * @{
19 *
20 * @brief Provides APIs for operating devices,
21 * including flash, GPIO, I2C, PWM, UART, and watchdog APIs.
22 *
23 *
24 *
25 * @since 2.2
26 * @version 2.2
27 */
28
29/**
30 * @file iot_gpio.h
31 *
32 * @brief Declares functions for operating GPIO devices.
33 *
34 * These functions are used for GPIO initialization, input/output settings, and level settings. \n
35 *
36 * @since 2.2
37 * @version 2.2
38 */
39#ifndef IOT_GPIO_H
40#define IOT_GPIO_H
41
42/**
43 * @brief Enumerates GPIO level values.
44 */
45typedef enum {
46    /** Low GPIO level */
47    IOT_GPIO_VALUE0 = 0,
48    /** High GPIO level */
49    IOT_GPIO_VALUE1
50} IotGpioValue;
51
52/**
53 * @brief Enumerates GPIO directions.
54 */
55typedef enum {
56    /** Input */
57    IOT_GPIO_DIR_IN = 0,
58    /** Output */
59    IOT_GPIO_DIR_OUT
60} IotGpioDir;
61
62/**
63 * @brief Enumerates GPIO interrupt trigger modes.
64 */
65typedef enum {
66    /** Level-sensitive interrupt */
67    IOT_INT_TYPE_LEVEL = 0,
68    /** Edge-sensitive interrupt */
69    IOT_INT_TYPE_EDGE
70} IotGpioIntType;
71
72/**
73 * @brief Enumerates I/O interrupt polarities.
74 */
75typedef enum {
76    /** Interrupt at a low level or falling edge */
77    IOT_GPIO_EDGE_FALL_LEVEL_LOW = 0,
78    /** Interrupt at a high level or rising edge */
79    IOT_GPIO_EDGE_RISE_LEVEL_HIGH
80} IotGpioIntPolarity;
81
82/**
83 * @brief Indicates the GPIO interrupt callback.
84 *
85 */
86typedef void (*GpioIsrCallbackFunc) (char *arg);
87
88/**
89 * @brief Initializes a GPIO device.
90 *
91 * @param id Indicates the GPIO pin number.
92 * @return Returns {@link IOT_SUCCESS} if the GPIO device is initialized;
93 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
94 * @since 2.2
95 * @version 2.2
96 */
97unsigned int IoTGpioInit(unsigned int id);
98
99/**
100 * @brief Deinitializes a GPIO device.
101 *
102 * @param id Indicates the GPIO pin number.
103 * @return Returns {@link IOT_SUCCESS} if the GPIO device is deinitialized;
104 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
105 * @since 2.2
106 * @version 2.2
107 */
108unsigned int IoTGpioDeinit(unsigned int id);
109
110/**
111 * @brief Sets the direction for a GPIO pin.
112 *
113 * @param id Indicates the GPIO pin number.
114 * @param dir Indicates the GPIO input/output direction.
115 * @return Returns {@link IOT_SUCCESS} if the direction is set;
116 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
117 * @since 2.2
118 * @version 2.2
119 */
120unsigned int IoTGpioSetDir(unsigned int id, IotGpioDir dir);
121
122/**
123 * @brief Obtains the direction for a GPIO pin.
124 *
125 * @param id Indicates the GPIO pin number.
126 * @param dir Indicates the pointer to the GPIO input/output direction.
127 * @return Returns {@link IOT_SUCCESS} if the direction is obtained;
128 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
129 * @since 2.2
130 * @version 2.2
131 */
132unsigned int IoTGpioGetDir(unsigned int id, IotGpioDir *dir);
133
134/**
135 * @brief Sets the output level value for a GPIO pin.
136 *
137 * @param id Indicates the GPIO pin number.
138 * @param val Indicates the output level value.
139 * @return Returns {@link IOT_SUCCESS} if the output level value is set;
140 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
141 * @since 2.2
142 * @version 2.2
143 */
144unsigned int IoTGpioSetOutputVal(unsigned int id, IotGpioValue val);
145
146/**
147 * @brief Obtains the output level value of a GPIO pin.
148 *
149 * @param id Indicates the GPIO pin number.
150 * @param val Indicates the pointer to the output level value.
151 * @return Returns {@link IOT_SUCCESS} if the output level value is obtained;
152 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
153 * @since 2.2
154 * @version 2.2
155 */
156unsigned int IoTGpioGetOutputVal(unsigned int id, IotGpioValue *val);
157
158/**
159 * @brief Obtains the input level value of a GPIO pin.
160 *
161 * @param id Indicates the GPIO pin number.
162 * @param val Indicates the pointer to the input level value.
163 * @return Returns {@link IOT_SUCCESS} if the input level value is obtained;
164 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
165 * @since 2.2
166 * @version 2.2
167 */
168unsigned int IoTGpioGetInputVal(unsigned int id, IotGpioValue *val);
169
170/**
171 * @brief Enables the interrupt feature for a GPIO pin.
172 *
173 * This function can be used to set the interrupt type, interrupt polarity, and interrupt callback for a GPIO pin.
174 *
175 * @param id Indicates the GPIO pin number.
176 * @param intType Indicates the interrupt type.
177 * @param intPolarity Indicates the interrupt polarity.
178 * @param func Indicates the interrupt callback function.
179 * @param arg Indicates the pointer to the argument used in the interrupt callback function.
180 * @return Returns {@link IOT_SUCCESS} if the interrupt feature is enabled;
181 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
182 * @since 2.2
183 * @version 2.2
184 */
185unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity,
186                                    GpioIsrCallbackFunc func, char *arg);
187
188/**
189 * @brief Disables the interrupt feature for a GPIO pin.
190 *
191 * @param id Indicates the GPIO pin number.
192 * @return Returns {@link IOT_SUCCESS} if the interrupt feature is disabled;
193 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
194 * @since 2.2
195 * @version 2.2
196 */
197unsigned int IoTGpioUnregisterIsrFunc(unsigned int id);
198
199/**
200 * @brief Masks the interrupt feature for a GPIO pin.
201 *
202 * @param id Indicates the GPIO pin number.
203 * @param mask Indicates whether the interrupt function is masked.
204 * The value <b>1</b> means to mask the interrupt function, and <b>0</b> means not to mask the interrupt function.
205 * @return Returns {@link IOT_SUCCESS} if the interrupt feature is masked;
206 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
207 * @since 2.2
208 * @version 2.2
209 */
210unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask);
211
212/**
213 * @brief Sets the interrupt trigger mode of a GPIO pin.
214 *
215 * This function configures a GPIO pin based on the interrupt type and interrupt polarity.
216 *
217 * @param id Indicates the GPIO pin number.
218 * @param intType Indicates the interrupt type.
219 * @param intPolarity Indicates the interrupt polarity.
220 * @return Returns {@link IOT_SUCCESS} if the interrupt trigger mode is set;
221 * returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
222 * @since 2.2
223 * @version 2.2
224 */
225unsigned int IoTGpioSetIsrMode(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity);
226
227#endif // IOT_GPIO_H
228/** @} */
229