linux下GPIO的用户层操作(sysfs)

linux的GPIO通过sysfs为用户提供服务,下面是linux kernel里的说明文档,学习一下。

GPIO Sysfs Interface for Userspace
==================================

Platforms which use the "gpiolib" implementors framework may choose to
configure a sysfs user interface to GPIOs. This is different from the
debugfs interface, since it provides control over GPIO direction and
value instead of just showing a gpio state summary. Plus, it could be
present on production systems without debugging support.

Given appropriate hardware documentation for the system, userspace could
know for example that GPIO #23 controls the write protect line used to
protect boot loader segments in flash memory. System upgrade procedures
may need to temporarily remove that protection, first importing a GPIO,
then changing its output state, then updating the code before re-enabling
the write protection. In normal use, GPIO #23 would never be touched,
and the kernel would have no need to know about it.

Again depending on appropriate hardware documentation, on some systems
userspace GPIO can be used to determine system configuration data that
standard kernels won‘t know about. And for some tasks, simple userspace
GPIO drivers could be all that the system really needs.

DO NOT ABUSE SYSFS TO CONTROL HARDWARE THAT HAS PROPER KERNEL DRIVERS.
PLEASE READ THE DOCUMENT NAMED "drivers-on-gpio.txt" IN THIS DOCUMENTATION
DIRECTORY TO AVOID REINVENTING KERNEL WHEELS IN USERSPACE. I MEAN IT.
REALLY.

Paths in Sysfs
--------------
There are three kinds of entries in /sys/class/gpio:

   -	Control interfaces used to get userspace control over GPIOs;

   -	GPIOs themselves; and

   -	GPIO controllers ("gpio_chip" instances).

That‘s in addition to standard files including the "device" symlink.

The control interfaces are write-only:

    /sys/class/gpio/

    	"export" ... Userspace may ask the kernel to export control of
		a GPIO to userspace by writing its number to this file.

		Example:  "echo 19 > export" will create a "gpio19" node
		for GPIO #19, if that‘s not requested by kernel code.

    	"unexport" ... Reverses the effect of exporting to userspace.

		Example:  "echo 19 > unexport" will remove a "gpio19"
		node exported using the "export" file.

