How does embedded Linux operate GPIO?

Author: Liu Kai
Link : https://www.zhihu.com/question/19704852/answer/19760467
Source: Zhihu The
copyright belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

If it is on the Linux kernel that has been adapted, then I believe that there is a completed gpiochip, which can be seen in the user space /sys/class/gpio directory, such as:
export
gpiochip0/
gpiochip32/
gpiochip64/
gpiochip96/
unexport

Then check the manual to see which GPIO you need to use, for example:

If you want to use GPIO1_20
then the GPIO Number is 1 x 32 + 20 = 54

There are two cases of use:
1. User space:
echo 54 > export
In this way, a gpio54 folder will be generated in this /sys/class/gpio directory.
There are two files that need to be used in the folder:
direction is used to configure the input (in) or output (out)
value If the GPIO is configured as an input, then you can view the current potential of the GPIO through cat value; if it is configured as an output, you can specify this GPIO port through echo 1/0 > value output level.

2. Kernel space (driver):
#include <linux/gpio.h>
gpio_request_one(54, GPIOF_INIT_HIGH, "gpio1_20")
here is configured as output, default high level, alias (label) is gpio1_20 - just for you Take a name for the IO port.
gpio_request_one(54, GPIOF_IN, "gpio1_20")
This is configured as an input.
Don't forget to free
gpio_free(54) after use;
see the specific GPIO interface: Linux/Documentation/gpio.txt

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811655&siteId=291194637