1 /** 2 * @file gpio.h 3 * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited 4 */ 5 6 #ifndef AOS_HAL_GPIO_H 7 #define AOS_HAL_GPIO_H 8 9 #ifdef __cplusplus 10 extern "C" { 11 #endif 12 13 /** @addtogroup hal_gpio GPIO 14 * gpio hal API. 15 * 16 * @{ 17 */ 18 19 #include <stdint.h> 20 21 #include "aos/gpioc.h" 22 23 /* 24 * Pin configuration 25 */ 26 typedef enum { 27 ANALOG_MODE, /**< Used as a function pin, input and output analog */ 28 IRQ_MODE, /**< Used to trigger interrupt */ 29 INPUT_PULL_UP, /**< Input with an internal pull-up resistor - use with devices 30 that actively drive the signal low - e.g. button connected to ground */ 31 INPUT_PULL_DOWN, /**< Input with an internal pull-down resistor - use with devices 32 that actively drive the signal high - e.g. button connected to a power rail */ 33 INPUT_HIGH_IMPEDANCE, /**< Input - must always be driven, either actively or by an external pullup resistor */ 34 OUTPUT_PUSH_PULL, /**< Output actively driven high and actively driven low - 35 must not be connected to other active outputs - e.g. LED output */ 36 OUTPUT_OPEN_DRAIN_NO_PULL, /**< Output actively driven low but is high-impedance when set high - 37 can be connected to other open-drain/open-collector outputs. 38 Needs an external pull-up resistor */ 39 OUTPUT_OPEN_DRAIN_PULL_UP, /**< Output actively driven low and is pulled high 40 with an internal resistor when set high - 41 can be connected to other open-drain/open-collector outputs. */ 42 OUTPUT_OPEN_DRAIN_AF, /**< Alternate Function Open Drain Mode. */ 43 OUTPUT_PUSH_PULL_AF, /**< Alternate Function Push Pull Mode. */ 44 } gpio_config_t; 45 46 /* 47 * GPIO dev struct 48 */ 49 typedef struct { 50 uint8_t port; /**< gpio port */ 51 aos_gpioc_ref_t *gpioc; /**gpio device fd*/ 52 int32_t gpioc_index; 53 int32_t pin_index; 54 gpio_config_t config; /**< gpio config */ 55 void *priv; /**< priv data */ 56 } gpio_dev_t; 57 58 /* 59 * GPIO interrupt trigger 60 */ 61 typedef enum { 62 IRQ_TRIGGER_RISING_EDGE = 0x1, /**< Interrupt triggered at input signal's rising edge */ 63 IRQ_TRIGGER_FALLING_EDGE = 0x2, /**< Interrupt triggered at input signal's falling edge */ 64 IRQ_TRIGGER_BOTH_EDGES = IRQ_TRIGGER_RISING_EDGE | IRQ_TRIGGER_FALLING_EDGE, 65 IRQ_TRIGGER_LEVEL_HIGH = 0x4, 66 IRQ_TRIGGER_LEVEL_LOW = 0x5, 67 } gpio_irq_trigger_t; 68 69 70 typedef enum { 71 GPIO_INPUT = 0x0000U, /**< Input Floating Mode */ 72 GPIO_OUTPUT_PP = 0x0001U, /**< Output Push Pull Mode */ 73 GPIO_OUTPUT_OD = 0x0011U, /**< Output Open Drain Mode */ 74 } hal_gpio_mode_t; 75 76 typedef enum { 77 GPIO_PinState_Reset = 0, /**< Pin state 0 */ 78 GPIO_PinState_Set = !GPIO_PinState_Reset, /**< Pin state 1 */ 79 } gpio_pinstate_t; 80 81 /* 82 * GPIO interrupt callback handler 83 */ 84 typedef void (*gpio_irq_handler_t)(void *arg); 85 86 /** 87 * Initialises a GPIO pin 88 * 89 * @note Prepares a GPIO pin for use. 90 * 91 * @param[in] gpio the gpio pin which should be initialised 92 * 93 * @return 0 : on success, otherwise is error 94 */ 95 int32_t aos_hal_gpio_init(gpio_dev_t *gpio); 96 97 /** 98 * Sets an output GPIO pin high 99 * 100 * @note Using this function on a gpio pin which is set to input mode is undefined. 101 * 102 * @param[in] gpio the gpio pin which should be set high 103 * 104 * @return 0 : on success, otherwise is error 105 */ 106 int32_t aos_hal_gpio_output_high(gpio_dev_t *gpio); 107 108 /** 109 * Sets an output GPIO pin low 110 * 111 * @note Using this function on a gpio pin which is set to input mode is undefined. 112 * 113 * @param[in] gpio the gpio pin which should be set low 114 * 115 * @return 0 : on success, otherwise is error 116 */ 117 int32_t aos_hal_gpio_output_low(gpio_dev_t *gpio); 118 119 /** 120 * Trigger an output GPIO pin's output. Using this function on a 121 * gpio pin which is set to input mode is undefined. 122 * 123 * @param[in] gpio the gpio pin which should be toggled 124 * 125 * @return 0 : on success, otherwise is error 126 */ 127 int32_t aos_hal_gpio_output_toggle(gpio_dev_t *gpio); 128 129 130 /** 131 * Get the state of an input GPIO pin. Using this function on a 132 * gpio pin which is set to output mode will return an undefined value. 133 * 134 * @param[in] gpio the gpio pin which should be read 135 * 136 * @return result 137 */ 138 int32_t aos_hal_gpio_get(gpio_dev_t *gpio); 139 140 /** 141 * Get the state of an input GPIO pin. Using this function on a 142 * gpio pin which is set to output mode will return an undefined value. 143 * 144 * @param[in] gpio the gpio pin which should be read 145 * @param[out] value gpio value 146 * 147 * @return 0 : on success, otherwise is error 148 */ 149 int32_t aos_hal_gpio_input_get(gpio_dev_t *gpio, uint32_t *value); 150 151 152 /** 153 * Enables an interrupt trigger for an input GPIO pin. 154 * Using this function on a gpio pin which is set to 155 * output mode is undefined. 156 * 157 * @param[in] gpio the gpio pin which will provide the interrupt trigger 158 * @param[in] trigger the type of trigger (rising/falling edge or both) 159 * @param[in] handler a function pointer to the interrupt handler 160 * @param[in] arg an argument that will be passed to the interrupt handler 161 * 162 * @return 0 : on success, otherwise is error 163 */ 164 int32_t aos_hal_gpio_enable_irq(gpio_dev_t *gpio, gpio_irq_trigger_t trigger, 165 aos_gpio_irq_handler_t handler, void *arg); 166 167 /** 168 * Disables an interrupt trigger for an input GPIO pin. 169 * Using this function on a gpio pin which has not been setted up using 170 * @ref hal_gpio_input_irq_enable is undefined. 171 * 172 * @param[in] gpio the gpio pin which provided the interrupt trigger 173 * 174 * @return 0 : on success, otherwise is error 175 */ 176 int32_t aos_hal_gpio_disable_irq(gpio_dev_t *gpio); 177 178 /** 179 * Clear an interrupt status for an input GPIO pin. 180 * Using this function on a gpio pin which has generated a interrupt. 181 * 182 * @param[in] gpio the gpio pin which provided the interrupt trigger 183 * 184 * @return 0 : on success, otherwise is error 185 */ 186 int32_t aos_hal_gpio_clear_irq(gpio_dev_t *gpio); 187 188 /** 189 * Set a GPIO pin in default state. 190 * 191 * @param[in] gpio the gpio pin which should be deinitialised 192 * 193 * @return 0 : on success, otherwise is error 194 */ 195 int32_t aos_hal_gpio_finalize(gpio_dev_t *gpio); 196 197 /** @} */ 198 199 #ifdef __cplusplus 200 } 201 #endif 202 203 #endif /* AOS_HAL_GPIO_H */ 204 205