3 * Simplified I2C access routines
4 * Copyright (c) 2013 Gordon Henderson
5 ***********************************************************************
6 * This file is part of wiringPi:
7 * https://projects.drogon.net/raspberry-pi/wiringpi/
9 * wiringPi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
14 * wiringPi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with wiringPi.
21 * If not, see <http://www.gnu.org/licenses/>.
22 ***********************************************************************
28 #include <sys/ioctl.h>
29 #include <linux/i2c-dev.h>
32 #include "wiringPiI2C.h"
38 *********************************************************************************
41 int wiringPiI2CRead (int fd)
43 return i2c_smbus_read_byte (fd) ;
48 * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
49 * Read an 8 or 16-bit value from a regsiter on the device
50 *********************************************************************************
53 int wiringPiI2CReadReg8 (int fd, int reg)
55 return i2c_smbus_read_byte_data (fd, reg) ;
58 int wiringPiI2CReadReg16 (int fd, int reg)
60 return i2c_smbus_read_word_data (fd, reg) ;
67 *********************************************************************************
70 int wiringPiI2CWrite (int fd, int data)
72 return i2c_smbus_write_byte (fd, data) ;
77 * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
78 * Write an 8 or 16-bit value to the given register
79 *********************************************************************************
82 int wiringPiI2CWriteReg8 (int fd, int reg, int data)
84 return i2c_smbus_write_byte_data (fd, reg, data) ;
87 int wiringPiI2CWriteReg16 (int fd, int reg, int data)
89 return i2c_smbus_write_word_data (fd, reg, data) ;
95 * Open the I2C device, and regsiter the target device
96 *********************************************************************************
99 int wiringPiI2CSetup (int devId)
104 if ((rev = piBoardRev ()) < 0)
106 fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
111 device = "/dev/i2c-0" ;
113 device = "/dev/i2c-1" ;
115 if ((fd = open (device, O_RDWR)) < 0)
118 if (ioctl (fd, I2C_SLAVE, devId) < 0)