chiark / gitweb /
Lots of changes here. Added new I2C test code, a new serialTest program,
[wiringPi.git] / wiringPi / wiringPiI2C.c
1 /*
2  * wiringPiI2C.c:
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/
8  *
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.
13  *
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.
18  *
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  ***********************************************************************
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <sys/ioctl.h>
29 #include <linux/i2c-dev.h>
30
31 #include "wiringPi.h"
32 #include "wiringPiI2C.h"
33
34
35 /*
36  * wiringPiI2CRead:
37  *      Simple device read
38  *********************************************************************************
39  */
40
41 int wiringPiI2CRead (int fd)
42 {
43   return i2c_smbus_read_byte (fd) ;
44 }
45
46
47 /*
48  * wiringPiI2CReadReg8: wiringPiI2CReadReg16:
49  *      Read an 8 or 16-bit value from a regsiter on the device
50  *********************************************************************************
51  */
52
53 int wiringPiI2CReadReg8 (int fd, int reg)
54 {
55   return i2c_smbus_read_byte_data (fd, reg) ;
56 }
57
58 int wiringPiI2CReadReg16 (int fd, int reg)
59 {
60   return i2c_smbus_read_word_data (fd, reg) ;
61 }
62
63
64 /*
65  * wiringPiI2CWrite:
66  *      Simple device write
67  *********************************************************************************
68  */
69
70 int wiringPiI2CWrite (int fd, int data)
71 {
72   return i2c_smbus_write_byte (fd, data) ;
73 }
74
75
76 /*
77  * wiringPiI2CWriteReg8: wiringPiI2CWriteReg16:
78  *      Write an 8 or 16-bit value to the given register
79  *********************************************************************************
80  */
81
82 int wiringPiI2CWriteReg8 (int fd, int reg, int data)
83 {
84   return i2c_smbus_write_byte_data (fd, reg, data) ;
85 }
86
87 int wiringPiI2CWriteReg16 (int fd, int reg, int data)
88 {
89   return i2c_smbus_write_word_data (fd, reg, data) ;
90 }
91
92
93 /*
94  * wiringPiI2CSetup:
95  *      Open the I2C device, and regsiter the target device
96  *********************************************************************************
97  */
98
99 int wiringPiI2CSetup (int devId)
100 {
101   int rev, fd ;
102   char *device ;
103
104   if ((rev = piBoardRev ()) < 0)
105   {
106     fprintf (stderr, "wiringPiI2CSetup: Unable to determine Pi board revision\n") ;
107     exit (1) ;
108   }
109
110   if (rev == 1)
111     device = "/dev/i2c-0" ;
112   else
113     device = "/dev/i2c-1" ;
114
115   if ((fd = open (device, O_RDWR)) < 0)
116     return -1 ;
117
118   if (ioctl (fd, I2C_SLAVE, devId) < 0)
119     return -1 ;
120
121   return fd ;
122 }