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