Difference between revisions of "WiringNP: NanoPi NEO/NEO2/Air GPIO Programming with C"

From FriendlyELEC WiKi
Jump to: navigation, search
(WiringNP的安装)
(Nov-14-2018)
 
(44 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[WiringPi for NanoPi/zh|查看中文]]
+
[[WiringNP: NanoPi NEO/NEO2/Air GPIO Programming with C/zh|查看中文]]
  
 
==Introduction to WiringPi==
 
==Introduction to WiringPi==
Line 6: Line 6:
  
 
==Introduction to WiringNP==
 
==Introduction to WiringNP==
The wiringPi library was initially developed for BCM2835, and later migrated to Allwinner H3 and renamed as WiringNP. Now the WiringNP works with FriendlyElec's [[NanoPi M1]] and [[NanoPi NEO]]. The first version was made by [https://github.com/wertyzp wertyzp].<br />
+
The wiringPi library was initially developed for BCM2835, and later migrated to Allwinner H3 and renamed as WiringNP. Now the WiringNP works with FriendlyElec's [[NanoPi M1]], [[NanoPi NEO]] and [[NanoPi NEO2]]. The first version was made by [https://github.com/wertyzp wertyzp]. FriendlyElec developed a version for Allwinner H5 based NanoPi NEO2.<br />
 
<br />
 
<br />
 
Here is WiringNP's project page: https://github.com/friendlyarm/WiringNP<br />
 
Here is WiringNP's project page: https://github.com/friendlyarm/WiringNP<br />
  
==Install WiringNP==
+
==Install WiringNP on NEO/NEO2==
 
Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:<br />
 
Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:<br />
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
git clone https://github.com/friendlyarm/WiringNP
 
git clone https://github.com/friendlyarm/WiringNP
 
cd WiringNP/
 
cd WiringNP/
 +
chmod 755 build
 +
./build
 +
</syntaxhighlight>
 +
 +
==Install WiringNP on NanoPi M1==
 +
Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:<br />
 +
<syntaxhighlight lang="bash">
 +
git clone https://github.com/friendlyarm/WiringNP
 +
cd WiringNP/
 +
git checkout nanopi-m1
 
chmod 755 build
 
chmod 755 build
 
./build
 
./build
Line 24: Line 34:
 
gpio readall
 
gpio readall
 
</syntaxhighlight>
 
</syntaxhighlight>
If your installation is successful the following messages will show up:
+
If your installation is successful the following messages will show up. Here is the message list for NEO2:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
root@FriendlyARM:~# gpio readall
 
root@FriendlyARM:~# gpio readall
+-----+-----+----------+------+---+-NanoPI M1+---+------+----------+-----+-----+
+
  +-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
 
  | BCM | wPi |  Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 
  | BCM | wPi |  Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 
  +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 
  +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
  |    |    |    3.3v |      |  |  1 || 2  |  |      | 5v       |    |    |
+
  |    |    |    3.3V |      |  |  1 || 2  |  |      | 5V       |    |    |
  |  12 |  8 |   SDA.0 | ALT5 | 0 |  3 || 4  |  |      | 5V      |    |    |
+
  |  12 |  8 | GPIOA12 | OFF | 0 |  3 || 4  |  |      | 5V      |    |    |
  |  11 |  9 |   SCL.0 | ALT5 | 0 |  5 || 6  |  |      | 0v      |    |    |
+
  |  11 |  9 | GPIOA11 | OFF | 0 |  5 || 6  |  |      | 0v      |    |    |
  | 203 |  7 |   GPIO.7 |  OUT | 0 |  7 || 8  | 0 | ALT5 | TxD3    | 15  | 198 |
+
  | 203 |  7 | GPIOG11 |  OUT | 1 |  7 || 8  | 0 | OFF  | GPIOG6  | 15  | 198 |
  |    |    |      0v |      |  |  9 || 10 | 0 | ALT5 | RxD3    | 16  | 199 |
