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

From FriendlyELEC WiKi
Jump to: navigation, search
(Install WiringNP)
(Verify WiringNP)
Line 34: 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 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 | ALT5 | 0 |  3 || 4  |  |      | 5V      |    |    |
  |  11 |  9 |   SCL.0 | ALT5 | 0 |  5 || 6  |  |      | 0v      |    |    |
+
  |  11 |  9 | GPIOA11 | ALT5 | 0 |  5 || 6  |  |      | 0v      |    |    |
  | 203 |  7 |   GPIO.7 OUT | 0 |  7 || 8  | 0 | ALT5 | TxD3    | 15  | 198 |
+
  | 203 |  7 | GPIOG11 OFF | 0 |  7 || 8  | 0 | ALT5 | GPIOG6  | 15  | 198 |
  |    |    |      0v |      |  |  9 || 10 | 0 | ALT5 | RxD3    | 16  | 199 |
+
  |    |    |      0v |      |  |  9 || 10 | 0 | ALT5 | 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 | ALT4 | 0 | 19 || 20 |  |      | 0v      |    |    |
  |  65 |  13 |     MISO | ALT4 | 0 | 21 || 22 | 0 | ALT5 | RTS2    | 6  | 1  |
+
  |  65 |  13 |   GPIOC1 | ALT4 | 0 | 21 || 22 | 0 | OFF  | GPIOA1  | 6  | 1  |
  |  66 |  14 |     SCLK | ALT4 | 0 | 23 || 24 | 0 | ALT4 | CE0      | 10  | 67 |
+
  |  66 |  14 |   GPIOC2 | ALT4 | 0 | 23 || 24 | 0 | ALT4 | 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 NEO2--+------+----------+-----+-----+
  
  +-----+----NanoPI M1 Debug UART---+----+
+
  +-----+----NanoPi 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 | ALT5 | 0 | 38 |
  |  4 |  32 GPIO.4 | ALT5 | 0 | 43 |
+
  |  5 |  33 GPIO.5 | ALT5 | 0 | 44 |
+
 
  +-----+-----+----------+------+---+----+
 
  +-----+-----+----------+------+---+----+
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 07:02, 16 May 2017

查看中文

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 H3 Based Boards(NanoPi M1/NanoPi NEO)

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 H5 Based Board(NanoPi 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/
git checkout h5
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 NEO2--+------+----------+-----+-----+
 | BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
 +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 |     |     |     3.3V |      |   |  1 || 2  |   |      | 5V       |     |     |
 |  12 |   8 |  GPIOA12 | ALT5 | 0 |  3 || 4  |   |      | 5V       |     |     |
 |  11 |   9 |  GPIOA11 | ALT5 | 0 |  5 || 6  |   |      | 0v       |     |     |
 | 203 |   7 |  GPIOG11 |  OFF | 0 |  7 || 8  | 0 | ALT5 | GPIOG6   | 15  | 198 |
 |     |     |       0v |      |   |  9 || 10 | 0 | ALT5 | 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 | ALT4 | 0 | 19 || 20 |   |      | 0v       |     |     |
 |  65 |  13 |   GPIOC1 | ALT4 | 0 | 21 || 22 | 0 | OFF  | GPIOA1   | 6   | 1   |
 |  66 |  14 |   GPIOC2 | ALT4 | 0 | 23 || 24 | 0 | ALT4 | GPIOC3   | 10  | 67  |
 +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
 | BCM | wPi |   Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
 +-----+-----+----------+------+---+-NanoPi NEO2--+------+----------+-----+-----+
 
 +-----+----NanoPi NEO2 Debug UART-+----+
 | BCM | wPi |   Name   | Mode | V | Ph |
 +-----+-----+----------+------+---+----+
 |   4 |  17 |   GPIOA4 | ALT5 | 0 | 37 |
 |   5 |  18 |   GPIOA5 | ALT5 | 0 | 38 |
 +-----+-----+----------+------+---+----+

5 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

6 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.

6.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.


6.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.

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

7.1 Initialization

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

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

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

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

7.2 Core Functions

7.2.1 void pinMode (int pin, int mode)

This function sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.
This function has no effect when in Sys mode.
If you need to change the pin mode you can do it with the gpio program in a script before you start your program.

7.2.2 void pullUpDnControl (int pin, int pud)

This function sets the pull-up or pull-down resistor mode on the given pin, which should be set as an input.
Unlike the Arduino the NanoPi M1 has both pull-up an down internal resistors.
The parameter pud should 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.
This function has no effect on the M1's GPIO pins when in Sys mode.
If you need to activate a pull-up/pull-down you can do it with the gpio program in a script before you start your program.

7.2.3 void digitalWrite (int pin, int value)

This function is used to write the value HIGH or LOW (1 or 0) to the given pin which should have been previously set as an output.
WiringPi treats any non-zero number as HIGH, however 0 is the only representation of LOW.

7.2.4 void pwmWrite (int pin, int value)

This function is used to write a value to the PWM register for a given pin. The range is 0-1024. Other PWM devices may have other PWM ranges.
This function is not able to control the M1's on-board PWM when in Sys mode.

7.2.5 digitalRead(int pin);

This function returns the value read at a given pin. It will be HIGH or LOW (1 or 0) depending on the logic level at the pin.

7.2.6 analogRead (int pin) ;

This function returns the value read on a supplied analog input pin. You will need to register additional analog modules to enable this function for devices such as the Gertboard, quick2Wire analog board, etc.

7.2.7 analogWrite (int pin, int value) ;

This function writes a given value to a supplied analog pin. You will need to register additional analog modules to enable this function for devices such as the Gertboard.

8 Update Log

8.1 December-31-2016

  • Released English Version