sysfs gpio is the Linux standard user space operation GPIO interface. Users can use the command line or apply directly the GPIO input/output , high and low level properties and so on... In general, GPIO debugging or simple GPIO applications(such as IR-Cut operation), can be quickly developed through the sysfs gpio interface.
In the kernel opensource's directory run the command:$ make menuconfig in order to access the configuration interface, then select the following options:
Device Drivers --->
-*- GPIO Support --->
[*] /sys/class/gpio/... (sysfs interface)
In general, the kernel's default configuration has been checked for this option.
Before operating on sysfs gpio, we need to apply for it first. It is worth noting that because the application of sysfs gpio will be in the kernel request_gpio. If you try to apply again for sysfs gpio, it will fail because the gpio has already been applied in the kernel.
apply/release GPIO method:
$ cd /sys/class/gpio
$ echo [gpio_num] > export #apply for GPIO
$ echo [gpio_num] > unexport #release GPIO
Notes:gpio_num is the gpio's number 。Formula is:
PA(n) = 0 * 32 + n
PB(n) = 1 * 32 + n
PC(n) = 2 * 32 + n
...
for example:Apply PB(10) = 1 * 32 + 10 = 42
$ echo 42 > export
After applying/sys/class/gpio's directory will have the gpio42 directory。
$ echo 42 > unexport
gpio42's directory will disapear after release. After release, gpio's status will be recovered but it will always be in applying status(level)。
After applying gpio, access the gpioN directory,for example:gpio42,then proceed as follows:
$ echo out > direction #set PB10 as an output mode
$ echo in > direction #set PB10 as an input mode
gpioN's directory has an activelow node,represents the current gpio of a finite level, by default it is 0, which means gpio is low when the input/output value is 0, gpio is high when input/output is 1. Similarly, gpio is high when activelow is 1,or input/output value is 0,and gpio is low when input/output value is 1. We can say that gpio's level = value ^active_low.
$ echo 0 > active_low #value is 0,represents low level. value is 0, high.
$ echo 1 > active_low #value is 1,represents low level. value is 0, high.
gpioN's directory has a value node,representing gpioN's level:if gpio is on input mode,then read the value's content XOR activelow (which will determine the GPIO's level); if gpio is on output mode,then set the gpio's level (value's content XOR activelow).
$ cat value #read level (input mode)
$ echo 0 > value #set level (output mode)