• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: Provides HAL GPIO \n
16  *
17  * History: \n
18  * 2022-07-22, Create file. \n
19  */
20 
21 #ifndef HAL_GPIO_H
22 #define HAL_GPIO_H
23 
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include "common_def.h"
27 #include "errcode.h"
28 #include "platform_core.h"
29 #include "gpio_porting.h"
30 
31 #ifdef __cplusplus
32 #if __cplusplus
33 extern "C" {
34 #endif /* __cplusplus */
35 #endif /* __cplusplus */
36 
37 /**
38  * @defgroup drivers_hal_gpio_api GPIO
39  * @ingroup  drivers_hal_gpio
40  * @{
41  */
42 
43 #define HAL_GPIO_MAX_CHANNEL GPIO_CHANNELS_NUM
44 #define HAL_GPIO_CHANNEL_OFFSET 32
45 
46 /**
47  * @if Eng
48  * @brief  GPIO MAX PIN numbers.
49  * @else
50  * @brief  GPIO最大PIN管脚数量。
51  * @endif
52  */
53 #define PIN_MAX_NUMBER                    PIN_NONE
54 
55 /**
56  * @if Eng
57  * @brief  GPIO Interrupt type.
58  * @else
59  * @brief  GPIO中断类型。
60  * @endif
61  */
62 #define GPIO_INTERRUPT_RISING_EDGE            0x00000001
63 #define GPIO_INTERRUPT_FALLING_EDGE           0x00000002
64 #define GPIO_INTERRUPT_LOW                    0x00000004
65 #define GPIO_INTERRUPT_HIGH                   0x00000008
66 #define GPIO_INTERRUPT_DEDGE                  (GPIO_INTERRUPT_RISING_EDGE | GPIO_INTERRUPT_FALLING_EDGE)
67 
68 /**
69  * @if Eng
70  * @brief  GPIO directions.
71  * @else
72  * @brief  GPIO输入输出方向。
73  * @endif
74  */
75 typedef enum gpio_direction {
76     GPIO_DIRECTION_INPUT,
77     GPIO_DIRECTION_OUTPUT
78 } gpio_direction_t;
79 
80 /**
81  * @if Eng
82  * @brief  GPIO LEVEL.
83  * @else
84  * @brief  GPIO高低电平。
85  * @endif
86  */
87 typedef enum gpio_level {
88     GPIO_LEVEL_LOW,
89     GPIO_LEVEL_HIGH
90 } gpio_level_t;
91 
92 /**
93  * @brief  GPIO INTTYPE.
94  */
95 typedef enum gpio_intr_type {
96     GPIO_LVL_LEVEL_SENSITIVE,     /*!< GPIO Interrupt is level sensitive   (used for input/output). */
97     GPIO_LVL_EDGE_SENSITIVE,      /*!< GPIO Interrupt is edge sensitive  (used for input/output). */
98 } gpio_intr_type_t;
99 
100 /**
101  * @brief  GPIO POLARITY.
102  */
103 typedef enum gpio_polarity {
104     GPIO_ACTIVE_LOW,     /*!< GPIO is at a low logic level   (used for input/output). */
105     GPIO_ACTIVE_HIGH,    /*!< GPIO is at a high logic level  (used for input/output). */
106 } gpio_polarity_t;
107 
108 /**
109  * @if Eng
110  * @brief  GPIO both edge enable/disable.
111  * @else
112  * @brief  GPIO双边沿使能或去使能。
113  * @endif
114  */
115 typedef enum gpio_dedge {
116     GPIO_DEDGE_DISABLED,        /*!< @if Eng GPIO single edge sensitive.
117                                          @else   GPIO单边沿中断。 @endif */
118     GPIO_DEDGE_ENABLED,         /*!< @if Eng GPIO dual edge sensitive.
119                                          @else   GPIO双边沿中断。 @endif */
120 } gpio_dedge_t;
121 
122 /**
123  * @if Eng
124  * @brief  Definition of the contorl ID of hal GPIO.
125  * @else
126  * @brief  GPIO控制ID定义。
127  * @endif
128  */
129 typedef enum hal_gpio_ctrl_id {
130     GPIO_CTRL_TOGGLE = 0,                /*!< @if Eng Toggle the value of a GPIO previously claimed for output.
131                                               @else   GPIO输出电平状态反转 @endif */
132     GPIO_CTRL_ENABLE_INTERRUPT,          /*!< @if Eng enable interrupt for a specific PIN.
133                                               @else   GPIO使能指定端口的中断 @endif */
134     GPIO_CTRL_DISABLE_INTERRUPT,         /*!< @if Eng Disable interrupt for a specific PIN.
135                                               @else   GPIO去使能指定端口的中断 @endif */
136     GPIO_CTRL_CLEAR_INTERRUPT,           /*!< @if Eng Clear the interrupt for a pin configured as GPIO input.
137                                               @else   GPIO清理中断 @endif */
138     GPIO_CTRL_SUSPEND,                   /*!< @if Eng Suspend all of the GPIO channels.
139                                               @else   挂起所有GPIO通道。 @endif */
140     GPIO_CTRL_RESUME,                    /*!< @if Eng Suspend all of the GPIO channels.
141                                               @else   挂起所有GPIO通道。 @endif */
142     GPIO_CTRL_MAX
143 } hal_gpio_ctrl_id_t;
144 
145 /**
146  * @if Eng
147  * @brief  Context of GPIO module.
148  * @else
149  * @brief  GPIO模块配置信息。
150  * @endif
151  */
152 typedef struct {
153     pin_t pin_max_num;                          /*!< @if Eng Indicates the max number of PIN.
154                                                      @else   最大PIN数。 @endif */
155     pin_t ulp_pin_start;                        /*!< @if Eng Indicates the start pin of ulp.
156                                                      @else   ULP的起始管脚。 @endif */
157     pin_t ulp_pin_end;                          /*!< @if Eng Indicates the end pin of ulp.
158                                                      @else   ULP的终止管脚。 @endif */
159     bool is_ulp_enable;                         /*!< @if Eng Indicates the enabled state of ulp.
160                                                      @else   ULP的使能状态。 @endif */
161     bool is_irq_enable;                         /*!< @if Eng Indicates the enabled irq.
162                                                      @else   中断的使能状态。 @endif */
163     uint32_t irq_list[GPIO_CHANNELS_NUM];       /*!< @if Eng Indicates the list of irq index foreach channel.
164                                                      @else   每个通道的中断号索引列表。 @endif */
165     uint32_t claimed[GPIO_CHANNELS_NUM];        /*!< @if Eng Indicates which PIN have been claimed as GPIO.
166                                                      @else   指示哪个PIN已被声明为GPIO。 @endif */
167     uint32_t direction[GPIO_CHANNELS_NUM];      /*!< @if Eng Indicates if the PIN has been claimed as Input or Output.
168                                                      @else   指示PIN是否已声明为输入或输出。 @endif */
169 } gpio_context_t;
170 
171 /**
172  * @if Eng
173  * @brief  Typedef for the GPIO Callback.
174  *         It returns the number of the PIN which originated the interrupt.
175  * @param  [in]  pin PIN that caused the interrupt.
176  * @param  [in]  param Parameter sent to callback.
177  * @else
178  * @brief  GPIO回调的类型定义,它返回发起中断的PIN的编号。
179  * @param  [in]  pin 中断的PIN的编号.
180  * @param  [in]  param 回调函数.
181  * @endif
182  */
183 typedef void (*gpio_callback_t)(pin_t pin, uintptr_t param);
184 
185 /**
186  * @if Eng
187  * @brief  Context of GPIO HAL module.
188  * @else
189  * @brief  GPIO HAL层模块配置信息。
190  * @endif
191  */
192 typedef struct {
193     pin_t pin_max_num;
194     bool is_ulp_enable;
195     pin_t ulp_pin_start;
196     pin_t ulp_pin_end;
197     gpio_channel_t ulp_pin_channel;
198     gpio_callback_t callback[PIN_MAX_NUMBER];
199 } hal_gpio_context_t;
200 
201 /**
202  * @if Eng
203  * @brief  HAL GPIO init interface.
204  * @retval ERRCODE_SUCC   Success.
205  * @retval Other        Failure. For details, see @ref errcode_t.
206  * @else
207  * @brief  HAL层GPIO的初始化接口。
208  * @retval ERRCODE_SUCC 成功。
209  * @retval Other        失败,参考 @ref errcode_t 。
210  * @endif
211  */
212 void hal_gpio_init(void);
213 
214 /**
215  * @if Eng
216  * @brief  HAL GPIO deinit interface.
217  * @retval ERRCODE_SUCC   Success.
218  * @retval Other        Failure. For details, see @ref errcode_t.
219  * @else
220  * @brief  HAL层GPIO去初始化接口。
221  * @retval ERRCODE_SUCC 成功。
222  * @retval Other        失败,参考 @ref errcode_t 。
223  * @endif
224  */
225 void hal_gpio_deinit(void);
226 
227 /**
228  * @if Eng
229  * @brief  HAL GPIO setdir device interface.
230  * @param  [in]  pin PIN to use. see @ref pin_t.
231  * @param  [in]  dir Input or output direction. see @ref gpio_direction_t.
232  * @retval ERRCODE_SUCC   Success.
233  * @retval Other        Failure. For details, see @ref errcode_t.
234  * @else
235  * @brief  HAL层GPIO设置输入输出方向接口。
236  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
237  * @param  [in]  dir 输入输出方向, 参考 @ref gpio_direction_t 。
238  * @retval ERRCODE_SUCC 成功。
239  * @retval Other        失败,参考 @ref errcode_t 。
240  * @endif
241  */
242 errcode_t hal_gpio_setdir(pin_t pin, gpio_direction_t dir);
243 
244 /**
245  * @if Eng
246  * @brief  HAL GPIO setdir device interface.
247  * @param  [in]  pin PIN to use. see @ref pin_t.
248  * @retval GPIO input or output direction.
249  * @else
250  * @brief  HAL层GPIO获取输入输出方向接口。
251  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
252  * @retval GPIO输入输出方向 。
253  * @endif
254  */
255 gpio_direction_t hal_gpio_getdir(pin_t pin);
256 
257 /**
258  * @if Eng
259  * @brief  HAL GPIO control interface.
260  * @param  [in]  pin PIN to use. see @ref pin_t
261  * @param  [in]  id ID of the GPIO control.
262  * @retval ERRCODE_SUCC   Success.
263  * @retval Other        Failure. For details, see @ref errcode_t.
264  * @else
265  * @brief  HAL层GPIO控制接口。
266  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
267  * @param  [in]  id GPIO控制请求ID。
268  * @retval ERRCODE_SUCC 成功。
269  * @retval Other        失败,参考 @ref errcode_t 。
270  * @endif
271  */
272 typedef errcode_t (*hal_gpio_ctrl_t)(pin_t pin, hal_gpio_ctrl_id_t id);
273 
274 /**
275  * @if Eng
276  * @brief  HAL GPIO control interface.
277  * @param  [in]  pin PIN to use. see @ref pin_t
278  * @param  [in]  id ID of the GPIO control.
279  * @retval ERRCODE_SUCC   Success.
280  * @retval Other        Failure. For details, see @ref errcode_t.
281  * @else
282  * @brief  HAL层GPIO控制接口。
283  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
284  * @param  [in]  id GPIO控制请求ID。
285  * @retval ERRCODE_SUCC 成功。
286  * @retval Other        失败,参考 @ref errcode_t 。
287  * @endif
288  */
289 errcode_t hal_gpio_ctrl(pin_t pin, hal_gpio_ctrl_id_t id);
290 
291 /**
292  * @if Eng
293  * @brief  HAL GPIO output interface.
294  * @param  [in]  pin PIN to use. see @ref pin_t.
295  * @param  [in]  level  Set to HIGH or LOW the output of a GPIO previously claimed for output. see @ref gpio_level_t.
296  * @retval ERRCODE_SUCC   Success.
297  * @retval Other        Failure. For details, see @ref errcode_t.
298  * @else
299  * @brief  HAL层GPIO的output接口 。
300  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
301  * @param  [in]  level 设置output输出HIGH or LOW,参考 @ref gpio_level_t 。
302  * @retval ERRCODE_SUCC 成功。
303  * @retval Other        失败,参考 @ref errcode_t 。
304  * @endif
305  */
306 errcode_t hal_gpio_output(pin_t pin, gpio_level_t level);
307 
308 /**
309  * @if Eng
310  * @brief  HAL GPIO get output value interface.
311  * @param  [in]  pin PIN to use. see @ref pin_t.
312  * @retval GPIO output level value(high or low).
313  * @else
314  * @brief  HAL层获取GPIO的output值接口 。
315  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
316  * @retval GPIO输出电平值 。
317  * @endif
318  */
319 gpio_level_t hal_gpio_get_outval(pin_t pin);
320 
321 /**
322  * @if Eng
323  * @brief  HAL GPIO get input level interface.
324  * @param  [in]  pin PIN to use. see @ref pin_t.
325  * @retval GPIO level, see @ref gpio_level_t.
326  * @else
327  * @brief  HAL层GPIO的input接口 。
328  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
329  * @retval GPIO电平,参考 @ref gpio_level_t 。
330  * @endif
331  */
332 gpio_level_t hal_gpio_get_inval(pin_t pin);
333 
334 /**
335  * @if Eng
336  * @brief  HAL GPIO register a callback for the PIN.
337  * @param  [in]  pin PIN to use. see @ref pin_t.
338  * @param  [in]  trigger Trigger under which the callback will be invoked.
339  * @param  [in]  callback Pointer to callback. see @ref gpio_callback_t.
340  * @param  [in]  need_callback Whether callback is needed.
341  * @retval ERRCODE_SUCC   Success.
342  * @retval Other        Failure. For details, see @ref errcode_t.
343  * @else
344  * @brief  HAL层GPIO的注册中断接口。
345  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
346  * @param  [in]  trigger 触发回调的触发器。
347  * @param  [in]  callback 指向回调的指针,参考 @ref gpio_callback_t 。
348  * @param  [in]  need_callback 是否需要回调 。
349  * @retval ERRCODE_SUCC 成功。
350  * @retval Other        失败,参考 @ref errcode_t 。
351  * @endif
352  */
353 errcode_t hal_gpio_register(pin_t pin, uint32_t trigger, gpio_callback_t callback, bool need_callback);
354 
355 /**
356  * @if Eng
357  * @brief  HAL GPIO unregister a callback for the PIN.
358  * @param  [in]  pin PIN to use. see @ref pin_t.
359  * @retval ERRCODE_SUCC   Success.
360  * @retval Other        Failure. For details, see @ref errcode_t.
361  * @else
362  * @brief  HAL层GPIO的注册中断接口。
363  * @param  [in]  pin io引脚, 参考 @ref pin_t 。
364  * @retval ERRCODE_SUCC 成功。
365  * @retval Other        失败,参考 @ref errcode_t 。
366  * @endif
367  */
368 errcode_t hal_gpio_unregister(pin_t pin);
369 
370 /**
371  * @if Eng
372  * @brief  Get the base address of specified GPIO registers.
373  * @else
374  * @brief  获取指定Channel GPIO寄存器的基地址。
375  * @endif
376  */
377 uintptr_t hal_gpio_base_addrs_get(uint32_t i);
378 
379 /**
380  * @if Eng
381  * @brief  Init the GPIO which will set the base address of registers.
382  * @else
383  * @brief  初始化GPIO,设置寄存器的基地址。
384  * @endif
385  */
386 void hal_gpio_regs_init(void);
387 
388 /**
389  * @}
390  */
391 #ifdef __cplusplus
392 #if __cplusplus
393     }
394 #endif /* __cplusplus */
395 #endif /* __cplusplus */
396 
397 #endif