1 /* 2 * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 /****************************************************************************** 17 * @file drv_gpio.h 18 * @brief header file for gpio driver 19 * @version V1.0 20 * @date 02. June 2017 21 ******************************************************************************/ 22 23 #ifndef _CSI_GPIO_H_ 24 #define _CSI_GPIO_H_ 25 26 #ifdef __cplusplus 27 extern "C" { 28 #endif 29 30 #include <stdint.h> 31 #include <stdbool.h> 32 #include <drv_common.h> 33 34 /// definition for gpio pin handle. 35 typedef void *gpio_pin_handle_t; 36 37 /****** GPIO specific error codes *****/ 38 typedef enum { 39 GPIO_ERROR_MODE = (DRV_ERROR_SPECIFIC + 1), ///< Specified Mode not supported 40 GPIO_ERROR_DIRECTION, ///< Specified direction not supported 41 GPIO_ERROR_IRQ_MODE, ///< Specified irq mode not supported 42 } gpio_error_e; 43 44 /*----- GPIO Control Codes: Mode -----*/ 45 typedef enum { 46 GPIO_MODE_PULLNONE = 0, ///< pull none for input 47 GPIO_MODE_PULLUP, ///< pull up for input 48 GPIO_MODE_PULLDOWN, ///< pull down for input 49 GPIO_MODE_OPEN_DRAIN, ///< open drain mode for output 50 GPIO_MODE_PUSH_PULL, ///< push-pull mode for output 51 } gpio_mode_e; 52 53 /*----- GPIO Control Codes: Mode Parameters: Data Bits -----*/ 54 typedef enum { 55 GPIO_DIRECTION_INPUT = 0, ///< gpio as input 56 GPIO_DIRECTION_OUTPUT, ///< gpio as output 57 } gpio_direction_e; 58 59 /*----- GPIO Control Codes: Mode Parameters: Parity -----*/ 60 typedef enum { 61 GPIO_IRQ_MODE_RISING_EDGE = 0, ///< interrupt mode for rising edge 62 GPIO_IRQ_MODE_FALLING_EDGE, ///< interrupt mode for falling edge 63 GPIO_IRQ_MODE_DOUBLE_EDGE, ///< interrupt mode for double edge 64 GPIO_IRQ_MODE_LOW_LEVEL, ///< interrupt mode for low level 65 GPIO_IRQ_MODE_HIGH_LEVEL, ///< interrupt mode for high level 66 } gpio_irq_mode_e; 67 68 typedef void (*gpio_event_cb_t)(int32_t idx); ///< gpio Event call back. 69 70 /** 71 \brief Initialize GPIO handle. 72 \param[in] gpio_pin gpio pin idx. 73 \param[in] cb_event event callback function \ref gpio_event_cb_t 74 \return gpio_pin_handle 75 */ 76 gpio_pin_handle_t csi_gpio_pin_initialize(int32_t gpio_pin, gpio_event_cb_t cb_event); 77 78 /** 79 \brief De-initialize GPIO pin handle.stops operation and releases the software resources used by the handle. 80 \param[in] handle gpio pin handle to operate. 81 \return error code 82 */ 83 int32_t csi_gpio_pin_uninitialize(gpio_pin_handle_t handle); 84 85 /** 86 \brief control gpio power. 87 \param[in] handle gpio handle to operate. 88 \param[in] state power state.\ref csi_power_stat_e. 89 \return error code 90 */ 91 int32_t csi_gpio_power_control(gpio_pin_handle_t handle, csi_power_stat_e state); 92 93 /** 94 \brief config pin mode 95 \param[in] pin gpio pin handle to operate. 96 \param[in] mode \ref gpio_mode_e 97 \return error code 98 */ 99 int32_t csi_gpio_pin_config_mode(gpio_pin_handle_t handle, 100 gpio_mode_e mode); 101 102 /** 103 \brief config pin direction 104 \param[in] pin gpio pin handle to operate. 105 \param[in] dir \ref gpio_direction_e 106 \return error code 107 */ 108 int32_t csi_gpio_pin_config_direction(gpio_pin_handle_t handle, 109 gpio_direction_e dir); 110 111 /** 112 \brief config pin 113 \param[in] pin gpio pin handle to operate. 114 \param[in] mode \ref gpio_mode_e 115 \param[in] dir \ref gpio_direction_e 116 \return error code 117 */ 118 int32_t csi_gpio_pin_config(gpio_pin_handle_t handle, 119 gpio_mode_e mode, 120 gpio_direction_e dir); 121 122 /** 123 \brief Set one or zero to the selected GPIO pin. 124 \param[in] pin gpio pin handle to operate. 125 \param[in] value value to be set 126 \return error code 127 */ 128 int32_t csi_gpio_pin_write(gpio_pin_handle_t handle, bool value); 129 130 /** 131 \brief Get the value of selected GPIO pin. 132 \param[in] pin gpio pin handle to operate. 133 \param[out] value buffer to store the pin value 134 \return error code 135 */ 136 int32_t csi_gpio_pin_read(gpio_pin_handle_t handle, bool *value); 137 138 /** 139 \brief set GPIO interrupt mode. 140 \param[in] pin gpio pin handle to operate. 141 \param[in] mode irq mode to be set 142 \param[in] enable enable flag 143 \return error code 144 */ 145 int32_t csi_gpio_pin_set_irq(gpio_pin_handle_t handle, gpio_irq_mode_e mode, bool enable); 146 147 148 #ifdef __cplusplus 149 } 150 #endif 151 152 #endif /* _CSI_GPIO_H_ */ 153