User-level operation of Gpio under Linux (SYSFS)

Source: Internet
Author: User

Linux Gpio provides services to users through SYSFS, and here is a description of the Linux kernel, which you'll learn about.

GPIO Sysfs Interface for userspace==================================platforms which use the "Gpiolib" implementors The framework may choose Toconfigure a SYSFS the user interface to GPIOs. This was different from Thedebugfs interface, since it provides control over GPIO direction andvalue instead of just Showin G A Gpio state summary. Plus, it could bepresent on production systems without debugging support. Given appropriate hardware documentation for the system, userspace couldknow for example that GPIO #23 controls the Write Protect line used Toprotect boot loader segments in flash memory. System upgrade Proceduresmay need to temporarily remove this protection, first importing a gpio,then changing its output s Tate, then updating the code before re-enablingthe write protection. In normal use, GPIO #23 would never is touched,and the kernel would has no need to know about it. Again depending on appropriate hardware documentation, on some systemsuserspace GPIO can is used to determine system confi GUration data Thatstandard Kernels won ' t know about. And for some tasks, simple userspacegpio drivers could is all, the system really needs. Do not abuse SYSFS to CONTROL HARDWARE, which has PROPER KERNEL DRIVERS. Please READ the DOCUMENT NAMED "Drivers-on-gpio.txt" in this documentationdirectory to AVOID reinventing KERNEL WHEELS in Userspace. I MEAN IT. Really. Paths in Sysfs--------------There is three kinds of entries In/sys/class/gpio:-control interfaces used to get USERSPA   CE 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 is write-only:/sys/class/gpio/"Export" ... Userspace may ask the kernel to export control ofa a GPIO to a userspace by writing it number to this file. Example: "echo > Export"Would create a" gpio19 "Nodefor GPIO #19, if that's not requested by kernel code. "Unexport" ... Reverses the effect of exporting to userspace. Example: "echo > Unexport"Would remove a" gpio19 "node exported using the" Export "file. Gpio Signals has paths like/sys/class/gpio/gpio42/(for Gpio #42) and has the following Read/write attributes:/sys/class/gpio/gpion/through export just created the document pieces"direction"... reads as either" in "or" out ". This value maynormally is written. Writing as "out" defaults toinitializing, the value as low.  To ensure glitch freeoperation, values ' low ' and ' high ' may be written toconfigure the GPIO as a output with that initial Value.  Note that this attribute *will not exist* if the KERNELDOESN ' t support changing the direction of a GPIO, Orit was exported By kernel code, didn ' t explicitlyallow userspace to reconfigure this GPIO ' s direction.value"... reads as either 0 (low) or 1 (high). If the Gpiois configured as an output, this value could be written;any nonzero value are treated as high. If the PIN can be configured as interrupt-generating Interruptand if it had been configured to generate interrupts (see th Edescription of "Edge"), you can poll (2) on that file Andpoll (2) would return whenever the interrupt was triggered. Ifyou Use Poll (2), set the events Pollpri and Pollerr. If Youuse Select (2), set the file descriptor in Exceptfds. Afterpoll (2) returns, either Lseek (2) to the beginning of the Sysfsfile and read the new value or close the file and Re-op En itto Read the value. Edge "... reads as either" None "," Rising "," Falling ", or" both ". Write these strings to select the signal edge (s) that would make poll (2) on the "value" file return. This file exists only if the PIN can be configured as Aninterrupt generating input pin. " Active_low "... reads as either 0 (false) or 1 (true). Writeany nonzero value to invert the value attribute Bothfor reading and writing. Existing and Subsequentpoll (2) support configuration via the edge attributefor "rising" and "falling" edges would follow th Issetting. GPIO controllers has paths like/sys/class/gpio/gpiochip42/(for Thecontroller implementing GPIOs starting at #42) and ha ve the followingread-only attributes:/sys/class/gpio/gpiochipn/"base" ... same as N, the first gpio managed by thi S chip "label" ... provided for diagnostics (not always unique) "Ngpio" ... how many GPIOs the manages (N to n + ngpio-1) Board documentation should in most cases cover what GPIOs is used forwhat purposes. However, those numbers is not always stable; GPIOs ona daughtercard might be different depending on the base board being used,or other cards in the stack. In such cases, the need to use thegpiochip nodes (possibly on conjunction with schematics) to determinethe correct GPI O number to use for a given signal. Exporting from Kernel code--------------------------Kernel CodE can explicitly manage exports of GPIOs which have already beenrequested using Gpio_request ():/* Export the Gpio to users Pace */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 *de V, const char *name, struct gpio_desc *desc); After a kernel driver requests a GPIO, it is only being made available inthe Sysfs interface by Gpiod_export (). The driver can control whether thesignal direction change. This helps drivers prevent userspace Codefrom accidentally clobbering important the system state. This explicit exporting can and debugging (by making some kindsof experiments easier), or can provide an always-ther E interface that ' ssuitable for documenting as part of a board the support package. After the Gpio have been exported, Gpiod_export_link () allows creatingsymlinks from elsewhere on Sysfs to the Gpio SYSFS No De. DRivers Canuse this to provide the interface under their own device in SYSFS witha descriptive name. 

GPIO20 Control LED
Zynq>pwd/sys/class/Gpiozynq> Echo ->Export Zynq>lsexport gpio20 gpiochip0 unexportzynq>CD Gpio20zynq>Lsactive_low Device Direction edge subsystem uevent value
Zynq> Echo out>Direction Zynq>Cat Direction outZynq> Echo1>value//output High level ZYNQ> Echo0> value//Output Low level

User-level operation of Gpio under Linux (SYSFS)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.