Hardware Reference
In-Depth Information
1 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
sysgpio.c : Open/configure /sys GPIO pin
3
4
Here we must open the /sys/class/gpio/gpio17/value and do a
5
poll(2) on it, so that we can sense the MCP23017 interrupts.
6
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
7
8 typedef enum {
9 gp_export = 0, /
/sys/class/gpio/export
/
10 gp_unexport, /
/sys/class/gpio/unexport
/
11 gp_direction, /
/sys/class/gpo%d/direction
/
12 gp_edge, /
/sys/class/gpio%d/edge
/
13 gp_value /
/sys/class/gpio%d/value
/
14 } gpio_path_t;
15
16 /
17
Internal : Create a pathname for type in buf.
18
/
19 static const char
20 gpio_setpath(int pin,gpio_path_t type,char
buf,unsigned bufsiz) {
21 static const char
paths[] = {
22 "export", "unexport", "gpio%d/direction",
23 "gpio%d/edge", "gpio%d/value" };
24 int slen;
25
26 strncpy(buf,"/sys/class/gpio/",bufsiz);
27 bufsiz -= (slen = strlen(buf));
28 snprintf(buf+slen,bufsiz,paths[type],pin);
29 return buf;
30 }
31
32 /
33
Open /sys/class/gpio%d/value for edge detection :
34
/
35 static int
36 gpio_open_edge(int pin) {
37 char buf[128];
38 FILE
f;
39 int fd;
40
41 /
/
42 gpio_setpath(pin,gp_export,buf,sizeof buf);
43 f = fopen(buf, "w");
44 assert(f);
45 fprintf(f,"%d\n",pin);
46 fclose(f);
47
Export pin: /sys/class/gpio/export
Search WWH ::




Custom Search