解决方法链接:https://stackoverflow.com/questions/30938991/access-gpio-sys-class-gpio-as-non-root/30940526
硬件获取触发权限、开放对应端口
1
2
3
4
5
6
7
8
9
10
11
12
13//1.开放权限
gedit /etc/udev/rules.d/99-galaxy-u3v.rules
##添加
SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R root:gpio /sys/class/gpio && chmod -R 770 /sys/class/gpio'"
//2.设置触发
gedit /etc/rc.local
##添加
sudo chmod 770 /sys/class/gpio/unexport /sys/class/gpio/export
sudo echo 246 > /sys/class/gpio/export
sudo chmod -R 777 /sys/class/gpio/gpio246
sudo echo out > /sys/class/gpio/gpio246/direcion
sudo echo 0 > /sys/class/gpio/gpio246/value
C++具体实现
gpioset.hgpioset.cpp1
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
//int keepgoing = 1;
enum PIN_DIRECTION
{
INPUT_PIN=0,
OUTPUT_PIN=1
};
enum PIN_VALUE
{
LOW = 0,
HIGH = 1
};
/*****************************************/
/* gpio export */
int gpio_export(unsigned int gpio);
/* gpio unexport */
int gpio_unexport(unsigned int gpio);
/* gpio set dir */
int gpio_set_dir(unsigned int gpio, const char *dir);
/* gpio set value */
int gpio_set_value(unsigned int gpio, unsigned int value);
/* gpio get value */
int gpio_get_value(unsigned int gpio, unsigned int *value);
/* gpio set edge
控制中断触发模式:引脚被配置为中断后可以使用poll()函数监听引脚
非中断引脚:echo "none" >edge
上升沿触发:rising
下降沿触发:falling
边沿触发:both
*/
int gpio_set_edge(unsigned int gpio, const char *edge);
/* gpio fd open */
int gpio_fd_open(unsigned int gpio, unsigned int dir);
/* gpio fd close */
int gpio_fd_close(int fd);
/*// Callback called when SIGINT is sent to the process (Ctrl-C)
void signal_handler(int sig);*/main.cpp / 具体调用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
/* gpio export */
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
if (fd < 0)
{
printf ("\nFailed export GPIO-%d\n", gpio);
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
printf ("\nSucessfully export GPIO-%d\n", gpio);
return 1;
}
/* gpio unexport */
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
fd = open(SYSFS_GPIO_DIR "/unexport", O_WRONLY);
if (fd < 0)
{
printf ("\nFailed unexport GPIO-%d\n", gpio);
return fd;
}
len = snprintf(buf, sizeof(buf), "%d", gpio);
write(fd, buf, len);
close(fd);
printf ("\nSucessfully unexport GPIO-%d\n", gpio);
return 1;
}
/* gpio set dir */
int gpio_set_dir(unsigned int gpio, const char *dir)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
printf ("\nFailed set GPIO-%d direction\n", gpio);
return fd;
}
write(fd, dir, strlen(dir)+1);
close(fd);
printf ("\nSucessfully set GPIO-%d direction\n", gpio);
return 1;
}
/* gpio set value */
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
printf ("\nFailed set GPIO-%d value\n", gpio);
return fd;
}
if (value!=0)
{
int i = write(fd, "1", 2);
printf ("\nGPIO-%d value set high\n", gpio);
}
else
{
write(fd, "0", 2);
printf ("\nGPIO-%d value set low\n", gpio);
}
close(fd);
printf ("\nSucessfully set GPIO-%d value\n", gpio);
return 1;
}
/* gpio get value */
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0)
{
printf ("\nFailed get GPIO-%d value\n", gpio);
return fd;
}
read(fd, &ch, 1);
if (ch != '0')
{
*value = 1;
} else
{
*value = 0;
}
close(fd);
printf ("\nSucessfully get GPIO-%d value\n", gpio);
return 0;
}
/* gpio set edge */
int gpio_set_edge(unsigned int gpio, const char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
printf ("\nFailed set GPIO-%d edge\n", gpio);
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
/* gpio fd open */
int gpio_fd_open(unsigned int gpio, unsigned int dir)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, dir | O_NONBLOCK );
if (fd < 0)
{
perror("gpio/fd_open");
}
return fd;
}
/* gpio fd close */
int gpio_fd_close(int fd)
{
return close(fd);
}
// Callback called when SIGINT is sent to the process (Ctrl-C)
/*void signal_handler(int sig)
{
printf( "Ctrl-C pressed, cleaning up and exiting..\n" );
keepgoing = 0;
}*/1
2
3gpio_set_dir(246,"out");
gpio_set_value(246,0);
gpio_set_value(246,1);