-
Notifications
You must be signed in to change notification settings - Fork 25
/
i2c.c
288 lines (247 loc) · 5.06 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
285
286
287
288
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include "i2c.h"
#ifdef _DEBUG
# include <stdio.h>
# define DEBUG(...) fprintf(stderr,"[DEBUG] --- " __VA_ARGS__)
#else
# define DEBUG(...)
#endif
int i2c_connect(int adapter_nr)
{
int fd;
char filename[20];
snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
fd = open(filename, O_RDWR);
DEBUG("Connected %s on fd %d\n",filename, fd);
if(fd < 0)
fprintf(stderr, "Error opening %s: %m\n", filename);
return fd;
}
void i2c_disconnect(int fd)
{
DEBUG("Disconnecting fd %d\n", fd);
close(fd);
}
bool i2c_set_slave_device_addr(int fd, int addr)
{
if (ioctl(fd, I2C_SLAVE, addr) < 0) {
fprintf(stderr, "Error setting slave device address 0x%x: %m\n",addr);
return false;
}
return true;
}
int i2c_insist_read_byte(int bus, __u8 addr, useconds_t delay_us)
{
int data;
usleep(delay_us);
data = i2c_smbus_read_byte_data(bus, addr);
if ((0 > data) || (0xFF == data)) {
usleep(delay_us);
data = i2c_smbus_read_byte_data(bus, addr);
if ((0 > data) || (0xFF == data)) {
usleep(delay_us);
data = i2c_smbus_read_byte_data(bus, addr);
if ((0 > data) || (0xFF == data)) {
fprintf(stderr, "i2c_smbus_read_byte_data failed: addr 0x%02X\n", addr);
return -1;
}
}
}
return data;
}
int i2c_insist_read_word(int bus, __u8 addr, useconds_t delay_us)
{
int data;
usleep(delay_us);
data = i2c_smbus_read_word_data(bus, addr);
if ((0 > data) || (0xFFFF == data)) {
usleep(delay_us);
data = i2c_smbus_read_word_data(bus, addr);
if ((0 > data) || (0xFFFF == data)) {
usleep(delay_us);
data = i2c_smbus_read_word_data(bus, addr);
if ((0 > data) || (0xFFFF == data)) {
fprintf(stderr, "i2c_smbus_read_word_data failed: addr 0x%02X\n", addr);
return -1;
}
}
}
return data;
}
/***************************************************************
* User-space bitbang I2C implementation *
***************************************************************/
#define I2C_CLK_FILE "/sys/class/gpio/gpio25/direction"
#define I2C_SDA_FILE "/sys/class/gpio/gpio16/direction"
#define I2C_SDA_READ_FILE "/sys/class/gpio/gpio16/value"
static bool get_signal(const char *fname, int *value)
{
char buf[16];
int cnt, fd = open(fname, O_RDONLY);
if (0 > fd) {
fprintf(stderr, "Can not open file %s:%m\n", fname);
return false;
}
cnt = read(fd, buf, sizeof(buf) - 1);
if (0 >= cnt) {
fprintf(stderr, "Can not read file %s\n", fname);
close(fd);
return false;
}
close(fd);
switch (buf[0]) {
case '1':
*value = 1;
break;
case '0':
*value = 0;
break;
default:
buf[cnt] = 0;
fprintf(stderr, "Wrong value (%s) in file %s\n",
buf, fname);
return false;
}
return true;
}
static bool set_signal(const char *fname, int value)
{
char buf[16];
int len, cnt, fd = open(fname, O_WRONLY);
if (0 > fd) {
fprintf(stderr, "Can not open file %s:%m\n", fname);
return false;
}
if (value > 0) {
strcpy(buf, "high");
len = 4;
} else if (value == 0) {
strcpy(buf, "low");
len = 3;
} else {
strcpy(buf, "in");
len = 2;
}
cnt = write(fd, buf, len);
if (len != cnt) {
fprintf(stderr, "Can not write file %s\n", fname);
close(fd);
return false;
}
close(fd);
return true;
}
static inline void i2c_clk(int value)
{
set_signal(I2C_CLK_FILE, value);
}
static inline void i2c_data(int value)
{
set_signal(I2C_SDA_FILE, value);
}
static inline void i2c_data_dir_in(void)
{
set_signal(I2C_SDA_FILE, -1);
}
static inline int i2c_getbit(void)
{
int value;
if (!get_signal(I2C_SDA_READ_FILE, &value))
return 1;
return value;
}
static uint8_t i2c_inbyte(bool ack)
{
uint8_t value = 0;
int bitvalue;
int i;
i2c_clk(0);
i2c_data_dir_in();
for (i = 0; i < 8; ++i) {
i2c_clk(1);
bitvalue = i2c_getbit();
value |= bitvalue;
if (i < 7)
value <<= 1;
i2c_clk(0);
}
if (ack)
i2c_data(0);
i2c_clk(1);
i2c_clk(0);
i2c_data_dir_in();
return value;
}
static void i2c_start(void)
{
i2c_clk(0);
i2c_data(1);
i2c_clk(1);
i2c_data(0);
}
static void i2c_stop(void)
{
i2c_clk(0);
i2c_data(0);
i2c_clk(1);
i2c_data(1);
}
static bool i2c_outbyte(uint8_t x)
{
int i;
int ack;
i2c_clk(0);
for (i = 0; i < 8; ++i) {
if (x & 0x80)
i2c_data(1);
else
i2c_data(0);
i2c_clk(1);
i2c_clk(0);
x <<= 1;
}
i2c_data_dir_in();
i2c_clk(1);
ack = i2c_getbit();
i2c_clk(0);
return !ack;
}
__s32 bitbang_read_byte_data(__u8 dev_addr, __u8 command)
{
uint8_t ret;
i2c_start();
/* Device address & Write */
if (!i2c_outbyte(dev_addr << 1))
return -1;
/* Command */
if (!i2c_outbyte(command))
return -1;
/* Repeated Start */
i2c_start();
/* Device address & Read */
if (!i2c_outbyte((dev_addr << 1) | 1))
return -1;
ret = i2c_inbyte(false);
i2c_stop();
return ret;
}
__s32 bitbang_write_byte_data(__u8 dev_addr, __u8 command, __u8 value)
{
i2c_start();
/* Device address & Write */
if (!i2c_outbyte(dev_addr << 1))
return -1;
/* Command */
if (!i2c_outbyte(command))
return -1;
/* Value */
if (!i2c_outbyte(value))
return -1;
i2c_stop();
return value;
}