Repository navigation
Expand file tree
/
Copy pathgpio-test.c
More file actions
363 lines (292 loc) · 11 KB
/
Copy pathgpio-test.c
File metadata and controls
363 lines (292 loc) · 11 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//***************************************************************************
// GPIO Wrapper Test
// File gpio-test.c
//
// Usage:
// gpio-test --detect Enumerate all gpiochips + line names
// gpio-test --list
// gpio-test --input <physPin> [--loops <n>] [--interval <ms>]
// gpio-test --output <physPin> [--loops <n>] [--interval <ms>]
// gpio-test --toggle <physPin> [--loops <n>] [--interval <ms>]
// gpio-test --interrupt <physPin>
// gpio-test --config <path> (default: configs)
//
// Examples:
// gpio-test --detect
// gpio-test --list
// gpio-test --input 11 --loops 10 --interval 500
// gpio-test --output 7 --loops 6 --interval 200
// gpio-test --interrupt 27
//***************************************************************************
#include <cstdio>
#include <cstring>
#include <csignal>
#include <chrono>
#include <thread>
#include <atomic>
#include <map>
#include "lib/common.h"
#include "gpio.h"
//***************************************************************************
// Signal handling
//***************************************************************************
static std::atomic<bool> interrupted {false};
static void onSignal(int) { interrupted = true; }
static void onGpioEdge(int physPin, bool value)
{
printf(" [ISR] phys=%-3d value=%d\n", physPin, value ? 1 : 0);
}
//***************************************************************************
// Helpers
//***************************************************************************
static void printUsage(const char* prog)
{
printf(
"Usage:\n"
" %s --detect Enumerate all gpiochips and line names\n"
" %s --list List all pins from gpio.json\n"
" %s --input <phys> [--loops n] [--interval ms]\n"
" %s --output <phys> [--loops n] [--interval ms]\n"
" %s --toggle <phys> [--loops n] [--interval ms]\n"
" %s --interrupt <phys> Wait for edge (Ctrl+C to stop)\n"
" %s --config <path> Config path (default: configs)\n",
prog, prog, prog, prog, prog, prog, prog);
}
//***************************************************************************
// Detect: enumerate all gpiochips and line names
//***************************************************************************
#if defined(GPIOD)
static int testDetect()
{
bool anyChip {false};
for (int i = 0; i < 16; ++i)
{
char path[32];
snprintf(path, sizeof(path), "/dev/gpiochip%d", i);
gpiod_chip* chip {gpiod_chip_open(path)};
if (!chip)
continue;
anyChip = true;
gpiod_chip_info* cinfo {gpiod_chip_get_info(chip)};
const char* cname {cinfo ? gpiod_chip_info_get_name(cinfo) : "?"};
unsigned int nlines = cinfo ? gpiod_chip_info_get_num_lines(cinfo) : 0;
printf("%-18s name=%-20s lines=%u\n", path, cname ? cname : "?", nlines);
printf(" %5s %-30s %s\n", "Offs", "Line name", "Status");
printf(" %.60s\n", "------------------------------------------------------------");
for (unsigned int j = 0; j < nlines; ++j)
{
gpiod_line_info* linfo {gpiod_chip_get_line_info(chip, j)};
if (!linfo)
continue;
const char* name {gpiod_line_info_get_name(linfo)};
bool used {gpiod_line_info_is_used(linfo)};
const char* consumer {gpiod_line_info_get_consumer(linfo)};
if (name && name[0])
{
printf(" %5u %-30s %s%s%s\n",
j, name,
used ? "used" : "free",
(used && consumer && consumer[0]) ? " by " : "",
(used && consumer && consumer[0]) ? consumer : "");
}
gpiod_line_info_free(linfo);
}
if (cinfo)
gpiod_chip_info_free(cinfo);
gpiod_chip_close(chip);
printf("\n");
}
if (!anyChip)
printf("No gpiochip devices found under /dev/gpiochip*\n");
return 0;
}
#endif
//***************************************************************************
// List pins
//***************************************************************************
static int testList(Gpio& gpio)
{
std::map<int, PinInfo> pins;
if (gpio.getPinList(pins) != success)
{
printf("ERROR: getPinList() failed\n");
return 1;
}
printf("%4s %-14s gpio pull int %-5s %s\n", "Phys", "Name", "Volt", "Description");
printf("%.80s\n", "--------------------------------------------------------------------------------");
for (const auto& [phys, info] : pins)
{
printf("%4d %-14s %-4s %-4s %s %-5s %s\n",
phys,
info.name.c_str(),
info.gpio ? "yes" : "no",
info.pull ? "yes" : "no",
info.interrupt ? "yes" : "no",
info.voltage.c_str(),
info.description.c_str());
}
printf("\n%zu pin(s) loaded.\n", pins.size());
return 0;
}
//***************************************************************************
// Input test
//***************************************************************************
static int testInput(Gpio& gpio, int phys, int loops, int intervalMs)
{
printf("Setup phys %d as input (pullUp)...\n", phys);
fflush(stdout);
if (gpio.setupPin(phys, Gpio::dirIn, Gpio::edgeNone, Gpio::pudUp) != success)
{
printf("FAILED\n");
return 1;
}
printf("OK\n");
printf("Reading %d time(s) every %d ms:\n", loops, intervalMs);
for (int i = 0; i < loops && !interrupted; ++i)
{
bool v {gpio.digitalRead(phys)};
printf(" [%2d] phys %d = %d\n", i + 1, phys, v ? 1 : 0);
std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs));
}
return 0;
}
//***************************************************************************
// Output test
//***************************************************************************
static int testOutput(Gpio& gpio, int phys, int loops, int intervalMs)
{
printf("Setup phys %d as output...\n", phys);
fflush(stdout);
if (gpio.setupPin(phys, Gpio::dirOut, Gpio::edgeNone, Gpio::pudOff) != success)
{
printf("FAILED\n");
return 1;
}
printf("OK\n");
printf("Writing alternating 0/1, %d time(s) every %d ms:\n", loops, intervalMs);
for (int i = 0; i < loops && !interrupted; ++i)
{
bool val {(i % 2 == 0)};
int rc {gpio.digitalWrite(phys, val)};
printf(" [%2d] digitalWrite(%d, %d) -> %s\n", i + 1, phys, val ? 1 : 0, rc == success ? "OK" : "FAILED");
std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs));
}
gpio.digitalWrite(phys, false);
printf("Pin set LOW on exit.\n");
return 0;
}
//***************************************************************************
// Toggle test
//***************************************************************************
static int testToggle(Gpio& gpio, int phys, int loops, int intervalMs)
{
printf("Setup phys %d as output for toggle test...\n", phys);
fflush(stdout);
if (gpio.setupPin(phys, Gpio::dirOut, Gpio::edgeNone, Gpio::pudOff) != success)
{
printf("FAILED\n");
return 1;
}
printf("OK\n");
gpio.digitalWrite(phys, false);
printf("Toggling %d time(s) every %d ms:\n", loops, intervalMs);
for (int i = 0; i < loops && !interrupted; ++i)
{
int rc {gpio.digitalToggle(phys)};
bool v {gpio.digitalRead(phys)};
printf(" [%2d] toggle phys %d -> %d %s\n", i + 1, phys, v ? 1 : 0, rc == success ? "OK" : "FAILED");
std::this_thread::sleep_for(std::chrono::milliseconds(intervalMs));
}
gpio.digitalWrite(phys, false);
printf("Pin set LOW on exit.\n");
return 0;
}
//***************************************************************************
// Interrupt test
//***************************************************************************
static int testInterrupt(Gpio& gpio, int phys)
{
printf("Setup phys %d as input with edge=both + pullUp...\n", phys);
fflush(stdout);
if (gpio.setupPin(phys, Gpio::dirIn, Gpio::edgeBoth, Gpio::pudUp) != success)
{
printf("FAILED\n");
return 1;
}
printf("OK\n");
printf("Enabling interrupt...\n");
fflush(stdout);
if (gpio.enableInterrupt(phys, onGpioEdge) != success)
{
printf("FAILED\n");
return 1;
}
printf("OK\n");
printf("Waiting for edges on phys %d (Ctrl+C to stop)...\n", phys);
while (!interrupted)
std::this_thread::sleep_for(std::chrono::milliseconds(100));
printf("\nDisabling interrupt...\n");
gpio.disableInterrupt(phys);
printf("OK\n");
return 0;
}
//***************************************************************************
// Main
//***************************************************************************
int main(int argc, char* argv[])
{
signal(SIGINT, onSignal);
signal(SIGTERM, onSignal);
const char* configPath {"configs"};
const char* mode {nullptr};
int physPin {-1};
int loops {10};
int intervalMs {500};
for (int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "--detect") == 0) { mode = "detect"; }
else if (strcmp(argv[i], "--list") == 0) { mode = "list"; }
else if (strcmp(argv[i], "--input") == 0 && i + 1 < argc) { mode = "input"; physPin = atoi(argv[++i]); }
else if (strcmp(argv[i], "--output") == 0 && i + 1 < argc) { mode = "output"; physPin = atoi(argv[++i]); }
else if (strcmp(argv[i], "--toggle") == 0 && i + 1 < argc) { mode = "toggle"; physPin = atoi(argv[++i]); }
else if (strcmp(argv[i], "--interrupt") == 0 && i + 1 < argc) { mode = "interrupt"; physPin = atoi(argv[++i]); }
else if (strcmp(argv[i], "--config") == 0 && i + 1 < argc) { configPath = argv[++i]; }
else if (strcmp(argv[i], "--loops") == 0 && i + 1 < argc) { loops = atoi(argv[++i]); }
else if (strcmp(argv[i], "--interval") == 0 && i + 1 < argc) { intervalMs = atoi(argv[++i]); }
else
{
printf("Unknown argument: %s\n", argv[i]);
printUsage(argv[0]);
return 1;
}
}
if (!mode)
{
printUsage(argv[0]);
return 1;
}
#if defined(GPIOD)
if (strcmp(mode, "detect") == 0)
return testDetect();
#else
if (strcmp(mode, "detect") == 0)
{
printf("--detect ist nur mit -DGPIOD verfügbar\n");
return 1;
}
#endif
Gpio gpio("gpio-test", configPath);
printf("Initializing GPIO (config: %s)...\n", configPath);
if (gpio.init() != success)
{
printf("ERROR: gpio.init() failed\n");
return 1;
}
printf("Init OK\n\n");
if (strcmp(mode, "list") == 0) return testList(gpio);
else if (strcmp(mode, "input") == 0) return testInput(gpio, physPin, loops, intervalMs);
else if (strcmp(mode, "output") == 0) return testOutput(gpio, physPin, loops, intervalMs);
else if (strcmp(mode, "toggle") == 0) return testToggle(gpio, physPin, loops, intervalMs);
else if (strcmp(mode, "interrupt") == 0) return testInterrupt(gpio, physPin);
return 0;
}