+
  |    |    |      0v |      |  |  9 || 10 | 0 | OFF  | GPIOG7  | 16  | 199 |
  |  0 |  0 |     RxD2 | ALT5 | 0 | 11 || 12 | 0 | OFF  | GPIO.1   | 1  | 6  |
+
  |  0 |  0 |   GPIOA0 | OFF | 0 | 11 || 12 | 0 | OFF  | GPIOA6   | 1  | 6  |
  |  2 |  2 |     TxD2 |  OFF | 0 | 13 || 14 |  |      | 0v      |    |    |
+
  |  2 |  2 |   GPIOA2 |  OFF | 0 | 13 || 14 |  |      | 0v      |    |    |
  |  3 |  3 |     CTS2 |  OFF | 0 | 15 || 16 | 0 | OFF  | GPIO.4   | 4  | 200 |
+
  |  3 |  3 |   GPIOA3 |  OFF | 0 | 15 || 16 | 0 | OFF  | GPIOG8   | 4  | 200 |
  |    |    |    3.3v |      |  | 17 || 18 | 0 | OFF  | GPIO.5   | 5  | 201 |
+
  |    |    |    3.3v |      |  | 17 || 18 | 0 | OFF  | GPIOG9   | 5  | 201 |
  |  64 |  12 |     MOSI | ALT4 | 0 | 19 || 20 |  |      | 0v      |    |    |
+
  |  64 |  12 |   GPIOC0 | OFF | 0 | 19 || 20 |  |      | 0v      |    |    |
  |  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT5 | RTS2    | 6  | 1  |
+
  |  65 |  13 |   GPIOC1 | OFF | 0 | 21 || 22 | 0 | OFF  | GPIOA1  | 6  | 1  |
  |  66 |  14 |    SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 10  | 67  |
+
  |  66 |  14 |  GPIOC2 |  OFF | 0 | 23 || 24 | 0 | OFF  | GPIOC3   | 10 | 67 |
|    |    |      0v |      | 25 || 26 | 0 | OFF  | GPIO.11  | 11  | 17  |
+
|  19 |  30 |    SDA.1 | ALT4 | 0 | 27 || 28 | 0 | ALT4 | SCL.1    | 31  | 18  |
+
|  20 |  21 |  GPIO.21 |  OFF | 0 | 29 || 30 |  |      | 0v      |    |    |
+
|  21 |  22 |  GPIO.22 |  OFF | 0 | 31 || 32 | 0 | OFF  | RTS1    | 26  | 7  |
+
|  8 |  23 | GPIO.23 | OFF | 0 | 33 || 34 |  |      | 0v      |    |    |
+
|  16 |  24 |  GPIO.24 | ALT4 | 0 | 35 || 36 | 0 | OFF  | CTS1    | 27  | 13  |
+
|   9 25 | GPIO.25 |  OFF | 0 | 37 || 38 | 0 | OFF  | TxD1    | 28  | 15  |
+
|    |    |      0v |      |  | 39 || 40 | 0 | ALT4 | RxD1    | 29  | 14 |
+
 
  +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 
  +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 
  | BCM | wPi |  Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
 
  | BCM | wPi |  Name  | Mode | V | Physical | V | Mode | Name    | wPi | BCM |
  +-----+-----+----------+------+---+-NanoPI M1+---+------+----------+-----+-----+
+
  +-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
  
  +-----+----NanoPI M1 Debug UART---+----+
+
  +-----+----NanoPi NEO/NEO2 Debug UART-+----+
 
  | BCM | wPi |  Name  | Mode | V | Ph |
 
  | BCM | wPi |  Name  | Mode | V | Ph |
 
  +-----+-----+----------+------+---+----+
 
  +-----+-----+----------+------+---+----+
|    |    |      0v |      |  | 41 |
+
  |  4 |  17 GPIOA4 | ALT5 | 0 | 37 |
|    |    |      5v |      |  | 42 |
+
  |  5 |  18 GPIOA5 | ALT4 | 0 | 38 |
  |  4 |  32 GPIO.4 | ALT5 | 0 | 43 |
