问题描述
在Android和Linux下使用旧版sysfs GPIO,过程的第一步是 export
您要使用的特定GPIO引脚。当完成GPIO引脚的 unexport
操作后。
Using the legacy sysfs GPIO under Android and Linux the first step in the process is toe export
the particular GPIO pins you want to use. And when you are done with the GPIO pin to unexport
it.
我一直在寻找 export
命令实际上可以执行任何操作,但是我发现的所有内容都与内置的 bash
命令无关,而该命令与GPIO无关。
I've been looking for an explanation of what the export
command actually does however everything I've found is about the builtin bash
command which has nothing to do with GPIO.
然后我意识到从命令行实际的命令是 echo 938> / sys / class / gpio / export
和 / sys / class / gpio / export
是文件夹中的特殊设备文件/ sys / class / gpio
。
Then I realized the actual command from the command line was echo 938 > /sys/class/gpio/export
and /sys/class/gpio/export
is a special device file in folder /sys/class/gpio
.
我发现的唯一注释表明将GPIO引脚号写入 / sys / class / gpio / export
会使与该GPIO引脚关联的GPIO特殊文件导出到用户空间,然后,它允许用户应用程序使用指定的GPIO引脚以及对特殊设备文件的文件I / O。
The only comment that I have found indicates that writing the GPIO pin number to /sys/class/gpio/export
causes the GPIO special file associated with that GPIO pin to be "exported to user space" which then allows a user application to use the specified GPIO pin with file I/O to the special device file.
用户空间可能要求内核通过将GPIO的编号写入此文件来将GPIO的控制导出到用户空间
。
Userspace may ask the kernel to export control of a GPIO to userspaceby writing its number to this file.
范例: echo 19> export将为GPIO#19创建一个 gpio19节点,如果内核代码未要求则为
。
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.
示例: echo 19> unexport将删除使用 export文件导出
的 gpio19节点。
Example: "echo 19 > unexport" will remove a "gpio19" node exportedusing the "export" file.
因此,如果我指定回声938 / sys / class / gpio / export
,然后是特殊设备文件文件夹 / sys / class / gpio / gpio938
,其中包含特殊设备文件 / sys / class / gpio / gpio938 / value
和 / sys / class / gpio / gpio938 / direction
已创建。而当我执行 echo 938> / sys / class / gpio / unexport
会删除那些特殊的设备文件吗?
So if I specify echo 938 > /sys/class/gpio/export
then a special device file folder /sys/class/gpio/gpio938
with special device files /sys/class/gpio/gpio938/value
and /sys/class/gpio/gpio938/direction
are created. And when I do an echo 938 > /sys/class/gpio/unexport
then those special device files are removed?
在研究将GPIO引脚与Android 5.1下的DragonBoard 410C一起使用时,在线课程关于该设备,我说要在启动初始化脚本中添加以下几行。
In researching about using GPIO pins with a DragonBoard 410C under Android 5.1 an online course about this device I am taking said to add the following lines to the boot initialization script.
set -A pins 938 915 1017 926 937 930 914 971 901 936 935
for i in 0 1 2 3 4 5 6 7 8 9 10
do
echo ${pins[i]} > /sys/class/gpio/export;
chmod 777 /sys/class/gpio/gpio${pins[i]};
chmod 777 /sys/class/gpio/gpio${pins[i]}/value;
chmod 777 /sys/class/gpio/gpio${pins[i]}/direction;
done
我的理解是,这些命令为GPIO引脚938、915、1017创建了特殊的设备文件, 926、937、914、901、936、935,以便应用程序可以读取和写入这些GPIO引脚,以执行诸如通过将值写入例如 / sys /来打开和关闭LED的操作。 class / gpio / gpio938 / value
。
My understanding is that these commands create the special device files for GPIO pins 938, 915, 1017, 926, 937, 914, 901, 936, 935 so that an application can read and write to these GPIO pins to do something such as turning an LED on and off by writing values to, for instance /sys/class/gpio/gpio938/value
.
我对这个启动初始化脚本的理解是,这消除了用户使用 sudo 命令和每个shell命令行,以便用户在运行使用 sysfs
访问GPIO引脚的应用程序之前由用户执行这些命令。
My understanding about this boot initialization script is that this removes the need for a user to use the sudo
command with each of the shell command lines in order to perform these commands by a user before running an application that accesses the GPIO pins using sysfs
. Is that true?
我的问题
这些特殊设备文件是什么? / sys / class / gpio / export
和 / sys / class / gpio / unexport
以及它们如何与Linux内核中的某种功能连接,从而创建并销毁 / sys / class / gpio
文件夹中的特殊设备文件?
What are these special device files /sys/class/gpio/export
and /sys/class/gpio/unexport
and how are they connected to some kind of functionality in the Linux kernel which creates and destroys special device files in the /sys/class/gpio
folder?
建议对启动初始化脚本进行的更改是特殊的设备文件,表示由任何人创建的GPIO引脚,因此应用程序可以只使用这些引脚,而不必担心 export
或 unexport
?用户应用程序可以执行对特殊设备的读/写操作,而不必使用 sudo echo 938>。 / sys / class / gpio / export
首先?
With the suggested change to the boot initialization script are the special device files representing the GPIO pins created with access by anyone so an application program can just use the pins and not bother with export
or unexport
? A user application can just perform read/write to the special device without having to use sudo echo 938 > /sys/class/gpio/export
first?
由启动初始化脚本创建的这些特殊文件的访问和共享权限是什么,并且可以将多个应用程序
What is the access and sharing permissions for these special files created by the boot initialization script and can multiple applications be manipulating the same GPIO pins simultaneously?
推荐答案
Linux文件系统中有几种目录结构不是实际的磁盘文件目录。取而代之的是这些目录结构和文件。其中包含伪文件或Linux操作系统服务以及作为文件显示的数据,可以使用文件操作进行访问,但不是存储在永久性存储区(例如硬盘或固态磁盘)上的实际文件。
There are several directory structures within the Linux file system that are not actual disk file directories. Instead these directory structures and the "files" within them are pseudo files or Linux operating system services and data that are presented as files and can be accessed using file operations but are not actual files stored on a persistent store such as a hard disk or solid state disk.
类比伪文件
从用户角度考虑这些伪文件的一种方法是它们是接口,该接口使用文件系统语义来请求执行某些操作。文件系统语义映射到以下通用操作和行为:
A way to think about these pseudo files from a user perspective is they are a kind of Remote Procedure Call interface to the Linux kernel that uses file system semantics to request that some operation be done. The file system semantics map to the following generic actions and behavior:
- 打开伪文件意味着打开用户应用程序与Linux中某些功能之间的连接内核
- 读取伪文件是指通过连接读取Linux内核中某些功能所提供的数据块
- 写入伪文件是指通过连接将请求消息发送到Linux内核中的某些功能(消息可以是带有数据的命令,仅命令或仅数据)
- 关闭伪文件意味着:关闭用户应用程序与Linux内核中的某些功能之间的连接
- open the pseudo file means to open a connection between the user application and some functionality within the Linux kernel
- read the pseudo file means to read a block of data provided by some functionality within the Linux kernel through the connection
- write the pseudo file means to send a request message to some functionality within the Linux kernel through the connection (the message may be a command with data, a command only, or data only)
- close the pseudo file means to close a connection between the user application and some functionality within the Linux kernel
不同的伪文件公开了不同的Linux内核数据和服务,这意味着接口规范为文件操作如何映射到通过伪文件公开的Linux内核功能的不同,不仅取决于Linux ker,伪文件的内核功能或处理程序,以及Linux内核版本。
Different pseudo files expose different Linux kernel data and services which means that the interface specification as to how the file operations map to the Linux kernel functionality exposed through the pseudo file will vary depending not only on the Linux kernel functionality or handler for the pseudo file but also the Linux kernel version.
此StackOverFlow发布,,其中包含 / sys
中伪文件处理程序的简单示例,其中显示了提供功能接口的基础是Linux内核需要将新的伪文件的处理程序挂接到Linux内核中。
This StackOverFlow posting, Create sysfs entry from kernel module , contains a simple example of a handler for a pseudo file in /sys
showing the basics of providing the function interfaces the Linux kernel needs to hook the handler for the new pseudo file into the Linux kernel.
此StackOverFlow发布,,其中包含一个伪文件处理程序的简单示例在 / proc
中。
This StackOverFlow posting, How to create proc entry under /proc/driver? , contains a simple example of a handler for a pseudo file in /proc
.
这两个简单示例的结构都与源代码相似。但是,这些特定示例可能正在使用不推荐使用的Linux内核接口,因此我提供这些链接只是为了说明伪文件处理程序的基础功能。
Both of these simple examples have a similar structure to the source code. However these specific examples may be using deprecated Linux kernel interfaces so I provide these links only to illustrate the underlying functionality of a pseudo file handler.
导出和取消导出
通常,运行Linux的基础硬件的GPIO引脚不暴露给用户应用程序。 Linux内核使用设备驱动程序使用这些引脚与设备进行交互。
Normally the GPIO pins of the underlying hardware on which Linux is running are not exposed to user applications. The pins are used by the Linux kernel using device drivers to interact with devices.
export
的目的是公开选定的GPIO引脚作为伪文件发送到用户空间,允许用户应用程序与某些硬件执行自己的交互。并非所有可用的或可能的GPIO引脚都可以暴露。使用 export
可以暴露哪些引脚,这取决于在Linux内核中插入了什么 / sys
处理程序,以及处理程序允许。
The purpose of export
is to expose selected GPIO pins to user space as pseudo files allowing a user application to perform their own interactions with some hardware. Not all available or possible GPIO pins may be exposed. What pins can be exposed using export
will depend on what /sys
handlers have been inserted into the Linux kernel and what those handlers allow.
实际公开哪些伪文件以及如何使用这些伪文件将取决于GPIO引脚的功能,例如数字引脚与模拟引脚与支持PWM或具有上拉或下拉电阻的引脚。公开哪些文件还取决于 / sys / class / gpio /
的处理程序提供的功能。 GPIO引脚可能具有上拉或下拉电阻,但处理程序可能未提供接口来对其进行操作。
What pseudo files are actually exposed and how those pseudo files are used will depend on the function of the GPIO pin, e.g. a digital pin versus an analog pin versus a pin that supports PWM or has pullup or pulldown resistors. What files are exposed will also depend on what functionality the handler for /sys/class/gpio/
provides. A GPIO pin may have a pullup or pulldown resistor that could be used but the handler may not provide an interface to manipulate it.
对 export的请求
伪文件将创建一个代表请求的GPIO引脚的伪文件目录。这是通过将请求写入 export
伪文件并包含一条消息的消息完成的,该消息包含 export
命令所需的数据正确识别请求的GPIO引脚。然后,此消息由Linux内核中的GPIO export
sysfs处理程序处理,以创建表示GPIO引脚的伪文件文件夹以及提供用户之间接口的伪文件。应用程序和指定GPIO引脚的sysfs处理程序。处理程序提供物理GPIO引脚和引脚设备驱动程序与伪文件表示或接口之间的层。
A request to the export
pseudo file will create a pseudo file directory representing the requested GPIO pin. This is done by writing a request to the export
pseudo file with a message containing the data the export
command needs in order to properly identify the GPIO pin requested. This message is then processed by the GPIO export
sysfs handler in the Linux kernel to create the pseudo file folder representing the GPIO pin along with the pseudo files that provide the interface between the user application and the sysfs handler for the specified GPIO pin. The handler provides the layer between the physical GPIO pin and pin device driver and the pseudo file representation or interface.
unexport
伪文件删除GPIO引脚伪文件,以便不再与用户应用程序中的GPIO引脚进行交互。
The unexport
pseudo file removes the GPIO pin pseudo file so that interacting with the represented GPIO pin from a user application is no longer available.
有关PWM sysfs支持的注意事项:通过sysfs接口和 / sys
支持GPIO引脚,还支持PWM引脚。根文件夹为 / sys / class / pwm
,其功能在体系结构上与GPIO引脚相似。有一个类似的 export
和 unexport
功能使PWM引脚可用,并且使用导出的PWM伪文件是通过标准的与代表PWM引脚的伪文件夹相关联的一组文件上的文件操作。请参见其中描述了 PWM通过MPP_4引脚(即低速扩展连接器上的引脚28)公开的基本知识。
Note concerning PWM sysfs support: Just as there is support for GPIO pins through the sysfs interface and /sys
there is also support for PWM pins. The root folder is /sys/class/pwm
and the functionality is similar in architecture to that for GPIO pins. There is a similar export
and unexport
functionality to make the PWM pins available and using the exported PWM pseudo files are through standard file operations on a set of files associated with a pseudo file folder representing the PWM pin. See Using PMIC PWM on Dragonboard410c which describes the basics of "PWM is exposed via MPP_4 pin, which is pin 28 on the Low Speed Expansion Connector."
启动脚本已更改
启动脚本更改使用 / sys / class / gpio / export
创建请求的GPIO伪文件。但是,创建的伪文件具有一组在创建伪文件时设置的默认访问权限。由于创建是在具有root特权的初始化过程中进行的,因此 chmod
命令用于允许任何用户应用程序与创建的伪文件进行交互,而不仅仅是创建的用户root。
The boot script changes use the /sys/class/gpio/export
to create the requested GPIO pseudo file. However the created pseudo file has a set of default access permissions that are set when the pseudo file is created. Since the creation is during initialization with root privileges, the chmod
command is used to allow any user application to interact with the created pseudo files and not just the user, root, which created them.
由于导出
是在引导和初始化过程中完成的,因此其目的是创建GPIO引脚伪文件,该伪文件将保留下来。
Since the export
is being done during boot up and initialization, the intent is to create GPIO pin pseudo files which will stay in place while the device is powered up and to stay in place as long as the device is in use.
DragonBoard 410C低功耗连接器上的每个GPIO引脚都由几个伪表示。文件, value
用于传达引脚的值(高或低)和方向
用于传达引脚的方向(无论是输入引脚还是输出引脚)。因此,我们需要对每个希望用户应用程序访问的伪文件执行 chmod
,包括这些伪文件所在的伪文件文件夹,例如 / sys / class / gpio / gpio938
其中包含 / sys / class / gpio / gpio938 / value
和 / sys / class / gpio / gpio938 /方向
。
Each GPIO pins on the low power connector of the DragonBoard 410C are represented by several pseudo files, value
which is used to communicate the value of the pin (whether it is high or low) and direction
which is used to communicate the direction of the pin (whether it is an input pin or an output pin). So we need to do a chmod
on each of these pseudo files we want the user application to access including the pseudo file folder in which these pseudo files are located, for example /sys/class/gpio/gpio938
which contains /sys/class/gpio/gpio938/value
and /sys/class/gpio/gpio938/direction
.
这篇关于/ sys / class / gpio / export和`/ sys / class / gpio / unexport机制是什么?底层的sysfs功能是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!