-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsd_GPIO.c.old
861 lines (662 loc) · 17.6 KB
/
sd_GPIO.c.old
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include "utils.h"
#include "sd_GPIO.h"
const char *_piModelNames [16] =
{
"Model A", // 0
"Model B", // 1
"Model A+", // 2
"Model B+", // 3
"Pi 2", // 4
"Alpha", // 5
"CM", // 6
"Unknown07", // 07
"Pi 3", // 08
"Pi Zero", // 09
"CM3", // 10
"Unknown11", // 11
"Pi Zero-W", // 12
"Pi 3+", // 13
"Unknown New 14", // 14
"Unknown New 15", // 15
} ;
static bool _ever = false;
void gpioDelay (unsigned int howLong);
#ifndef GPIO_SYSFS_MODE
static volatile uint32_t * _gpioReg = MAP_FAILED;
int piBoardId ()
{
FILE *cpuFd ;
char line [120] ;
char *c ;
unsigned int revision ;
//int bRev, bType, bProc, bMfg, bMem, bWarranty;
int bType;
if ((cpuFd = fopen ("/proc/cpuinfo", "r")) == NULL)
logMessage (LOG_ERR, "piBoardId: Unable to open /proc/cpuinfo") ;
while (fgets (line, 120, cpuFd) != NULL)
if (strncmp (line, "Revision", 8) == 0)
break ;
fclose (cpuFd) ;
if (strncmp (line, "Revision", 8) != 0)
logMessage (LOG_ERR, "piBoardId: No \"Revision\" line") ;
// Chomp trailing CR/NL
for (c = &line [strlen (line) - 1] ; (*c == '\n') || (*c == '\r') ; --c)
*c = 0 ;
logMessage (LOG_DEBUG, "piBoardId: Revision string: %s\n", line) ;
// Scan to the first character of the revision number
for (c = line ; *c ; ++c)
if (*c == ':')
break ;
if (*c != ':')
logMessage (LOG_ERR, "piBoardId: Unknown \"Revision\" line (no colon)") ;
// Chomp spaces
++c ;
while (isspace (*c))
++c ;
if (!isxdigit (*c))
logMessage (LOG_ERR, "piBoardId: Unknown \"Revision\" line (no hex digit at start of revision)") ;
revision = (unsigned int)strtol (c, NULL, 16) ; // Hex number with no leading 0x
// Check for new way:
if ((revision & (1 << 23)) != 0) // New way
{/*
bRev = (revision & (0x0F << 0)) >> 0 ;*/
bType = (revision & (0xFF << 4)) >> 4 ;
/*
bProc = (revision & (0x0F << 12)) >> 12 ; // Not used for now.
bMfg = (revision & (0x0F << 16)) >> 16 ;
bMem = (revision & (0x07 << 20)) >> 20 ;
bWarranty = (revision & (0x03 << 24)) != 0 ;
*/
logMessage (LOG_DEBUG, "piBoard Model: %s\n", _piModelNames[bType]) ;
return bType;
}
logMessage (LOG_ERR, "piBoard Model: UNKNOWN\n");
return PI_MODEL_UNKNOWN;
}
bool gpioSetup() {
int fd;
unsigned int piGPIObase = 0;
switch ( piBoardId() )
{
case PI_MODEL_A:
case PI_MODEL_B:
case PI_MODEL_AP:
case PI_MODEL_BP:
case PI_ALPHA:
case PI_MODEL_CM:
case PI_MODEL_ZERO:
case PI_MODEL_ZERO_W:
//case PI_MODEL_UNKNOWN:
piGPIObase = (GPIO_BASE_P1 + GPIO_OFFSET);
break ;
default:
piGPIObase = (GPIO_BASE_P2 + GPIO_OFFSET);
break ;
}
fd = open("/dev/mem", O_RDWR | O_SYNC);
if (fd<0)
{
logMessage (LOG_ERR, "Failed to open '/dev/mem' for GPIO access (are we root?)\n");
return false;
}
_gpioReg = mmap
(
0,
GPIO_LEN,
PROT_READ|PROT_WRITE|PROT_EXEC,
MAP_SHARED|MAP_LOCKED,
fd,
piGPIObase);
close(fd);
_ever = true;
return true;
}
int pinMode(unsigned gpio, unsigned mode) {
int reg, shift;
if (! validGPIO(gpio))
return -1;
reg = gpio / 10;
shift = (gpio % 10) * 3;
_gpioReg[reg] = (_gpioReg[reg] & ~(7 << shift)) | (mode << shift);
return true;
}
int getPinMode(unsigned gpio) {
int reg, shift;
if (! validGPIO(gpio))
return -1;
reg = gpio / 10;
shift = (gpio % 10) * 3;
return (*(_gpioReg + reg) >> shift) & 7;
}
int digitalRead(unsigned gpio) {
unsigned bank, bit;
if (! validGPIO(gpio))
return -1;
bank = gpio >> 5;
bit = (1 << (gpio & 0x1F));
if ((*(_gpioReg + GPLEV0 + bank) & bit) != 0)
return 1;
else
return 0;
}
int digitalWrite(unsigned gpio, unsigned level) {
unsigned bank, bit;
if (! validGPIO(gpio))
return -1;
bank = gpio >> 5;
bit = (1 << (gpio & 0x1F));
if (level == 0)
*(_gpioReg + GPCLR0 + bank) = bit;
else
*(_gpioReg + GPSET0 + bank) = bit;
return true;
}
int setPullUpDown(unsigned gpio, unsigned pud)
{
unsigned bank, bit;
if (! validGPIO(gpio))
return -1;
bank = gpio >> 5;
bit = (1 << (gpio & 0x1F));
/*
if (gpio > PI_MAX_GPIO)
SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);
*/
if (pud > PUD_UP || pud < PUD_OFF)
return false;
//SOFT_ERROR(PI_BAD_PUD, "gpio %d, bad pud (%d)", gpio, pud);
*(_gpioReg + GPPUD) = pud;
gpioDelay(1);
*(_gpioReg + GPPUDCLK0 + bank) = bit;
gpioDelay(1);
*(_gpioReg + GPPUD) = 0;
*(_gpioReg + GPPUDCLK0 + bank) = 0;
return true;
}
#else
bool gpioSetup() {return true;}
int pinMode (unsigned pin, unsigned mode)
{
//static const char s_directions_str[] = "in\0out\0";
if (! validGPIO(pin))
return -1;
char path[SYSFS_PATH_MAX];
int fd;
/*
if ( pinExport(pin) != true) {
logMessage (LOG_DEBUG, "start pinMode (pinExport) failed\n");
return false;
}
*/
snprintf(path, SYSFS_PATH_MAX, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open gpio direction for writing!\n");
logMessage (LOG_ERR, "Failed to open gpio '%s' for writing!\n",path);
return false;
}
//if (-1 == write(fd, &s_directions_str[INPUT == mode ? 0 : 3], INPUT == mode ? 2 : 3)) {
if (-1 == write(fd, (INPUT==mode?"in\n":"out\n"),(INPUT==mode?3:4))) {
//fprintf(stderr, "Failed to set direction!\n");
logMessage (LOG_ERR, "Failed to setup gpio input/output on '%s'!\n",path);
displayLastSystemError("");
return false;
}
close(fd);
return true;
}
int getPinMode(unsigned gpio) {
char path[SYSFS_PATH_MAX];
char value_str[SYSFS_READ_MAX];
int fd;
if (! validGPIO(gpio))
return -1;
snprintf(path, SYSFS_PATH_MAX, "/sys/class/gpio/gpio%d/direction", gpio);
fd = open(path, O_RDONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open gpio direction for writing!\n");
logMessage (LOG_ERR, "Failed to open gpio '%s' for reading!\n",path);
return false;
}
if (-1 == read(fd, value_str, SYSFS_READ_MAX)) {
//fprintf(stderr, "Failed to read value!\n");
logMessage (LOG_ERR, "Failed to read value on '%s'!\n",path);
displayLastSystemError("");
return(-1);
}
close(fd);
if (strcasecmp(value_str, "out")==0)
return OUTPUT;
return INPUT;
}
int digitalRead (unsigned pin)
{
char path[SYSFS_PATH_MAX];
char value_str[SYSFS_READ_MAX];
int fd;
if (! validGPIO(pin))
return -1;
snprintf(path, SYSFS_PATH_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open gpio value for reading!\n");
logMessage (LOG_ERR, "Failed to open gpio '%s' for reading!\n",path);
return(-1);
}
if (-1 == read(fd, value_str, SYSFS_READ_MAX)) {
//fprintf(stderr, "Failed to read value!\n");
logMessage (LOG_ERR, "Failed to read value on '%s'!\n",path);
displayLastSystemError("");
return(-1);
}
close(fd);
return(atoi(value_str));
}
int digitalWrite (unsigned pin, unsigned value)
{
//static const char s_values_str[] = "01";
char path[SYSFS_PATH_MAX];
int fd;
if (! validGPIO(pin))
return -1;
snprintf(path, SYSFS_PATH_MAX, "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open gpio value for writing!\n");
logMessage (LOG_ERR, "Failed to open gpio '%s' for writing!\n",path);
return false;
}
//if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
if (1 != write(fd, (LOW==value?"0":"1"), 1)) {
//fprintf(stderr, "Failed to write value!\n");
logMessage (LOG_ERR, "Failed to write value to '%s'!\n",path);
displayLastSystemError("");
return false;
}
close(fd);
return true;
}
#endif
void gpioDelay (unsigned int howLong) // Microseconds (1000000 = 1 second)
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
nanosleep (&sleeper, &dummy) ;
}
bool isExported(unsigned pin)
{
char path[SYSFS_PATH_MAX];
struct stat sb;
snprintf(path, SYSFS_PATH_MAX, "/sys/class/gpio/gpio%d/", pin);
if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode))
return true;
else
return false;
}
bool pinExport(unsigned pin)
{
char buffer[SYSFS_READ_MAX];
ssize_t bytes_written;
int fd;
if (! validGPIO(pin))
return -1;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open export for writing!\n");
logMessage (LOG_ERR, "Failed to open '/sys/class/gpio/export' for writing!\n");
return false;
}
bytes_written = snprintf(buffer, SYSFS_READ_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return true;
}
bool pinUnexport(unsigned pin)
{
char buffer[SYSFS_READ_MAX];
ssize_t bytes_written;
int fd;
if (! validGPIO(pin))
return -1;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open unexport for writing!\n");
logMessage (LOG_ERR, "Failed to open '/sys/class/gpio/unexport' for writing!\n");
return false;
}
bytes_written = snprintf(buffer, SYSFS_READ_MAX, "%d", pin);
write(fd, buffer, bytes_written);
close(fd);
return true;
}
bool edgeSetup (unsigned pin, unsigned value)
{
//static const char s_values_str[] = "01";
char path[SYSFS_PATH_MAX];
int fd;
if (! validGPIO(pin))
return -1;
snprintf(path, SYSFS_PATH_MAX, "/sys/class/gpio/gpio%d/edge", pin);
fd = open(path, O_WRONLY);
if (-1 == fd) {
//fprintf(stderr, "Failed to open gpio value for writing!\n");
logMessage (LOG_ERR, "Failed to open gpio '%s' for writing!\n",path);
return false;
}
int rtn = 0;
if (value==INT_EDGE_RISING)
rtn = write(fd, "rising", 6);
else if (value==INT_EDGE_FALLING)
rtn = write(fd, "falling", 7);
else if (value==INT_EDGE_BOTH)
rtn = write(fd, "both", 4);
else
rtn = write(fd, "none", 4);
if (rtn <= 0) {
logMessage (LOG_ERR, "Failed to setup edge on '%s'!\n",path);
displayLastSystemError("");
return false;
}
close(fd);
return true;
}
#include <poll.h>
#include <pthread.h>
#include <sys/ioctl.h>
struct threadGPIOinterupt{
void (*function)(void *args);
void *args;
unsigned pin;
};
static pthread_mutex_t pinMutex ;
#define MAX_FDS 64
static unsigned int _sysFds [MAX_FDS] =
{
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
} ;
void pushSysFds(int fd)
{
int i;
for (i=0; i< MAX_FDS; i++) {
if (_sysFds[i] == -1) {
_sysFds[i] = fd;
return;
}
}
}
void gpioShutdown() {
int i;
_ever = false;
for (i=0; i< MAX_FDS; i++) {
if (_sysFds[i] != -1) {
//printf("Closing fd %d\n",i);
close(_sysFds[i]);
_sysFds[i] = -1;
} else {
break;
}
}
}
int waitForInterrupt (int pin, int mS, int fd)
{
int x;
uint8_t c ;
struct pollfd polls ;
// Setup poll structure
polls.fd = fd ;
polls.events = POLLPRI | POLLERR | POLLHUP | POLLNVAL;
// Wait for something ...
x = poll (&polls, 1, mS) ;
// If no error, do a dummy read to clear the interrupt
// A one character read appars to be enough.
if (x > 0)
{
lseek (fd, 0, SEEK_SET) ; // Rewind
(void)read (fd, &c, 1) ; // Read & clear
}
return x ;
}
static void *interruptHandler (void *arg)
{
struct threadGPIOinterupt *stuff = (struct threadGPIOinterupt *) arg;
int pin = stuff->pin;
void (*function)(void *args) = stuff->function;
void *args = stuff->args;
stuff->pin = -1;
char path[SYSFS_PATH_MAX];
int fd, count, i ;
uint8_t c ;
sprintf(path, "/sys/class/gpio/gpio%d/value", pin);
if ((fd = open(path, O_RDONLY)) < 0)
{
logMessage (LOG_ERR, "Failed to open '%s'!\n",path);
return NULL;
}
pushSysFds(fd);
// Clear any initial pending interrupt
ioctl (fd, FIONREAD, &count) ;
for (i = 0 ; i < count ; ++i)
read (fd, &c, 1);
while (_ever == true) {
if (waitForInterrupt (pin, -1, fd) > 0) {
function(args);
} else {
printf("SOMETHING FAILED, reset\n");
gpioDelay(1);
}
}
printf("interruptHandler ended\n");
close(fd);
return NULL ;
}
bool registerGPIOinterrupt(int pin, int mode, void (*function)(void *args), void *args )
{
pthread_t threadId ;
struct threadGPIOinterupt stuff;
if (! validGPIO(pin))
return false;
// Check it's exported
if (! isExported(pin))
pinExport(pin);
// if the pin is putput, set as input to setup edge then reset to output.
if (getPinMode(pin) == OUTPUT) {
pinMode(pin, INPUT);
edgeSetup(pin, mode);
pinMode(pin, OUTPUT);
} else {
edgeSetup(pin, mode);
}
stuff.function = function;
stuff.args = args;
stuff.pin = pin;
pthread_mutex_lock (&pinMutex) ;
if (pthread_create (&threadId, NULL, interruptHandler, (void *)&stuff) < 0)
return false;
else {
while (stuff.pin == pin)
gpioDelay(1);
}
pthread_mutex_unlock (&pinMutex) ;
return true ;
}
#if defined(TEST_HARNESS) || defined(GPIO_MONITOR) || defined(GPIO_RW)
#include <stdarg.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
int _log_level = LOG_DEBUG;
void logMessage(int level, char *format, ...)
{
if (level > _log_level)
return;
char buffer[256];
va_list args;
va_start(args, format);
strncpy(buffer, " ", 8);
int size = vsnprintf (&buffer[8], 246-10, format, args);
va_end(args);
fprintf (stderr, buffer);
}
void displayLastSystemError (const char *on_what)
{
fputs (strerror (errno), stderr);
fputs (": ", stderr);
fputs (on_what, stderr);
fputc ('\n', stderr);
logMessage (LOG_ERR, "%d : %s", errno, on_what);
}
#endif //TEST_HARNESS || GPIO_MONITOR
#ifdef GPIO_RW
void errorParms()
{
printf("Missing Parameters:-\n\t[read|write] pin <value>\n\tgpio write 17 1\n");
exit(1);
}
int main(int argc, char *argv[]) {
bool isWrite=false;
int pin = 0;
int value = 0;
_log_level = LOG_ERR;
if (argc < 3) {
errorParms();
}
if (strcmp (argv[1], "read") == 0)
{
isWrite = false;
} else if (strcmp (argv[1], "write") == 0) {
isWrite = true;
if (argc < 4)
errorParms();
} else {
errorParms();
}
pin = atoi(argv[2]);
if (! gpioSetup()) {
logMessage (LOG_ERR, "Failed to setup GPIO\n");
return 1;
}
if (isWrite) {
value = atoi(argv[3]);
int pmode = getPinMode(pin);
pinMode (pin, OUTPUT);
digitalWrite(pin, value);
//if (pmode != OUTPUT)
// pinMode (pin, pmode);
}
printf ("%d\n", digitalRead(pin));
return 0;
}
#endif
#ifdef GPIO_MONITOR
bool FOREVER = true;
void intHandler(int signum) {
static int called=0;
logMessage (LOG_INFO, "Stopping! - signel(%d)\n",signum);
gpioShutdown();
FOREVER = false;
called++;
if (called > 3)
exit(1);
}
void event_trigger (int pin)
{
printf("Pin %d triggered, state=%d\n",pin,digitalRead(pin));
}
int main(int argc, char *argv[]) {
int i;
if (! gpioSetup()) {
logMessage (LOG_ERR, "Failed to setup GPIO\n");
return 1;
}
signal(SIGINT, intHandler);
signal(SIGTERM, intHandler);
signal(SIGSEGV, intHandler);
for (i=GPIO_MIN; i <= GPIO_MAX; i++) {
printf ("Pin %d is %d\n", i, digitalRead(i));
if (registerGPIOinterrupt (i, INT_EDGE_BOTH, (void *)&event_trigger, (void *)i) != true)
{
displayLastSystemError ("Unable to set interrupt handler for specified pin, exiting");
gpioShutdown();
return 1;
}
}
while(FOREVER) {
sleep(10);
}
return 0;
}
#endif //GPIO_MONITOR
//#define TEST_HARNESS
#ifdef TEST_HARNESS
#define GPIO_OFF 0x00005000 /* Offset from IO_START to the GPIO reg's. */
/* IO_START and IO_BASE are defined in hardware.h */
#define GPIO_START (IO_START_2 + GPIO_OFF) /* Physical addr of the GPIO reg. */
#define GPIO_BASE_NEW (IO_BASE_2 + GPIO_OFF) /* Virtual addr of the GPIO reg. */
void *myCallBack(void * args) {
printf("Ping\n");
//struct threadGPIOinterupt *stuff = (struct threadGPIOinterupt *) args;
//printf("Pin is %d\n",stuff->pin);
}
#define PIN 17
#define POUT 27
int main(int argc, char *argv[]) {
int repeat = 3;
// if (-1 == GPIOExport(POUT) || -1 == GPIOExport(PIN))
// return(1);
gpioSetup();
/*
pinUnexport(POUT);
pinUnexport(PIN);
pinExport(POUT);
pinExport(PIN);
*/
sleep(1);
//edgeSetup(POUT, INT_EDGE_BOTH);
if (-1 == pinMode(POUT, OUTPUT) || -1 == pinMode(PIN, INPUT))
return (2);
//edgeSetup(PIN, INT_EDGE_RISING);
//edgeSetup(POUT, INT_EDGE_RISING);
if (pinExport(POUT) != true)
printf("Error exporting pin\n");
/*
if (registerGPIOinterrupt(POUT, INT_EDGE_RISING, (void *)&myCallBack, (void *)&repeat ) != true)
printf("Error registering interupt\n");
if (registerGPIOinterrupt(PIN, INT_EDGE_RISING, (void *)&myCallBack, (void *)&repeat ) != true)
printf("Error registering interupt\n");
*/
do {
printf("Writing %d to GPIO %d\n", repeat % 2, POUT);
if (-1 == digitalWrite(POUT, repeat % 2))
return (3);
printf("Read %d from GPIO %d (input)\n", digitalRead(PIN), PIN);
printf("Read %d from GPIO %d (output)\n", digitalRead(POUT), POUT);
usleep(500 * 1000);
} while (repeat--);
gpioShutdown();
sleep(1);
if (-1 == pinUnexport(POUT) || -1 == pinUnexport(PIN))
return (4);
sleep(1);
return (0);
}
#endif