GPIO operation under linux

1. Kernel configuration

It can be configured through the graphical interface of make menuconfig

Device Drivers --->
       GPIO Support --->
              【*】 /sys/class/gpio/...(sysfs interface)

Or: modify the configuration file arch/arm/configs/imx6_defconfig

CONFIG_GENERIC_GPIO=y

2-pin configuration

Configure pins in the kernel, take GPIO3 23 as an example: arch/arm/mach-mx6/board-mx6q_sabreauto.c

static iomux_v3_cfg_t mx6q_sabreauto_pads[] = {

.....
MX6Q_PAD_EIM_D23__GPIO_3_23, // brank 3 gpio23
.....

}

3 User level control

To control the GPIO through sysfs, first access the /sys/class/gpio directory, and write the GPIO number to the export file, so that the operation interface of the GPIO is exposed from the kernel space to the user space. The operation interface of the GPIO includes direction and value, etc.

Check out the catalog:

root@freescale /App$ ls /sys/class/gpio/
export       gpiochip0    gpiochip160  gpiochip32   gpiochip96
gpio87       gpiochip128  gpiochip192  gpiochip64   unexport

3.1 Calculation

nr=(P -1)* 32 + N

(3-1)*32 +23 = 87

3.2 Export

/sys/class/gpio# echo 87> export

After exporting, you can see that the files in this directory are as follows:

root@freescale /App$ ls /sys/class/gpio/gpio87/
active_low  direction   edge        power       subsystem   uevent      value

If it cannot be exported, the GPIO pin may be occupied, for example, it is occupied by other drivers

3.3 Setting the direction

/sys/class/gpio/# echo out > gpio87/direction

3.4 Viewing directions

root@freescale /App$ cat /sys/class/gpio/gpio87/direction 
out

3.5 Set the output

root@freescale /App$ echo 1 /sys/class/gpio/gpio87/value 
1 /sys/class/gpio/gpio87/value
root@freescale /App$ echo 0 /sys/class/gpio/gpio87/value 
0 /sys/class/gpio/gpio87/value

3.6 View output value

root@freescale /App$ cat /sys/class/gpio/gpio87/value 
0

3.7 Cancel export

root@freescale /App$ echo 87 >/sys/class/gpio/unexport  
root@freescale /App$ ls /sys/class/gpio/               
export       gpiochip128  gpiochip192  gpiochip64   unexport
gpiochip0    gpiochip160  gpiochip32   gpiochip96

refer to

https://developer.ridgerun.com/wiki/index.php?title=How_to_use_GPIO_signals#References
https://blog.csdn.net/u014213012/article/details/53140781
linux-3.0.35/documentation/gpio.txt

Guess you like

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