WiringNP: NanoPi NEO/NEO2/Air GPIO Programming with C/zh

From FriendlyELEC WiKi
Revision as of 09:29, 30 November 2016 by Tzs (Talk | contribs) (WiringNP示例)

Jump to: navigation, search

1 WiringPi简介

wiringPi库最早是由Gordon Henderson所编写并维护的一个用C语言写成的类库,除了GPIO库,还包括了I2C库、SPI库、UART库和软件PWM库等,由于wiringPi的API函数和arduino非常相似,这也使得它广受欢迎。
wiringPi库除了提供wiringPi类库及其头文件外,还提供了一个命令行工具gpio:可以用来设置和读写GPIO管脚,以方便在Shell脚本中控制GPIO管脚。

2 WiringNP简介

wiringPi库最初是为BCM2835芯片编写的,后被爱好者移植到Allwinner H3平台,针对NanoPi的wiringPi衍生版本其项目命名为WiringNP,目前支持NanoPi M1NanoPi NEO开发板, 最初版本是由网友wertyzp提供的版本。

WiringNP项目主页: https://github.com/friendlyarm/WiringNP

3 WiringNP的安装

通过ssh,或者串口终端进入开发板的命令行,从 github 上下载WiringNP代码并编译安装:

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

3.1 测试安装

WiringNP包括一套gpio命令,使用gpio命令可以控制NanoPi上的各种接口,通过以下指令可以测试WiringNP是否安装成功:

gpio readall

如果安装成功会显示NanoPI的IO图,例如NanoPI M1会显示如下信息:

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 WiringNP示例

将一个Matrix - LED通过连接至NanoPi M1,像下图这样:
Matrix-LED_nanopi_m1

其中,引脚的连接对应如下:

Matrix-LED NanoPi M1
S Pin7
V Pin4
G Pin6

然后用WiringNP实现一个LED闪烁的例子:

vi test.c

然后键入如下代码:

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

编译运行:

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

看到LED灯一闪一闪的,就表示成功了。