-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.c
More file actions
344 lines (314 loc) · 7.44 KB
/
i2c.c
File metadata and controls
344 lines (314 loc) · 7.44 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Copyright 2013 Batov
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <sys/ioctl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#define MISSING_FUNC_FMT "Error: Adapter does not have %s capability\n"
#define END_OPT -1
#define TRUE 1
#define FALSE 0
#define EXIT_SUCCESS 0
#define EXIT_NOT_SUCCESS 1
static const char *optString = "r:s:g:h?";
struct Args_t
{
char reg;
__u8 set[30];
int setSize;
int get;
char help;
} Args;
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file,I2C_SMBUS,&args);
}
static inline int open_i2c_dev(int i2cbus, char *filename, size_t size, int quiet)
{
int file;
snprintf(filename, size, "/dev/i2c/%d", i2cbus);
filename[size - 1] = '\0';
file = open(filename, O_RDWR);
if (file < 0 && (errno == ENOENT || errno == ENOTDIR))
{
sprintf(filename, "/dev/i2c-%d", i2cbus);
file = open(filename, O_RDWR);
}
if (file < 0 && !quiet)
{
if (errno == ENOENT)
{
fprintf(stderr, "Error: Could not open file "
"`/dev/i2c-%d' or `/dev/i2c/%d': %s\n",
i2cbus, i2cbus, strerror(ENOENT));
}
else
{
fprintf(stderr, "Error: Could not open file "
"`%s': %s\n", filename, strerror(errno));
if (errno == EACCES)
fprintf(stderr, "Run as root?\n");
}
return EXIT_NOT_SUCCESS;
}
return file;
}
static inline int set_slave_addr(int file, int address)
{
if (ioctl(file, I2C_SLAVE, address) < 0) {
fprintf(stderr,
"Error: Could not set address to 0x%02x: %s\n",
address, strerror(errno));
return -errno;
}
return EXIT_SUCCESS;
}
static inline int check_funcs(int file, int daddress)
{
unsigned long funcs;
if (ioctl(file, I2C_FUNCS, &funcs) < 0) {
fprintf(stderr, "Error: Could not get the adapter "
"functionality matrix: %s\n", strerror(errno));
return EXIT_NOT_SUCCESS;
}
if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus receive byte");
}
if (daddress >= 0
&& !(funcs & I2C_FUNC_SMBUS_WRITE_BYTE)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus send byte");
}
if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE_DATA)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus read byte");
}
if (!(funcs & I2C_FUNC_SMBUS_READ_WORD_DATA)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus read word");
}
if (!(funcs & I2C_FUNC_SMBUS_READ_BLOCK_DATA)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus read block data");
}
if (!(funcs & I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
fprintf(stderr, MISSING_FUNC_FMT, "SMBus read i2c block data");
}
return EXIT_SUCCESS;
}
static inline __s32 i2c_smbus_read_i2c_block_data ( int file,
__u8 command,
__u8 length,
__u8 * values
){
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
data.block[0] = length;
if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
I2C_SMBUS_I2C_BLOCK_DATA,&data))
return EXIT_NOT_SUCCESS;
else {
for (i = 1; i <= data.block[0]; i++)
values[i-1] = data.block[i];
return data.block[0];
}
}
static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
__u8 length, __u8 *values)
{
union i2c_smbus_data data;
int i;
if (length > 32)
length = 32;
for (i = 1; i <= length; i++)
data.block[i] = values[i-1];
data.block[0] = length;
return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
I2C_SMBUS_I2C_BLOCK_DATA, &data);
}
static inline void init_args()
{
Args.help = TRUE;
Args.get = 0;
Args.reg = 0;
Args.setSize = 0;
}
static inline void reverse()
{
int i;
__u8 swap;
for (i = 0; i < Args.setSize/2; ++i)
{
swap = Args.set[i];
Args.set[i] = Args.set[Args.setSize-1-i];
Args.set[Args.setSize-1-i] = swap;
}
}
static inline int parse_setArg()
{
int i = 0;
unsigned char byteval;
char f = FALSE;
if (optarg[0] != '0' && optarg[1] != 'x')
{
printf("Use 0x0 format\n");
return EXIT_NOT_SUCCESS;
}
else optarg += 2;
while (optarg[i] && !f)
{
if (sscanf(optarg+2*i, "%2hhx", &byteval) != 1)
{
f = TRUE;
Args.setSize = i;
}
else
{
if (i == 0) Args.setSize = 1;
Args.set[i] = byteval;
i++;
}
reverse();
}
return EXIT_SUCCESS;
}
static inline int parse_getArg()
{
if (sscanf(optarg, "%d", &Args.get) != 1)
{
printf("Operand is invalid\n");
return EXIT_NOT_SUCCESS;
}
if (Args.get == 0)
{
printf("Operand is invalid\n");
return EXIT_NOT_SUCCESS;
}
return EXIT_SUCCESS;
}
static inline char parse_args(int argc, char* argv[])
{
init_args();
int opt = getopt(argc, argv, optString);
while (opt != END_OPT)
{
switch (opt)
{
case 'r':
{
if (sscanf(optarg, "0x%2hhx", &Args.reg) != 1)
{
printf("Register is invalid\n");
printf("Use 0x0 format\n");
return EXIT_NOT_SUCCESS;
}
Args.help = FALSE;
break;
}
case 's':
{
if (Args.get != 0) // if set and get together
{
printf("set or get?\n");
return EXIT_NOT_SUCCESS;
}
if (parse_setArg() != EXIT_SUCCESS) return EXIT_NOT_SUCCESS;
Args.help = FALSE;
break;
}
case 'g':
{
if (Args.setSize != 0) // if set and get together
{
printf("set or get?\n");
return EXIT_NOT_SUCCESS;
}
if (parse_getArg() != EXIT_SUCCESS) return EXIT_NOT_SUCCESS;
Args.help = FALSE;
break;
}
default:
{
return EXIT_NOT_SUCCESS;
break;
}
}
opt = getopt(argc, argv, optString);
}
if ((Args.setSize == 0) && (Args.get == 0))
{
printf("set or get?\n");
Args.help = TRUE;
}
if ((Args.help))
{
printf("I2C tool \n");
printf(" -s [VALUE] set value to slave\n");
printf(" -g [SIZE] get bytes from slave\n");
printf(" -r [REGISTER]\n");
printf("e.g. i2c -s 0x64 -r 0x16\n");
printf(" i2c -g 4 -r 0x26\n");
return EXIT_NOT_SUCCESS;
}
if (!Args.reg)
{
printf(" -r [REGISTER]\n");
return EXIT_NOT_SUCCESS;
}
return EXIT_SUCCESS;
}
int main(int argc,char* argv[])
{
if ((parse_args(argc,argv)) == 1) return EXIT_NOT_SUCCESS;
if (Args.help == EXIT_NOT_SUCCESS) return EXIT_NOT_SUCCESS;
char filename[20];
int fd;
if ((fd = open_i2c_dev(2,filename,sizeof(filename),0))< 0) return EXIT_NOT_SUCCESS;
if (check_funcs(fd,0x48) < 0) return EXIT_NOT_SUCCESS;
if (set_slave_addr(fd,0x48) < 0) return EXIT_NOT_SUCCESS;
int i = 0;
if (Args.get)
{
__u8 block[Args.get];
if (i2c_smbus_read_i2c_block_data(fd,Args.reg,Args.get,block+0) != Args.get)
{
printf("Get data failed");
return EXIT_NOT_SUCCESS;
}
else
{
printf("0x");
for (i = Args.get-1;i>=0;i--)
printf("%02x", block[i]);
printf("\n");
}
}
else
{
if (i2c_smbus_write_i2c_block_data(fd,Args.reg,Args.setSize,Args.set) != EXIT_SUCCESS)
{
printf("Set data failed");
return EXIT_NOT_SUCCESS;
}
}
close(fd);
return EXIT_SUCCESS;
}