1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv_gpio.h
7  * @brief    header file for gpio driver
8  * @version  V1.0
9  * @date     02. June 2017
10  * @model    gpio
11  ******************************************************************************/
12 
13 #ifndef _CSI_GPIO_H_
14 #define _CSI_GPIO_H_
15 
16 
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <drv/common.h>
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 /// definition for gpio pin_handle.
25 typedef void *gpio_pin_handle_t;
26 
27 /****** GPIO specific error codes *****/
28 typedef enum {
29     GPIO_ERROR_MODE  = (DRV_ERROR_SPECIFIC + 1),      ///< Specified Mode not supported
30     GPIO_ERROR_DIRECTION,                        ///< Specified direction not supported
31     GPIO_ERROR_IRQ_MODE,                         ///< Specified irq mode not supported
32 } gpio_error_e;
33 
34 /*----- GPIO Control Codes: Mode -----*/
35 typedef enum {
36     GPIO_MODE_PULLNONE         = 0,    ///< pull none for input
37     GPIO_MODE_PULLUP,                  ///< pull up for input
38     GPIO_MODE_PULLDOWN,                ///< pull down for input
39     GPIO_MODE_OPEN_DRAIN,              ///< open drain mode for output
40     GPIO_MODE_PUSH_PULL,               ///< push-pull mode for output
41 } gpio_mode_e;
42 
43 /*----- GPIO Control Codes: Mode Parameters: Data Bits -----*/
44 typedef enum {
45     GPIO_DIRECTION_INPUT             = 0,    ///< gpio as input
46     GPIO_DIRECTION_OUTPUT,                   ///< gpio as output
47 } gpio_direction_e;
48 
49 /*----- GPIO Control Codes: Mode Parameters: Parity -----*/
50 typedef enum {
51     GPIO_IRQ_MODE_RISING_EDGE     = 0,       ///< interrupt mode for rising edge
52     GPIO_IRQ_MODE_FALLING_EDGE,              ///< interrupt mode for falling edge
53     GPIO_IRQ_MODE_DOUBLE_EDGE,               ///< interrupt mode for double edge
54     GPIO_IRQ_MODE_LOW_LEVEL,                 ///< interrupt mode for low level
55     GPIO_IRQ_MODE_HIGH_LEVEL,                ///< interrupt mode for high level
56 } gpio_irq_mode_e;
57 
58 typedef void (*gpio_event_cb_t)(int32_t idx);   ///< gpio Event call back.
59 
60 /**
61   \brief       Initialize GPIO handle.
62   \param[in]   gpio_pin    gpio pin idx.
63   \param[in]   cb_event  event callback function \ref gpio_event_cb_t
64   \return      gpio_pin_handle
65 */
66 gpio_pin_handle_t csi_gpio_pin_initialize(int32_t gpio_pin, gpio_event_cb_t cb_event);
67 
68 /**
69   \brief       De-initialize GPIO pin_handle.stops operation and releases the software resources used by the handle.
70   \param[in]   handle    gpio pin_handle to operate.
71   \return      error code
72 */
73 int32_t csi_gpio_pin_uninitialize(gpio_pin_handle_t handle);
74 
75 /**
76   \brief       control gpio power.
77   \param[in]   handle  gpio handle to operate.
78   \param[in]   state   power state.\ref csi_power_stat_e.
79   \return      error code
80 */
81 int32_t csi_gpio_power_control(gpio_pin_handle_t handle, csi_power_stat_e state);
82 
83 /**
84   \brief       config pin mode
85   \param[in]   pin       gpio pin_handle to operate.
86   \param[in]   mode      \ref gpio_mode_e
87   \return      error code
88 */
89 int32_t csi_gpio_pin_config_mode(gpio_pin_handle_t handle,
90                                  gpio_mode_e mode);
91 
92 /**
93   \brief       config pin direction
94   \param[in]   pin       gpio pin_handle to operate.
95   \param[in]   dir       \ref gpio_direction_e
96   \return      error code
97 */
98 int32_t csi_gpio_pin_config_direction(gpio_pin_handle_t handle,
99                                       gpio_direction_e dir);
100 
101 /**
102   \brief       config pin
103   \param[in]   pin       gpio pin_handle to operate.
104   \param[in]   mode      \ref gpio_mode_e
105   \param[in]   dir       \ref gpio_direction_e
106   \return      error code
107 */
108 int32_t csi_gpio_pin_config(gpio_pin_handle_t handle,
109                             gpio_mode_e mode,
110                             gpio_direction_e dir);
111 
112 /**
113   \brief       Set one or zero to the selected GPIO pin.
114   \param[in]   pin       gpio pin_handle to operate.
115   \param[in]   value     value to be set
116   \return      error code
117 */
118 int32_t csi_gpio_pin_write(gpio_pin_handle_t handle, bool value);
119 
120 /**
121   \brief       Get the value of  selected GPIO pin.
122   \param[in]   pin       gpio pin_handle to operate.
123   \param[out]  value     buffer to store the pin value
124   \return      error code
125 */
126 int32_t csi_gpio_pin_read(gpio_pin_handle_t handle, bool *value);
127 
128 /**
129   \brief       set GPIO interrupt mode.
130   \param[in]   pin       gpio pin_handle to operate.
131   \param[in]   mode      irq mode to be set
132   \param[in]   enable    enable flag
133   \return      error code
134 */
135 int32_t csi_gpio_pin_set_irq(gpio_pin_handle_t handle, gpio_irq_mode_e mode, bool enable);
136 
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #endif /* _CSI_GPIO_H_ */
143