+
  |  5 |  33 GPIO.5 | ALT5 | 0 | 44 |
+
 
  +-----+-----+----------+------+---+----+
 
  +-----+-----+----------+------+---+----+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==NanoPi M1的GPIO在WiringNP上编码方式==
+
<!---
在上一章节gpio readall命令得到的列表中,Physical这一列显示的NanoPI M1的物量引脚编号,从1~40,<br />
+
==Map NanoPi M1's GPIO Pins to WiringNP's Indexes==
wPi 这一列代表的是 WiringNP 操作这个引脚时需要使用的编号,这种编号方式非常方便编程,下图是NanoPi M1的所有引脚编码:<br />
+
In the previous section we got a list by running "gpio readall". In the list the "Physical" column stands for the M1's GPIO pins' indexes from 1 to 40<br />
 +
The "wPi" column stands for the indexes when users use WiringNP to access GPIO pins. Here is the numbering scheme for all NanoPi M1's GPIO pins to wPi's indexes:<br />
 
[[File:WiringNP-GPIO-ALL.jpg|frameless|600px]]
 
[[File:WiringNP-GPIO-ALL.jpg|frameless|600px]]
 +
--->
  
==WiringNP示例==
+
==Code Sample with WiringNP==
将一个[[Matrix - LED]]通过连接至NanoPi M1,像下图这样:<br>
+
Here is a setup to connect a [[Matrix - LED]] module to a NanoPi NEO2:<br>
[[File:Matrix-LED_nanopi_m1.jpg|frameless|300px|Matrix-LED_nanopi_m1]]
+
[[File:WiringNP-LED-Demo.jpg|frameless|300px|WiringNP-LED-Demo]]<br />
 
+
Here is the connection:<br/>
其中,引脚的连接对应如下:
+
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
Line 85: Line 86:
 
|}
 
|}
  
接下来我们用WiringNP实现一个LED闪烁的例子, <br />
+
We will show how to use WiringNP to control a LED module.<br />
首先通过命令gpio readall可以看到物理引脚Pin7所对应的wPi编号是7,所以接下来在代码中使用数字7就可以操作这个引脚。 <br />
+
In the following code samples index 7 stands for Pin7. We will use "7" to access this physical pin.<br />
  
=== C语言示例 ===
+
=== C Source Code ===
创建一个C源文件:
+
Make a C source file:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
vi test.c
 
vi test.c
 
</syntaxhighlight>
 
</syntaxhighlight>
然后键入如下代码:
+
Type the following lines:
 
<syntaxhighlight lang="c">
 
<syntaxhighlight lang="c">
 
#include <wiringPi.h>
 
#include <wiringPi.h>
Line 109: Line 110:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
编译test.c并运行:
+
Compile and run "test.c":
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
gcc -Wall -o test test.c -lwiringPi -lpthread
 
gcc -Wall -o test test.c -lwiringPi -lpthread
./test  
+
sudo ./test  
 
</syntaxhighlight>
 
</syntaxhighlight>
看到LED灯一闪一闪的,就表示成功了。
+
You can see the LED is blinking.
  
 +
===PWM Code Sample===
 +
The PWM pin in NanoPi NEO/NEO2 is multiplexing which can be set to either PWM or SerialPort0. To set this pin to PWM you need to run "sudo npi-config" and enter the "Advanced Options" menu to Enable/Disable PWM. Note: after PWM is enabled SerialPort0 will not be accessed and you need to login your board via SSH.<br />
 +
<br />
 +
Here is a hardware setup to connect a [[Matrix - Buzzer]] to a NanoPi NEO2:<br />
 +
[[File:WiringNP-PWM-Demo.jpg|frameless|300px|WiringNP-PWM-Demo]]<br />
 +
 +
Create a source file in C:<br/>
 +
<syntaxhighlight lang="bash">
 +
vi pwmtest.c
 +
