Post by jiatang on Aug 19, 2016 8:38:14 GMT
In embedded devices,we often need to control device's GPIO,and we general write one driver program. Actually there is one easy way to control GPIO,that is “/sys/class/gpio” mode. Using this method , you donot need to write drivers, use GPIO directly, the kernel is smaller and
the development is more convenient.
First ,you log File System through the serial port ,to check if there is “/sys/class/gpio” folder or not. If there isn't exit the folder,you need increase sysfs interface when you compile the kernel Device Drivers——> GPIO Support——> /sys/class/gpio/....
1、gpio_operation:Via /sys/ file interface to Operat IO port GPIO mapping to file system;
2、Control GPIO directory located in /sys/class/gpio;
3、Use /sys/class/gpio/export file to notify system which GPIO number need to be exported;
4、Use /sys/class/gpio/unexport to notify system cancel exports GPIO
5、/sys/class/gpio/gpiochipX directly which saves GPIO register's information of system,which includes the first number base 、register name、total PIN number exports a PIN's foot steps of register control PIN;
(X means number)
6、First we need to count PIN number,PIN number = register cardinal number of control PIN + register digit of control PIN
7、Wite number to /sys/class/gpio/export ,example if you want to export 12 PIN,you can use the command which will be introduced ;
8:echo 12 > /sys/class/gpio/export;
9、We can use direction file to define input/output direction;
10:echo out > direction;
11、There are accepted parameters:in, out, high, low,and high/low all set the direction as output,and set value as corresponding 1/0;
12、“value” file is port's numerical,that is 1 or 0;
13:echo 1 & > value.
/sys/class/gpio introductions:
Test:Since we want to use sysfs method to operat GPIO,we need to find out the PIN number that corresponds to the kernel(each GPIO number have its only number in kernel),you can use the following method to check out kernel number:
1)After NanoPi M3 booting,enter the root file system;
2)Type these commands in terminal:
# cd /sys/class/gpio
# for i in gpiochip* ; do echo `cat $i/label`: `cat $i/base` ; done
nxp-gpio.0: 0
nxp-gpio.4: 128
nxp-gpio.5: 160
nxp-gpio.1: 32
nxp-gpio.2: 64
nxp-gpio.3: 96
Actually gpio.0 is mean GPIOA,gpio.1 is mean GPIOB,and the other is the same.
If we want to use GPIO30 PIN of NanoPi M3 in your application,the PIN number can be expressed as 64+30=94,that is mean we use 94 PIN GPIO. How to use the GPIO of NanoPi M3
the development is more convenient.
First ,you log File System through the serial port ,to check if there is “/sys/class/gpio” folder or not. If there isn't exit the folder,you need increase sysfs interface when you compile the kernel Device Drivers——> GPIO Support——> /sys/class/gpio/....
1、gpio_operation:Via /sys/ file interface to Operat IO port GPIO mapping to file system;
2、Control GPIO directory located in /sys/class/gpio;
3、Use /sys/class/gpio/export file to notify system which GPIO number need to be exported;
4、Use /sys/class/gpio/unexport to notify system cancel exports GPIO
5、/sys/class/gpio/gpiochipX directly which saves GPIO register's information of system,which includes the first number base 、register name、total PIN number exports a PIN's foot steps of register control PIN;
(X means number)
6、First we need to count PIN number,PIN number = register cardinal number of control PIN + register digit of control PIN
7、Wite number to /sys/class/gpio/export ,example if you want to export 12 PIN,you can use the command which will be introduced ;
8:echo 12 > /sys/class/gpio/export;
9、We can use direction file to define input/output direction;
10:echo out > direction;
11、There are accepted parameters:in, out, high, low,and high/low all set the direction as output,and set value as corresponding 1/0;
12、“value” file is port's numerical,that is 1 or 0;
13:echo 1 & > value.
/sys/class/gpio introductions:
Test:Since we want to use sysfs method to operat GPIO,we need to find out the PIN number that corresponds to the kernel(each GPIO number have its only number in kernel),you can use the following method to check out kernel number:
1)After NanoPi M3 booting,enter the root file system;
2)Type these commands in terminal:
# cd /sys/class/gpio
# for i in gpiochip* ; do echo `cat $i/label`: `cat $i/base` ; done
nxp-gpio.0: 0
nxp-gpio.4: 128
nxp-gpio.5: 160
nxp-gpio.1: 32
nxp-gpio.2: 64
nxp-gpio.3: 96
Actually gpio.0 is mean GPIOA,gpio.1 is mean GPIOB,and the other is the same.
If we want to use GPIO30 PIN of NanoPi M3 in your application,the PIN number can be expressed as 64+30=94,that is mean we use 94 PIN GPIO. How to use the GPIO of NanoPi M3