-
Notifications
You must be signed in to change notification settings - Fork 21
/
onion-i2c.c
449 lines (363 loc) · 10.9 KB
/
onion-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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <onion-i2c.h>
// get a file handle to the device
int _i2c_getFd(int adapterNum, int *devHandle)
{
int status;
char pathname[255];
// define the path to open
status = snprintf(pathname, sizeof(pathname), I2C_DEV_PATH, adapterNum);
// check the filename
if (status < 0 || status >= sizeof(pathname)) {
// add errno
return EXIT_FAILURE;
}
// create a file descriptor for the I2C bus
#ifdef I2C_ENABLED
*devHandle = open(pathname, O_RDWR);
#else
*devHandle = 0;
#endif
// check the defvice handle
if (*devHandle < 0) {
// add errno
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// release the device file handle
int _i2c_releaseFd(int devHandle)
{
#ifdef I2C_ENABLED
if ( close(devHandle) < 0 ) {
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}
// set the device address
int _i2c_setDevice(int devHandle, int addr)
{
#ifdef I2C_ENABLED
// set to 7-bit addr
if ( ioctl(devHandle, I2C_TENBIT, 0) < 0 ) {
return EXIT_FAILURE;
}
// set the address
if ( ioctl(devHandle, I2C_SLAVE, addr) < 0 ) {
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}
// set the 10bit device address
int _i2c_setDevice10bit(int devHandle, int addr)
{
#ifdef I2C_ENABLED
// set to 10-bit addr
if ( ioctl(devHandle, I2C_TENBIT, 1) < 0 ) {
return EXIT_FAILURE;
}
// set the address
if ( _i2c_setDevice(devHandle, addr) != EXIT_SUCCESS ) {
return EXIT_FAILURE;
}
#endif
return EXIT_SUCCESS;
}
// generic function to write a buffer to the i2c bus
int _i2c_writeBuffer(int devNum, int devAddr, uint8_t *buffer, int size)
{
int status;
int fd, index;
// open the file handle
status = _i2c_getFd(devNum, &fd);
// set the device address
if ( status == EXIT_SUCCESS ) {
status = _i2c_setDevice(fd, devAddr);
}
onionPrint(ONION_SEVERITY_DEBUG_EXTRA, "%s writing buffer:\n", I2C_PRINT_BANNER);
for (index = 0; index < size; index++) {
onionPrint(ONION_SEVERITY_DEBUG_EXTRA, "\tbuffer[%d]: 0x%02x\n", index, buffer[index]);
}
// perform the write
if ( status == EXIT_SUCCESS ) {
#ifdef I2C_ENABLED
// write to the i2c device
status = write(fd, buffer, size);
if (status != size) {
onionPrint(ONION_SEVERITY_FATAL, "%s write issue for register 0x%02x, errno is %d: %s\n", I2C_PRINT_BANNER, buffer[0], errno, strerror(errno) );
status = EXIT_FAILURE;
}
else {
status = EXIT_SUCCESS;
}
#endif
}
// release the device file handle
status |= _i2c_releaseFd(fd);
return (status);
}
// generic function to write a buffer to the i2c bus
int i2c_writeBuffer(int devNum, int devAddr, int addr, uint8_t *buffer, int size)
{
int status;
uint8_t *bufferNew;
// allocate the new buffer
size++; // adding addr to buffer
bufferNew = malloc( size * sizeof *bufferNew );
// add the address to the data buffer
bufferNew[0] = addr;
memcpy( &bufferNew[1], &buffer[0], (size-1) * sizeof *buffer );
// perform the write
status = _i2c_writeBuffer(devNum, devAddr, bufferNew, size);
// free the allocated memory
free(bufferNew);
return (status);
}
// generic function to write a buffer to the i2c bus (no in-device address
int i2c_writeBufferRaw(int devNum, int devAddr, uint8_t *buffer, int size)
{
return _i2c_writeBuffer(devNum, devAddr, buffer, size);
}
// write n bytes to the i2c bus
int i2c_write(int devNum, int devAddr, int addr, int val)
{
int status;
int size, tmp, index;
uint8_t buffer[I2C_BUFFER_SIZE];
//// buffer setup
// clear the buffer
memset( buffer, 0, sizeof(buffer) );
// push the address and data values into the buffer
buffer[0] = (addr & 0xff);
buffer[1] = (val & 0xff);
size = 2;
// if value is more than 1-byte, add to the buffer
tmp = (val >> 8); // start with byte 1
index = 2;
while (tmp > 0x00) {
buffer[index] = (uint8_t)(tmp & 0xff);
tmp = tmp >> 8; // advance the tmp data by a byte
index++; // increment the index
size++; // increase the size
}
onionPrint(ONION_SEVERITY_DEBUG, "%s Writing to device 0x%02x: addr = 0x%02x, data = 0x%02x (data size: %d)\n", I2C_PRINT_BANNER, devAddr, addr, val, (size-1) );
// write the buffer
status = _i2c_writeBuffer(devNum, devAddr, buffer, size);
return (status);
}
// write a specified number of bytes to the i2c bus
int i2c_writeBytes(int devNum, int devAddr, int addr, int val, int numBytes)
{
int status;
int size, index;
uint8_t buffer[I2C_BUFFER_SIZE];
//// buffer setup
// clear the buffer
memset( buffer, 0, sizeof(buffer) );
// push the address and data values into the buffer
buffer[0] = (addr & 0xff);
size = 1;
// add all data bytes to buffer
index = 1;
for (index = 0; index < numBytes; index++) {
buffer[index+1] = (uint8_t)( (val >> (8*index)) & 0xff );
size++; // increase the size
}
onionPrint(ONION_SEVERITY_DEBUG, "%s Writing to device 0x%02x: addr = 0x%02x, data = 0x%02x (data size: %d)\n", I2C_PRINT_BANNER, devAddr, addr, val, (size-1) );
// write the buffer
status = _i2c_writeBuffer(devNum, devAddr, buffer, size);
return (status);
}
// read a byte from the i2c bus
int i2c_read(int devNum, int devAddr, int addr, uint8_t *buffer, int numBytes)
{
int status, size, index;
int fd;
onionPrint(ONION_SEVERITY_DEBUG, "%s Reading %d byte%s from device 0x%02x: addr = 0x%02x", I2C_PRINT_BANNER, numBytes, (numBytes > 1 ? "s": ""), devAddr, addr);
// open the device file handle
status = _i2c_getFd(devNum, &fd);
// set the device address
if ( status == EXIT_SUCCESS ) {
status = _i2c_setDevice(fd, devAddr);
}
// perform the read
if ( status == EXIT_SUCCESS ) {
//// set addr
// clear the buffer
memset( buffer, 0, numBytes );
// push the address and data values into the buffer
buffer[0] = (addr & 0xff);
size = 1;
#ifdef I2C_ENABLED
// write to the i2c device
status = write(fd, buffer, size);
if (status != size) {
onionPrint(ONION_SEVERITY_FATAL, "%s write issue for register 0x%02x, errno is %d: %s\n", I2C_PRINT_BANNER, addr, errno, strerror(errno) );
}
#endif
//// read data
// clear the buffer
memset( buffer, 0, numBytes );
#ifdef I2C_ENABLED
// read from the i2c device
size = numBytes;
status = read(fd, buffer, size);
if (status != size) {
onionPrint(ONION_SEVERITY_FATAL, "%s read issue for register 0x%02x, errno is %d: %s\n", I2C_PRINT_BANNER, addr, errno, strerror(errno) );
status = EXIT_FAILURE;
}
else {
status = EXIT_SUCCESS;
}
#else
buffer[0] = 0x0;
size = 1;
/*
// for debug
printf("Setting buffer... it has length of %d\n", strlen(buffer) );
buffer[0] = 0x34;
buffer[1] = 0x12;
size = 2;
printf("Done setting buffer... it has length of %d\n", strlen(buffer) );
printf("size is %d\n", size);*/
#endif
//// print the data
onionPrint(ONION_SEVERITY_DEBUG, "\tread %d byte%s, value: 0x", size, (size > 1 ? "s" : "") );
for (index = (size-1); index >= 0; index--) {
onionPrint(ONION_SEVERITY_DEBUG, "%02x", (buffer[index] & 0xff) );
}
onionPrint(ONION_SEVERITY_DEBUG, "\n");
}
// release the device file handle
status |= _i2c_releaseFd(fd);
return (status);
}
// read a byte from the i2c bus
int i2c_readRaw(int devNum, int devAddr, uint8_t *buffer, int numBytes)
{
int status, size, index;
int fd;
onionPrint(ONION_SEVERITY_DEBUG, "%s Reading %d byte%s from device 0x%02x", I2C_PRINT_BANNER, numBytes, (numBytes > 1 ? "s": ""), devAddr);
// open the device file handle
status = _i2c_getFd(devNum, &fd);
// set the device address
if ( status == EXIT_SUCCESS ) {
status = _i2c_setDevice(fd, devAddr);
}
// perform the read
if ( status == EXIT_SUCCESS ) {
//// read data
// clear the buffer
memset( buffer, 0, numBytes );
#ifdef I2C_ENABLED
// read from the i2c device
size = numBytes;
status = read(fd, buffer, size);
if (status != size) {
onionPrint(ONION_SEVERITY_FATAL, "%s read issue, errno is %d: %s\n", I2C_PRINT_BANNER, errno, strerror(errno) );
status = EXIT_FAILURE;
}
else {
status = EXIT_SUCCESS;
}
#else
buffer[0] = 0x0;
size = 1;
/*
// for debug
printf("Setting buffer... it has length of %d\n", strlen(buffer) );
buffer[0] = 0x34;
buffer[1] = 0x12;
size = 2;
printf("Done setting buffer... it has length of %d\n", strlen(buffer) );
printf("size is %d\n", size);*/
#endif
//// print the data
onionPrint(ONION_SEVERITY_DEBUG, "\tread %d byte%s, value: 0x", size, (size > 1 ? "s" : "") );
for (index = (size-1); index >= 0; index--) {
onionPrint(ONION_SEVERITY_DEBUG, "%02x", (buffer[index] & 0xff) );
}
onionPrint(ONION_SEVERITY_DEBUG, "\n");
}
// release the device file handle
status |= _i2c_releaseFd(fd);
return (status);
}
// read a single byte from the i2c bus
int i2c_readByte(int devNum, int devAddr, int addr, int *val)
{
int status;
uint8_t buffer[I2C_BUFFER_SIZE];
status = i2c_read ( devNum,
devAddr,
addr,
buffer,
1
);
*val = (int)(buffer[0]);
return (status);
}
// read n-bytes from the i2c bus from a m-byte address
int i2c_readMultiByteAddr (int devNum, int devAddr, uint8_t *addrBuffer, int addrLength, uint8_t *buffer, int dataLength)
{
int status, size, index;
int fd;
// onionPrint(ONION_SEVERITY_DEBUG, "%s Reading %d byte%s from device 0x%02x: addr = 0x%02x", I2C_PRINT_BANNER, dataLength, (dataLength > 1 ? "s": ""), devAddr, addr);
// open the device file handle
status = _i2c_getFd(devNum, &fd);
// set the device address
if ( status == EXIT_SUCCESS ) {
status = _i2c_setDevice(fd, devAddr);
}
// perform the read
if ( status == EXIT_SUCCESS ) {
#ifdef I2C_ENABLED
if (addrLength > 0) {
// write to the i2c device
status = write(fd, addrBuffer, addrLength);
if (status != addrLength) {
onionPrint(ONION_SEVERITY_FATAL, "%s write issue, errno is %d: %s\n", I2C_PRINT_BANNER, errno, strerror(errno) );
}
}
#endif
//// read data
// clear the buffer
memset( buffer, 0, dataLength );
#ifdef I2C_ENABLED
// read from the i2c device
size = dataLength;
status = read(fd, buffer, size);
if (status != size) {
onionPrint(ONION_SEVERITY_FATAL, "%s read issue, errno is %d: %s\n", I2C_PRINT_BANNER, errno, strerror(errno) );
status = EXIT_FAILURE;
}
else {
status = EXIT_SUCCESS;
}
#endif
}
// release the device file handle
status |= _i2c_releaseFd(fd);
return (status);
}
// write n-bytes to a m-byte to the i2c bus
int i2c_writeMultiByteAddr (int devNum, int devAddr, uint8_t *addrBuffer, int addrLength, uint8_t *buffer, int dataLength)
{
int status, i, size;
uint8_t *bufferNew;
// allocate the new buffer
size = addrLength + dataLength; // buffer is the size of the address and data bytes combined
bufferNew = malloc( size * sizeof *bufferNew );
// add the address to the buffer
for (i = 0; i < addrLength; i++) {
bufferNew[i] = addrBuffer[i];
}
// copy the data to the buffer (starting at the first index after the last address byte)
memcpy( &bufferNew[i], &buffer[0], (size-1) * sizeof *buffer );
// perform the write
status = _i2c_writeBuffer(devNum, devAddr, bufferNew, size);
// free the allocated memory
free(bufferNew);
return (status);
}