wiringPi是对树莓派IO口编程的一个库。简化了各类GPIO、PWM、UART、I2C、SPI的编程接口。官网地址为
http://wiringpi.com/
以下是官网描述
- WiringPi is a GPIO access library written in C for the BCM2835 used in the Raspberry Pi. It’s released under the GNU LGPLv3 license and is usable from C and C++ and many other languages with suitable wrappers (See below) It’s designed to be familiar to people who have used the Arduino “wiring” system1
- The original Raspberry Pi Model A and B version B1 was a $35 single board computer with a 26-pin General Purpose Input/Output (GPIO) connector and this carries a set of signals and buses. There are 8 general purpose digital I/O pins – these can be programmed as either digital outputs or inputs. One of these pins can be designated for hardware PWM output too. Additionally there is a 2-wire I2C interface and a 4-wire SPI interface (with a 2nd select line, making it 5 pins in total) and the serial UART with a further 2 pins.
复制代码
安装很简单,这里采用直接从源码编译。
1:
[size=0.75em]git clone git://git.drogon.net/wiringPi
[size=0.75em]2: [size=0.75em]cd wiringPi
[size=0.75em]3: git pull origin
[size=0.75em]4: [size=0.75em]cd wiringPi
[size=0.75em]5: ./build
搞定!
IO口默认基本都是GPIO-IN功能,使用I2C之前必须先切换到I2C功能
输入sudo raspi-config
测试如下:
gpio readall显示所有引脚状态
连接I2C引脚到外部I2C模块。手头上的是微雪的FRAM模块,地址是0x50~0x57,后面的0~7分别是不同的page。
使用i2cdetect可见
采用i2cdump可查看fram里面的内容
自己编写C代码如下:
- #include <stdio.h>
- #include <wiringPiI2C.h>
- int main(void)
- {
- int buf[10] = {0};
- const int devId = 0b1010000;
- int fd = wiringPiI2CSetup(devId);
- if (fd < 0) {
- printf("Error setup I2C device %b\n", devId);
- exit(1);
- }
- buf[0] = wiringPiI2CRead(fd);
- printf("buf = %c\n", buf[0]);
- return 0;
- }
复制代码编译如下:
gcc -I /usr/local/include/ -L /usr/local/lib/ -lwiringPi test_wiringPiI2C.c -o test_wiringPiI2C
最终读取的结果与i2cdump的结果相同;