• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 ASR Microelectronics (Shanghai) Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _DUET_GPIO_H_
17 #define _DUET_GPIO_H_
18 #include <stdint.h>
19 #include <errno.h>
20 #define GPIO0_INDEX 0
21 #define GPIO1_INDEX 1
22 #define GPIO2_INDEX 2
23 #define GPIO3_INDEX 3
24 #define GPIO4_INDEX 4
25 #define GPIO5_INDEX 5
26 #define GPIO6_INDEX 6
27 #define GPIO7_INDEX 7
28 #define GPIO8_INDEX 8
29 #define GPIO9_INDEX 9
30 #define GPIO10_INDEX 10
31 #define GPIO11_INDEX 11
32 #define GPIO12_INDEX 12
33 #define GPIO13_INDEX 13
34 #define GPIO14_INDEX 14
35 #define GPIO15_INDEX 15
36 #define GPIO16_INDEX 16
37 #define GPIO17_INDEX 17
38 #define GPIO18_INDEX 18
39 #define GPIO19_INDEX 19
40 #define GPIO20_INDEX 20
41 #define GPIO21_INDEX 21
42 #define GPIO22_INDEX 22
43 #define GPIO23_INDEX 23
44 #define GPIO24_INDEX 24
45 #define GPIO25_INDEX 25
46 #define GPIO26_INDEX 26
47 #define GPIO27_INDEX 27
48 #define GPIO28_INDEX 28
49 #define GPIO29_INDEX 29
50 #define GPIO30_INDEX 30
51 #define GPIO31_INDEX 31
52 #define DUET_GPIO_NUM_PER_GROUP 16
53 
54 #define DUET_GPIO_TOTAL_NUM     24
55 
56 /*
57  * Pin configuration
58  */
59 typedef enum {
60     DUET_ANALOG_MODE,               /* Used as a function pin, input and output analog */
61     DUET_IRQ_MODE,                  /* Used to trigger interrupt */
62     DUET_INPUT_PULL_UP,             /* Input, with an internal pull-up resistor */
63     DUET_INPUT_PULL_DOWN,           /* Input, with an internal pull-down resistor */
64     DUET_INPUT_HIGH_IMPEDANCE,      /* Input, must always be driven, either actively or by an external pullup resistor */
65     DUET_OUTPUT_PUSH_PULL,          /* Output, actively driven high and actively driven low */
66     DUET_OUTPUT_OPEN_DRAIN_NO_PULL, /* Output, actively driven low. When set high, is high-impedance */
67     DUET_OUTPUT_OPEN_DRAIN_PULL_UP, /* Output, actively driven low. When set high, is pulled high with an internal resistor */
68 } duet_gpio_config_t;
69 
70 /*
71  * GPIO dev struct
72  */
73 typedef struct {
74     uint8_t       port;    /* gpio port */
75     duet_gpio_config_t config;  /* gpio config */
76     void         *priv;    /* priv data */
77 } duet_gpio_dev_t;
78 
79 /*
80  * GPIO interrupt trigger
81  */
82 typedef enum {
83     DUET_IRQ_TRIGGER_RISING_EDGE  = 0x1, /* Interrupt triggered at input signal's rising edge  */
84     DUET_IRQ_TRIGGER_FALLING_EDGE = 0x2, /* Interrupt triggered at input signal's falling edge */
85     DUET_IRQ_TRIGGER_BOTH_EDGES   = DUET_IRQ_TRIGGER_RISING_EDGE | DUET_IRQ_TRIGGER_FALLING_EDGE,
86 } duet_gpio_irq_trigger_t;
87 
88 /*
89  * GPIO interrupt callback handler
90  */
91 typedef void (*duet_gpio_irq_handler_t)(void *arg);
92 
93 typedef struct {
94     duet_gpio_irq_handler_t cb;
95     void *arg;
96 } duet_gpio_cb_t;
97 
98 /**
99  * Initialises a GPIO pin
100  *
101  * @note  Prepares a GPIO pin for use.
102  *
103  * @param[in]  gpio           the gpio pin which should be initialised
104  * @param[in]  configuration  A structure containing the required gpio configuration
105  *
106  * @return  0 : on success, EIO : if an error occurred with any step
107  */
108 int32_t duet_gpio_init(duet_gpio_dev_t *gpio);
109 
110 /**
111  * Sets an output GPIO pin high
112  *
113  * @note  Using this function on a gpio pin which is set to input mode is undefined.
114  *
115  * @param[in]  gpio  the gpio pin which should be set high
116  *
117  * @return  0 : on success, EIO : if an error occurred with any step
118  */
119 int32_t duet_gpio_output_high(duet_gpio_dev_t *gpio);
120 
121 /**
122  * Sets an output GPIO pin low
123  *
124  * @note  Using this function on a gpio pin which is set to input mode is undefined.
125  *
126  * @param[in]  gpio  the gpio pin which should be set low
127  *
128  * @return  0 : on success, EIO : if an error occurred with any step
129  */
130 int32_t duet_gpio_output_low(duet_gpio_dev_t *gpio);
131 
132 /**
133  * Trigger an output GPIO pin's output. Using this function on a
134  * gpio pin which is set to input mode is undefined.
135  *
136  * @param[in]  gpio  the gpio pin which should be set low
137  *
138  * @return  0 : on success, EIO : if an error occurred with any step
139  */
140 int32_t duet_gpio_output_toggle(duet_gpio_dev_t *gpio);
141 
142 /**
143  * Get the state of an input GPIO pin. Using this function on a
144  * gpio pin which is set to output mode will return an undefined value.
145  *
146  * @param[in]  gpio   the gpio pin which should be read
147  * @param[in]  value  gpio value
148  *
149  * @return  0 : on success, EIO : if an error occurred with any step
150  */
151 int32_t duet_gpio_input_get(duet_gpio_dev_t *gpio, uint32_t *value);
152 
153 /**
154  * Enables an interrupt trigger for an input GPIO pin.
155  * Using this function on a gpio pin which is set to
156  * output mode is undefined.
157  *
158  * @param[in]  gpio     the gpio pin which will provide the interrupt trigger
159  * @param[in]  trigger  the type of trigger (rising/falling edge)
160  * @param[in]  handler  a function pointer to the interrupt handler
161  * @param[in]  arg      an argument that will be passed to the interrupt handler
162  *
163  * @return  0 : on success, EIO : if an error occurred with any step
164  */
165 int32_t duet_gpio_enable_irq(duet_gpio_dev_t *gpio, duet_gpio_irq_trigger_t trigger,
166                              duet_gpio_irq_handler_t handler, void *arg);
167 
168 /**
169  * Disables an interrupt trigger for an input GPIO pin.
170  * Using this function on a gpio pin which has not been set up
171  * using @ref duet_gpio_input_irq_enable is undefined.
172  *
173  * @param[in]  gpio  the gpio pin which provided the interrupt trigger
174  *
175  * @return  0 : on success, EIO : if an error occurred with any step
176  */
177 int32_t duet_gpio_disable_irq(duet_gpio_dev_t *gpio);
178 
179 /**
180  * Disables an interrupt trigger for an input GPIO pin.
181  * Using this function on a gpio pin which has not been set up
182  * using @ref duet_gpio_input_irq_enable is undefined.
183  *
184  * @param[in]  gpio  the gpio pin which provided the interrupt trigger
185  *
186  * @return  0 : on success, EIO : if an error occurred with any step
187  */
188 int32_t duet_gpio_clear_irq(duet_gpio_dev_t *gpio);
189 
190 /**
191  * Set a GPIO pin in default state.
192  *
193  * @param[in]  gpio  the gpio pin which should be deinitialised
194  *
195  * @return  0 : on success, EIO : if an error occurred with any step
196  */
197 int32_t duet_gpio_finalize(duet_gpio_dev_t *gpio);
198 
199 #endif // _DUET_GPIO_H_
200