forked from johnyHV/OrangePi-Zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPIO.c
85 lines (73 loc) · 1.22 KB
/
GPIO.c
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
#include <sys/stat.h>
#include <sys/types.h>
//#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
//#include <fcntl.h>
/* gcc GPIO.c -o GPIO */
int fd;
char buf[255];
/* create GPIO */
void gpioExport(int gpio)
{
// int fd;
// char buf[255];
fd = open("/sys/class/gpio/export", O_WRONLY);
sprintf(buf, "%d", gpio);
write(fd, buf, strlen(buf));
close(fd);
}
/*IN/OUT GPIO*/
void gpioDirection(int gpio, int direction) // 1 for output, 0 for input
{
sprintf(buf, "/sys/class/gpio/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (direction)
{
write(fd, "out", 3);
}
else
{
write(fd, "in", 2);
}
close(fd);
}
/*SET GPIO */
void gpioSet(int gpio, int value)
{
sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
sprintf(buf, "%d", value);
write(fd, buf, 1);
close(fd);
}
/* GET GPIO*/
int gpioRead(int gpio)
{
char value;
int retn = 0;
sprintf(buf, "/sys/class/gpio/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
read(fd, &value, 1);
if(value == '0')
{
retn = 0;
}
else
{
retn = 1;
}
close(fd);
return retn;
}
/* MAIN */
void main()
{
gpioExport(3);
gpioDirection(3,1);
gpioSet(3,1);
}
/*EOF*/