at24c32 access library
smbus_access.h
Go to the documentation of this file.
1 /* @file */
9 #ifndef __SMBUS_ACCESS_H__
10 #define __SMBUS_ACCESS_H__
11 #include <stdio.h>
12 static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
13  int size, union i2c_smbus_data *data)
14 {
15  struct i2c_smbus_ioctl_data args;
16 
17  args.read_write = read_write;
18  args.command = command;
19  args.size = size;
20  args.data = data;
21  return ioctl(file,I2C_SMBUS,&args);
22 }
23 
24 
25 static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
26 {
27  return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
28 }
29 
30 static inline __s32 i2c_smbus_read_byte(int file)
31 {
32  union i2c_smbus_data data;
33  if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
34  return -1;
35  else
36  return 0x0FF & data.byte;
37 }
38 
39 static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
40 {
41  return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
42  I2C_SMBUS_BYTE,NULL);
43 }
44 
45 static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
46 {
47  union i2c_smbus_data data;
48  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
49  I2C_SMBUS_BYTE_DATA,&data))
50  return -1;
51  else
52  return 0x0FF & data.byte;
53 }
54 
55 static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command,
56  __u8 value)
57 {
58  union i2c_smbus_data data;
59  data.byte = value;
60  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
61  I2C_SMBUS_BYTE_DATA, &data);
62 }
63 
64 static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
65 {
66  union i2c_smbus_data data;
67  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
68  I2C_SMBUS_WORD_DATA,&data))
69  return -1;
70  else
71  return 0x0FFFF & data.word;
72 }
73 
74 static inline __s32 i2c_smbus_write_word_data(int file, __u8 command,
75  __u16 value)
76 {
77  union i2c_smbus_data data;
78  data.word = value;
79  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
80  I2C_SMBUS_WORD_DATA, &data);
81 }
82 
83 static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
84 {
85  union i2c_smbus_data data;
86  data.word = value;
87  if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
88  I2C_SMBUS_PROC_CALL,&data))
89  return -1;
90  else
91  return 0x0FFFF & data.word;
92 }
93 
94 
95 /* Returns the number of read bytes */
96 static inline __s32 i2c_smbus_read_block_data(int file, __u8 command,
97  __u8 *values)
98 {
99  union i2c_smbus_data data;
100  int i;
101  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
102  I2C_SMBUS_BLOCK_DATA,&data))
103  return -1;
104  else {
105  for (i = 1; i <= data.block[0]; i++)
106  values[i-1] = data.block[i];
107  return data.block[0];
108  }
109 }
110 
111 static inline __s32 i2c_smbus_write_block_data(int file, __u8 command,
112  __u8 length, __u8 *values)
113 {
114  union i2c_smbus_data data;
115  int i;
116  if (length > 32)
117  length = 32;
118  for (i = 1; i <= length; i++)
119  data.block[i] = values[i-1];
120  data.block[0] = length;
121  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
122  I2C_SMBUS_BLOCK_DATA, &data);
123 }
124 
125 /* Returns the number of read bytes */
126 static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
127  __u8 *values)
128 {
129  union i2c_smbus_data data;
130  int i;
131  if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
132  I2C_SMBUS_I2C_BLOCK_DATA,&data))
133  return -1;
134  else {
135  for (i = 1; i <= data.block[0]; i++)
136  values[i-1] = data.block[i];
137  return data.block[0];
138  }
139 }
140 
141 static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
142  __u8 length, __u8 *values)
143 {
144  union i2c_smbus_data data;
145  int i;
146  if (length > 32)
147  length = 32;
148  for (i = 1; i <= length; i++)
149  data.block[i] = values[i-1];
150  data.block[0] = length;
151  return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
152  I2C_SMBUS_I2C_BLOCK_DATA, &data);
153 }
154 
155 /* Returns the number of read bytes */
156 static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
157  __u8 length, __u8 *values)
158 {
159  union i2c_smbus_data data;
160  int i;
161  if (length > 32)
162  length = 32;
163  for (i = 1; i <= length; i++)
164  data.block[i] = values[i-1];
165  data.block[0] = length;
166  if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
167  I2C_SMBUS_BLOCK_PROC_CALL,&data))
168  return -1;
169  else {
170  for (i = 1; i <= data.block[0]; i++)
171  values[i-1] = data.block[i];
172  return data.block[0];
173  }
174 }
175 #endif