您尚未登录。

楼主 #1 2021-02-28 18:16:51

马前卒
会员
注册时间: 2020-08-08
已发帖子: 60
积分: 57

Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

https://developer.ridgerun.com/wiki/index.php/How_to_use_GPIO_signals

https://developer.ridgerun.com/wiki/index.php?title=Gpio-int-test.c

Gpio-int-test.c:

/* Copyright (c) 2011, RidgeRun
 * All rights reserved.
 *
 * Contributors include:
 *   Todd Fischer
 *   Brad Lu
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by the RidgeRun.
 * 4. Neither the name of the RidgeRun nor the
 *    names of its contributors may be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY RIDGERUN ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL RIDGERUN BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>

 /****************************************************************
 * Constants
 ****************************************************************/
 
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64

/****************************************************************
 * 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) {
		perror("gpio/export");
		return fd;
	}
 
	len = snprintf(buf, sizeof(buf), "%d", gpio);
	write(fd, buf, len);
	close(fd);
 
	return 0;
}

/****************************************************************
 * 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) {
		perror("gpio/export");
		return fd;
	}
 
	len = snprintf(buf, sizeof(buf), "%d", gpio);
	write(fd, buf, len);
	close(fd);
	return 0;
}

/****************************************************************
 * gpio_set_dir
 ****************************************************************/
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
	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) {
		perror("gpio/direction");
		return fd;
	}
 
	if (out_flag)
		write(fd, "out", 4);
	else
		write(fd, "in", 3);
 
	close(fd);
	return 0;
}

/****************************************************************
 * 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) {
		perror("gpio/set-value");
		return fd;
	}
 
	if (value)
		write(fd, "1", 2);
	else
		write(fd, "0", 2);
 
	close(fd);
	return 0;
}

/****************************************************************
 * 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) {
		perror("gpio/get-value");
		return fd;
	}
 
	read(fd, &ch, 1);

	if (ch != '0') {
		*value = 1;
	} else {
		*value = 0;
	}
 
	close(fd);
	return 0;
}


/****************************************************************
 * gpio_set_edge
 ****************************************************************/

int gpio_set_edge(unsigned int gpio, 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) {
		perror("gpio/set-edge");
		return fd;
	}
 
	write(fd, edge, strlen(edge) + 1); 
	close(fd);
	return 0;
}

/****************************************************************
 * gpio_fd_open
 ****************************************************************/

int gpio_fd_open(unsigned int gpio)
{
	int fd, len;
	char buf[MAX_BUF];

	len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
 
	fd = open(buf, O_RDONLY | O_NONBLOCK );
	if (fd < 0) {
		perror("gpio/fd_open");
	}
	return fd;
}

/****************************************************************
 * gpio_fd_close
 ****************************************************************/

int gpio_fd_close(int fd)
{
	return close(fd);
}

/****************************************************************
 * 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_export(gpio);
	gpio_set_dir(gpio, 0);
	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;
}

离线

楼主 #2 2021-02-28 18:23:15

马前卒
会员
注册时间: 2020-08-08
已发帖子: 60
积分: 57

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

可以设置 上升沿/下降沿/两个都有 触发
可以监控多个GPIO
轮询时间间隔默认3秒,可以自行调整.

离线

#3 2021-03-01 09:52:26

JasonWoo
会员
注册时间: 2019-06-04
已发帖子: 84
积分: 55.5

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

相当不错,调用用户态的sysfs那就不需要编写额外驱动了。

离线

#4 2021-03-01 13:50:31

yomkk
会员
注册时间: 2020-09-23
已发帖子: 12
积分: 12

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

前段时间需要写一个控制GPIO的程序,原本也是想用这种方式的,后来听说sysfs控制gpio的方式即将被弃用,所以就换用libgpiod了。

最近编辑记录 yomkk (2021-03-01 13:50:58)

离线

#5 2022-03-28 13:41:37

how0723
会员
注册时间: 2021-12-12
已发帖子: 65
积分: 53

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

不错,感谢

离线

#6 2022-10-19 11:01:29

yuess133
会员
注册时间: 2020-12-10
已发帖子: 18
积分: 13.5

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

yomkk 说:

前段时间需要写一个控制GPIO的程序,原本也是想用这种方式的,后来听说sysfs控制gpio的方式即将被弃用,所以就换用libgpiod了。

你好,能否给个libgpiod使用 的例子,我这里提示 #include<gpiod.h>  , 没有gpiod.h这个文件

离线

#7 2022-10-19 14:56:23

海石生风
会员
所在地: 深圳
注册时间: 2019-07-02
已发帖子: 522
积分: 643
个人网站

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

@yuess133
内核不带gpiod.h这个头文件,要在用户空间安装好libgpiod这个库才能用。

离线

#8 2022-10-20 09:22:31

jxdqwer
会员
注册时间: 2022-10-17
已发帖子: 16
积分: 1

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

挺不错的,要是可以增加epoll方式就更好了,

离线

#9 2022-10-20 10:56:25

海石生风
会员
所在地: 深圳
注册时间: 2019-07-02
已发帖子: 522
积分: 643
个人网站

Re: Linux 应用层 C 代码 POLL 轮询 GPIO 引脚中断 (转)

jxdqwer 说:

挺不错的,要是可以增加epoll方式就更好了,

其实Linux内核组已经建议不要用sysfs来操作gpio了,建议用libgpiod,它支持异步方式响应gpio中断信号,这种方式就类似于裸机的中断处理了。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn