]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1_FreedomStudio/bsp/install/include/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 / bsp / install / include / 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
9 /*!
10  * @file gpio.h
11  * @brief API for manipulating general-purpose input/output
12  */
13
14 struct metal_gpio;
15
16 struct __metal_gpio_vtable {
17     int (*disable_input)(struct metal_gpio *, long pins);
18     long (*output)(struct metal_gpio *);
19     int (*enable_output)(struct metal_gpio *, long pins);
20     int (*output_set)(struct metal_gpio *, long value);
21     int (*output_clear)(struct metal_gpio *, long value);
22     int (*output_toggle)(struct metal_gpio *, long value);
23     int (*enable_io)(struct metal_gpio *, long pins, long dest);
24 };
25
26 /*!
27  * @struct metal_gpio
28  * @brief The handle for a GPIO interface
29  */
30 struct metal_gpio {
31         const struct __metal_gpio_vtable *vtable;
32 };
33
34 /*!
35  * @brief Get a GPIO device handle
36  * @param device_num The GPIO device index
37  * @return The GPIO device handle, or NULL if there is no device at that index
38  */
39 struct metal_gpio *metal_gpio_get_device(int device_num);
40
41 /*!
42  * @brief Disable input on a pin
43  * @param gpio The handle for the GPIO interface
44  * @param pin The pin number indexed from 0
45  * @return 0 if the input is successfully disabled
46  */
47 inline int metal_gpio_disable_input(struct metal_gpio *gpio, int pin) {
48     if(!gpio) {
49         return 1;
50     }
51
52     return gpio->vtable->disable_input(gpio, (1 << pin));
53 }
54
55 /*!
56  * @brief Enable output on a pin
57  * @param gpio The handle for the GPIO interface
58  * @param pin The pin number indexed from 0
59  * @return 0 if the output is successfully enabled
60  */
61 inline int metal_gpio_enable_output(struct metal_gpio *gpio, int pin) {
62     if(!gpio) {
63         return 1;
64     }
65
66     return gpio->vtable->enable_output(gpio, (1 << pin));
67 }
68
69 /*!
70  * @brief Set the output value of a GPIO pin
71  * @param gpio The handle for the GPIO interface
72  * @param pin The pin number indexed from 0
73  * @param value The value to set the pin to
74  * @return 0 if the output is successfully set
75  */
76 inline int metal_gpio_set_pin(struct metal_gpio *gpio, int pin, int value) {
77     if(!gpio) {
78         return 1;
79     }
80
81     if(value == 0) {
82         return gpio->vtable->output_clear(gpio, (1 << pin));
83     } else {
84         return gpio->vtable->output_set(gpio, (1 << pin));
85     }
86 }
87
88 /*!
89  * @brief Get the value of the GPIO pin
90  * @param gpio The handle for the GPIO interface
91  * @param pin The pin number indexed from 0
92  * @return The value of the GPIO pin
93  */
94 inline int metal_gpio_get_pin(struct metal_gpio *gpio, int pin) {
95     if(!gpio) {
96         return 0;
97     }
98
99     long value = gpio->vtable->output(gpio);
100
101     if(value & (1 << pin)) {
102         return 1;
103     } else {
104         return 0;
105     }
106 }
107
108 /*!
109  * @brief Clears the value of the GPIO pin
110  * @param gpio The handle for the GPIO interface
111  * @param pin The pin number indexed from 0
112  * @return 0 if the pin is successfully cleared
113  */
114 inline int metal_gpio_clear_pin(struct metal_gpio *gpio, int pin) {
115     if(!gpio) {
116         return 1;
117     }
118
119     return gpio->vtable->output_clear(gpio, (1 << pin));
120 }
121
122 /*!
123  * @brief Toggles the value of the GPIO pin
124  * @param gpio The handle for the GPIO interface
125  * @param pin The pin number indexed from 0
126  * @return 0 if the pin is successfully toggled
127  */
128 inline int metal_gpio_toggle_pin(struct metal_gpio *gpio, int pin) {
129     if(!gpio) {
130         return 1;
131     }
132
133     return gpio->vtable->output_toggle(gpio, (1 << pin));
134 }
135
136 /*!
137  * @brief Enables and sets the pinmux for a GPIO pin
138  * @param gpio The handle for the GPIO interface
139  * @param pin The bitmask for the pin to enable pinmux on
140  * @param io_function The IO function to set
141  * @return 0 if the pinmux is successfully set
142  */
143 inline int metal_gpio_enable_pinmux(struct metal_gpio *gpio, int pin, int io_function) {
144     if(!gpio) {
145         return 1;
146     }
147
148     return gpio->vtable->enable_io(gpio, (1 << pin), (io_function << pin));
149 }
150
151 #endif