1 /*
2  * Copyright (C) 2017-2020 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     8. Apr 2020
10  * @model    gpio
11  ******************************************************************************/
12 
13 #ifndef _DRV_GPIO_H_
14 #define _DRV_GPIO_H_
15 
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include <drv/common.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /**
25  * \enum     csi_gpio_dir_t
26  * \brief    GPIO dir define
27  */
28 typedef enum {
29     GPIO_DIRECTION_INPUT       = 0,    ///< GPIO as input
30     GPIO_DIRECTION_OUTPUT,             ///< GPIO as output
31 } csi_gpio_dir_t;
32 
33 /**
34  * \enum     csi_gpio_pin_state_t
35  * \brief    GPIO pin state define
36  */
37 typedef enum {
38     GPIO_PIN_LOW                = 0,   ///< GPIO low level
39     GPIO_PIN_HIGH,                     ///< GPIO high level
40 } csi_gpio_pin_state_t;
41 
42 /**
43  * \enum     csi_gpio_mode_t
44  * \brief    GPIO mode define
45  */
46 typedef enum {
47     GPIO_MODE_PULLNONE         = 0,    ///< Pull none for input
48     GPIO_MODE_PULLUP,                  ///< Pull up for input
49     GPIO_MODE_PULLDOWN,                ///< Pull down for input
50     GPIO_MODE_OPEN_DRAIN,              ///< Open drain mode for output
51     GPIO_MODE_PUSH_PULL,               ///< Push-pull mode for output
52 } csi_gpio_mode_t;
53 
54 /**
55  * \enum     csi_gpio_irq_mode_t
56  * \brief    GPIO irq triger type
57  */
58 typedef enum {
59     GPIO_IRQ_MODE_RISING_EDGE  = 0,    ///< Interrupt mode for rising edge
60     GPIO_IRQ_MODE_FALLING_EDGE,        ///< Interrupt mode for falling edge
61     GPIO_IRQ_MODE_BOTH_EDGE,           ///< Interrupt mode for both edge
62     GPIO_IRQ_MODE_LOW_LEVEL,           ///< Interrupt mode for low level
63     GPIO_IRQ_MODE_HIGH_LEVEL,          ///< Interrupt mode for high level
64 } csi_gpio_irq_mode_t;
65 
66 /**
67  * \struct   csi_gpio_t
68  * \brief    GPIO control block
69  */
70 
71 typedef struct csi_gpio csi_gpio_t;
72 struct csi_gpio {
73     csi_dev_t           dev;           ///< Hw-dev info
74     void (*callback)(csi_gpio_t *gpio, uint32_t pins, void *arg); ///<   Call-back of gpio port
75     void                *arg;          ///< User param passed to callback
76     void                *priv;         ///< User private param
77 };
78 
79 /**
80   \brief       Initialize GPIO Port handle
81   \param[in]   gpio        GPIO port handle
82   \param[in]   port_idx    GPIO port index
83   \return      Error code
84 */
85 csi_error_t csi_gpio_init(csi_gpio_t *gpio, uint32_t port_idx);
86 
87 /**
88   \brief       De-initialize GPIO pin.stops operation
89                releases the software resources used by the gpio-pin
90   \param[in]   gpio    GPIO port handle
91   \return      None
92 */
93 void csi_gpio_uninit(csi_gpio_t *gpio);
94 
95 /**
96   \brief       Config pin direction
97   \param[in]   gpio        GPIO port handle
98   \param[in]   pin_mask    Pin mask need to be set
99   \param[in]   dir         \ref csi_gpio_dir_t
100   \return      Error code
101 */
102 csi_error_t csi_gpio_dir(csi_gpio_t *gpio, uint32_t pin_mask, csi_gpio_dir_t dir);
103 
104 /**
105   \brief       Config pin mode
106                If one of pins config error, then the rest of pins will not config, and function return CSI_ERROR
107                If one or more pin unsupport, function will return CSI_UNSUPPORT, but the other pin still configured
108   \param[in]   gpio        GPIO port handle
109   \param[in]   pin_mask    Pin mask need to be set
110   \param[in]   mode        \ref csi_gpio_mode_t
111   \return      Error code
112 */
113 csi_error_t csi_gpio_mode(csi_gpio_t *gpio, uint32_t pin_mask, csi_gpio_mode_t mode);
114 
115 /**
116   \brief       Config gpio irq params
117   \param[in]   gpio        GPIO port handle
118   \param[in]   pin_mask    Pin mask need to be set
119   \param[in]   mode        Interrupt trigger mode \ref csi_gpio_irq_mode_t
120   \return      Error code
121 */
122 csi_error_t csi_gpio_irq_mode(csi_gpio_t *gpio, uint32_t pin_mask, csi_gpio_irq_mode_t mode);
123 
124 /**
125   \brief       Enable or disable gpio pin interrupt
126   \param[in]   gpio        GPIO port handle
127   \param[in]   pin_mask    Pin mask need to be set
128   \param[in]   enable      0:disable  1:enable
129   \return      Error code
130 */
131 csi_error_t csi_gpio_irq_enable(csi_gpio_t *gpio, uint32_t pin_mask, bool enable);
132 
133 /**
134   \brief       Set debonce of gpio when gpio configed as input
135   \param[in]   gpio        GPIO port handle
136   \param[in]   pin_mask    Pin mask need to be set
137   \param[in]   enbale      0: disable   1:enable
138   \return      Error code
139 */
140 csi_error_t csi_gpio_debonce(csi_gpio_t *gpio, uint32_t pin_mask, bool enable);
141 /**
142   \brief       Set one or zero to the selected pin mask
143   \param[in]   gpio        GPIO port handle
144   \param[in]   pin_mask    Pin mask need to be set
145   \param[in]   value       Value to be set \ref csi_gpio_pin_state_t
146   \return      None
147 */
148 void  csi_gpio_write(csi_gpio_t *gpio, uint32_t pin_mask, csi_gpio_pin_state_t value);
149 
150 /**
151   \brief       Toggle output gpio value,ex.if previous value is 1, then output 0
152   \param[in]   gpio        GPIO port handle
153   \param[in]   pin_mask    Pin mask need to be set
154   \return      None
155 */
156 void csi_gpio_toggle(csi_gpio_t *gpio, uint32_t pin_mask);
157 
158 /**
159   \brief       Get the value of  selected GPIO pin mask
160   \param[in]   gpio        GPIO port handle
161   \param[in]   pin_mask    Pin mask need to be set
162   \return      According to the bit mask, the corresponding pin status is obtained
163 */
164 uint32_t csi_gpio_read(csi_gpio_t *gpio, uint32_t pin_mask);
165 
166 /**
167   \brief       Attach the interrupt callback to the port
168   \param[in]   gpio        GPIO port handle
169   \param[in]   callback    Callback function
170   \param[in]   arg         User param passed to callback
171   \return      Error code
172 */
173 csi_error_t  csi_gpio_attach_callback(csi_gpio_t *gpio, void *callback, void *arg);
174 
175 /**
176   \brief       Detach the interrupt callback to the port
177   \param[in]   gpio    GPIO port handle
178   \return      None
179 */
180 void         csi_gpio_detach_callback(csi_gpio_t *gpio);
181 
182 /**
183   \brief       Enable gpio power manage
184   \param[in]   gpio    GPIO handle to operate
185   \return      Error code
186 */
187 csi_error_t csi_gpio_enable_pm(csi_gpio_t *gpio);
188 
189 /**
190   \brief       Disable gpio power manage
191   \param[in]   gpio    GPIO handle to operate
192   \return      None
193 */
194 void csi_gpio_disable_pm(csi_gpio_t *gpio);
195 
196 #ifdef __cplusplus
197 }
198 #endif
199 
200 #endif /* _DRV_GPIO_H_ */
201