</syntaxhighlight>
 +
Type the following code:
 +
<syntaxhighlight lang="c">
 +
#include <wiringPi.h>
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <stdint.h>
 +
int main (void)
 +
{
 +
  int l ;
 +
  printf ("PWM test program\n") ;
 +
 +
  //using wiringPi Pin Number
 +
  int pin = 18;             
 +
  if (wiringPiSetup () == -1)
 +
    exit (1) ;             
 +
             
 +
  /*
 +
  //using Physical Pin Number
 +
  int pin = 38;             
 +
  if (wiringPiSetupPhys() == -1)
 +
    exit (1) ;                 
 +
  */                           
 +
             
 +
  /*
 +
  //using BCM Pin Number
 +
  int pin = 5;         
 +
  if (wiringPiSetupGpio() == -1)
 +
    exit (1);                 
 +
  */                           
 +
                           
 +
  pinMode (pin, PWM_OUTPUT);
 +
  for (;;) {                     
 +
    for (l = 0 ; l < 1024 ; ++l) {
 +
      pwmWrite (pin, l) ;       
 +
      delay (1) ;       
 +
    }                             
 +
    for (l = 1023 ; l >= 0 ; --l) {
 +
      pwmWrite (pin, l) ;         
 +
      delay (1) ;       
 +
    }           
 +
  }       
 +
  return 0 ;
 +
}
 +
</syntaxhighlight>
 +
Compile the pwmtest.c file and run the generated executable:
 +
<syntaxhighlight lang="bash">
 +
gcc -Wall -o pwmtest pwmtest.c -lwiringPi -lpthread
 +
./pwmtest
 +
</syntaxhighlight>
 +
Connect a PWM beeper to a NEO/NEO2 and the beeper will sound.
  
=== Shell示例 ===
+
=== Shell Script ===
创建一个shell脚本:
+
Make a shell script:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
vi test.sh
 
vi test.sh
 
</syntaxhighlight>
 
</syntaxhighlight>
然后键入如下代码:
+
Type the following lines:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
LED=7
 
LED=7
Line 133: Line 194:
 
done
 
done
 
</syntaxhighlight>
 
</syntaxhighlight>
运行脚本:
+
Run the script:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
source test.sh
+
sudo source test.sh
 
</syntaxhighlight>
 
</syntaxhighlight>
会看到LED闪烁了。
+
You can see the LED is blinking.
  
 +
==Regular WiringNP APIs==
 +
Note:most of the descriptions for the following functions are from http://wiringpi.com/. To get more details refer to http://wiringpi.com/.
  
==WiringNP 常用API速查==
+
=== Initialization ===
=== 初始化函数 ===
+
 
==== wiringPiSetup (void) ====
 
==== wiringPiSetup (void) ====
该函数初始化wiringPi,并假定程序将使用wiringPi的管脚定义图。具体管脚映射,可以通过gpio readall命令来查看。<br />
+
This function initializes wiringPi and uses the pin specifications defined by wiringPi. To check detailed pin specifications you can command "gpio readall".<br/>
该函数需要root权限。
+
You need to call this function as root.
  
 
==== int wiringPiSetupGpio(void) ====
 
==== int wiringPiSetupGpio(void) ====
该函数和wiringPiSetup函数类似,区别在于假定程序使用的是CPU的GPIO管脚定义,而没有重新映射。<br />
+
This function is similar to the wiringPiSetup function. The only difference is that this function assumes the GPIO's pin specifications are the ones defined by CPU and haven't been redefined.<br/>
该函数需要root权限。
+
You need to call this function as root.
  
 
==== int wiringPiSetupPhys (void) ====
 
==== int wiringPiSetupPhys (void) ====
该函数和wiringPiSetup函数类似,区别在于不允许程序使用物理管脚定义,仅支持P1接口。<br />
+
This function is similar to the wiringPiSetup function. The only difference is that it only supports P1's interface and doesn't support physical pin specifications.<br/>
该函数需要root权限。
+
You need to call this function as root.
  
 
==== int wiringPiSetupSys (void) ====
 
