Power & Source of Big Ideas

How to use the GPIO of NanoPi M3

Moderators: chensy, FATechsupport

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.
jjm wrote:
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.


Good post but you make some mistake in explanation example:
If you want using GPIO30 you must know which line it is. You give correct explanation about a pin number(its a sum) but be aware, GPIO30 in line B according to documentation, so it's must looks like this(on M3):
- Physical iron pin on board 13
- Name GPIO30(actually is GPIOB30) mapped to this pin
- B -line starts form 32, so the kernel gpio pin will be 62.

Another thing about value setting
echo 1 & > value

why do you use '&' here. It not works for me, and regular doc say that you must set just 0 or 1 value.

Did you already work with i2c & spi with same approach?

Who is online

In total there are 26 users online :: 0 registered, 0 hidden and 26 guests (based on users active over the past 5 minutes)
Most users ever online was 5185 on Wed Jan 22, 2020 1:44 pm

Users browsing this forum: No registered users and 26 guests