]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/freedom-metal/metal/gpio.h
Update RISCC-V-RV32-SiFive_HiFive1_FreedomStudio project to latest tools and metal...
[freertos] / FreeRTOS / Demo / RISC-V_RV32_SiFive_HiFive1_FreedomStudio / freedom-metal / metal / gpio.h
1 /* Copyright 2019 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
3
4 #ifndef METAL__GPIO_H
5 #define METAL__GPIO_H
6
7 #include <metal/compiler.h>
8 #include <metal/interrupt.h>
9
10 /*!
11  * @file gpio.h
12  * @brief API for manipulating general-purpose input/output
13  */
14
15 struct metal_gpio;
16
17 struct __metal_gpio_vtable {
18     int (*disable_input)(struct metal_gpio *, long pins);
19     int (*enable_input)(struct metal_gpio *, long pins);
20     long (*input)(struct metal_gpio *);
21     long (*output)(struct metal_gpio *);
22     int (*disable_output)(struct metal_gpio *, long pins);
23     int (*enable_output)(struct metal_gpio *, long pins);
24     int (*output_set)(struct metal_gpio *, long value);
25     int (*output_clear)(struct metal_gpio *, long value);
26     int (*output_toggle)(struct metal_gpio *, long value);
27     int (*enable_io)(struct metal_gpio *, long pins, long dest);
28     int (*disable_io)(struct metal_gpio *, long pins);
29     int (*config_int)(struct metal_gpio *, long pins, int intr_type);
30     int (*clear_int)(struct metal_gpio *, long pins, int intr_type);
31     struct metal_interrupt* (*interrupt_controller)(struct metal_gpio *gpio);
32     int (*get_interrupt_id)(struct metal_gpio *gpio, int pin);
33 };
34
35 #define METAL_GPIO_INT_DISABLE       0
36 #define METAL_GPIO_INT_RISING        1
37 #define METAL_GPIO_INT_FALLING       2
38 #define METAL_GPIO_INT_BOTH_EDGE     3
39 #define METAL_GPIO_INT_LOW           4
40 #define METAL_GPIO_INT_HIGH          5
41 #define METAL_GPIO_INT_BOTH_LEVEL    6
42 #define METAL_GPIO_INT_MAX           7
43
44 /*!
45  * @struct metal_gpio
46  * @brief The handle for a GPIO interface
47  */
48 struct metal_gpio {
49         const struct __metal_gpio_vtable *vtable;
50 };
51
52 /*!
53  * @brief Get a GPIO device handle
54  * @param device_num The GPIO device index
55  * @return The GPIO device handle, or NULL if there is no device at that index
56  */
57 struct metal_gpio *metal_gpio_get_device(unsigned int device_num);
58
59 /*!
60  * @brief enable input on a pin
61  * @param gpio The handle for the GPIO interface
62  * @param pin The pin number indexed from 0
63  * @return 0 if the input is successfully enabled
64  */
65 __inline__ int metal_gpio_enable_input(struct metal_gpio *gpio, int pin) {
66     if(!gpio) {
67         return 1;
68     }
69
70     return gpio->vtable->enable_input(gpio, (1 << pin));
71 }
72
73 /*!
74  * @brief Disable input on a pin
75  * @param gpio The handle for the GPIO interface
76  * @param pin The pin number indexed from 0
77  * @return 0 if the input is successfully disabled
78  */
79 __inline__ int metal_gpio_disable_input(struct metal_gpio *gpio, int pin) {
80     if(!gpio) {
81         return 1;
82     }
83
84     return gpio->vtable->disable_input(gpio, (1 << pin));
85 }
86
87 /*!
88  * @brief Enable output on a pin
89  * @param gpio The handle for the GPIO interface
90  * @param pin The pin number indexed from 0
91  * @return 0 if the output is successfully enabled
92  */
93 __inline__ int metal_gpio_enable_output(struct metal_gpio *gpio, int pin) {
94     if(!gpio) {
95         return 1;
96     }
97
98     return gpio->vtable->enable_output(gpio, (1 << pin));
99 }
100
101 /*!
102  * @brief Disable output on a pin
103  * @param gpio The handle for the GPIO interface
104  * @param pin The pin number indexed from 0
105  * @return 0 if the output is successfully disabled
106  */
107 __inline__ int metal_gpio_disable_output(struct metal_gpio *gpio, int pin) {
108     if(!gpio) {
109         return 1;
110     }
111
112     return gpio->vtable->disable_output(gpio, (1 << pin));
113 }
114
115 /*!
116  * @brief Set the output value of a GPIO pin
117  * @param gpio The handle for the GPIO interface
118  * @param pin The pin number indexed from 0
119  * @param value The value to set the pin to
120  * @return 0 if the output is successfully set
121  */
122 __inline__ int metal_gpio_set_pin(struct metal_gpio *gpio, int pin, int value) {
123     if(!gpio) {
124         return 1;
125     }
126
127     if(value == 0) {
128         return gpio->vtable->output_clear(gpio, (1 << pin));
129     } else {
130         return gpio->vtable->output_set(gpio, (1 << pin));
131     }
132 }
133
134 /*!
135  * @brief Get the value of the GPIO pin
136  * @param gpio The handle for the GPIO interface
137  * @param pin The pin number indexed from 0
138  * @return The value of the GPIO pin
139  */
140 __inline__ int metal_gpio_get_input_pin(struct metal_gpio *gpio, int pin) {
141     if(!gpio) {
142         return 0;
143     }
144
145     long value = gpio->vtable->input(gpio);
146
147     if(value & (1 << pin)) {
148             return 1;
149     } else {
150             return 0;
151     }
152 }
153
154 /*!
155  * @brief Get the value of the GPIO pin
156  * @param gpio The handle for the GPIO interface
157  * @param pin The pin number indexed from 0
158  * @return The value of the GPIO pin
159  */
160 __inline__ int metal_gpio_get_output_pin(struct metal_gpio *gpio, int pin) {
161     if(!gpio) {
162         return 0;
163     }
164
165     long value = gpio->vtable->output(gpio);
166
167     if(value & (1 << pin)) {
168         return 1;
169     } else {
170         return 0;
171     }
172 }
173
174 /*!
175  * @brief Clears the value of the GPIO pin
176  * @param gpio The handle for the GPIO interface
177  * @param pin The pin number indexed from 0
178  * @return 0 if the pin is successfully cleared
179  */
180 __inline__ int metal_gpio_clear_pin(struct metal_gpio *gpio, int pin) {
181     if(!gpio) {
182         return 1;
183     }
184
185     return gpio->vtable->output_clear(gpio, (1 << pin));
186 }
187
188 /*!
189  * @brief Toggles the value of the GPIO pin
190  * @param gpio The handle for the GPIO interface
191  * @param pin The pin number indexed from 0
192  * @return 0 if the pin is successfully toggled
193  */
194 __inline__ int metal_gpio_toggle_pin(struct metal_gpio *gpio, int pin) {
195     if(!gpio) {
196         return 1;
197     }
198
199     return gpio->vtable->output_toggle(gpio, (1 << pin));
200 }
201
202 /*!
203  * @brief Enables and sets the pinmux for a GPIO pin
204  * @param gpio The handle for the GPIO interface
205  * @param pin The bitmask for the pin to enable pinmux on
206  * @param io_function The IO function to set
207  * @return 0 if the pinmux is successfully set
208  */
209 __inline__ int metal_gpio_enable_pinmux(struct metal_gpio *gpio, int pin, int io_function) {
210     if(!gpio) {
211         return 1;
212     }
213
214     return gpio->vtable->enable_io(gpio, (1 << pin), (io_function << pin));
215 }
216
217 /*!
218  * @brief Disables the pinmux for a GPIO pin
219  * @param gpio The handle for the GPIO interface
220  * @param pin The bitmask for the pin to disable pinmux on
221  * @return 0 if the pinmux is successfully set
222  */
223 __inline__ int metal_gpio_disable_pinmux(struct metal_gpio *gpio, int pin) {
224     if(!gpio) {
225         return 1;
226     }
227
228     return gpio->vtable->disable_io(gpio, (1 << pin));
229 }
230
231 /*!
232  * @brief Config gpio interrupt type
233  * @param gpio The handle for the GPIO interface
234  * @param pin The bitmask for the pin to enable gpio interrupt
235  * @param intr_type The interrupt type
236  * @return 0 if the interrupt mode is setup properly
237  */
238 __inline__ int metal_gpio_config_interrupt(struct metal_gpio *gpio, int pin, int intr_type) {
239     if(!gpio) {
240         return 1;
241     }
242
243     return gpio->vtable->config_int(gpio, (1 << pin), intr_type);
244 }
245
246 /*!
247  * @brief Clear gpio interrupt status
248  * @param gpio The handle for the GPIO interface
249  * @param pin The bitmask for the pin to clear gpio interrupt
250  * @param intr_type The interrupt type to be clear
251  * @return 0 if the interrupt is cleared
252  */
253 __inline__ int metal_gpio_clear_interrupt(struct metal_gpio *gpio, int pin, int intr_type) {
254     if(!gpio) {
255         return 1;
256     }
257
258     return gpio->vtable->clear_int(gpio, (1 << pin), intr_type);
259 }
260
261 /*!
262  * @brief Get the interrupt controller for a gpio
263  *
264  * @param gpio The handle for the gpio
265  * @return A pointer to the interrupt controller responsible for handling
266  * gpio interrupts.
267  */
268 __inline__ struct metal_interrupt*
269     metal_gpio_interrupt_controller(struct metal_gpio *gpio) {
270     return gpio->vtable->interrupt_controller(gpio);
271 }
272
273 /*!
274  * @brief Get the interrupt id for a gpio
275  *
276  * @param gpio The handle for the gpio
277  * @param pin The bitmask for the pin to get gpio interrupt id
278  * @return The interrupt id corresponding to a gpio.
279  */
280 __inline__ int metal_gpio_get_interrupt_id(struct metal_gpio *gpio, int pin) {
281     return gpio->vtable->get_interrupt_id(gpio, pin);
282 }
283
284 #endif