==== int wiringPiSetupSys (void) ====
该函数初始化wiringPi,使用/sys/class/gpio接口,而不是直接通过操作硬件来实现。<br />
+
This function initializes wiringPi and access hardware by using "/sys/class/gpio" APIs rather than commanding hardware directly.<br/>  
该函数可以使用非root权限用户,在此种模式下的管脚号是CPU的GPIO管脚号,和wiringPiSetupGpio函数类似。<br />
+
This function can be called by any users. The GPIO pin numbers used by this function should be the ones defined by CPU. This is similar to the rules followed by the wiringPiSetupGpio function.<br/>  
在此种模式下,在运行程序前,您需要通过/sys/class/gpio接口导出要使用的管脚。<br />
+
Before calling this function make sure to call "/sys/class/gpio" APIs to list the pins you want to access.<br/>  
你可以在一个独立的shell脚本中来导出将要使用的管脚,或者使用系统的system()函数来调用GPIO命令。
+
You can list the pins you want to access in a separate shell script. Or you can command GPIO pins by calling "system()".
  
=== 核心函数 ===
+
=== Core Functions ===
 
==== void pinMode (int pin, int mode) ====
 
==== void pinMode (int pin, int mode) ====
使用该函数可以将某个引脚配置为INPUT(输入) OUTPUT(输出)PWM_OUTPUT(脉冲输出)或者GPIO_CLOCK(时钟)。<br />
+
This function sets a pin to INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.<br/>  
在Sys模式下,这个函数没有影响。<br />
+
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not make change that pin's mode.<br/>
你可以通过调用GPIO命令在shell脚本中来设置管脚的模式。
+
You can set a pin's mode in a shell script.
  
 
==== void pullUpDnControl (int pin, int pud) ====
 
==== void pullUpDnControl (int pin, int pud) ====
使用该函数可以设置指定管脚使用上拉或者下拉电阻模式,通常当需要管脚作为输入引脚时,需要设定此项。<br />
+
This function sets a pin to the pull-up or pull-down resistor mode when that pin is set to INPUT.<br />
不同于Arduino,CPU有内部上拉和下拉电阻这两种模式。<br />
+
Unlike the Arduino the NanoPi M1 has both the pull-up an pull-down resistor modes.<br />
参数pud可以为PUD_OFF(无上拉或下拉电阻)、PUD_DOWN(内部下拉至地线)或者PUD_UP(内部上拉至3.3V)。<br />
+
The parameter pud can be: PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v).<br />
在NanoPi M1上,内部上拉和下拉电阻有接近100KΩ。<br />
+
The internal pull up/down resistors have a value of approximately 100KΩ on the NanoPi M1.<br />
该函数在Sys模式下无作用。<br />
+
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not make change that pin's mode.<br />
如果你需要激活上拉或下拉电阻的话,在启动程序前,可以通过在脚本中调用GPIO命令来实现。
+
If you need to activate a pull-up/pull-down resistor you can do it by using GPIO commands in a script before you start your program.
  
 
==== void digitalWrite (int pin, int value) ====
 
==== void digitalWrite (int pin, int value) ====
使用该函数可以向指定的管脚写入HIGH(高)或者LOW(低),写入前,需要将管脚设置为输出模式。<br />
+
This function writes either HIGH or LOW to a pin. Before you call this function to access a pin make sure you have set that pin to OUTPUT.<br />
wiringPi将任何的非0值作为HIGH(高)来对待,因此,0是唯一能够代表LOW(低)的数值。
+
WiringPi treat any non-0 value to HIGH and 0 is the only value that is treated as LOW.
  
 
==== void pwmWrite (int pin, int value) ====
 
