RocketLogger 2.1.1
gpiod_test.c
Go to the documentation of this file.
1#include <errno.h>
2#include <stdbool.h>
3#include <stdio.h>
4#include <stdlib.h>
5#include <string.h>
6
7#include <gpiod.h>
8#include <unistd.h>
9
10#include "gpio.hpp"
11
13#define GPIO_FHR1 30
15#define GPIO_FHR2 60
17#define GPIO_LED_STATUS 45
19#define GPIO_LED_ERROR 44
21#define GPIO_BUTTON 26
23#define GPIO_POWER 31
24
26static struct gpiod_chip *gpio_chip0 = nullptr;
27
29static struct gpiod_chip *gpio_led_error_chip = nullptr;
30
32static struct gpiod_chip *gpio_led_status_chip = nullptr;
33
35static struct gpiod_line *gpio_power = nullptr;
36
38static struct gpiod_line *gpio_led_error = nullptr;
39
41static struct gpiod_line *gpio_led_status = nullptr;
42
43int main(void) {
44 int ret = 0;
45
46 gpio_chip0 = gpiod_chip_open_by_number(0);
47 if (gpio_chip0 == nullptr) {
48 fprintf(stderr, "Failed opening GPIO controller 0. %d message: %s\n",
49 errno, strerror(errno));
50 exit(EXIT_FAILURE);
51 }
52
53 gpio_led_error_chip = gpiod_chip_open_by_number(1);
54 if (gpio_led_error_chip == nullptr) {
55 fprintf(stderr, "Failed opening GPIO controller 1. %d message: %s\n",
56 errno, strerror(errno));
57 exit(EXIT_FAILURE);
58 }
59 gpio_led_status_chip = gpiod_chip_open_by_number(GPIO_LED_STATUS / 32);
60 if (gpio_led_status_chip == nullptr) {
61 fprintf(stderr, "Failed opening GPIO controller 1. %d message: %s\n",
62 errno, strerror(errno));
63 exit(EXIT_FAILURE);
64 }
65
66 gpio_power = gpiod_chip_get_line(gpio_chip0, GPIO_POWER);
67 if (gpio_power == nullptr) {
68 fprintf(stderr, "Failed opening power GPIO line. %d message: %s\n",
69 errno, strerror(errno));
70 exit(EXIT_FAILURE);
71 }
72
73 gpio_led_error =
74 gpiod_chip_get_line(gpio_led_error_chip, GPIO_LED_ERROR % 32);
75 if (gpio_led_error == nullptr) {
76 fprintf(stderr, "Failed opening error LED GPIO line. %d message: %s\n",
77 errno, strerror(errno));
78 exit(EXIT_FAILURE);
79 }
80
81 gpio_led_status =
82 gpiod_chip_get_line(gpio_led_status_chip, GPIO_LED_STATUS % 32);
83 if (gpio_led_status == nullptr) {
84 fprintf(stderr, "Failed opening status LED GPIO line. %d message: %s\n",
85 errno, strerror(errno));
86 exit(EXIT_FAILURE);
87 }
88
89 // get GPIO line info
90 const char *gpio_power_name = gpiod_line_name(gpio_power);
91 bool gpio_power_used = gpiod_line_is_used(gpio_power);
92 printf("Power GPIO name: %s\t in use: %d\n", gpio_power_name,
93 gpio_power_used);
94
95 const char *gpio_led_error_name = gpiod_line_name(gpio_led_error);
96 bool gpio_led_error_used = gpiod_line_is_used(gpio_led_error);
97 printf("Error LED GPIO name: %s\t in use: %d\n", gpio_led_error_name,
98 gpio_led_error_used);
99
100 const char *gpio_led_status_name = gpiod_line_name(gpio_led_status);
101 bool gpio_led_status_used = gpiod_line_is_used(gpio_led_status);
102 printf("Error LED GPIO name: %s\t in use: %d\n", gpio_led_status_name,
103 gpio_led_status_used);
104
105 // request and configure power GPIO line
106 ret = gpiod_line_request_output(gpio_power, "gpio_pwr", 0);
107 if (ret < 0) {
108 fprintf(stderr, "Failed configuring power GPIO line. %d message: %s\n",
109 errno, strerror(errno));
110 exit(EXIT_FAILURE);
111 }
112 ret = gpiod_line_request_output(gpio_led_error, "gpio_led_error", 0);
113 if (ret < 0) {
114 fprintf(stderr,
115 "Failed configuring error LED GPIO line. %d message: %s\n",
116 errno, strerror(errno));
117 exit(EXIT_FAILURE);
118 }
119 ret = gpiod_line_request_output(gpio_led_status, "gpio_led_status", 0);
120 if (ret < 0) {
121 fprintf(stderr,
122 "Failed configuring status LED GPIO line. %d message: %s\n",
123 errno, strerror(errno));
124 exit(EXIT_FAILURE);
125 }
126
127 // get GPIO line info
128 const char *gpio_power_name1 = gpiod_line_name(gpio_power);
129 bool gpio_power_used1 = gpiod_line_is_used(gpio_power);
130 printf("Power GPIO name: %s\t in use: %d\n", gpio_power_name1,
131 gpio_power_used1);
132
133 const char *gpio_led_error_name1 = gpiod_line_name(gpio_led_error);
134 bool gpio_led_error_used1 = gpiod_line_is_used(gpio_led_error);
135 printf("Error LED GPIO name: %s\t in use: %d\n", gpio_led_error_name1,
136 gpio_led_error_used1);
137
138 const char *gpio_led_status_name1 = gpiod_line_name(gpio_led_status);
139 bool gpio_led_status_used1 = gpiod_line_is_used(gpio_led_status);
140 printf("Error LED GPIO name: %s\t in use: %d\n", gpio_led_status_name1,
141 gpio_led_status_used1);
142
143 // power up for 5 seconds
144 printf("power up RocketLogger Cape\n");
145 ret = gpiod_line_set_value(gpio_power, 1);
146 if (ret < 0) {
147 fprintf(stderr, "Failed setting power GPIO line. %d message: %s\n",
148 errno, strerror(errno));
149 exit(EXIT_FAILURE);
150 }
151 ret = gpiod_line_set_value(gpio_led_error, 1);
152 if (ret < 0) {
153 fprintf(stderr, "Failed setting LED error GPIO line. %d message: %s\n",
154 errno, strerror(errno));
155 exit(EXIT_FAILURE);
156 }
157 ret = gpiod_line_set_value(gpio_led_status, 1);
158 if (ret < 0) {
159 fprintf(stderr, "Failed setting LED status GPIO line. %d message: %s\n",
160 errno, strerror(errno));
161 exit(EXIT_FAILURE);
162 }
163
164 sleep(5);
165
166 printf("power down RocketLogger Cape\n");
167 ret = gpiod_line_set_value(gpio_power, 0);
168 if (ret < 0) {
169 fprintf(stderr, "Failed setting power GPIO line. %d message: %s\n",
170 errno, strerror(errno));
171 exit(EXIT_FAILURE);
172 }
173 ret = gpiod_line_set_value(gpio_led_error, 0);
174 if (ret < 0) {
175 fprintf(stderr, "Failed setting LED error GPIO line. %d message: %s\n",
176 errno, strerror(errno));
177 exit(EXIT_FAILURE);
178 }
179 ret = gpiod_line_set_value(gpio_led_status, 0);
180 if (ret < 0) {
181 fprintf(stderr, "Failed setting LED status GPIO line. %d message: %s\n",
182 errno, strerror(errno));
183 exit(EXIT_FAILURE);
184 }
185
186 // release resources
187 gpiod_line_release(gpio_led_status);
188 gpiod_line_release(gpio_led_error);
189 gpiod_line_release(gpio_power);
190
191 gpiod_line_update(gpio_led_status);
192 gpiod_line_update(gpio_led_error);
193 gpiod_line_update(gpio_power);
194
195 // get GPIO line info
196 const char *gpio_power_name2 = gpiod_line_name(gpio_power);
197 bool gpio_power_used2 = gpiod_line_is_used(gpio_power);
198 printf("Power GPIO name: %s\t in use: %d\n", gpio_power_name2,
199 gpio_power_used2);
200
201 const char *gpio_led_error_name2 = gpiod_line_name(gpio_led_error);
202 bool gpio_led_error_used2 = gpiod_line_is_used(gpio_led_error);
203 printf("Error LED GPIO name: %s\t in use: %d\n", gpio_led_error_name2,
204 gpio_led_error_used2);
205
206 const char *gpio_led_status_name2 = gpiod_line_name(gpio_led_status);
207 bool gpio_led_status_used2 = gpiod_line_is_used(gpio_led_status);
208 printf("Error LED GPIO name: %s\t in use: %d\n", gpio_led_status_name2,
209 gpio_led_status_used2);
210
211 gpiod_line_close_chip(gpio_led_status);
212 gpiod_line_close_chip(gpio_led_error);
213 gpiod_line_close_chip(gpio_power);
214
215 // abstracted GPIO handling
216 // //////////////////////////////////////////////////////////////////////
217 printf("Wait and test GPIO API\n");
218
219 sleep(2);
220
221 ret = gpio_init();
222 if (ret < 0) {
223 fprintf(stderr, "Failed setting power GPIO line. %d message: %s\n",
224 errno, strerror(errno));
225 exit(EXIT_FAILURE);
226 }
227
228 // setup GPIO line
229 gpio_power = gpio_setup(GPIO_POWER, GPIO_MODE_OUT, "gpio_pwr");
230 if (gpio_power == nullptr) {
231 fprintf(stderr, "Failed configuring power GPIO line. %d message: %s\n",
232 errno, strerror(errno));
233 exit(EXIT_FAILURE);
234 }
235 gpio_led_error =
236 gpio_setup(GPIO_LED_ERROR, GPIO_MODE_OUT, "gpio_led_error");
237 if (gpio_led_error == nullptr) {
238 fprintf(stderr,
239 "Failed configuring error LED GPIO line. %d message: %s\n",
240 errno, strerror(errno));
241 exit(EXIT_FAILURE);
242 }
243 gpio_led_status =
244 gpio_setup(GPIO_LED_STATUS, GPIO_MODE_OUT, "gpio_led_status");
245 if (gpio_led_status == nullptr) {
246 fprintf(stderr,
247 "Failed configuring status LED GPIO line. %d message: %s\n",
248 errno, strerror(errno));
249 exit(EXIT_FAILURE);
250 }
251
252 // power up for 5 seconds
253 printf("power up RocketLogger Cape\n");
254 ret = gpio_set_value(gpio_power, 1);
255 if (ret < 0) {
256 fprintf(stderr, "Failed setting power GPIO line. %d message: %s\n",
257 errno, strerror(errno));
258 exit(EXIT_FAILURE);
259 }
260 ret = gpio_set_value(gpio_led_error, 1);
261 if (ret < 0) {
262 fprintf(stderr, "Failed setting LED error GPIO line. %d message: %s\n",
263 errno, strerror(errno));
264 exit(EXIT_FAILURE);
265 }
266 ret = gpio_set_value(gpio_led_status, 1);
267 if (ret < 0) {
268 fprintf(stderr, "Failed setting LED status GPIO line. %d message: %s\n",
269 errno, strerror(errno));
270 exit(EXIT_FAILURE);
271 }
272
275 if (gpio_button == nullptr) {
276 fprintf(stderr,
277 "Failed setting up interrupt for button GPIO line. %d message: "
278 "%s\n",
279 errno, strerror(errno));
280 exit(EXIT_FAILURE);
281 }
282
283 struct timespec timeout = {
284 .tv_sec = 3,
285 .tv_nsec = 0,
286 };
287
288 printf("wait for button press with timeout of %ld.%09ld sec\n",
289 timeout.tv_sec, timeout.tv_nsec);
290
291 do {
292 ret = gpio_wait_interrupt(gpio_button, &timeout);
293 if (ret < 0) {
294 fprintf(
295 stderr,
296 "Failed waiting for button GPIO interrupt. %d message: %s\n",
297 errno, strerror(errno));
298 exit(EXIT_FAILURE);
299 }
300 printf("button interrupt triggered with level %d\n", ret);
301 } while (ret != 1);
302
303 printf("power down RocketLogger Cape\n");
304 ret = gpio_set_value(gpio_power, 0);
305 if (ret < 0) {
306 fprintf(stderr, "Failed setting power GPIO line. %d message: %s\n",
307 errno, strerror(errno));
308 exit(EXIT_FAILURE);
309 }
310 ret = gpio_set_value(gpio_led_error, 0);
311 if (ret < 0) {
312 fprintf(stderr, "Failed setting LED error GPIO line. %d message: %s\n",
313 errno, strerror(errno));
314 exit(EXIT_FAILURE);
315 }
316 ret = gpio_set_value(gpio_led_status, 0);
317 if (ret < 0) {
318 fprintf(stderr, "Failed setting LED status GPIO line. %d message: %s\n",
319 errno, strerror(errno));
320 exit(EXIT_FAILURE);
321 }
322
324 gpio_release(gpio_power);
325 gpio_release(gpio_led_error);
326 gpio_release(gpio_led_status);
327
328 gpio_deinit();
329
330 return 0;
331}
int gpio_set_value(gpio_t *gpio, int value)
Definition gpio.c:135
int gpio_init()
Definition gpio.c:77
void gpio_release(gpio_t *gpio)
Definition gpio.c:130
void gpio_deinit()
Definition gpio.c:92
int gpio_wait_interrupt(gpio_t *gpio, const struct timespec *timeout)
Definition gpio.c:187
gpio_t * gpio_setup_interrupt(int gpio_number, gpio_interrupt_t edge, const char *name)
Definition gpio.c:148
gpio_t * gpio_setup(int gpio_number, gpio_mode_t mode, const char *name)
Definition gpio.c:103
@ GPIO_INTERRUPT_BOTH
Interrupt on both edges.
Definition gpio.h:66
@ GPIO_MODE_OUT
GPIO write mode.
Definition gpio.h:56
struct gpiod_line gpio_t
Definition gpio.h:72
#define GPIO_POWER
Linux sysfs GPIO number of RocketLogger cape power enable.
Definition gpiod_test.c:23
int main(void)
Definition gpiod_test.c:43
#define GPIO_LED_STATUS
Linux sysfs GPIO number of status LED.
Definition gpiod_test.c:17
#define GPIO_LED_ERROR
Linux sysfs GPIO number of error LED.
Definition gpiod_test.c:19
#define GPIO_BUTTON
Linux sysfs GPIO number of start/stop button.
Definition gpiod_test.c:21
gpio_t * gpio_button
GPIO handle for user button.