Difference between revisions of "WiringPi for RK3399"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
 
(updated by API)
 
(12 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[WiringNP: NanoPi NEO/NEO2/Air GPIO Programming with C/zh|查看中文]]
+
[[WiringPi for RK3399/zh|查看中文]]
  
 
==Introduction to WiringPi==
 
==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.<br />
+
The wiringPi library was initially developed by Gordon Henderson in C. It has libraries to access GPIO, I2C, SPI, UART, PWM and etc. Because its APIs are very similar to Arduino's APIs it is very popular among developers.<br/>  
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.<br />
+
The wiringPi library has various libraries, header files and a commandline utility:gpio. The gpio utility can be used to read and write GPIO pins.<br />
  
==Introduction to WiringNP==
+
The wiringPi library was initially developed for BCM2835, and has been ported to FriendlyELEC-RK3399. It works with [[NanoPi M4]], [[NanoPi NEO4]] and [[NanoPC-T4]].<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 />
+
Current version: 2.44<br />
 +
WiringPi's home page: http://wiringpi.com<br />
 +
<br />
 +
FriendlyELEC has developed a Python version too and here is the reference link: [[WiringPi-Python for RK3399]]<br />
  
==Install WiringNP on NEO/NEO2==
+
==Supported OS==
Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:<br />
+
* FriendlyCore
<syntaxhighlight lang="bash">
+
* FriendlyDesktop
git clone https://github.com/friendlyarm/WiringNP
+
Note: by default FriendlyELEC's latest ROMs already have the wiringPi utility. If a ROM doesn't have it you can refer to the instructions in this wiki to manually install it.
cd WiringNP/
+
chmod 755 build
+
./build
+
</syntaxhighlight>
+
  
==Install WiringNP on NanoPi M1==
+
==Supproted Boards==
Log into your nano board via SSH, open a terminal and install the WiringNP library by running the following commands:<br />
+
* NanoPC T4
 +
* NanoPi M4
 +
* NanoPi NEO4
 +
 
 +
==Install WiringPi on T4/M4/NEO4==
 +
Log in a board(T4/M4/NEO4) via SSH and run the following commands:<br />
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
git clone https://github.com/friendlyarm/WiringNP
+
# Delete "old" version
cd WiringNP/
+
wget http://112.124.9.243/wiringpi/remove_oldversion_wiringPi.sh
git checkout nanopi-m1
+
chmod 755 remove_oldversion_wiringPi.sh
chmod 755 build
+
sudo ./remove_oldversion_wiringPi.sh
./build
+
# Download and install wiringPi for RK3399
 +
wget wget http://112.124.9.243/wiringpi/wiringpi-v2.44-friendlyelec-rk3399.deb
 +
sudo dpkg -i  wiringpi-v2.44-friendlyelec-rk3399.deb
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Verify WiringNP ====
+
====Test====
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:
+
You can test whether or not your installation is successful:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
gpio readall
 
gpio readall
 
</syntaxhighlight>
 
</syntaxhighlight>
If your installation is successful the following messages will show up. Here is the message list for NEO2:
+
If your installation is successful your board's pin settings will be listed. For example here is a list of M4's pin settings:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
root@FriendlyARM:~# gpio readall
+
root@NanoPi-M4:~# gpio readall
  +-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
+
+------+-----+----------+------+ Model  NanoPi-M4 +------+----------+-----+------+
  | BCM | wPi |  Name  | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+
  | GPIO | wPi |  Name  | Mode | V | Physical | V | Mode |   Name   | wPi | GPIO |
  +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
+
  +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
  |     |    |    3.3V |      |  |  1 || 2  |  |      | 5V      |    |     |
+
  |     |    |    3.3V |      |  |  1 || 2  |  |      | 5V      |    |     |
  | 12 |   8 | GPIOA12 | OFF | 0 |  3 || 4  |  |      | 5V      |    |     |