==== void pwmWrite (int pin, int value) ====
使用该函数可以将值写入指定管脚的PWM寄存器中,可设置的值为0~1024,其他PWM讴备可能有不同的PWM范围。<br />
+
This function writes a value to a PWM register. The value can be any one in 0~1024. Different PWM devices may take different value ranges.<br />
当在Sys模式时,该函数不可用来控制板上PWM。<br />
+
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not be able to write a value to that pin.<br />
  
 
==== digitalRead(int pin); ====
 
==== digitalRead(int pin); ====
使用该函数可以读取指定管脚的值,读取到的值为HIGH(1)或者LOW(0),该值取决于该管脚的逻辑电平的高低。<br />
+
This function reads a pin's value. The value is either HIGH(1) or LOW(0).<br />
  
 
==== analogRead (int pin) ; ====
 
==== analogRead (int pin) ; ====
该函数返回所指定的模拟输入管脚的值。你需要添加额外的模拟模块来使用该函数,比如Gertboard,quick2Wire模拟板等。<br />
+
This function returns a value it reads from an analog pin. To effectively call this function make sure you have connected a working analog device such as Gertboard and quick2Wire to your board.<br />
  
 
==== analogWrite (int pin, int value) ; ====
 
==== analogWrite (int pin, int value) ; ====
该函数将指定的值写入到指定的管脚。你需要添加额外的模拟模块来使用该函数,比如Gertboard等。<br />
+
This function writes a value to an analog pin. To effectively call this function make sure you have connected a working analog device such as Gertboard to your board.<br/>
 +
 
 +
==Update Log==
 +
===December-31-2016===
 +
* Released English Version
 +
 
 +
===May-16-2017===
 +
* Updated section 2 by adding support for H5 based NEO2
 +
* Added section 4 by adding support for H5 based NEO2
 +
* Added section 5.2: code sample on how to access a PWM device
 +
 
 +
===May-17-2017===
 +
* Updated section 4 by adding installation steps for M1
 +
 
 +
===May-24-2017===
 +
* Updated section 5.2 by adding a PWM code sample for NEO2
 +
 
 +
===Nov-14-2018===
 +
* Updated section 6

Latest revision as of 05:59, 14 November 2018

查看中文

1 Introduction to WiringPi

The wiringPi library was initially developed by Gordon Henderson in C. It contains libraries to access GPIO, I2C, SPI, UART, PWM and etc.
The wiringPi library contains various libraries, header files and a commandline utility:gpio. The gpio utility can be used to read and write GPIO pins.

2 Introduction to WiringNP

The wiringPi library was initially developed for BCM2835, and later migrated to Allwinner H3 and renamed as WiringNP. Now the WiringNP works with FriendlyElec's NanoPi M1, NanoPi NEO and NanoPi NEO2. The first version was made by wertyzp. FriendlyElec developed a version for Allwinner H5 based NanoPi NEO2.

Here is WiringNP's project page: https://github.com/friendlyarm/WiringNP

3 Install WiringNP on NEO/NEO2

Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:

git clone https://github.com/friendlyarm/WiringNP
cd WiringNP/
chmod 755 build
./build

4 Install WiringNP on NanoPi M1

Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:

git clone https://github.com/friendlyarm/WiringNP
cd WiringNP/
git checkout nanopi-m1
chmod 755 build
./build

4.1 Verify WiringNP

The WiringNP library contains a set of gpio commands. Users can use them to access the GPIO pins on a nano board. You can verify your WiringNP by running the following command:

gpio readall

If your installation is successful the following messages will show up. Here is the message list for NEO2:

