我尝试在linux中使用GPIO Interrut(下降沿)。(使用GPIO Expander PCAL6524)。
首先,我将GPIO设置为low_active / direction,如下所示。
static int pcal6524_setup(struct i2c_client *client, int gpio,
unsigned int ngpio, void *context)
{
int status;
unsigned char i;
char label[15];
gpio += INPUT_BASE;
for (i = 0; i < 8; ++i) {
snprintf(label, 10, "exp_input%d", i);
status = gpio_request(gpio, label);
if (status)
goto out_free;
exp_gpio[i] = gpio++;
status = gpio_direction_input(exp_gpio[i]);
if (status) {
gpio_free(exp_gpio[i]);
exp_gpio[i] = -EINVAL;
goto out_free;
}
status = gpio_export(exp_gpio[i], 1);
if (status) {
gpio_free(exp_gpio[i]);
exp_gpio[i] = -EINVAL;
goto out_free;
}
status = gpio_sysfs_set_active_low(exp_gpio[i], 1);
if (status < 0) {
gpio_free(exp_gpio[i]);
exp_gpio[i] = -EINVAL;
goto out_free;
}
}
return status;
out_free:
for (i = 0; i < 4; ++i) {
if (exp_gpio[i] != -EINVAL) {
gpio_free(exp_gpio[i]);
exp_gpio[i] = -EINVAL;
}
}
return status;
}
我检查了GPIO是否设置在&#34; / sys / class / gpio / gpioN&#34; - active_low:1,direction:in
但现在我不知道如何设置GPIO下降沿。 我尝试在JNI中设置GPIO下降沿,如下所示。 但是发生了开放错误。(/ sys / class / gpio / gpioN / edge)
/****************************************************************
* gpio_set_edge
****************************************************************/
#define GPIO_PATH "/sys/class/gpio"
int gpio_set_edge(unsigned int gpio, char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), GPIO_PATH "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0) {
perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
以下是检查和继续GPIO下降沿中断的主程序。
/****************************************************************
* Main
****************************************************************/
int main(int argc, char **argv, char **envp)
{
struct pollfd fdset[2];
int nfds = 2;
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpio;
int len;
if (argc < 2) {
printf("Usage: gpio-int <gpio-pin>\n\n");
printf("Waits for a change in the GPIO pin voltage level or input on stdin\n");
exit(-1);
}
gpio = atoi(argv[1]);
gpio_set_edge(gpio, "rising");
gpio_fd = gpio_fd_open(gpio);
timeout = POLL_TIMEOUT;
while (1) {
memset((void*)fdset, 0, sizeof(fdset));
fdset[0].fd = STDIN_FILENO;
fdset[0].events = POLLIN;
fdset[1].fd = gpio_fd;
fdset[1].events = POLLPRI;
rc = poll(fdset, nfds, timeout);
if (rc < 0) {
printf("\npoll() failed!\n");
return -1;
}
if (rc == 0) {
printf(".");
}
if (fdset[1].revents & POLLPRI) {
lseek(fdset[1].fd, 0, SEEK_SET);
len = read(fdset[1].fd, buf, MAX_BUF);
printf("\npoll() GPIO %d interrupt occurred\n", gpio);
}
if (fdset[0].revents & POLLIN) {
(void)read(fdset[0].fd, buf, 1);
printf("\npoll() stdin read 0x%2.2X\n", (unsigned int) buf[0]);
}
fflush(stdout);
}
gpio_fd_close(gpio_fd);
return 0;
}
我找到了为什么发生了开放错误.. 当我检查gpio的文件属性时,我发现gpio的读/写权限如下。
130 | shell @ test_ref:/ sys / class / gpio / gpio218 $ ll -rw-r - r-- root root 4096 2009-09-01 01:56 active_low
lrwxrwxrwx root root 2009-09-01 01:56 device - &gt; ../../../ 0-0022
-rw-r - r-- root root 4096 2009-09-01 01:56方向
-rw-r - r-- root root 4096 2009-09-01 00:30 edge
drwxr-xr-x root root 2009-09-01 00:00 power
lrwxrwxrwx root root 2009-09-01 01:56子系统 - &gt; ../../../../../../../类/ GPIO
-rw-r - r-- root root 4096 2009-09-01 00:00 uevent
-rw-r - r-- root root 4096 2009-09-01 00:00 value
如上所述,gpio / edge的写入权限已被拒绝......
如何在linux内核中允许gpio / edge的写权限?
由于