1 /**
2  * @file gpio.h
3  * @copyright Copyright (C) 2015-2018 Alibaba Group Holding Limited
4  */
5 
6 #ifndef HAL_GPIO_H
7 #define 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 typedef enum {
22     ANALOG_MODE,
23     /* 触发中断 */
24     IRQ_MODE,
25     /* 输入上拉 */
26     INPUT_PULL_UP,
27     /* 输入下拉 */
28     INPUT_PULL_DOWN,
29     /* 输入浮空 */
30     INPUT_HIGH_IMPEDANCE,
31     /* 推挽输出 */
32     OUTPUT_PUSH_PULL,
33     /* 开漏输出无上拉 */
34     OUTPUT_OPEN_DRAIN_NO_PULL,
35     /* 开漏输出有上拉 */
36     OUTPUT_OPEN_DRAIN_PULL_UP,
37     /* 开漏输出替代模式 */
38     OUTPUT_OPEN_DRAIN_AF,
39     /* 推挽输出替代模式 */
40     OUTPUT_PUSH_PULL_AF,
41 } gpio_config_t;
42 
43 /*
44  * GPIO dev struct
45  */
46 typedef struct {
47     uint8_t        port;   /**< gpio port */
48     gpio_config_t  config; /**< gpio config */
49     void          *priv;   /**< priv data */
50 } gpio_dev_t;
51 
52 typedef enum {
53     /* 上升沿触发 */
54     IRQ_TRIGGER_RISING_EDGE     = 1,
55     /* 下降沿触发 */
56     IRQ_TRIGGER_FALLING_EDGE,
57     /* 上升下降沿触发 */
58     IRQ_TRIGGER_BOTH_EDGES,
59 } gpio_irq_trigger_t;
60 
61 
62 typedef enum {
63     GPIO_INPUT     = 0x0000U, /**< Input Floating Mode */
64     GPIO_OUTPUT_PP = 0x0001U, /**< Output Push Pull Mode */
65     GPIO_OUTPUT_OD = 0x0011U, /**< Output Open Drain Mode */
66 } hal_gpio_mode_t;
67 
68 typedef enum {
69     GPIO_PinState_Reset = 0,                    /**< Pin state 0 */
70     GPIO_PinState_Set   = !GPIO_PinState_Reset, /**< Pin state 1 */
71 } gpio_pinstate_t;
72 
73 /*
74  * GPIO interrupt callback handler
75  */
76 typedef void (*gpio_irq_handler_t)(void *arg);
77 
78 /**
79  * Initialises a GPIO pin
80  *
81  * @note  Prepares a GPIO pin for use.
82  *
83  * @param[in]  gpio           the gpio pin which should be initialised
84  *
85  * @return  0 : on success,  otherwise is error
86  */
87 int32_t hal_gpio_init(gpio_dev_t *gpio);
88 
89 /**
90  * Sets an output GPIO pin high
91  *
92  * @note  Using this function on a gpio pin which is set to input mode is undefined.
93  *
94  * @param[in]  gpio  the gpio pin which should be set high
95  *
96  * @return  0 : on success,  otherwise is error
97  */
98 int32_t hal_gpio_output_high(gpio_dev_t *gpio);
99 
100 /**
101  * Sets an output GPIO pin low
102  *
103  * @note  Using this function on a gpio pin which is set to input mode is undefined.
104  *
105  * @param[in]  gpio  the gpio pin which should be set low
106  *
107  * @return  0 : on success,  otherwise is error
108  */
109 int32_t hal_gpio_output_low(gpio_dev_t *gpio);
110 
111 /**
112  * Trigger an output GPIO pin's output. Using this function on a
113  * gpio pin which is set to input mode is undefined.
114  *
115  * @param[in]  gpio  the gpio pin which should be toggled
116  *
117  * @return  0 : on success,  otherwise is error
118  */
119 int32_t hal_gpio_output_toggle(gpio_dev_t *gpio);
120 
121 /**
122  * Get the state of an input GPIO pin. Using this function on a
123  * gpio pin which is set to output mode will return an undefined value.
124  *
125  * @param[in]  gpio   the gpio pin which should be read
126  * @param[out] value  gpio value
127  *
128  * @return  0 : on success,  otherwise is error
129  */
130 int32_t hal_gpio_input_get(gpio_dev_t *gpio, uint32_t *value);
131 
132 /**
133  * Enables an interrupt trigger for an input GPIO pin.
134  * Using this function on a gpio pin which is set to
135  * output mode is undefined.
136  *
137  * @param[in]  gpio     the gpio pin which will provide the interrupt trigger
138  * @param[in]  trigger  the type of trigger (rising/falling edge or both)
139  * @param[in]  handler  a function pointer to the interrupt handler
140  * @param[in]  arg      an argument that will be passed to the interrupt handler
141  *
142  * @return  0 : on success,  otherwise is error
143  */
144 int32_t hal_gpio_enable_irq(gpio_dev_t *gpio, gpio_irq_trigger_t trigger,
145                             gpio_irq_handler_t handler, void *arg);
146 
147 /**
148  * Disables an interrupt trigger for an input GPIO pin.
149  * Using this function on a gpio pin which has not been setted up using
150  * @ref hal_gpio_input_irq_enable is undefined.
151  *
152  * @param[in]  gpio  the gpio pin which provided the interrupt trigger
153  *
154  * @return  0 : on success,  otherwise is error
155  */
156 int32_t hal_gpio_disable_irq(gpio_dev_t *gpio);
157 
158 /**
159  * Clear an interrupt status for an input GPIO pin.
160  * Using this function on a gpio pin which has generated a interrupt.
161  *
162  * @param[in]  gpio  the gpio pin which provided the interrupt trigger
163  *
164  * @return  0 : on success,  otherwise is error
165  */
166 int32_t hal_gpio_clear_irq(gpio_dev_t *gpio);
167 
168 /**
169  * Set a GPIO pin in default state.
170  *
171  * @param[in]  gpio  the gpio pin which should be deinitialised
172  *
173  * @return  0 : on success,  otherwise is error
174  */
175 int32_t hal_gpio_finalize(gpio_dev_t *gpio);
176 
177 /** @} */
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 #endif /* HAL_GPIO_H */
184 
185