-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wire.h
168 lines (142 loc) · 3.15 KB
/
Wire.h
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
#ifndef _WIRE_H
#define _WIRE_H
#include <unistd.h> // read, write, close
#include <cstdint>
#include <cstring> // memcpy
#include <cerrno>
#include <string>
#include <linux/i2c-dev.h>
#ifndef I2C_M_RD
#include <linux/i2c.h>
#endif
#include <sys/ioctl.h>
#include <fcntl.h>
#ifdef DBG
#include <ctime.h>
#endif
class Wire
{
public:
Wire(uint8_t bus):
_flags(0),
_addr(0),
_regb(1) {
setBus(bus);
}
Wire(uint8_t bus, uint16_t addr):
Wire(bus) {
setAddr(addr);
}
Wire(uint8_t bus, uint16_t addr, uint8_t width):
Wire(bus) {
setAddr(addr, width);
}
void setBus(uint8_t bus) {
_busdev = "/dev/i2c-" + std::to_string(bus);
}
void setAddr(uint16_t addr) {
_addr = addr;
if (_addr > 0xFF)
_flags |= I2C_M_TEN;
}
void setAddr(uint16_t addr, uint8_t width) {
setAddr(addr);
// TODO
// _regb = width / 8;
}
int write(int32_t reg, uint8_t buf[], uint16_t count) {
int rc = 0;
uint8_t tbuf[count + _regb];
uint32_t tcount = count + _regb;
uint16_t addr;
if (_addr < 3)
return -ENXIO;
if (reg < 0)
return -EINVAL;
#ifdef DBG
printf("going to WRITE to %d from %p, count %d\n", reg, buf, count);
struct timespec tm, tm2;
clock_gettime(CLOCK_REALTIME, &tm);
#endif
int file = ::open(_busdev.c_str(), O_RDWR);
if (file == -1) {
return -errno;
}
// TODO: reg addr
addr = _addr;
tbuf[0] = reg;
//
if (ioctl(file, I2C_SLAVE, addr) == -1) {
rc = -errno;
goto writeclose;
}
memcpy(&tbuf[_regb], buf, count);
if (::write(file, tbuf, tcount) != tcount) {
rc = -errno;
goto writeclose;
}
#ifdef DBG
clock_gettime(CLOCK_REALTIME, &tm2);
fprintf(stderr, "write complete in %.1fus\n",
((tm2.tv_nsec - tm.tv_nsec) / 100 ) / 10.0);
#endif
writeclose:
::close(file);
return rc;
}
int read(int32_t reg, uint8_t buf[], uint16_t count) {
int rc = 0;
struct i2c_msg msgs[2];
struct i2c_rdwr_ioctl_data iodata;
uint8_t rbuf[_regb];
uint16_t addr;
uint8_t p = 0;
if (_addr < 3)
return -ENXIO;
#ifdef DBG
printf("going to READ from %d to %p, count %d\n", reg, buf, count);
struct timespec tm, tm2;
clock_gettime(CLOCK_REALTIME, &tm);
#endif
int file = ::open(_busdev.c_str(), O_RDWR);
if (file == -1) {
return -errno;
}
if (reg >= 0) {
// TODO: reg addr
addr = _addr;
rbuf[0] = reg;
//
msgs[p].addr = addr;
msgs[p].flags = _flags;
msgs[p].len = _regb;
msgs[p].buf = (typeof i2c_msg::buf)rbuf;
p++;
}
msgs[p].addr = _addr;
msgs[p].flags = _flags | I2C_M_RD;
msgs[p].len = count;
msgs[p].buf = (typeof i2c_msg::buf)buf;
p++;
iodata.msgs = msgs;
iodata.nmsgs = p;
if (ioctl(file, I2C_RDWR, &iodata) != p) {
rc = -errno;
goto readclose;
}
#ifdef DBG
clock_gettime(CLOCK_REALTIME, &tm2);
fprintf(stderr, "read complete in %.1fus\n",
((tm2.tv_nsec - tm.tv_nsec) / 100 ) / 10.0);
#endif
readclose:
::close(file);
return rc;
}
private:
std::string _busdev;
uint16_t _addr;
uint8_t _regb;
uint16_t _flags;
};
#endif // _WIRE_H