GPIO signals have paths like /sys/class/gpio/gpio42/ (for GPIO #42)
and have the following read/write attributes:

    /sys/class/gpio/gpioN/    通过export刚刚创建的文件件

	"direction" ... reads as either "in" or "out". This value may
		normally be written. Writing as "out" defaults to
		initializing the value as low. To ensure glitch free
		operation, values "low" and "high" may be written to
		configure the GPIO as an output with that initial value.

		Note that this attribute *will not exist* if the kernel
		doesn‘t support changing the direction of a GPIO, or
		it was exported by kernel code that didn‘t explicitly
		allow userspace to reconfigure this GPIO‘s direction.

	"value" ... reads as either 0 (low) or 1 (high). If the GPIO
		is configured as an output, this value may be written;
		any nonzero value is treated as high.

		If the pin can be configured as interrupt-generating interrupt
		and if it has been configured to generate interrupts (see the
		description of "edge"), you can poll(2) on that file and
		poll(2) will return whenever the interrupt was triggered. If
		you use poll(2), set the events POLLPRI and POLLERR. If you
		use select(2), set the file descriptor in exceptfds. After
		poll(2) returns, either lseek(2) to the beginning of the sysfs
		file and read the new value or close the file and re-open it
		to read the value.

	"edge" ... reads as either "none", "rising", "falling", or
		"both". Write these strings to select the signal edge(s)
		that will make poll(2) on the "value" file return.

		This file exists only if the pin can be configured as an
		interrupt generating input pin.

	"active_low" ... reads as either 0 (false) or 1 (true). Write
		any nonzero value to invert the value attribute both
		for reading and writing. Existing and subsequent
		poll(2) support configuration via the edge attribute
		for "rising" and "falling" edges will follow this
		setting.

GPIO controllers have paths like /sys/class/gpio/gpiochip42/ (for the
controller implementing GPIOs starting at #42) and have the following
read-only attributes:

    /sys/class/gpio/gpiochipN/

    	"base" ... same as N, the first GPIO managed by this chip

    	"label" ... provided for diagnostics (not always unique)

        "ngpio" ... how many GPIOs this manages (N to N + ngpio - 1)

Board documentation should in most cases cover what GPIOs are used for
what purposes. However, those numbers are not always stable; GPIOs on
a daughtercard might be different depending on the base board being used,
or other cards in the stack. In such cases, you may need to use the
gpiochip nodes (possibly in conjunction with schematics) to determine
the correct GPIO number to use for a given signal.

Exporting from Kernel code
--------------------------
Kernel code can explicitly manage exports of GPIOs which have already been
requested using gpio_request():

	/* export the GPIO to userspace */
	int gpiod_export(struct gpio_desc *desc, bool direction_may_change);

	/* reverse gpio_export() */
	void gpiod_unexport(struct gpio_desc *desc);

	/* create a sysfs link to an exported GPIO node */
	int gpiod_export_link(struct device *dev, const char *name,
		      struct gpio_desc *desc);

After a kernel driver requests a GPIO, it may only be made available in
the sysfs interface by gpiod_export(). The driver can control whether the
signal direction may change. This helps drivers prevent userspace code
from accidentally clobbering important system state.

This explicit exporting can help with debugging (by making some kinds
of experiments easier), or can provide an always-there interface that‘s
suitable for documenting as part of a board support package.

After the GPIO has been exported, gpiod_export_link() allows creating
symlinks from elsewhere in sysfs to the GPIO sysfs node. Drivers can
use this to provide the interface under their own device in sysfs with
a descriptive name.
GPIO20控制LEDzynq> pwd
/sys/class/gpio
zynq> echo 20 > export
zynq> ls
export     gpio20     gpiochip0  unexport
zynq> cd gpio20
zynq> ls
active_low  device      direction   edge        subsystem   uevent      value
zynq> echo out> direction
zynq> cat direction
out
zynq> echo 1 > value     //  输出高电平
zynq> echo 0 > value     //   输出低电平
时间: 2024-08-11 09:40:12

linux下GPIO的用户层操作(sysfs)的相关文章

linux下非root用户的sudo问题

linux下的root用户是个超级管理员,一般是不用这个用户登录进行操作的,但有时候需要root权限,又不想切换用户的话可以使用sudo命令.但是不是所有的用户都可以使用sudo命令的. 首先可能会遇到下面的提示: ××× is not in the sudoers file.  This incident will be reported. 解决办法: 1.修改/etc/sudoers 文件 #su - #chmod u+w /etc/sudoers 在 root    ALL=(ALL) 

在linux下如何终止用户会话?

公司使用的监控工具是cacti,在阈值里的设置是当已登录用户超过2个的时候就会报警.那么,我的疑问就出来了,如何终止多于的用户会话呢?我自己又是哪个用户会话呢,总不能自己把自己给终止会话了吧. 第一步:查看哪些IP在操作服务器,who命令 [[email protected] ~]# who root     tty1         2014-04-20 08:42 root     pts/0        2014-04-20 09:10 (192.168.204.50) 第二步:查看服务

linux下MySQL安装登录及操作

linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-client-4.0.16-0.i386.rpm 下载地址为:http://www.mysql.com/downloads/mysql-4.0.html, 打开此网页,下拉网页找到“Linux x86 RPM downloads”项,找到“Server”和“Client programs”项,下载需要的

linux下mysql的用户管理及访问问题

1.mysql的安装配置 linux下有一个很神奇的东西叫yum,只要有源,用yum来安装是一件非常容易的事,什么都不用管,它会为你解决好一些软件依赖的问题.一键安装mysql: [[email protected] ~]# yum install mysql-server mysql-devel 安装完成后我们就可以使用mysql了: [[email protected] ~]# /etc/init.d/mysqld start Starting mysqld: [ OK ] [[email 

Linux下非root用户安装软件

下面简要说一下Linux下非root用户安装软件的一般流程:1. 获取源代码,一般是wget方式,ubuntu可以使用apt-get source来获取源代码.2. 解压源代码,一般使用tar -zxvf xxx.tar.gz即可3. 切换到解压后的目录,运行 ./configure.其选项可以通过 ./configure –help来获取,非root用户下最重要的应该是定义安装目录,即应该定义 ./configure –prefix=/path/to/bin, 对于一些依赖库,可能还需要使用

Linux下的删除过期文件操作

在实际的软件开发项目中,经常会有实现删除过期文件的需求.即要求程序能够自动删除多长时间之前生成的文件.本文提供了一个示例代码,为相关的开发工作提供了参考. 程序如下(本程序用makefile进行编译): /*********************************************************************** 版权所有 (C)2014, Zhou Zhaoxiong.* * 文件名称: TestFileDelete.c* 内容摘要: 用于演示Linux下过期

linux下非root用户如何修改root权限的文件

在linux下会出现把一些配置文件参数配错.root密码忘记等导致系统无法启动或进入root的窘迫境地,本文以redhat  enterprise linux server 6.4为例介绍root身份修改root权限配置文件的参数后,root无法登录,然后通过linux的rescue模式改回配置文件,巧妙以非root身份修改了root权限的文件的解决方案. 首先,插入linux安装盘重启系统,等到引导进入安装会话,上下移动方向键,选rescue installed system: 第二,进入修复

Linux下如何修改用户默认目录

Linux下默认的用户目录一般为/home/xxx(root用户除外),有些时候我们可能需要修改这个目录,下面我就给大家分享2中修改的方法 工具/原料 Linux操作系统 方法/步骤 1 1.切换到root用户,直接修改/etc/passwd文件,找到你的用户名你一行,如下图所示修改路径,然后保存即可. 2 2.切换到root用户,使用usermod命令,例如usermod -d /tmp test (test为你的用户名),使用该命令请确保该用户下没有运行的软件或进程 3 最后切换到普通账户,

linux下日常较频繁需要操作的文件

1.linux下日常较频繁需要操作的文件: 网卡配置文件/etc/sysconfig/network-scripts/ifcfg-ethx注:不同品牌的服务器或者不同的系统版本之间,可能网卡名有所不同,配置时需要注意. DNS配置文件/etc/resolv.conf 远程配置文件/etc/ssh/sshd_config 防火墙配置文件/etc/sysconfig/iptables 日志文件/var/log/xx ;如secure记录登陆等安全日志:boot.log记录引导日志:messages记