1 /* Copyright 2019 SiFive, Inc */
2 /* SPDX-License-Identifier: Apache-2.0 */
7 #include <metal/compiler.h>
11 * @brief API for manipulating general-purpose input/output
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);
28 * @brief The handle for a GPIO interface
31 const struct __metal_gpio_vtable *vtable;
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
39 struct metal_gpio *metal_gpio_get_device(int device_num);
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
47 inline int metal_gpio_disable_input(struct metal_gpio *gpio, int pin) {
52 return gpio->vtable->disable_input(gpio, (1 << pin));
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
61 inline int metal_gpio_enable_output(struct metal_gpio *gpio, int pin) {
66 return gpio->vtable->enable_output(gpio, (1 << pin));
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
76 inline int metal_gpio_set_pin(struct metal_gpio *gpio, int pin, int value) {
82 return gpio->vtable->output_clear(gpio, (1 << pin));
84 return gpio->vtable->output_set(gpio, (1 << pin));
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
94 inline int metal_gpio_get_pin(struct metal_gpio *gpio, int pin) {
99 long value = gpio->vtable->output(gpio);
101 if(value & (1 << pin)) {
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
114 inline int metal_gpio_clear_pin(struct metal_gpio *gpio, int pin) {
119 return gpio->vtable->output_clear(gpio, (1 << pin));
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
128 inline int metal_gpio_toggle_pin(struct metal_gpio *gpio, int pin) {
133 return gpio->vtable->output_toggle(gpio, (1 << pin));
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
143 inline int metal_gpio_enable_pinmux(struct metal_gpio *gpio, int pin, int io_function) {
148 return gpio->vtable->enable_io(gpio, (1 << pin), (io_function << pin));