forked from aventuri/sigma_tcp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathi2c.c
133 lines (110 loc) · 2.72 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
/**
* Copyright (C) 2012 Analog Devices, Inc.
*
* THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
*
**/
#include <fcntl.h>
#include <stdio.h>
#include <error.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include "sigma_tcp.h"
static int i2c_fd;
static int i2c_dev_addr;
static int i2c_open(int argc, char *argv[])
{
int ret;
char *endp;
if (argc < 4) {
fprintf(stderr, "i2c: Usage %s i2c <i2c-dev> <i2c-addr>\n", argv[0]);
return 1;
}
i2c_fd = open(argv[2], O_RDWR);
if (i2c_fd < 0) {
perror("i2c: Failed to open i2c device");
return 1;
}
i2c_dev_addr = strtoul(argv[3], &endp, 0);
if (i2c_dev_addr < 0 || i2c_dev_addr > 255 || *endp != '\0') {
fprintf(stderr, "i2c: Invalid I2C address: \"%s\"\n", argv[3]);
return 1;
}
ret = ioctl(i2c_fd, I2C_SLAVE_FORCE, i2c_dev_addr);
if (ret < 0) {
perror("i2c: Failed to set i2c device address");
return 1;
}
printf("i2c: Initalized for device %s-%x\n", argv[2], i2c_dev_addr);
return 0;
}
static int i2c_read(unsigned int addr, unsigned int len, uint8_t *data)
{
uint8_t addr_buf[2];
struct i2c_msg msg[2];
struct i2c_rdwr_ioctl_data xfer = {
.msgs = msg,
.nmsgs = 2,
};
addr_buf[0] = (addr >> 8) & 0xff;
addr_buf[1] = addr & 0xff;
msg[0].addr = i2c_dev_addr;
msg[0].flags = 0;
msg[0].buf = addr_buf;
msg[0].len = 2;
msg[1].addr = i2c_dev_addr;
msg[1].flags = I2C_M_RD;
msg[1].buf = data;
msg[1].len = len;
return ioctl(i2c_fd, I2C_RDWR, &xfer);
}
#define MAX_BLOCK_LEN 1024
static int i2c_write(unsigned int addr, unsigned int len, const uint8_t *data)
{
uint8_t msg_buf[2 + len];
struct i2c_msg msg[1];
struct i2c_rdwr_ioctl_data xfer = {
.msgs = msg,
.nmsgs = 1,
};
int ret;
while (len > 0) {
int block_len = len > MAX_BLOCK_LEN ? MAX_BLOCK_LEN : len;
int retries = 10;
msg_buf[0] = addr >> 8;
msg_buf[1] = addr & 0xff;
memcpy(msg_buf + 2, data, block_len);
msg[0].addr = i2c_dev_addr;
msg[0].flags = 0;
msg[0].buf = msg_buf;
msg[0].len = block_len + 2;
printf("write %d bytes to 0x%04x\n", block_len, addr);
do {
ret = ioctl(i2c_fd, I2C_RDWR, &xfer);
if (ret < 0) {
usleep(10000);
printf("I2C error (%d), retries left: %d\n", errno, retries);
}
} while (ret < 0 && --retries);
if (retries == 0) {
printf("Fatal I2C error\n");
return ret;
}
len -= block_len;
addr += (block_len/4);
data += block_len;
}
return ret;
}
struct backend_ops i2c_backend_ops = {
.open = i2c_open,
.read = i2c_read,
.write = i2c_write,
};