-
Notifications
You must be signed in to change notification settings - Fork 138
/
I2c.cpp
193 lines (172 loc) · 4.31 KB
/
I2c.cpp
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
#include "I2c.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
int I2c::initI2C_RW(int bus, int address, int)
{
i2cBus = bus;
setAddress(address);
// open I2C device as a file
char namebuf[64];
snprintf(namebuf, sizeof(namebuf), "/dev/i2c-%d", i2cBus);
if ((i2cFile = open(namebuf, O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open %s I2C Bus\n", namebuf);
return(1);
}
// target device as slave
// this is useless if you only use I2C_RDWR, but is needed if you use
// ::write() or ::read()
if (ioctl(i2cFile, I2C_SLAVE, i2cAddress) < 0){
fprintf(stderr, "I2C_SLAVE address: %d failed...\n", i2cAddress);
return 2;
}
if(debugMode)
printf("(fd %d): %s, address: %d\n", i2cFile, namebuf, i2cAddress);
return 0;
}
int I2c::closeI2C()
{
if(i2cFile)
{
if(close(i2cFile) > 0)
{
fprintf(stderr, "Failed to close i2c file %d\n", i2cFile);
return 1;
}
}
i2cFile = 0;
return 0;
}
I2c::~I2c()
{
closeI2C();
}
//writeRegisters() and readRegisters(): with a little help from https://www.linuxquestions.org/questions/programming-9/reading-data-via-i2c-dev-4175499069/
//also interesting: read the source of linux/i2c-dev.h
int I2c::writeRegisters(
i2c_char_t reg,
i2c_char_t *values,
unsigned int size)
{
i2c_char_t outbuf[1 + size];
/* The first byte indicates which register we'll write */
outbuf[0] = reg;
/*
* The second and successive bytes indicate the value(s) to write.
* Note that not all devices will allow writing more than one byte.
*/
memcpy((void*)(outbuf + 1), (void*)values, size);
return write(outbuf, sizeof(outbuf));
}
int I2c::readRegisters(
i2c_char_t reg,
i2c_char_t *inbuf,
unsigned int readSize)
{
i2c_char_t outbuf[1];
/*
* In order to read a register, we first do a "dummy write" by writing
* 0 bytes to the register we want to read from.
*/
outbuf[0] = reg;
return writeRead(outbuf, sizeof(outbuf), inbuf, readSize);
}
void I2c::setAddress(int address)
{
i2cAddress = address;
}
int I2c::read(i2c_char_t *inbuf, unsigned int size)
{
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[1];
makeReadMessage(&messages[0], inbuf, size);
packets.msgs = messages;
packets.nmsgs = 1;
return doIoctl(&packets);
}
int I2c::write(i2c_char_t *outbuf, unsigned int size)
{
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[1];
makeWriteMessage(&messages[0], outbuf, size);
packets.msgs = messages;
packets.nmsgs = 1;
return doIoctl(&packets);
}
int I2c::writeRead(i2c_char_t* outbuf, unsigned int writeSize, i2c_char_t* inbuf, unsigned int readSize)
{
//this is called a repeated start: write and read in a single transaction
//with two START and only one STOP
//It is achieved by putting two messages (write, read) into packets
struct i2c_rdwr_ioctl_data packets;
struct i2c_msg messages[2];
makeWriteMessage(&messages[0], outbuf, writeSize);
makeReadMessage(&messages[1], inbuf, readSize);
packets.msgs = messages;
packets.nmsgs = 2;
return doIoctl(&packets);
}
void I2c::makeReadMessage(struct i2c_msg* message, i2c_char_t* inbuf, unsigned int readSize)
{
message->addr = i2cAddress;
message->flags = I2C_M_RD;
message->len = readSize;
message->buf = inbuf;
}
void I2c::makeWriteMessage(struct i2c_msg* message, i2c_char_t* outbuf, unsigned int writeSize)
{
message->addr = i2cAddress;
message->flags = 0;
message->len = writeSize;
message->buf = outbuf;
}
int I2c::doIoctl(i2c_rdwr_ioctl_data* packets)
{
if(debugMode)
{
printf("(fd %d) transaction. Address: %d\n", i2cFile, i2cAddress);
for(int n = 0; n < packets->nmsgs; ++n)
{
i2c_msg msg = packets->msgs[n];
printf("(fd %d) message[%d]: %s %d bytes", i2cFile, n, msg.flags == I2C_M_RD ? "read" : "write", msg.len);
if(msg.flags == 0)
{
printf(": ");
for(int j = 0; j < msg.len; ++j)
{
printf("%d ", msg.buf[j]);
}
}
printf("\n");
}
}
int ret = ioctl(i2cFile, I2C_RDWR, packets);
if(debugMode)
{
for(int n = 0; n < packets->nmsgs; ++n)
{
i2c_msg msg = packets->msgs[n];
if(msg.flags == I2C_M_RD)
{
printf("(fd %d) Read %d bytes: ", i2cFile, msg.len);
for(int j = 0; j < msg.len; ++j)
{
printf("%d ", msg.buf[j]);
}
printf("\n");
}
}
}
if(ret < 0) {
return 1;
}
return 0;
}
void I2c::setDebug(int newDebugMode)
{
debugMode = newDebugMode;
}