root@FriendlyARM:~# gpio readall
  +-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
 | BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
 +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 |     |     |     3.3V |      |   |  1 || 2  |   |      | 5V       |     |     |
 |  12 |   8 |  GPIOA12 |  OFF | 0 |  3 || 4  |   |      | 5V       |     |     |
 |  11 |   9 |  GPIOA11 |  OFF | 0 |  5 || 6  |   |      | 0v       |     |     |
 | 203 |   7 |  GPIOG11 |  OUT | 1 |  7 || 8  | 0 | OFF  | GPIOG6   | 15  | 198 |
 |     |     |       0v |      |   |  9 || 10 | 0 | OFF  | GPIOG7   | 16  | 199 |
 |   0 |   0 |   GPIOA0 |  OFF | 0 | 11 || 12 | 0 | OFF  | GPIOA6   | 1   | 6   |
 |   2 |   2 |   GPIOA2 |  OFF | 0 | 13 || 14 |   |      | 0v       |     |     |
 |   3 |   3 |   GPIOA3 |  OFF | 0 | 15 || 16 | 0 | OFF  | GPIOG8   | 4   | 200 |
 |     |     |     3.3v |      |   | 17 || 18 | 0 | OFF  | GPIOG9   | 5   | 201 |
 |  64 |  12 |   GPIOC0 |  OFF | 0 | 19 || 20 |   |      | 0v       |     |     |
 |  65 |  13 |   GPIOC1 |  OFF | 0 | 21 || 22 | 0 | OFF  | GPIOA1   | 6   | 1   |
 |  66 |  14 |   GPIOC2 |  OFF | 0 | 23 || 24 | 0 | OFF  | GPIOC3   | 10  | 67  |
 +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 | BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
 +-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
 
 +-----+----NanoPi NEO/NEO2 Debug UART-+----+
 | BCM | wPi |   Name   | Mode | V | Ph |
 +-----+-----+----------+------+---+----+
 |   4 |  17 |   GPIOA4 | ALT5 | 0 | 37 |
 |   5 |  18 |   GPIOA5 | ALT4 | 0 | 38 |
 +-----+-----+----------+------+---+----+


5 Code Sample with WiringNP

Here is a setup to connect a Matrix - LED module to a NanoPi NEO2:
WiringNP-LED-Demo
Here is the connection:

Matrix-LED NanoPi M1
S Pin7
V Pin4
G Pin6

We will show how to use WiringNP to control a LED module.
In the following code samples index 7 stands for Pin7. We will use "7" to access this physical pin.

5.1 C Source Code

Make a C source file:

vi test.c

Type the following lines:

#include <wiringPi.h>
int main(void)
{
  wiringPiSetup() ;
  pinMode (7, OUTPUT) ;
  for(;;)
  {
    digitalWrite(7, HIGH) ;
    delay (500) ;
    digitalWrite(7,  LOW) ;
    delay (500) ;
  }
}

Compile and run "test.c":

gcc -Wall -o test test.c -lwiringPi -lpthread
sudo ./test

You can see the LED is blinking.

5.2 PWM Code Sample

The PWM pin in NanoPi NEO/NEO2 is multiplexing which can be set to either PWM or SerialPort0. To set this pin to PWM you need to run "sudo npi-config" and enter the "Advanced Options" menu to Enable/Disable PWM. Note: after PWM is enabled SerialPort0 will not be accessed and you need to login your board via SSH.

Here is a hardware setup to connect a Matrix - Buzzer to a NanoPi NEO2:
WiringNP-PWM-Demo

Create a source file in C:

vi pwmtest.c

Type the following code:

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main (void)
{
  int l ;
  printf ("PWM test program\n") ;
 
  //using wiringPi Pin Number
  int pin = 18;              
  if (wiringPiSetup () == -1)
    exit (1) ;               
 
  /*
  //using Physical Pin Number
  int pin = 38;              
  if (wiringPiSetupPhys() == -1)
    exit (1) ;                  
  */                            
 
  /*
  //using BCM Pin Number
  int pin = 5;          
  if (wiringPiSetupGpio() == -1)
    exit (1);                   
  */                            
 
  pinMode (pin, PWM_OUTPUT);
  for (;;) {                      
    for (l = 0 ; l < 1024 ; ++l) {
      pwmWrite (pin, l) ;         
      delay (1) ;        
    }                              
    for (l = 1023 ; l >= 0 ; --l) {
      pwmWrite (pin, l) ;          
      delay (1) ;        
    }            
  }         
  return 0 ;
}