+
  |     |     | I2C2_SDA |     |   |  3 || 4  |  |      | 5V      |    |     |
  | 11 |   9 | GPIOA11 | OFF | 0 |  5 || 6  |  |      | 0v      |    |     |
+
  |     |     | I2C2_SCL |     |   |  5 || 6  |  |      | GND(0V)  |    |     |
  | 203 |  7 | GPIOG11 |  OUT | 1 |  7 || 8  | 0 | OFF | GPIOG6  | 15  | 198 |
+
  |   32 |  7 | GPIO1_A0 |  OUT | 0 |  7 || 8  |   | ALT | GPIO4_C1 | 15  | 145 |
  |     |    |       0v |      |  |  9 || 10 | 0 | OFF | GPIOG7  | 16  | 199 |
+
  |     |    | GND(0V) |      |  |  9 || 10 |   | ALT | GPIO4_C0 | 16  | 144 |
  |  0 |  0 |  GPIOA0 |  OFF | 0 | 11 || 12 | 0 | OFF  | GPIOA6   | 1  | |
+
  |  33 |  0 | GPIO1_A1 IN | 0 | 11 || 12 | 1 | IN   | GPIO1_C2 | 1  | 50  |
  |  2 |  2 |  GPIOA2 |  OFF | 0 | 13 || 14 |  |      | 0v      |    |     |
+
  |  35 |  2 | GPIO1_A3 IN | 0 | 13 || 14 |  |      | GND(0V)  |    |     |
  |  3 |  3 |  GPIOA3 |  OFF | 0 | 15 || 16 | 0 | OFF  | GPIOG8   | 4  | 200 |
+
  |  36 |  3 | GPIO1_A4 IN | 0 | 15 || 16 | 0 | IN   | GPIO1_C6 | 4  | 54  |
  |     |    |    3.3v |      |  | 17 || 18 | 0 | OFF  | GPIOG9   | 5  | 201 |
+
  |     |    |    3.3V |      |  | 17 || 18 | 0 | IN   | GPIO1_C7 | 5  | 55  |
  | 64 | 12 |   GPIOC0 | OFF | 0 | 19 || 20 |  |      | 0v      |    |     |
+
  |     |     | UART4_TX |     |   | 19 || 20 |  |      | GND(0V)  |    |     |
  | 65 | 13 |   GPIOC1 | OFF | 0 | 21 || 22 | 0 | OFF  | GPIOA1  | 6  | |
+
  |     |     | UART4_RX |     |   | 21 || 22 | 0 | IN  | GPIO1_D0 | 6  | 56  |
  | 66 | 14 |   GPIOC2 | OFF | 0 | 23 || 24 | 0 | OFF | GPIOC3   | 10 | 67 |
+
  |     |     | SPI1_CLK |     |   | 23 || 24 |   |     | SPI1_CSn |    |      |
  +-----+-----+----------+------+---+----++----+---+------+----------+-----+-----+
+
  |     |    |  GND(0V) |      |   | 25 || 26 |  | ALT | GPIO4_C5 | 11 |  149 |
  | BCM | wPi Name   | Mode | V | Physical | V | Mode | Name     | wPi | BCM |
+
  |      |    | I2C2_SDA |      |  | 27 || 28 |  |      | I2C2_SCL |    |      |
  +-----+-----+----------+------+---+-NanoPi NEO/NEO2--+------+----------+-----+-----+
+
  |     |     | I2S0_LRX |      | 29 || 30 |   |     | GND(0V)  |     |     |
 
+
|      |    | I2S0_LTX |     |  | 31 || 32 |  |      | I2S_CLK  |    |      |
  +-----+----NanoPi NEO/NEO2 Debug UART-+----+
+
  |      |    | I2S0_SCL |      |  | 33 || 34 |  |      | GND(0V)  |    |      |
  | BCM | wPi Name   | Mode | V | Ph |
