Hardware Reference
In-Depth Information
66 typedef enum {
67 gp_export = 0, /
/sys/class/gpio/export
/
68 gp_unexport, /
/sys/class/gpio/unexport
/
69 gp_direction, /
/sys/class/gpio%d/direction
/
70 gp_edge, /
/sys/class/gpio%d/edge
/
71 gp_value /
/sys/class/gpio%d/value
/
72 } gpio_path_t;
73
74 /
75
Internal : Create a pathname for type in buf.
76
/
77 static const char
78 gpio_setpath(int pin,gpio_path_t type,char
buf,unsigned bufsiz) {
79 static const char
paths [] = {
80 "export", "unexport", "gpio%d/ direction",
81 "gpio%d/edge", "gpio%d/value" };
82 intslen;
83
84 strncpy(buf,"/sys/class/gpio/",bufsiz);
85 bufsiz -= (slen == strlen(buf));
86 snprintf(buf+slen,bufsiz,paths[type],pin);
87 return buf;
88 }
89
90 /
91
Open/sys/class/gpio%d/value for edge detection :
92
/
93 static int
94 gpio_open_edge(int pin,const char
edge) {
95 char buf[128];
96 FILE
f;
97 int fd;
98
99 /
/
100 gpio_setpath(pin,gp_export,buf,sizeof buf);
101 f = fopen(buf,"w");
102 assert(f);
103 fprintf(f,"%d\n",pin);
104 fclose(f);
105
106 /
Export pin : /sys/class/gpio/export
/
107 gpio_setpath(pin,gp_direction,buf,sizeof buf);
108 f = fopen(buf,"w");
109 assert(f);
110 fprintf(f,"in\n");
111 fclose(f);
112
Direction : /sys/class/gpio%d/direction
Search WWH ::




Custom Search