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 ***********************************************************************
27 * The Linux I2C code is actually the same (almost) as the SMBus code.
28 * SMBus is System Management Bus - and in essentially I2C with some
29 * additional functionality added, and stricter controls on the electrical
30 * specifications, etc. however I2C does work well with it and the
31 * protocols work over both.
33 * I'm directly including the SMBus functions here as some Linux distros
34 * lack the correct header files, and also some header files are GPLv2
35 * rather than the LGPL that wiringPi is released under - presumably because
36 * originally no-one expected I2C/SMBus to be used outside the kernel -
37 * however enter the Raspberry Pi with people now taking directly to I2C
38 * devices without going via the kernel...
40 * This may ultimately reduce the flexibility of this code, but it won't be
41 * hard to maintain it and keep it current, should things change.
43 * Information here gained from: kernel/Documentation/i2c/dev-interface
44 * as well as other online resources.
45 *********************************************************************************
54 #include <sys/ioctl.h>
57 #include "wiringPiI2C.h"
61 #define I2C_SLAVE 0x0703
62 #define I2C_SMBUS 0x0720 /* SMBus-level access */
64 #define I2C_SMBUS_READ 1
65 #define I2C_SMBUS_WRITE 0
67 // SMBus transaction types
69 #define I2C_SMBUS_QUICK 0
70 #define I2C_SMBUS_BYTE 1
71 #define I2C_SMBUS_BYTE_DATA 2
72 #define I2C_SMBUS_WORD_DATA 3
73 #define I2C_SMBUS_PROC_CALL 4
74 #define I2C_SMBUS_BLOCK_DATA 5
75 #define I2C_SMBUS_I2C_BLOCK_BROKEN 6
76 #define I2C_SMBUS_BLOCK_PROC_CALL 7 /* SMBus 2.0 */
77 #define I2C_SMBUS_I2C_BLOCK_DATA 8
81 #define I2C_SMBUS_BLOCK_MAX 32 /* As specified in SMBus standard */
82 #define I2C_SMBUS_I2C_BLOCK_MAX 32 /* Not specified but we use same structure */
84 // Structures used in the ioctl() calls
90 uint8_t block [I2C_SMBUS_BLOCK_MAX + 2] ; // block [0] is used for length + one more for PEC
93 struct i2c_smbus_ioctl_data
98 union i2c_smbus_data *data ;
101 static inline int i2c_smbus_access (int fd, char rw, uint8_t command, int size, union i2c_smbus_data *data)
103 struct i2c_smbus_ioctl_data args ;
105 args.read_write = rw ;
106 args.command = command ;
109 return ioctl (fd, I2C_SMBUS, &args) ;
116 *********************************************************************************
119 int wiringPiI2CRead (int fd)
121 union i2c_smbus_data data ;
123 if (i2c_smbus_access (fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data))
126 return data.byte & 0xFF ;
131 * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
132 * Read an 8 or 16-bit value from a regsiter on the device
133 *********************************************************************************
136 int wiringPiI2CReadReg8 (int fd, int reg)
138 union i2c_smbus_data data;
140 if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_BYTE_DATA, &data))
143 return data.byte & 0xFF ;
146 int wiringPiI2CReadReg16 (int fd, int reg)
148 union i2c_smbus_data data;
150 if (i2c_smbus_access (fd, I2C_SMBUS_READ, reg, I2C_SMBUS_WORD_DATA, &data))
153 return data.word & 0xFFFF ;
159 * Simple device write
160 *********************************************************************************
163 int wiringPiI2CWrite (int fd, int data)
165 return i2c_smbus_access (fd, I2C_SMBUS_WRITE, data, I2C_SMBUS_BYTE, NULL) ;
170 * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
171 * Write an 8 or 16-bit value to the given register
172 *********************************************************************************
175 int wiringPiI2CWriteReg8 (int fd, int reg, int value)
177 union i2c_smbus_data data ;
180 return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_BYTE_DATA, &data) ;
183 int wiringPiI2CWriteReg16 (int fd, int reg, int value)
185 union i2c_smbus_data data ;
188 return i2c_smbus_access (fd, I2C_SMBUS_WRITE, reg, I2C_SMBUS_WORD_DATA, &data) ;
193 * wiringPiI2CSetupInterface:
194 * Undocumented access to set the interface explicitly - might be used
195 * for the Pi's 2nd I2C interface...
196 *********************************************************************************
199 int wiringPiI2CSetupInterface (const char *device, int devId)
203 if ((fd = open (device, O_RDWR)) < 0)
204 return wiringPiFailure (WPI_ALMOST, "Unable to open I2C device: %s\n", strerror (errno)) ;
206 if (ioctl (fd, I2C_SLAVE, devId) < 0)
207 return wiringPiFailure (WPI_ALMOST, "Unable to select I2C device: %s\n", strerror (errno)) ;
215 * Open the I2C device, and regsiter the target device
216 *********************************************************************************
219 int wiringPiI2CSetup (const int devId)
224 rev = piBoardRev () ;
227 device = "/dev/i2c-0" ;
229 device = "/dev/i2c-1" ;
231 return wiringPiI2CSetupInterface (device, devId) ;