+
|      |    | I2S0SDI0 |      |  | 35 || 36 |  |      | I2S0SDO0 |    |      |
  +-----+-----+----------+------+---+----+
+
  |      |    | I2S0I1O3 |      |  | 37 || 38 |  |      | I2S0I2O2 |    |      |
|  4 |  17 |  GPIOA4 | ALT5 | 0 | 37 |
+
  |     |     |  GND(0V) |      | 39 || 40 |   |     | I2S0I3O1 |     |      |
|  5 |  18 |  GPIOA5 | ALT4 | 0 | 38 |
+
  +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
+-----+-----+----------+------+---+----+
+
root@NanoPi-M4:~#
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<!---
+
==Code Samples Using wiringPi==
==Map NanoPi M1's GPIO Pins to WiringNP's Indexes==
+
Connect a [[Matrix - LED]] module to your board and make sure the hardware setting is as follows:<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]]
+
--->
+
 
+
==Code Sample with WiringNP==
+
Here is a setup to connect a [[Matrix - LED]] module to a NanoPi NEO2:<br>
+
[[File:WiringNP-LED-Demo.jpg|frameless|300px|WiringNP-LED-Demo]]<br />
+
Here is the connection:<br/>
+
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
|Matrix-LED || NanoPi M1
+
|Matrix-LED || T4/M4/NEO4
 
|-
 
|-
 
|S    || Pin7
 
|S    || Pin7
Line 86: Line 82:
 
|}
 
|}
  
We will show how to use WiringNP to control a LED module.<br />
+
Here is a code sample showing how to make an LED blink using wiringPi<br />
In the following code samples index 7 stands for Pin7. We will use "7" to access this physical pin.<br />
+
In the code sample '7' stands for 'Pin7':<br />
  
=== C Source Code ===
+
===Code Sample in C===
Make a C source file:
+
Create a source file in C:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
vi test.c
 
vi test.c
Line 110: Line 106:
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
Compile and run "test.c":
+
Compile the test.c file and run it:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
gcc -Wall -o test test.c -lwiringPi -lpthread
+
gcc -Wall -o test test.c -lwiringPi -lwiringPiDev -lpthread -lrt -lm -lcrypt
 
sudo ./test  
 
sudo ./test  
 
</syntaxhighlight>
 
</syntaxhighlight>
You can see the LED is blinking.
+
If everything is correct you will observe that the LED blinks.
  
===PWM Code Sample===
+
===Code Sample in Shell===
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 />
+
Create a shell script:
<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 Script ===
+
Make a shell script:
+
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
vi test.sh
 
vi test.sh
Line 198: Line 133:
 
sudo source test.sh
 
sudo source test.sh
 
</syntaxhighlight>
 
</syntaxhighlight>
You can see the LED is blinking.
+
If everything is correct you will observe that the LED blinks.
  
==Popular WiringNP APIs==
+
===Code Sample in Python===
Note:most of the descriptions for the following functions are from http://wiringpi.com/. To get more details refer to http://wiringpi.com/.
+
You can refer to this link:[[WiringPi-Python for RK3399/zh]]
  
=== Initialization ===
+
==Regular WiringPi APIs==
 +
===Initialization===
 
==== wiringPiSetup (void) ====
 
==== 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".<br />
+
This function initializes wiringPi and uses the pin specifications defined by wiringPi. To check detailed pin specifications you can command "gpio readall".<br />
You must be root to call this function.
+
You need to call this function as root.
  
 
==== int wiringPiSetupGpio(void) ====
 
==== 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.<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 />
You must be root to call this function.
+
You need to call this function as root.
  
 
==== int wiringPiSetupPhys (void) ====
 
==== 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.<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 />
You must be root to call this function.
+
You need to call this function as root.
  
 
==== int wiringPiSetupSys (void) ====
 
