-
Notifications
You must be signed in to change notification settings - Fork 7
/
i2c.c
284 lines (239 loc) · 6.03 KB
/
i2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* peeknpoke I2C access file
*
* Copyright (c) 2012, Intel Corporation.
* Hari Kanigeri <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "pnp_utils_inc.h"
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#define I2C_DEV_PATH "/dev/i2c-%d"
struct i2c_message {
uint16_t addr; /*slave address*/
uint16_t flags;
uint16_t len; /* msg length*/
uint8_t *buf; /* pointer to msg data */
};
#define MAX_SMBUS_BLOCK_SIZE 32
#define DEF_I2C_SLAVE_FORCE 0x0706
#define DEF_I2C_SMBUS 0x0720
union i2c_smbus_data {
uint8_t byte;
uint16_t word;
uint8_t block[MAX_SMBUS_BLOCK_SIZE + 2];
};
struct i2c_smbus_ioctl_data {
char read_write;
uint8_t command;
int size;
union i2c_smbus_data *data;
};
int write_i2c_device(char bus, int addr, int reg, int size, int value)
{
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data data;
char i2c_dev[15];
int fd;
int ret;
snprintf(i2c_dev, sizeof(i2c_dev), I2C_DEV_PATH, bus);
printf("%s\n", i2c_dev);
fd = open(i2c_dev, O_RDWR | O_SYNC);
if (fd == -1) {
printf("Error opening %d\n", fd);
return -1;
}
ret = ioctl(fd, DEF_I2C_SLAVE_FORCE, addr);
if (ret == -1) {
printf("Error with the ioctl for address 0x%x\n", addr);
goto failed;
}
switch (size) {
case SMBUS_BYTE_DATA:
data.byte = value & 0xff;
break;
case SMBUS_WORD_DATA:
data.word = value & 0xffff;
break;
#if 0
case SMBUS_BLOCK_DATA:
if (array_size > (MAX_SMBUS_BLOCK_SIZE + 2)) {
printf("Error: data size for block transfer\n");
return -1;
}
memcpy(value, args.data, array_size);
break;
#endif
default:
ret = -1;
printf("Error size provided %d\n", size);
goto failed;
}
args.read_write = 0;
args.command = reg;
args.size = size;
args.data = &data;
ret = ioctl(fd, DEF_I2C_SMBUS, &args);
if (ret) {
printf("Failed to communicate with Device: error = %d\n", ret);
goto failed;
}
failed:
close(fd);
return ret;
}
int block_write_i2c_device(char bus, int addr, int reg, int size, uint8_t array_size, uint8_t *values)
{
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data data;
char i2c_dev[15];
int fd;
int ret;
snprintf(i2c_dev, sizeof(i2c_dev), I2C_DEV_PATH, bus);
fd = open(i2c_dev, O_RDWR | O_SYNC);
if (fd == -1) {
printf("Error opening %d\n", fd);
return -1;
}
ret = ioctl(fd, DEF_I2C_SLAVE_FORCE, addr);
if (ret == -1) {
printf("Error with the ioctl for address 0x%x\n", addr);
goto failed;
}
if (array_size > (MAX_SMBUS_BLOCK_SIZE + 1)) {
printf("Error: data size for block transfer\n");
ret = -1;
goto failed;
}
data.block[0] = array_size;
memcpy(&data.block[1], values, array_size);
args.read_write = 0;
args.command = reg;
args.size = size;
args.data = &data;
ret = ioctl(fd, DEF_I2C_SMBUS, &args);
if (ret) {
printf("Failed to communicate with Device: error = %d\n", ret);
goto failed;
}
failed:
close(fd);
return ret;
}
int read_i2c_device(char bus, int addr, int reg, int size, int *result)
{
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data data;
char i2c_dev[15];
int fd;
int ret;
if (result == NULL) {
printf("invalid input buffer \n");
return -1;
}
/*data.word = 0;*/
memset(&data, 0, sizeof(union i2c_smbus_data));
snprintf(i2c_dev, sizeof(i2c_dev), I2C_DEV_PATH, bus);
printf("%s\n", i2c_dev);
fd = open(i2c_dev, O_RDWR | O_SYNC);
printf("the value of /dev/i2c-%d is %d\n", bus, fd);
if (fd == -1) {
printf("Error opening %d\n", fd);
return -1;
}
ret = ioctl(fd, DEF_I2C_SLAVE_FORCE, addr);
if (ret == -1) {
printf("Error with the ioctl for address 0x%x\n", addr);
goto failed;
}
switch (size) {
case SMBUS_BYTE_DATA:
case SMBUS_WORD_DATA:
//case SMBUS_BLOCK_DATA:
args.size = size;
break;
default:
printf("Error size provided %d\n", size);
ret = -1;
goto failed;
}
args.read_write = 1;
args.command = reg;
args.data = &data;
ret = ioctl(fd, DEF_I2C_SMBUS, &args);
if (ret) {
printf("Failed to communicate with Device: error = %d\n", ret);
goto failed;
}
if (size == SMBUS_WORD_DATA)
*result = data.word;
else if (size == SMBUS_BYTE_DATA)
*result = data.byte;
printf("Result = 0x%x, reg=0x%x ret=0x%x\n", *result, reg, ret);
failed:
close(fd);
return ret;
}
int block_read_i2c_device(char bus, int addr, int reg, int size, uint8_t array_size, uint8_t *result)
{
struct i2c_smbus_ioctl_data args;
union i2c_smbus_data data;
char i2c_dev[15];
int fd;
int ret = 0;
int i;
int num_values;
if (result == NULL) {
printf("invalid input buffer \n");
return -1;
}
memset(&data, 0, sizeof(union i2c_smbus_data));
snprintf(i2c_dev, sizeof(i2c_dev), I2C_DEV_PATH, bus);
fd = open(i2c_dev, O_RDWR | O_SYNC);
if (fd == -1) {
printf("Error opening %d\n", fd);
return -1;
}
ret = ioctl(fd, DEF_I2C_SLAVE_FORCE, addr);
if (ret == -1) {
printf("Error with the ioctl for address 0x%x\n", addr);
goto failed;
}
data.block[0] = array_size - 1;
args.size = size;
args.read_write = 1;
args.command = reg;
args.data = &data;
ret = ioctl(fd, DEF_I2C_SMBUS, &args);
if (ret) {
printf("Failed to communicate with Device: error = %d\n", ret);
goto failed;
}
if (size == SMBUS_BLOCK_DATA || size == I2C_SMBUS_BLOCK_DATA) {
num_values = array_size;
for (i = 1; i < num_values; i++)
printf("value read = 0x%x ret=0x%x\n", data.block[i], ret);
}
memcpy(result, &data.block[1], array_size);
failed:
close(fd);
return ret;
}