pihwm
A lightweight C library for Raspberry Pi hardware modules.
 All Data Structures Files Functions Groups Pages
pi_i2c.c
Go to the documentation of this file.
1 
28 #include "config.h"
29 
30 #include <linux/i2c.h>
31 #include <linux/i2c-dev.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <stdint.h>
37 #include <errno.h>
38 #include <string.h>
39 #include <sys/ioctl.h>
40 
41 #include "pihwm.h"
42 #include "pi_i2c.h"
43 
56 int
58 {
59  int rev;
60 
61  if ( check_kernel_module("i2c_dev") < 0 )
62  {
63  debug("[%s] Kernel module \"i2c_dev\" not loaded.\n", __func__);
64  return -1;
65  }
66 
67  if ( check_kernel_module("i2c_bcm2708") < 0 )
68  {
69  debug("[%s] Kernel module \"i2c_bcm2708\" not loaded.\n", __func__);
70  return -1;
71  }
72 
73  rev = board_rev();
74 
75  if ( rev == REV_1 )
76  {
77  return i2c_init_name("/dev/i2c-0");
78  }
79  else if ( rev == REV_2 )
80  {
81  return i2c_init_name("/dev/i2c-1");
82  }
83  else
84  {
85  return -1;
86  }
87 
88 }
89 
99 int
100 i2c_init_name (char *devname)
101 {
102  int fd = open (devname, O_RDWR);
103 
104  if ( fd < 0 )
105  {
106  debug("[%s] Can't open %s : %s\n", __func__, devname, strerror (errno));
107  return -1;
108  }
109  else
110  {
111  return fd;
112  }
113 }
114 
123 int
124 i2c_select_device (unsigned int fd, unsigned int addr)
125 {
126  if ( ioctl (fd, I2C_SLAVE, addr) < 0 )
127  {
128  debug("[%s] Can't select device %s: %s\n", __func__, addr, strerror (errno));
129  return -1;
130  }
131  else
132  {
133  return 1;
134  }
135 }
136 
147 int
148 i2c_write (unsigned int fd, unsigned int addr, unsigned char *data, unsigned int len)
149 {
150 
151  if ( i2c_select_device (fd, addr) )
152  {
153  if ( write (fd, data, len) != len )
154  {
155  debug("[%s] I2C write (address: 0x%X) failed: %s\n", __func__, addr, strerror (errno));
156  return -1;
157  }
158  }
159  else
160  {
161  printf ("[%s] Can't select I2C device at address: 0x%X, write failed\n", __func__, addr);
162  return -1;
163  }
164 
165  return 1;
166 }
167 
178 int
179 i2c_read (unsigned int fd, unsigned int addr, unsigned char *data, unsigned int len)
180 {
181  char buf[len];
182 
183  if ( i2c_select_device (fd, addr) )
184  {
185  if ( read (fd, buf, len) != len )
186  {
187  debug("[%s]: I2C read (address: 0x%X) failed: %s\n", __func__, addr, strerror (errno));
188  return -1;
189  }
190  else
191  {
192  memcpy(data, buf, len);
193  return 1;
194  }
195  }
196  else
197  {
198  debug("[%s] Can't select I2C device at address: 0x%X, write failed\n", __func__, addr);
199  return -1;
200  }
201 }
202 
203 
204 /*
205 * FIXME: Test this.
206 int
207 i2c_read_write (unsigned int fd, unsigned int addr, unsigned char *write, unsigned char *read)
208 {
209 
210  struct i2c_msg msgs[2];
211  struct i2c_rdwr_ioctl_data rdwr;
212  int rc;
213 
214  msgs[0].addr = ADDR;
215  msgs[0].flags = 0;
216  msgs[0].buf = write_buf;
217  msgs[0].len = count(write_buf);
218 
219  msgs[1].addr = ADDR;
220  msgs[1].flags = I2C_M_RD;
221  msgs[1].buf = read_buf;
222  msgs[1].len = count(read_buf);
223 
224  rdwr.msgs = msgs;
225  rdwr.nmsgs = 2;
226 
227  if ( i2c_select_device (fd, addr) ){
228  // FIXME: add error checking here
229  ioctl( fd , I2C_RDWR , &rdwr );
230  } else {
231  debug ("[%s] Can't select I2C device at address: 0x%X, read failed\n", __func__, addr);
232  return -1;
233  }
234 
235  return 1;
236 }
237 */
238