【1】wiringPi是仅应用于树莓派平台的GPIO控制库函数,遵循GNU LGPLv3开源协议,并由GIT工具维护,任何人都可以免费使用该软件包。wiringPi使用C或者C++开发,可以很方便的被其它语言转换调用,例如python、ruby或者PHP等。WiringPi中的函数类似于Arduino的wiring系统,这使得熟悉arduino的用户使用wringPi更为方便。
【2】wiringPi包括一套gpio控制命令,使用gpio命令时可以很方便的控制树莓派GPIO管脚。用户可以通过shell脚本利用gpio命令控制或查询GPIO管脚。树莓派具有26个普通IO引脚,利用端口复用时支持I2C、SPI和UART通信协议,不使用复用时也可以作为GPIO使用。
【3】wiringPi可以扩展,用户可以利用wiringPi的内部模块或把自定义的扩展模块集成到wiringPi中以扩展更多的GPIO接口或实现更多的功能。
wiringPi官网:http://wiringpi.com/
作者:Gordon Henderson
wiringPi作者声明:
WiringPi is developed directly on a Raspberry Pi running 32-bit Raspbian. I do not support any other platform, cross compiling or operating systems.
翻译:WiringPi是直接在树莓派32位操作系统Raspbin上开发的,不支持其它操作系统、平台或交叉编译环境。
2.1、安装wiringPi
$sudo apt-get install wiringpi
//如果需要更新WiringPi,可以使用系统更新命令:
$sudo apt-get update
$sudo apt-get upgrade
WiringPi安装完成后,可以使用下面的命令测试是否安装成功:
$sudo gpio -v
root@raspberrypi:~# gpio -v
gpio version: 2.50
Copyright (c) 2012-2018 Gordon Henderson
This is free software with ABSOLUTELY NO WARRANTY.
For details type: gpio -warranty
Raspberry Pi Details:
Type: Pi Zero-W, Revision: 01, Memory: 512MB, Maker: Sony
* Device tree is enabled.
*--> Raspberry Pi Zero W Rev 1.1
* This Raspberry Pi supports user-level GPIO access.
root@raspberrypi:~#
2.2、WiringPi的引脚定义
WiringPi对树莓派物理引脚进行了封装,定义了一套自己的引脚编号。
如果要查看当前树莓派的引脚编号,可以使用如下命令:
$sudo gpio readall
root@raspberrypi:~# gpio readall
+-----+-----+---------+------+---+-Pi ZeroW-+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| | | 3.3v | | | 1 || 2 | | | 5v | | |
| 2 | 8 | SDA.1 | IN | 1 | 3 || 4 | | | 5v | | |
| 3 | 9 | SCL.1 | IN | 1 | 5 || 6 | | | 0v | | |
| 4 | 7 | GPIO. 7 | IN | 1 | 7 || 8 | 1 | ALT0 | TxD | 15 | 14 |
| | | 0v | | | 9 || 10 | 1 | ALT0 | RxD | 16 | 15 |
| 17 | 0 | GPIO. 0 | IN | 1 | 11 || 12 | 0 | IN | GPIO. 1 | 1 | 18 |
| 27 | 2 | GPIO. 2 | IN | 0 | 13 || 14 | | | 0v | | |
| 22 | 3 | GPIO. 3 | IN | 0 | 15 || 16 | 0 | IN | GPIO. 4 | 4 | 23 |
| | | 3.3v | | | 17 || 18 | 0 | IN | GPIO. 5 | 5 | 24 |
| 10 | 12 | MOSI | IN | 0 | 19 || 20 | | | 0v | | |
| 9 | 13 | MISO | IN | 0 | 21 || 22 | 0 | IN | GPIO. 6 | 6 | 25 |
| 11 | 14 | SCLK | IN | 0 | 23 || 24 | 1 | IN | CE0 | 10 | 8 |
| | | 0v | | | 25 || 26 | 1 | IN | CE1 | 11 | 7 |
| 0 | 30 | SDA.0 | IN | 1 | 27 || 28 | 1 | IN | SCL.0 | 31 | 1 |
| 5 | 21 | GPIO.21 | IN | 0 | 29 || 30 | | | 0v | | |
| 6 | 22 | GPIO.22 | IN | 0 | 31 || 32 | 0 | IN | GPIO.26 | 26 | 12 |
| 13 | 23 | GPIO.23 | IN | 0 | 33 || 34 | | | 0v | | |
| 19 | 24 | GPIO.24 | IN | 0 | 35 || 36 | 0 | IN | GPIO.27 | 27 | 16 |
| 26 | 25 | GPIO.25 | IN | 0 | 37 || 38 | 0 | IN | GPIO.28 | 28 | 20 |
| | | 0v | | | 39 || 40 | 0 | IN | GPIO.29 | 29 | 21 |
+-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+
| BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM |
+-----+-----+---------+------+---+-Pi ZeroW-+---+------+---------+-----+-----+
root@raspberrypi:~#
至此,wiringPi已经安装到了树莓派ZeroW上。
3.1 WiringPi配置函数(WiringPi Setup functions);
3.2 WiringPi核心函数(WiringPi Core functions);
3.3 树莓派专用函数(Raspberry Pi Specific functions);
3.4 时间函数(Time functions);
3.5 程序优先级/中断/线程函数(Process priority/interrupts/thread functions);
3.6 串口通信库函数(Serial Library functions);
3.7 SPI通信库函数(SPI Library functions);
3.8 I2C通信库函数(IC2 Library functions);
3.9 移位库函数(Shift Library functions);
3.10 软件PWM库函数(Software PWM Library functions);
3.11 软件方波/音频库函数(Software Tone Library functions);
wiringPi安装后,会将相应的*.h头文件添加到‘/usr/include’目录下
/usr/local/include/wiringPi.h 提供基础的服务,包括GPIO。
/usr/local/include/wiringPiI2C.h 提供I2C相关支持。
/usr/local/include/wiringPiSPI.h 提供SPI相关支持。
/usr/local/include/wiringPiShift.h
root@raspberrypi:/usr/lib# cd /usr/include/
root@raspberrypi:/usr/include# ls
ads1115.h drcSerial.h gertboard.h linux monetary.h piGlow.h rpc stdlib.h values.h
aio.h ds1302.h getopt.h locale.h mqueue.h piNes.h rpcsvc string.h video
aliases.h ds18b20.h glob.h malloc.h mtd pngconf.h sched.h strings.h wait.h
alloca.h elf.h gnumake.h math.h net png.h scrollPhat.h stropts.h wchar.h
argp.h endian.h gnu-versions.h max31855.h netash pnglibconf.h scsi sudo_plugin.h wctype.h
argz.h envz.h grp.h max5322.h netatalk poll.h search.h syscall.h wiringPi.h
ar.h err.h gshadow.h maxdetect.h netax25 printf.h semaphore.h sysexits.h wiringPiI2C.h
arm-linux-gnueabihf errno.h htu21d.h mcheck.h netdb.h proc_service.h setjmp.h syslog.h wiringPiSPI.h
arpa error.h iconv.h mcp23008.h neteconet protocols sgtty.h tar.h wiringSerial.h
asm-generic execinfo.h ifaddrs.h mcp23016.h netinet pseudoPins.h shadow.h termio.h wiringShift.h
assert.h expat_external.h inttypes.h mcp23016reg.h netipx pthread.h signal.h termios.h wordexp.h
bmp180.h expat.h iproute2 mcp23017.h netiucv pty.h sn3218.h tgmath.h wpiExtensions.h
byteswap.h fcntl.h langinfo.h mcp23s08.h netpacket pwd.h softPwm.h thread_db.h xen
c++ features.h lastlog.h mcp23s17.h netrom python2.7 softServo.h threads.h zconf.h
cifsidmap.h fenv.h lcd128x64.h mcp23x0817.h netrose python3.7 softTone.h time.h zlib.h
clif.h fmtmsg.h lcd.h mcp23x08.h nfs python3.7m sound ttyent.h
complex.h fnmatch.h libgen.h mcp3002.h nl_types.h rdma spawn.h uchar.h
cpio.h freetype2 libintl.h mcp3004.h nss.h re_comp.h sr595.h ucontext.h
crypt.h fstab.h libmnl mcp3422.h obstack.h regex.h stab.h ulimit.h
ctype.h fts.h libpng mcp4802.h paths.h regexp.h stdc-predef.h unistd.h
dirent.h ftw.h libpng16 memory.h pcf8574.h reglib stdint.h utime.h
dlfcn.h gconv.h limits.h misc pcf8591.h resolv.h stdio_ext.h utmp.h
drcNet.h gdb link.h mntent.h piFace.h rht03.h stdio.h utmpx.h
root@raspberrypi:/usr/include#
调用的时候只需要在代码中添加#include <wiringPi.h>即可,同理还有I2C、SPI、UART等头文件。
#include <wiringPi.h>
#include <stdio.h>
#define PIN 5
int main(void)
{
char i=0;
wiringPiSetup();
pinMode (PIN, OUTPUT);
for(i=0;;i++)
{
printf("gpio out: %d\r\n", i);
digitalWrite(PIN, i%2);
delay(500);
}
printf("gpio out end\r\n");
return 0;
}
然后使用
gcc main.c -o main -lwiringPi进行编译
然后使用./main进行执行
root@raspberrypi:/usr/include# cd /home/pi/T001_WiringPi_blink/
root@raspberrypi:/home/pi/T001_WiringPi_blink# ls
gpio_out gpio_out.c main main.c uart_inout uart_inout.c
root@raspberrypi:/home/pi/T001_WiringPi_blink# gcc main.c -o main -lwiringPi
root@raspberrypi:/home/pi/T001_WiringPi_blink# ./main
gpio out: 0
gpio out: 1
gpio out: 2
gpio out: 3
gpio out: 4
gpio out: 5
gpio out: 6
gpio out: 7
gpio out: 8
gpio out: 9
gpio out: 10
gpio out: 11
gpio out: 12
gpio out: 13
gpio out: 14
gpio out: 15
gpio out: 16
gpio out: 17
gpio out: 18
gpio out: 19
gpio out: 20
gpio out: 21
gpio out: 22
gpio out: 23
gpio out: 24
gpio out: 25
gpio out: 26
gpio out: 27
gpio out: 28
gpio out: 29
gpio out: 30
gpio out: 31
gpio out: 32
gpio out: 33
gpio out: 34
gpio out: 35
gpio out: 36
gpio out: 37
gpio out: 38
gpio out: 39
gpio out: 40
^Z
[1]+ Stopped ./main
root@raspberrypi:/home/pi/T001_WiringPi_blink#