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

From FriendlyELEC WiKi
Jump to: navigation, search
(Popular WiringNP APIs)
(Popular WiringNP APIs)
Line 154: Line 154:
  
 
==== int wiringPiSetupSys (void) ====
 
==== int wiringPiSetupSys (void) ====
该函数初始化wiringPi,使用/sys/class/gpio接口,而不是直接通过操作硬件来实现。<br />
+
This function initializes wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly.<br/>  
该函数可以使用非root权限用户,在此种模式下的管脚号是CPU的GPIO管脚号,和wiringPiSetupGpio函数类似。<br />
+
This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program.<br/>  
在此种模式下,在运行程序前,您需要通过/sys/class/gpio接口导出要使用的管脚。<br />
+
In this mode you can only use the pins which have been exported via the /sys/class/gpio interface before you run your program.<br/>  
你可以在一个独立的shell脚本中来导出将要使用的管脚,或者使用系统的system()函数来调用GPIO命令。
+
You can do this in a separate shell script, or by using the system() function from inside your program to call the gpio program.
  
 
=== 核心函数 ===
 
=== 核心函数 ===

Revision as of 15:57, 30 December 2016

查看中文

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 and NanoPi NEO. The first version was made by wertyzp.

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

3 Install WiringNP

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

3.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:

root@FriendlyARM:~# gpio readall
 +-----+-----+----------+------+---+-NanoPI M1+---+------+----------+-----+-----+
 | BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
 +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 |     |     |     3.3v |      |   |  1 || 2  |   |      | 5v       |     |     |
 |  12 |   8 |    SDA.0 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
 |  11 |   9 |    SCL.0 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
 | 203 |   7 |   GPIO.7 |  OUT | 0 |  7 || 8  | 0 | ALT5 | TxD3     | 15  | 198 |
 |     |     |       0v |      |   |  9 || 10 | 0 | ALT5 | RxD3     | 16  | 199 |
 |   0 |   0 |     RxD2 | ALT5 | 0 | 11 || 12 | 0 | OFF  | GPIO.1   | 1   | 6   |
 |   2 |   2 |     TxD2 |  OFF | 0 | 13 || 14 |   |      | 0v       |     |     |
 |   3 |   3 |     CTS2 |  OFF | 0 | 15 || 16 | 0 | OFF  | GPIO.4   | 4   | 200 |
 |     |     |     3.3v |      |   | 17 || 18 | 0 | OFF  | GPIO.5   | 5   | 201 |
 |  64 |  12 |     MOSI | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
 |  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT5 | RTS2     | 6   | 1   |
 |  66 |  14 |     SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 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 |
 +-----+-----+----------+------+---+-NanoPI M1+---+------+----------+-----+-----+
 
 +-----+----NanoPI M1 Debug UART---+----+
 | BCM | wPi |   Name   | Mode | V | Ph |
 +-----+-----+----------+------+---+----+
 |     |     |       0v |      |   | 41 |
 |     |     |       5v |      |   | 42 |
 |   4 |  32 |   GPIO.4 | ALT5 | 0 | 43 |
 |   5 |  33 |   GPIO.5 | ALT5 | 0 | 44 |
 +-----+-----+----------+------+---+----+

4 Map NanoPi M1's GPIO Pins to WiringNP's Indexes

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
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:
WiringNP-GPIO-ALL.jpg

5 Code Sample with WiringNP

Here is a setup to connect a Matrix - LED module to a NanoPi M1:
Matrix-LED_nanopi_m1

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.
By running "gpio readall" we can find that physical pin "Pin7" corresponds to the wPi index "7". We will use "7" to access this LED.

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
./test

You can see the LED is blinking.


5.2 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:

source test.sh

You can see the LED is blinking.

6 Popular WiringNP APIs

6.1 Initialization

6.1.1 wiringPiSetup (void)

This initializes wiringPi and assumes that the calling program is going to use the wiringPi pin numbering scheme. The numbering scheme can be checked by running "gpio readall".
You must be root to call this function.

6.1.2 int wiringPiSetupGpio(void)

This function is the same as the above one, however it allows the calling program to use the SoC's GPIO pin numbers directly without re-mapping.
You must be root to call this function.

6.1.3 int wiringPiSetupPhys (void)

This function is the same as "wiringPiSetup", however it allows the calling program to use the physical pin numbers on the P1 connector only.
You must be root to call this function.

6.1.4 int wiringPiSetupSys (void)

This function initializes wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly.
This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program.
In this mode you can only use the pins which have been exported via the /sys/class/gpio interface before you run your program.
You can do this in a separate shell script, or by using the system() function from inside your program to call the gpio program.

6.2 核心函数

6.2.1 void pinMode (int pin, int mode)

使用该函数可以将某个引脚配置为INPUT(输入) OUTPUT(输出)PWM_OUTPUT(脉冲输出)或者GPIO_CLOCK(时钟)。
在Sys模式下,这个函数没有影响。
你可以通过调用GPIO命令在shell脚本中来设置管脚的模式。

6.2.2 void pullUpDnControl (int pin, int pud)

使用该函数可以设置指定管脚使用上拉或者下拉电阻模式,通常当需要管脚作为输入引脚时,需要设定此项。
不同于Arduino,CPU有内部上拉和下拉电阻这两种模式。
参数pud可以为PUD_OFF(无上拉或下拉电阻)、PUD_DOWN(内部下拉至地线)或者PUD_UP(内部上拉至3.3V)。
在NanoPi M1上,内部上拉和下拉电阻有接近100KΩ。
该函数在Sys模式下无作用。
如果你需要激活上拉或下拉电阻的话,在启动程序前,可以通过在脚本中调用GPIO命令来实现。

6.2.3 void digitalWrite (int pin, int value)

使用该函数可以向指定的管脚写入HIGH(高)或者LOW(低),写入前,需要将管脚设置为输出模式。
wiringPi将任何的非0值作为HIGH(高)来对待,因此,0是唯一能够代表LOW(低)的数值。

6.2.4 void pwmWrite (int pin, int value)

使用该函数可以将值写入指定管脚的PWM寄存器中,可设置的值为0~1024,其他PWM讴备可能有不同的PWM范围。
当在Sys模式时,该函数不可用来控制板上PWM。

6.2.5 digitalRead(int pin);

使用该函数可以读取指定管脚的值,读取到的值为HIGH(1)或者LOW(0),该值取决于该管脚的逻辑电平的高低。

6.2.6 analogRead (int pin) ;

该函数返回所指定的模拟输入管脚的值。你需要添加额外的模拟模块来使用该函数,比如Gertboard,quick2Wire模拟板等。

6.2.7 analogWrite (int pin, int value) ;

该函数将指定的值写入到指定的管脚。你需要添加额外的模拟模块来使用该函数,比如Gertboard等。