==== int wiringPiSetupSys (void) ====
This function initializes wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly.<br/>  
+
This function initializes wiringPi and access hardware by using "/sys/class/gpio" APIs rather than commanding hardware directly.<br />
This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program.<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 />
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/>  
+
Before calling this function make sure to call "/sys/class/gpio" APIs to list the pins you want to access.<br />
You can do this in a separate shell script, or by using the system() function from inside your program to call the gpio program.
+
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 ===
 
=== Core Functions ===
 
==== void pinMode (int pin, int mode) ====
 
==== void pinMode (int pin, int mode) ====
This function sets the mode of a pin to either INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.<br/>  
+
This function sets a pin to INPUT, OUTPUT, PWM_OUTPUT or GPIO_CLOCK.<br />
This function has no effect when in Sys mode.<br/>
+
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not make change that pin's mode.<br />
If you need to change the pin mode you can do it with the gpio program in a script before you start your program.
+
You can set a pin's mode in a shell script.
  
 
==== void pullUpDnControl (int pin, int pud) ====
 
==== 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.<br />
+
This function sets a pin to the pull-up or pull-down resistor mode when that pin is set to INPUT.<br />
Unlike the Arduino the NanoPi M1 has both pull-up an down internal resistors.<br />
+
Unlike the Arduino the RK3399 has both the pull-up and pull-down resistor modes.<br />
The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 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 />
The internal pull up/down resistors have a value of approximately 100KΩ on the NanoPi M1.<br />
+
Note: after you call the wiringPiSetupSys function to access a pin calling this function will not make change that pin's mode.<br />
This function has no effect on the M1's GPIO pins when in Sys mode.<br />
+
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.
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.
+
  
 
==== void digitalWrite (int pin, int value) ====
 
==== 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.<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 treats any non-zero number as HIGH, however 0 is the only representation of 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) ====
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.<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 />
This function is not able to control the M1's on-board PWM when in Sys mode.<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); ====
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.<br />
+
This function reads a pin's value. The value is either HIGH(1) or LOW(0).<br />
  
 
==== analogRead (int pin) ; ====
 
==== 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.<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) ; ====
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.<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==
 
==Update Log==
===December-31-2016===
+
===November-14-2018===
 
* Released English Version
 
* 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
 

Latest revision as of 06:49, 6 September 2023

查看中文

1 Introduction to WiringPi

The wiringPi library was initially developed by Gordon Henderson in C. It has libraries to access GPIO, I2C, SPI, UART, PWM and etc. Because its APIs are very similar to Arduino's APIs it is very popular among developers.
The wiringPi library has various libraries, header files and a commandline utility:gpio. The gpio utility can be used to read and write GPIO pins.

The wiringPi library was initially developed for BCM2835, and has been ported to FriendlyELEC-RK3399. It works with NanoPi M4, NanoPi NEO4 and NanoPC-T4.

Current version: 2.44
WiringPi's home page: http://wiringpi.com

FriendlyELEC has developed a Python version too and here is the reference link: WiringPi-Python for RK3399

2 Supported OS

  • FriendlyCore
  • FriendlyDesktop

Note: by default FriendlyELEC's latest ROMs already have the wiringPi utility. If a ROM doesn't have it you can refer to the instructions in this wiki to manually install it.

3 Supproted Boards

  • NanoPC T4
  • NanoPi M4
  • NanoPi NEO4

4 Install WiringPi on T4/M4/NEO4

Log in a board(T4/M4/NEO4) via SSH and run the following commands:

# Delete "old" version
wget http://112.124.9.243/wiringpi/remove_oldversion_wiringPi.sh
chmod 755 remove_oldversion_wiringPi.sh
sudo ./remove_oldversion_wiringPi.sh
# Download and install wiringPi for RK3399
wget wget http://112.124.9.243/wiringpi/wiringpi-v2.44-friendlyelec-rk3399.deb
sudo dpkg -i  wiringpi-v2.44-friendlyelec-rk3399.deb

4.1 Test

