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控制LED
zynq> pwd
/sys/class/gpio
zynq> echo > 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 > value // 输出高电平
zynq> echo > value // 输出低电平
linux下GPIO的用户层操作(sysfs)的更多相关文章
- Linux下记录登录用户历史操作
前言:众所周知Linux是一个可以同时让多个用户登录的操作系统,每个用户的操作都影响着Linux运行,除了要做好安全工作以外,防止人为恶意损坏也是很关键的,比如有人恶意执行危险命令,要查找就得记录所有 ...
- Linux下记录所有用户的登录和操作日志
Linux下记录所有用户的登录和操作日志 一般我们可以用history命令来查看用户的操作记录,但是这个命令不能记录是哪个用户登录操作的,也不能记录详细的操作时间,且不完整:所以误操作而造成重要的 ...
- Linux下非root用户如何安装软件
Linux下非root用户如何安装软件 从windows转移到Linux的用户最开始总会有各种不适,因为这种不适最终放弃linux的不在少数.对于这类人只能说可惜,还没有领略到linux的美好就过早放 ...
- linux下MySQL安装登录及操作
linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-cl ...
- Linux 下创建 sftp 用户并限定目录
Linux 下创建 sftp 用户并限定目录 1.创建 sftpUser 用户组 [root@XXX ~]# groupadd sftpUser 2.创建 sftpUser 用户并指定目录 [root ...
- linux下非root用户的sudo问题
linux下的root用户是个超级管理员,一般是不用这个用户登录进行操作的,但有时候需要root权限,又不想切换用户的话可以使用sudo命令.但是不是所有的用户都可以使用sudo命令的. 首先可能会遇 ...
- linux下查看当前用户的 三个命令
linux下查看当前用户的 三个命令 1,whoami; 2,id -un; 3,who -H 可以列出当前所有的 NAME (用户名) LINE (窗口列表) TIME(开启时间 ...
- Linux下Wireshark普通用户不能获取网络接口问题
Linux下Wireshark普通用户不能获取网络接口问题 1.安装setcap, setcap 是libcap2-bin包的一部分,一般来说,这个包默认会已经装好. sudo apt-get ins ...
- Linux 下非root用户使用docker
Linux 下非root用户使用docker 通常我们使用linux系统的时候,最好是不要直接使用root账号,但是使用Docker的时候,默认又是不能使用非root用户的,关于原因,官方说法如下: ...
随机推荐
- Codeforces Round #566 (Div. 2) C. Beautiful Lyrics
链接: https://codeforces.com/contest/1182/problem/C 题意: You are given n words, each of which consists ...
- POJ1830(异或高斯消元)
对于某个开关,都有n个选项可能影响它的结果,如果会影响,则系数为1,否则系数为0:最后得到自由元的个数,自由元可选0也可选1. #include <cstdio> #include < ...
- Restful 1 -- REST、DRF(View源码解读、APIView源码解读)及框架实现
一.REST 1.什么是编程? 数据结构和算法的结合 2.什么是REST? - url用来唯一定位资源,http请求方式来区分用户行为 首先回顾我们曾经做过的图书管理系统,我们是这样设计url的,如下 ...
- CentOS,net core2 sdk nginx、supervisor、mysql
CentOS下 .net core2 sdk nginx.supervisor.mysql环境搭建 作为.neter,看到.net core 2.0的正式发布,心里是有点小激动的,迫不及待的体验了一把 ...
- 关系型数据库---MySQL---数据类型
一.每个数据表至少有一个数据列.用户必须为每一个数据列分别定义一个适当的数据类型: 1.整数(***Int) 1.1 默认情况下,整数类型包括:正整数.负整数: 1.2 如果给数据列定义了unsign ...
- Java8中的新特性Optional
Optional 类是一个可以为null的容器对象.如果值存在则isPresent()方法会返回true,调用get()方法会返回该对象.Optional 是个容器:它可以保存类型T的值,或者仅仅保存 ...
- Django使用网站图标
默认情况下,浏览器访问一个网站的时候,同时还会向服务器请求“/favicon.ico”这个URL,目的是获取网站的图标. 若是没有配置的话,Django就会返回一个404错误,并且浏览器接收到这个40 ...
- SSAS 部署之创建部署脚本
1.获取多维数据库的结构脚本: 当你的SSAS项目完成后,在Bin目录下会有一个SSAS.asdatabase文件. 2.打开“开始” ->Microsoft SQL Server 2008 R ...
- ruby 从命令行读取文本
最常见的方式就是使用内置的get 方法,这个方法可以从命令行读取用户的输入,并在默认的情况下把读入的文本赋值给预定义变量$_. 但是get方法会保留用户在输入字符串末尾所加的换行符,当用户在输入的字符 ...
- Json 后台转对象的方式或者获取属性值方式。
类似这类的 json字符串 后台转成 model 或者取其中一个属性值. 需要去掉前后引号 以及将转义字符去掉.空格代替 resoult = resoult.Substring(0, resoult. ...