Compile the pwmtest.c file and run the generated executable:

gcc -Wall -o pwmtest pwmtest.c -lwiringPi -lpthread
./pwmtest

Connect a PWM beeper to a NEO/NEO2 and the beeper will sound.

5.3 Shell Script

Make a shell script:

vi test.sh

Type the following lines:

LED=7
gpio mode $LED out
while true; do
  gpio write $LED 1
  sleep 0.5
  gpio write $LED 0
  sleep 0.5
done

Run the script:

sudo source test.sh

You can see the LED is blinking.

6 Regular WiringNP APIs

Note:most of the descriptions for the following functions are from http://wiringpi.com/. To get more details refer to http://wiringpi.com/.

6.1 Initialization

6.1.1 wiringPiSetup (void)

This function initializes wiringPi and uses the pin specifications defined by wiringPi. To check detailed pin specifications you can command "gpio readall".
You need to call this function as root.

6.1.2 int wiringPiSetupGpio(void)

This function is similar to the wiringPiSetup function. The only difference is that this function assumes the GPIO's pin specifications are the ones defined by CPU and haven't been redefined.
You need to call this function as root.

6.1.3 int wiringPiSetupPhys (void)

This function is similar to the wiringPiSetup function. The only difference is that it only supports P1's interface and doesn't support physical pin specifications.
You need to call this function as root.

6.1.4 int wiringPiSetupSys (void)

This function initializes wiringPi and access hardware by using "/sys/class/gpio" APIs rather than commanding hardware directly.
This function can be called by any users. The GPIO pin numbers used by this function should be the ones defined by CPU. This is similar to the rules followed by the wiringPiSetupGpio function.
Before calling this function make sure to call "/sys/class/gpio" APIs to list the pins you want to access.
You can list the pins you want to access in a separate shell script. Or you can command GPIO pins by calling "system()".

6.2 Core Functions

6.2.1 void pinMode (int pin, int mode)

This function sets a pin to INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not make change that pin's mode.
You can set a pin's mode in a shell script.

6.2.2 void pullUpDnControl (int pin, int pud)

This function sets a pin to the pull-up or pull-down resistor mode when that pin is set to INPUT.
Unlike the Arduino the NanoPi M1 has both the pull-up an pull-down resistor modes.
The parameter pud can be: PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v).
The internal pull up/down resistors have a value of approximately 100KΩ on the NanoPi M1.
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not make change that pin's mode.
If you need to activate a pull-up/pull-down resistor you can do it by using GPIO commands in a script before you start your program.

6.2.3 void digitalWrite (int pin, int value)

This function writes either HIGH or LOW to a pin. Before you call this function to access a pin make sure you have set that pin to OUTPUT.
WiringPi treat any non-0 value to HIGH and 0 is the only value that is treated as LOW.

6.2.4 void pwmWrite (int pin, int value)

This function writes a value to a PWM register. The value can be any one in 0~1024. Different PWM devices may take different value ranges.
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not be able to write a value to that pin.

6.2.5 digitalRead(int pin);

This function reads a pin's value. The value is either HIGH(1) or LOW(0).

6.2.6 analogRead (int pin) ;

This function returns a value it reads from an analog pin. To effectively call this function make sure you have connected a working analog device such as Gertboard and quick2Wire to your board.

6.2.7 analogWrite (int pin, int value) ;

This function writes a value to an analog pin. To effectively call this function make sure you have connected a working analog device such as Gertboard to your board.

7 Update Log

7.1 December-31-2016

  • Released English Version

7.2 May-16-2017

  • Updated section 2 by adding support for H5 based NEO2
  • Added section 4 by adding support for H5 based NEO2
  • Added section 5.2: code sample on how to access a PWM device

7.3 May-17-2017

  • Updated section 4 by adding installation steps for M1

7.4 May-24-2017

  • Updated section 5.2 by adding a PWM code sample for NEO2

7.5 Nov-14-2018

  • Updated section 6