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