-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_input_epoll.c
189 lines (160 loc) · 3.36 KB
/
gpio_input_epoll.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
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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <sys/types.h>
// IO
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define IN_GPIO (51) // for MicroZed
#define OUT_GPIO (47) // for MicroZed
#define MAX_BUF (100)
#define RET_OK (0)
static char *kVersion = "V0.1";
typedef enum {
DIR_IN = 0,
DIR_OUT = 1
} enum_DIR;
typedef enum {
EDGE_RISING = 0,
EDGE_FALLING
} enum_EDGE;
static int gpio_export(int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0) {
return fd; // error
}
printf("setting export ...\n");
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
return 0;
}
static int gpio_set_direction(int gpio, enum_DIR enum_dir)
{
int fd;
char buf[MAX_BUF];
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%2d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
return fd; // error
}
printf("setting direction ...\n");
if (enum_dir == DIR_OUT) {
strcpy(buf, "out");
} else {
strcpy(buf, "in");
}
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
static int gpio_set_edge(int gpio, enum_EDGE enum_edge)
{
int fd;
char buf[MAX_BUF];
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%2d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
return fd; // error
}
printf("setting edge ...\n");
if (enum_edge == EDGE_RISING) {
strcpy(buf, "rising");
} else {
strcpy(buf, "falling");
}
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
static int gpio_set_value(int gpio, int hi)
{
int fd, len;
char buf[MAX_BUF];
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%2d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
return fd; // error
}
len = snprintf(buf, sizeof(buf), "%d", hi);
write(fd, buf, len);
close(fd);
return 0;
}
static int gpio_get_value(int gpio)
{
int fd, len;
char buf[MAX_BUF];
char code;
snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%2d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0) {
return fd; // error
}
read(fd, &code, 1);
close(fd);
if (code == '1') {
return 1;
} else {
return 0;
}
}
int main(void) {
int ret;
int epfd; // for epoll
int iofd; // for gpio
struct epoll_event ev1;
struct epoll_event events;
int count=0;
char code;
// initialization
if (gpio_export(OUT_GPIO) != RET_OK || gpio_export(IN_GPIO) != RET_OK) {
printf("gpio not found\n");
return;
}
if (gpio_set_direction(OUT_GPIO, DIR_OUT) != RET_OK ||
gpio_set_direction(IN_GPIO, DIR_IN) != RET_OK) {
printf("gpio dir set fail\n");
return;
}
gpio_set_edge(IN_GPIO, EDGE_RISING);
// example of epoll()
epfd = epoll_create(1); /*** epoll ***/
iofd = open("/sys/class/gpio/gpio51/value", O_RDONLY);
if (iofd == 0) {
printf("open fd fail\n");
close(epfd);
return;
}
read(iofd, &code, 1); // need this to make epoll() work
ev1.events = EPOLLPRI | EPOLLET | EPOLLIN;
ev1.data.fd = iofd;
ret = epoll_ctl(epfd, EPOLL_CTL_ADD, iofd, &ev1); /*** epoll ***/
while(1) {
ret = epoll_wait(epfd, &events, 1, -1); /*** epoll ***/
if (ret == 0) {
continue;
}
if (ret > 0) {
lseek(iofd, 0, SEEK_SET);
read(iofd, &code, 1);
if (code == '1') {
printf("pushed\n");
count++;
if (count >= 3) {
break;
}
}
} else {
printf("ret is nonzero\n");
}
}
close(epfd);
close(iofd);
}