Operate GPIO through sysfs under linux

Operate GPIO through sysfs under linux

The operation of GPIO in embedded devices is the most basic operation. The general practice is to write a separate driver, which is the case with most examples on the web. In fact, there is a general GPIO operation interface under Linux, which is the "/sys/class/gpio" method that I want to introduce.

First, see if there is a folder "/sys/class/gpio" in the system. If not, please add Device Drivers-> GPIO Support-> /sys/class/gpio/… (sysfs interface) when compiling the kernel. Instructions for use of /sys/class/gpio:

1. The directory for controlling GPIO is located in /sys/class/gpio

2. The /sys/class/gpio/export file is used to notify the system of the GPIO pin number that needs to be exported for control

3. /sys/class/gpio/unexport is used to notify the system to cancel the export

4. The /sys/class/gpio/gpiochipX directory saves the information of the GPIO registers in the system, including the starting number base of each register control pin, the register name, and the total number of pins. The operation steps to derive a pin

5. First calculate the pin number, pin number = the register base of the control pin + the number of bits of the control pin register

6. Write this number to /sys/class/gpio/export, such as pin 12, echo 12 > /sys/class/gpio/export, after the command is successful, the /sys/class/gpio/gpio12 directory will be generated, if not The corresponding directory appears, stating that this pin is not exportable:

7. The direction file defines the input and input direction, which can be defined as output through the following commands. The parameters accepted by direction are: in, out, high, low. high/low set the direction as output at the same time

8. The value file is the value of the port, which is 1 or 0.

 

Taking DM8127 as an example to illustrate how to use sysfs interface to control GPIO

1. GPIO number and grouping of DM8127

1)GP0[0-31]、GP1[0-31]、GP2[0-31]、GP3[0-31];

2. Configure the function of IO

1) Configure the IO port function pins, refer to Table 5-13. PINCNTLx Registers MUXMODE Functions;

2) It can be configured in uboot or in the kernel devices.c file;

 

3. Operate /sys/class/gpio directory information

1) Information in the default directory

/sys/class/gpio # ls

export      gpio38      gpiochip32  gpiochip96

gpio35      gpiochip0   gpiochip64  unexport

2) Each gpiochipx has 32 pins, which exactly matches the number of GPx above;

4. We set the GP1[26] pin as output

1) The base address number corresponding to GP1[26] corresponds to gpiochip32. The corresponding total number is 32 + 26 = 58

2) Export the pins that need to be controlled

echo 58 > /sys/class/gpio/export

3) Set the output/input state of the pin, and output the input high and low levels

echo out > /sys/class/gpio/gpio58/direction 

echo 1 > /sys/class/gpio/gpio58/value 

echo 0 > /sys/class/gpio/gpio58/value

5. In addition, you can configure the input, interrupt trigger mode and so on.

Guess you like

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