pihwm  r1
A lightweight C library for Raspberry Pi hardware modules.
pi_i2c.c
Go to the documentation of this file.
1 
28 #include <linux/i2c.h>
29 #include <linux/i2c-dev.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <sys/ioctl.h>
38 
39 #include "pihwm.h"
40 
53 int
55 {
56  int rev;
57 
58  if ( check_kernel_module("i2c_dev") < 0 )
59  {
60  debug("[%s] Kernel module \"i2c_dev\" not loaded.\n", __func__);
61  return -1;
62  }
63 
64  if ( check_kernel_module("i2c_bcm2708") < 0 )
65  {
66  debug("[%s] Kernel module \"i2c_bcm2708\" not loaded.\n", __func__);
67  return -1;
68  }
69 
70  rev = board_rev();
71 
72  if ( rev == REV_1 )
73  {
74  return i2c_init_name("/dev/i2c-0");
75  }
76  else if ( rev == REV_2 )
77  {
78  return i2c_init_name("/dev/i2c-1");
79  }
80  else
81  {
82  return -1;
83  }
84 
85 }
86 
96 int
97 i2c_init_name (char *devname)
98 {
99  int fd = open (devname, O_RDWR);
100 
101  if ( fd < 0 )
102  {
103  debug("[%s] Can't open %s : %s\n", __func__, devname, strerror (errno));
104  return -1;
105  }
106  else
107  {
108  return fd;
109  }
110 }
111 
120 int
121 i2c_select_device (unsigned int fd, unsigned int addr)
122 {
123  if ( ioctl (fd, I2C_SLAVE, addr) < 0 )
124  {
125  debug("[%s] Can't select device %s: %s\n", __func__, addr, strerror (errno));
126  return -1;
127  }
128  else
129  {
130  return 1;
131  }
132 }
133 
144 int
145 i2c_write (unsigned int fd, unsigned int addr, unsigned char *data, unsigned int len)
146 {
147 
148  if ( i2c_select_device (fd, addr) )
149  {
150  if ( write (fd, data, len) != len )
151  {
152  debug("[%s] I2C write (address: 0x%X) failed: %s\n", __func__, addr, strerror (errno));
153  return -1;
154  }
155  }
156  else
157  {
158  printf ("[%s] Can't select I2C device at address: 0x%X, write failed\n", __func__, addr);
159  return -1;
160  }
161 
162  return 1;
163 }
164 
175 int
176 i2c_read (unsigned int fd, unsigned int addr, unsigned char *data, unsigned int len)
177 {
178  char buf[len];
179 
180  if ( i2c_select_device (fd, addr) )
181  {
182  if ( read (fd, buf, len) != len )
183  {
184  debug("[%s]: I2C read (address: 0x%X) failed: %s\n", __func__, addr, strerror (errno));
185  return -1;
186  }
187  else
188  {
189  memcpy(data, buf, len);
190  return 1;
191  }
192  }
193  else
194  {
195  debug("[%s] Can't select I2C device at address: 0x%X, write failed\n", __func__, addr);
196  return -1;
197  }
198 }
199 
200 
201 /*
202 * FIXME: Test this.
203 int
204 i2c_read_write (unsigned int fd, unsigned int addr, unsigned char *write, unsigned char *read)
205 {
206 
207  struct i2c_msg msgs[2];
208  struct i2c_rdwr_ioctl_data rdwr;
209  int rc;
210 
211  msgs[0].addr = ADDR;
212  msgs[0].flags = 0;
213  msgs[0].buf = write_buf;
214  msgs[0].len = count(write_buf);
215 
216  msgs[1].addr = ADDR;
217  msgs[1].flags = I2C_M_RD;
218  msgs[1].buf = read_buf;
219  msgs[1].len = count(read_buf);
220 
221  rdwr.msgs = msgs;
222  rdwr.nmsgs = 2;
223 
224  if ( i2c_select_device (fd, addr) ){
225  // FIXME: add error checking here
226  ioctl( fd , I2C_RDWR , &rdwr );
227  } else {
228  debug ("[%s] Can't select I2C device at address: 0x%X, read failed\n", __func__, addr);
229  return -1;
230  }
231 
232  return 1;
233 }
234 */
235 
int i2c_write(unsigned int fd, unsigned int addr, unsigned char *data, unsigned int len)
Initiates an I2C write operation.
Definition: pi_i2c.c:145
int i2c_select_device(unsigned int fd, unsigned int addr)
Selects a specific I2C slave device.
Definition: pi_i2c.c:121
int i2c_init_name(char *devname)
Initialises the i2c-dev interface for the sysfs entry specified by the devname parameter.
Definition: pi_i2c.c:97
int i2c_init()
Initialises the i2c-dev interface for the I2C peripheral exposed on the P1 header (which is dependent...
Definition: pi_i2c.c:54
int check_kernel_module(char *modulename)
Check if the kernel module specified is loaded.
Definition: pihwm.c:224
int board_rev()
Return board revision.
Definition: pihwm.c:198
Header for general library functionality.
int i2c_read(unsigned int fd, unsigned int addr, unsigned char *data, unsigned int len)
Initiates an I2C read operation.
Definition: pi_i2c.c:176