Post by Admin on Aug 14, 2016 23:29:24 GMT
We've edited this submission, OP can reply with any further input or thoughts.
Original
Title: How to use the GPIO of NanoPi M3
Text:
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.
Edited:
Title: Walkthrough: how to use the GPIO of NanoPi M3
Text:
[Admin: in line with our policies we have edited this post to make it more accessible, the OP can reply to accept or request further modifications. The initial section in brackets has been added by the editor.]
[Editor's note.
We've added this brief introduction - scroll down if you just want to see the walkthrough!
What is GPIO?
As Wikipedia defines,
One famous example of a GPIO Header is on the Raspberry Pi:
These headers on the Raspberry Pi are mostly GPIO pins -
Here is an example numbering them -
See full image here
They are an extremely easy way to expand the capabilities of a board! For example, it is possible to communicate with and attach an Arduino or other board, there are a multitude of expansion boards for Raspberry Pi's. Some of these are: music and audio; Arduino boards; servo expansions; ADC (analog) converters to read sensors; GPS; displays; etc etc etc.
And when it comes time to build your own expansion, the method that jiatang has written below is extremely effective, though in this case based on the NanoPi M3.
These can be anything - even as complicated as a full TFT screen.
Why the NanoPi M3
This is a NanoPi M3:
It is a small and extremely capable device, and as you can see, has GPIO pins of its own. What makes it unique is that despite its small form factor and competitive price (just $35), it has an:
And here's how it compares to the Raspberry Pi 2 for size:
Size comparison of Nano Pi m3 with Raspberry Pi 2
Based on this size comparison from their site
Its pinout is very similar to Raspberry Pi GPIO headers (some sites claim that it is fully compatible, but their site doesn't claim this) and from their site the pinout is:
Below, jiatang gives an extremely helpful and detailed Walkthrough on connecting to the GPIO without any driver program! Check it out.]
Walkthrough by jiatang
Introduction
In embedded devices, we often need to control device's GPIO, and we generally write a driver program to do so. Actually there is an easy way to control GPIO, that is “/sys/class/gpio” mode. Using this method, you do not need to write drivers, but instead can use GPIO directly, the kernel is smaller and the development is more convenient.
First, you log into File System through the serial port, to check if there is “/sys/class/gpio” folder or not. If there isn't, exit the folder; in this case you need to add to the sysfs interface when you compile the kernel Device Drivers——> GPIO Support——> /sys/class/gpio/.... [ED: after performing the above compilation, you should now have a /sys/class/gpio folder available and can follow the instructions below.]
Once /sys/class/gpio is available, here is how you can control GPIO pins directly
1) gpio_operation: performed via the /sys/ file interface to operate the IO port GPIO mapping to the file system
2) The control GPIO directory is located in /sys/class/gpio
3) Use the /sys/class/gpio/export file to notify the system which GPIO number(s) need to be exported
4) Use the /sys/class/gpio/unexport file to notify the system to cancel GPIO exports
5) /sys/class/gpio/gpiochipX (where X is a number) directly which saves GPIO register's information of system. This includes the first number base, register name, total PIN number exports a PIN's foot steps of register control PIN [Ed: we PM'd for clarification]
6) First we need to count the PIN number (figure out what PIN number we want to use). PIN number = register cardinal number of control PIN + register digit of control PIN [Ed: we PM'd for clarification, I don't entirely follow...]
7) Write the number to /sys/class/gpio/export
For example: if you want to export PIN 12, you would use the command in the next step: echo 12 > /sys/class/gpio/export; and so on for any other number.
8) echo 12 > /sys/class/gpio/export;
9) We use the direction file to define input/output direction [Remember, GPIO pins can be either input or output pins and are software-defined! In the next line jiatang uses "out" as the direction. The other option would be using the word "in". -ED]
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
Introducing /sys/class/gpio :
Test:Since we want to use sysfs method to operate 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 has booted,enter the root file system;
2)Type these commands in terminal:
The meaning of the PIN numbers:
Example
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.
This should now be fully set up to write to any GPIO Pin on the NanoPi M3.
[To-do: example program writing to a PIN -Ed]
Original
Title: How to use the GPIO of NanoPi M3
Text:
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.
Edited:
Title: Walkthrough: how to use the GPIO of NanoPi M3
Text:
[Admin: in line with our policies we have edited this post to make it more accessible, the OP can reply to accept or request further modifications. The initial section in brackets has been added by the editor.]
[Editor's note.
We've added this brief introduction - scroll down if you just want to see the walkthrough!
What is GPIO?
As Wikipedia defines,
General-purpose input/output (GPIO) is a generic pin on an integrated circuit whose behavior—including whether it is an input or output pin—is controllable by the user at run time. GPIO pins have no predefined purpose, and go unused by default.
One famous example of a GPIO Header is on the Raspberry Pi:
These headers on the Raspberry Pi are mostly GPIO pins -
Here is an example numbering them -
See full image here
They are an extremely easy way to expand the capabilities of a board! For example, it is possible to communicate with and attach an Arduino or other board, there are a multitude of expansion boards for Raspberry Pi's. Some of these are: music and audio; Arduino boards; servo expansions; ADC (analog) converters to read sensors; GPS; displays; etc etc etc.
And when it comes time to build your own expansion, the method that jiatang has written below is extremely effective, though in this case based on the NanoPi M3.
These can be anything - even as complicated as a full TFT screen.
Why the NanoPi M3
This is a NanoPi M3:
It is a small and extremely capable device, and as you can see, has GPIO pins of its own. What makes it unique is that despite its small form factor and competitive price (just $35), it has an:
- 8-core processor!
- Built-in Wifi!
- Built-in Bluetooth 4.0, Dual-mode!
- Software power-off and sleep (which Raspberry Pi does not support.)
- Loads of more features (official site) - (independent review with comments) .
And here's how it compares to the Raspberry Pi 2 for size:
Size comparison of Nano Pi m3 with Raspberry Pi 2
Based on this size comparison from their site
Pin# | Name | Pin# | Name |
1 | SYS_3.3V | 2 | VDD_5V |
3 | I2C0_SDA | 4 | VDD_5V |
5 | I2C0_SCL | 6 | DGND |
7 | GPIOD8/PPM | 8 | UART3_TXD/GPIOD21 |
9 | DGND | 10 | UART3_RXD/GPIOD17 |
11 | UART4_TX/GPIOB29 | 12 | GPIOD1/PWM0 |
13 | GPIOB30 | 14 | DGND |
15 | GPIOB31 | 16 | GPIOC14/PWM2 |
17 | SYS_3.3V | 18 | GPIOB27 |
19 | SPI0_MOSI/GPIOC31 | 20 | DGND |
21 | SPI0_MISO/GPIOD0 | 22 | UART4_RX/GPIOB28 |
23 | SPI0_CLK/GPIOC29 | 24 | SPI0_CS/GPIOC30 |
25 | DGND | 26 | GPIOB26 |
27 | I2C1_SDA | 28 | I2C1_SCL |
29 | GPIOC8 | 30 | DGND |
31 | GPIOC7 | 32 | GPIOC28 |
33 | GPIOC13/PWM1 | 34 | DGND |
35 | SPI2_MISO/GPIOC11 | 36 | SPI2_CS/GPIOC10 |
37 | AliveGPIO3 | 38 | SPI2_MOSI/GPIOC12 |
39 | DGND | 40 | SPI2_CLK/GPIOC9 |
Below, jiatang gives an extremely helpful and detailed Walkthrough on connecting to the GPIO without any driver program! Check it out.]
Walkthrough by jiatang
Introduction
In embedded devices, we often need to control device's GPIO, and we generally write a driver program to do so. Actually there is an easy way to control GPIO, that is “/sys/class/gpio” mode. Using this method, you do not need to write drivers, but instead can use GPIO directly, the kernel is smaller and the development is more convenient.
First, you log into File System through the serial port, to check if there is “/sys/class/gpio” folder or not. If there isn't, exit the folder; in this case you need to add to the sysfs interface when you compile the kernel Device Drivers——> GPIO Support——> /sys/class/gpio/.... [ED: after performing the above compilation, you should now have a /sys/class/gpio folder available and can follow the instructions below.]
Once /sys/class/gpio is available, here is how you can control GPIO pins directly
1) gpio_operation: performed via the /sys/ file interface to operate the IO port GPIO mapping to the file system
2) The control GPIO directory is located in /sys/class/gpio
3) Use the /sys/class/gpio/export file to notify the system which GPIO number(s) need to be exported
4) Use the /sys/class/gpio/unexport file to notify the system to cancel GPIO exports
5) /sys/class/gpio/gpiochipX (where X is a number) directly which saves GPIO register's information of system. This includes the first number base, register name, total PIN number exports a PIN's foot steps of register control PIN [Ed: we PM'd for clarification]
6) First we need to count the PIN number (figure out what PIN number we want to use). PIN number = register cardinal number of control PIN + register digit of control PIN [Ed: we PM'd for clarification, I don't entirely follow...]
7) Write the number to /sys/class/gpio/export
For example: if you want to export PIN 12, you would use the command in the next step: echo 12 > /sys/class/gpio/export; and so on for any other number.
8) echo 12 > /sys/class/gpio/export;
9) We use the direction file to define input/output direction [Remember, GPIO pins can be either input or output pins and are software-defined! In the next line jiatang uses "out" as the direction. The other option would be using the word "in". -ED]
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
Introducing /sys/class/gpio :
Test:Since we want to use sysfs method to operate 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 has booted,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
The meaning of the PIN numbers:
- gpio.0 means GPIOA
- gpio.1 means GPIOB
- the other pins are the same.
Example
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.
This should now be fully set up to write to any GPIO Pin on the NanoPi M3.
[To-do: example program writing to a PIN -Ed]