You can test whether or not your installation is successful:

gpio readall

If your installation is successful your board's pin settings will be listed. For example here is a list of M4's pin settings:

root@NanoPi-M4:~# gpio readall
 +------+-----+----------+------+ Model  NanoPi-M4 +------+----------+-----+------+
 | GPIO | wPi |   Name   | Mode | V | Physical | V | Mode |   Name   | wPi | GPIO |
 +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
 |      |     |     3.3V |      |   |  1 || 2  |   |      | 5V       |     |      |
 |      |     | I2C2_SDA |      |   |  3 || 4  |   |      | 5V       |     |      |
 |      |     | I2C2_SCL |      |   |  5 || 6  |   |      | GND(0V)  |     |      |
 |   32 |   7 | GPIO1_A0 |  OUT | 0 |  7 || 8  |   | ALT  | GPIO4_C1 | 15  |  145 |
 |      |     |  GND(0V) |      |   |  9 || 10 |   | ALT  | GPIO4_C0 | 16  |  144 |
 |   33 |   0 | GPIO1_A1 |   IN | 0 | 11 || 12 | 1 | IN   | GPIO1_C2 | 1   |  50  |
 |   35 |   2 | GPIO1_A3 |   IN | 0 | 13 || 14 |   |      | GND(0V)  |     |      |
 |   36 |   3 | GPIO1_A4 |   IN | 0 | 15 || 16 | 0 | IN   | GPIO1_C6 | 4   |  54  |
 |      |     |     3.3V |      |   | 17 || 18 | 0 | IN   | GPIO1_C7 | 5   |  55  |
 |      |     | UART4_TX |      |   | 19 || 20 |   |      | GND(0V)  |     |      |
 |      |     | UART4_RX |      |   | 21 || 22 | 0 | IN   | GPIO1_D0 | 6   |  56  |
 |      |     | SPI1_CLK |      |   | 23 || 24 |   |      | SPI1_CSn |     |      |
 |      |     |  GND(0V) |      |   | 25 || 26 |   | ALT  | GPIO4_C5 | 11  |  149 |
 |      |     | I2C2_SDA |      |   | 27 || 28 |   |      | I2C2_SCL |     |      |
 |      |     | I2S0_LRX |      |   | 29 || 30 |   |      | GND(0V)  |     |      |
 |      |     | I2S0_LTX |      |   | 31 || 32 |   |      | I2S_CLK  |     |      |
 |      |     | I2S0_SCL |      |   | 33 || 34 |   |      | GND(0V)  |     |      |
 |      |     | I2S0SDI0 |      |   | 35 || 36 |   |      | I2S0SDO0 |     |      |
 |      |     | I2S0I1O3 |      |   | 37 || 38 |   |      | I2S0I2O2 |     |      |
 |      |     |  GND(0V) |      |   | 39 || 40 |   |      | I2S0I3O1 |     |      |
 +------+-----+----------+------+---+----++----+---+------+----------+-----+------+
root@NanoPi-M4:~#

5 Code Samples Using wiringPi

Connect a Matrix - LED module to your board and make sure the hardware setting is as follows:

Matrix-LED T4/M4/NEO4
S Pin7
V Pin4
G Pin6

Here is a code sample showing how to make an LED blink using wiringPi
In the code sample '7' stands for 'Pin7':

5.1 Code Sample in C

Create a source file in C:

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 the test.c file and run it:

gcc -Wall -o test test.c -lwiringPi -lwiringPiDev -lpthread -lrt -lm -lcrypt
sudo ./test

If everything is correct you will observe that the LED blinks.

5.2 Code Sample in Shell

Create 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

If everything is correct you will observe that the LED blinks.

5.3 Code Sample in Python

You can refer to this link:WiringPi-Python for RK3399/zh

6 Regular WiringPi APIs

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 RK3399 has both the pull-up and 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).
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 November-14-2018

  • Released English Version