Difference between revisions of "WiringPi for RK3399"

From FriendlyELEC WiKi
Jump to: navigation, search
(updated by API)
 
(7 intermediate revisions by one other user not shown)
Line 26: Line 26:
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
 
# Delete "old" version
 
# Delete "old" version
wget http://112.124.9.243:8888/wiringpi/friendlyelec-rk3399/remove_oldversion_wiringPi.sh
+
wget http://112.124.9.243/wiringpi/remove_oldversion_wiringPi.sh
 
chmod 755 remove_oldversion_wiringPi.sh
 
chmod 755 remove_oldversion_wiringPi.sh
 
sudo ./remove_oldversion_wiringPi.sh
 
sudo ./remove_oldversion_wiringPi.sh
 
# Download and install wiringPi for RK3399
 
# Download and install wiringPi for RK3399
wget http://112.124.9.243:8888/wiringpi/friendlyelec-rk3399/wiringpi-v2.44-friendlyelec-rk3399.deb
+
wget wget http://112.124.9.243/wiringpi/wiringpi-v2.44-friendlyelec-rk3399.deb
 
sudo dpkg -i  wiringpi-v2.44-friendlyelec-rk3399.deb
 
sudo dpkg -i  wiringpi-v2.44-friendlyelec-rk3399.deb
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 138: Line 138:
 
You can refer to this link:[[WiringPi-Python for RK3399/zh]]
 
You can refer to this link:[[WiringPi-Python for RK3399/zh]]
  
==wiringPi 常用API速查==
+
==Regular WiringPi APIs==
=== 初始化函数 ===
+
===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 RK3399 has both the pull-up and 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 />
该函数在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==
==更新日志==
+
===November-14-2018===
===2018-11-10===
+
* Released English Version
首次发布
+

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