summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/RISC-V_RV32_SiFive_HiFive1-RevB_FreedomStudio/freedom-metal/metal/gpio.h
blob: 7645494ff61c949a1c67353cf3ae69a2c7bda367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* Copyright 2019 SiFive, Inc */
/* SPDX-License-Identifier: Apache-2.0 */

#ifndef METAL__GPIO_H
#define METAL__GPIO_H

#include <metal/compiler.h>
#include <metal/interrupt.h>

/*!
 * @file gpio.h
 * @brief API for manipulating general-purpose input/output
 */

struct metal_gpio;

struct __metal_gpio_vtable {
    int (*disable_input)(struct metal_gpio *, long pins);
    int (*enable_input)(struct metal_gpio *, long pins);
    long (*input)(struct metal_gpio *);
    long (*output)(struct metal_gpio *);
    int (*disable_output)(struct metal_gpio *, long pins);
    int (*enable_output)(struct metal_gpio *, long pins);
    int (*output_set)(struct metal_gpio *, long value);
    int (*output_clear)(struct metal_gpio *, long value);
    int (*output_toggle)(struct metal_gpio *, long value);
    int (*enable_io)(struct metal_gpio *, long pins, long dest);
    int (*disable_io)(struct metal_gpio *, long pins);
    int (*config_int)(struct metal_gpio *, long pins, int intr_type);
    int (*clear_int)(struct metal_gpio *, long pins, int intr_type);
    struct metal_interrupt* (*interrupt_controller)(struct metal_gpio *gpio);
    int (*get_interrupt_id)(struct metal_gpio *gpio, int pin);
};

#define METAL_GPIO_INT_DISABLE       0
#define METAL_GPIO_INT_RISING        1
#define METAL_GPIO_INT_FALLING       2
#define METAL_GPIO_INT_BOTH_EDGE     3
#define METAL_GPIO_INT_LOW           4
#define METAL_GPIO_INT_HIGH          5
#define METAL_GPIO_INT_BOTH_LEVEL    6
#define METAL_GPIO_INT_MAX           7

/*!
 * @struct metal_gpio
 * @brief The handle for a GPIO interface
 */
struct metal_gpio {
	const struct __metal_gpio_vtable *vtable;
};

/*!
 * @brief Get a GPIO device handle
 * @param device_num The GPIO device index
 * @return The GPIO device handle, or NULL if there is no device at that index
 */
struct metal_gpio *metal_gpio_get_device(unsigned int device_num);

/*!
 * @brief enable input on a pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return 0 if the input is successfully enabled
 */
__inline__ int metal_gpio_enable_input(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->enable_input(gpio, (1 << pin));
}

/*!
 * @brief Disable input on a pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return 0 if the input is successfully disabled
 */
__inline__ int metal_gpio_disable_input(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->disable_input(gpio, (1 << pin));
}

/*!
 * @brief Enable output on a pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return 0 if the output is successfully enabled
 */
__inline__ int metal_gpio_enable_output(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->enable_output(gpio, (1 << pin));
}

/*!
 * @brief Disable output on a pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return 0 if the output is successfully disabled
 */
__inline__ int metal_gpio_disable_output(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->disable_output(gpio, (1 << pin));
}

/*!
 * @brief Set the output value of a GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @param value The value to set the pin to
 * @return 0 if the output is successfully set
 */
__inline__ int metal_gpio_set_pin(struct metal_gpio *gpio, int pin, int value) {
    if(!gpio) {
	return 1;
    }

    if(value == 0) {
	return gpio->vtable->output_clear(gpio, (1 << pin));
    } else {
	return gpio->vtable->output_set(gpio, (1 << pin));
    }
}

/*!
 * @brief Get the value of the GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return The value of the GPIO pin
 */
__inline__ int metal_gpio_get_input_pin(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 0;
    }

    long value = gpio->vtable->input(gpio);

    if(value & (1 << pin)) {
	    return 1;
    } else {
	    return 0;
    }
}

/*!
 * @brief Get the value of the GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return The value of the GPIO pin
 */
__inline__ int metal_gpio_get_output_pin(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 0;
    }

    long value = gpio->vtable->output(gpio);

    if(value & (1 << pin)) {
	return 1;
    } else {
	return 0;
    }
}

/*!
 * @brief Clears the value of the GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return 0 if the pin is successfully cleared
 */
__inline__ int metal_gpio_clear_pin(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->output_clear(gpio, (1 << pin));
}

/*!
 * @brief Toggles the value of the GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The pin number indexed from 0
 * @return 0 if the pin is successfully toggled
 */
__inline__ int metal_gpio_toggle_pin(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->output_toggle(gpio, (1 << pin));
}

/*!
 * @brief Enables and sets the pinmux for a GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The bitmask for the pin to enable pinmux on
 * @param io_function The IO function to set
 * @return 0 if the pinmux is successfully set
 */
__inline__ int metal_gpio_enable_pinmux(struct metal_gpio *gpio, int pin, int io_function) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->enable_io(gpio, (1 << pin), (io_function << pin));
}

/*!
 * @brief Disables the pinmux for a GPIO pin
 * @param gpio The handle for the GPIO interface
 * @param pin The bitmask for the pin to disable pinmux on
 * @return 0 if the pinmux is successfully set
 */
__inline__ int metal_gpio_disable_pinmux(struct metal_gpio *gpio, int pin) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->disable_io(gpio, (1 << pin));
}

/*!
 * @brief Config gpio interrupt type
 * @param gpio The handle for the GPIO interface
 * @param pin The bitmask for the pin to enable gpio interrupt
 * @param intr_type The interrupt type
 * @return 0 if the interrupt mode is setup properly
 */
__inline__ int metal_gpio_config_interrupt(struct metal_gpio *gpio, int pin, int intr_type) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->config_int(gpio, (1 << pin), intr_type);
}

/*!
 * @brief Clear gpio interrupt status
 * @param gpio The handle for the GPIO interface
 * @param pin The bitmask for the pin to clear gpio interrupt
 * @param intr_type The interrupt type to be clear
 * @return 0 if the interrupt is cleared
 */
__inline__ int metal_gpio_clear_interrupt(struct metal_gpio *gpio, int pin, int intr_type) {
    if(!gpio) {
	return 1;
    }

    return gpio->vtable->clear_int(gpio, (1 << pin), intr_type);
}

/*!
 * @brief Get the interrupt controller for a gpio
 *
 * @param gpio The handle for the gpio
 * @return A pointer to the interrupt controller responsible for handling
 * gpio interrupts.
 */
__inline__ struct metal_interrupt*
    metal_gpio_interrupt_controller(struct metal_gpio *gpio) {
    return gpio->vtable->interrupt_controller(gpio);
}

/*!
 * @brief Get the interrupt id for a gpio
 *
 * @param gpio The handle for the gpio
 * @param pin The bitmask for the pin to get gpio interrupt id
 * @return The interrupt id corresponding to a gpio.
 */
__inline__ int metal_gpio_get_interrupt_id(struct metal_gpio *gpio, int pin) {
    return gpio->vtable->get_interrupt_id(gpio, pin);
}

#endif