Robot Control Library
gpio.h
Go to the documentation of this file.
1 /**
2  * <rc/gpio.h>
3  *
4  * @brief C interface for the Linux GPIO driver
5  *
6  * Developed and tested on the BeagleBone Black but should work fine on any
7  * Linux system with the new character-device gpio driver in kernel 4.8 and
8  * newer
9  *
10  * @author James Strawson
11  * @date 1/19/2018
12  *
13  * @addtogroup GPIO
14  * @ingroup IO
15  * @{
16  */
17 
18 #ifndef RC_GPIO_H
19 #define RC_GPIO_H
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include <stdint.h>
26 
27 #ifndef _GPIO_H_
28 #define GPIOHANDLE_REQUEST_INPUT (1UL << 0)
29 #define GPIOHANDLE_REQUEST_OUTPUT (1UL << 1)
30 #define GPIOHANDLE_REQUEST_ACTIVE_LOW (1UL << 2)
31 #define GPIOHANDLE_REQUEST_OPEN_DRAIN (1UL << 3)
32 #define GPIOHANDLE_REQUEST_OPEN_SOURCE (1UL << 4)
33 #endif
34 
35 
36 /**
37  * @brief Configures a gpio pin as input or output
38  *
39  * This configures the pin by making a gpio handle request to the character
40  * device driver. It accepts the same gpio handle request flags as defined in
41  * <linux/gpio.h>
42  *
43  * - GPIOHANDLE_REQUEST_INPUT
44  * - GPIOHANDLE_REQUEST_OUTPUT
45  * - GPIOHANDLE_REQUEST_ACTIVE_LOW
46  * - GPIOHANDLE_REQUEST_OPEN_DRAIN
47  * - GPIOHANDLE_REQUEST_OPEN_SOURCE
48  *
49  * Obviously the INPUT and OUTPUT flags cannot be used at the same time. If you
50  * don't know what the other flags mean just stick with INPUT and OUTPUT modes,
51  * that covers 99% of use cases.
52  *
53  * @param[in] chip The chip number, /dev/gpiochipX
54  * @param[in] pin The pin ID
55  * @param[in] handle_flags The handle flags
56  *
57  * @return 0 on success or -1 on failure.
58  */
59 int rc_gpio_init(int chip, int pin, int handle_flags);
60 
61 
62 /**
63  * @brief Sets the value of a GPIO pin when in output mode
64  *
65  * must call rc_gpio_init with the OUTPUT flag first.
66  *
67  * @param[in] chip The chip number, /dev/gpiochipX
68  * @param[in] pin The pin ID
69  * @param[in] value 0 for off (inactive), nonzero for on (active)
70  *
71  * @return 0 on success or -1 on failure
72  */
73 int rc_gpio_set_value(int chip, int pin, int value);
74 
75 
76 /**
77  * @brief Reads the value of a GPIO pin when in input mode or output mode.
78  *
79  * Must call rc_gpio_init first.
80  *
81  * @param[in] chip The chip number, /dev/gpiochipX
82  * @param[in] pin The pin ID
83  *
84  * @return 1 if pin is high, 0 if pin is low, -1 on error
85  */
86 int rc_gpio_get_value(int chip, int pin);
87 
88 
89 /** possible edge request **/
90 #ifndef _GPIO_H_
91 #define GPIOEVENT_REQUEST_RISING_EDGE (1UL << 0)
92 #define GPIOEVENT_REQUEST_FALLING_EDGE (1UL << 1)
93 #define GPIOEVENT_REQUEST_BOTH_EDGES ((1UL << 0) | (1UL << 1))
94 #endif
95 
96 /**
97  * @brief Initializes a pin for interrupt event polling and normal reading.
98  *
99  * Handle flags exists if the user wishes to configure the pic as active-low,
100  * open-source, or open-drain. This is usually not necessary and can be left at
101  * 0. This function returns the file descriptor used for polling in case the
102  * user wants to use a polling method other than rc_gpio_poll.
103  *
104  * @param[in] chip The chip number, /dev/gpiochipX
105  * @param[in] pin The pin ID
106  * @param[in] handle_flags Additional pin configuration flags, this can
107  * usually be left as 0
108  * @param[in] event_flags The event flags, GPIOEVENT_REQUEST_RISING_EDGE,
109  * GPIOEVENT_REQUEST_FALLING_EDGE, or GPIOEVENT_REQUEST_BOTH_EDGES
110  *
111  * @return File descriptor for the GPIO event or -1 on failure
112  */
113 int rc_gpio_init_event(int chip, int pin, int handle_flags, int event_flags);
114 
115 /** possible return values for rc_gpio_poll **/
116 #define RC_GPIOEVENT_ERROR -1
117 #define RC_GPIOEVENT_TIMEOUT 0
118 #define RC_GPIOEVENT_RISING_EDGE 1
119 #define RC_GPIOEVENT_FALLING_EDGE 2
120 
121 /**
122  * @brief polls a pin when configured for interrupt event polling
123  *
124  * This polls for an event and then reads one event from the queue.
125  *
126  * @param[in] chip The chip number, /dev/gpiochipX
127  * @param[in] pin The pin ID
128  * @param[in] timeout_ms The timeout in milliseconds. Negative value causes
129  * infinite timeout, a value of 0 makes the function return immediately after
130  * reading an event in the queue.
131  * @param[out] event_time_ns pointer where the time of the gpio event occured.
132  * Units are nanoseconds since epoch. Set this as NULL if you don't want to keep
133  * the time.
134  *
135  * @return returns RC_GPIO_EVENT_ERROR, RC_GPIO_EVENT_TIMEOUT,
136  * RC_GPIO_EVENT_RISING_EDGE, or RC_GPIO_EVENT_FALLING_EDGE to indicate what
137  * happened.
138  */
139 int rc_gpio_poll(int chip, int pin, int timeout_ms, uint64_t* event_time_ns);
140 
141 
142 /**
143  * @brief closes the file descriptor for a pin
144  *
145  * Not strictly necessary to run at the end of your program since linux will
146  * clean this up for you. However this is sometimes useful in the middle of a
147  * program when a pin is no longer needed.
148  *
149  * @param[in] chip The chip number, /dev/gpiochipX
150  * @param[in] pin The pin ID
151  */
152 void rc_gpio_cleanup(int chip, int pin);
153 
154 
155 
156 
157 #ifdef __cplusplus
158 }
159 #endif
160 
161 #endif // RC_GPIO_H
162 
163 ///@} end group GPIO
int rc_gpio_init(int chip, int pin, int handle_flags)
Configures a gpio pin as input or output.
int rc_gpio_init_event(int chip, int pin, int handle_flags, int event_flags)
Initializes a pin for interrupt event polling and normal reading.
int rc_gpio_set_value(int chip, int pin, int value)
Sets the value of a GPIO pin when in output mode.
void rc_gpio_cleanup(int chip, int pin)
closes the file descriptor for a pin
int rc_gpio_get_value(int chip, int pin)
Reads the value of a GPIO pin when in input mode or output mode.
int rc_gpio_poll(int chip, int pin, int timeout_ms, uint64_t *event_time_ns)
polls a pin when configured for interrupt event polling