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