-
Notifications
You must be signed in to change notification settings - Fork 10
/
I2cPort.cpp
353 lines (287 loc) · 9.55 KB
/
I2cPort.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
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
/*
Copyright (C) 2014 Cagdas Caglak http://expcodes.blogspot.com.tr/
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "I2cPort.h"
namespace cacaosd_i2cport {
/**
* @funtion I2cPort()
*/
I2cPort::I2cPort() {
this->connection_open = false;
}
/**
* @funtion I2cPort(uint8_t bus_address)
* @param bus_addr I2C Bus address.
*/
I2cPort::I2cPort(uint8_t bus_address) {
this->connection_open = false;
this->bus_address = bus_address;
this->path = (char *) calloc(PATH_SIZE, sizeof(char));
sprintf(path, "/dev/i2c-%d", this->bus_address);
}
/**
* @funtion I2cPort(uint8_t device_address, uint8_t bus_address)
* @param device_address Device Address
* @param bus_address I2C Bus address.
*/
I2cPort::I2cPort(uint8_t device_address, uint8_t bus_address) {
this->connection_open = false;
this->bus_address = bus_address;
this->path = (char *) calloc(PATH_SIZE, sizeof(char));
this->device_address = device_address;
sprintf(path, "/dev/i2c-%d", this->bus_address);
}
/** Default Destructor
* @funtion ~I2cPort()
*
*/
I2cPort::~I2cPort() {
free(path);
this->closeConnection();
}
/**
* @funtion setBusAddress(uint8_t bus_address)
* @param bus_address I2C Bus address.
*/
void I2cPort::setBusAddress(uint8_t bus_address) {
free(path);
this->bus_address = bus_address;
this->path = (char *) calloc(PATH_SIZE, sizeof(char));
sprintf(path, "/dev/i2c-%d", this->bus_address);
}
/**
* @funtion getBusAddress()
* @return bus_addr I2C Bus address.
*/
uint8_t I2cPort::getBusAddress() const {
return this->bus_address;
}
/**
* @funtion setDeviceAddress(uint8_t device_address)
* @param dev_addr Device Address
*/
void I2cPort::setDeviceAddress(uint8_t device_address) {
this->device_address = device_address;
}
/**
* @funtion getDeviceAddress()
* @return device_address Device Address
*/
uint8_t I2cPort::getDeviceAddress() const {
return this->device_address;
}
/**
* @function openConnection()
* @return file type of int
*/
void I2cPort::openConnection() {
int file;
if ((file = open(path, O_RDWR)) < 0) {
msg_error("%s do not open. Address %d.", path, device_address);
exit(1);
}
if (ioctl(file, I2C_SLAVE, device_address) < 0) {
msg_error("Can not join I2C Bus. Address %d.", device_address);
exit(1);
}
if (file < 0) {
this->connection_open = false;
msg_error("Connection was not established.");
exit(1);
}
this->connection_open = true;
this->file_descriptor = file;
}
/** Close connection.
* @function closeConnection()
* @return void
*/
void I2cPort::closeConnection() {
this->connection_open = false;
close(this->file_descriptor);
}
/**
* @function writeBit(uint8_t DATA_REGADD, uint8_t data, int bitNum)
* @param DATA_REGADD Data Register Address.
* @param data Writing data.
* @param bitNum Bit Number for writing.
* @return void.
*/
void I2cPort::writeBit(uint8_t DATA_REGADD, uint8_t data, uint8_t bitNum) {
int8_t temp = readByte(DATA_REGADD);
if (data == 0) {
temp = temp & ~(1 << bitNum);
} else if (data == 1) {
temp = temp | (1 << bitNum);
} else {
msg_warning("Value must be 0 or 1! --> Address %d.", device_address);
}
writeByte(DATA_REGADD, temp);
}
/**
* @function writeBits(uint8_t dev_addr, uint8_t DATA_REGADD, uint8_t data, int length, int startBit)
* @param dev_addr Device Address.
* @param DATA_REGADD Data Register Address.
* @param length Bits length.
* @param startBit Starting point of the data.
* @return void.
*/
void I2cPort::writeMoreBits(uint8_t DATA_REGADD, uint8_t data, uint8_t length,
uint8_t startBit) {
int8_t temp = readByte(DATA_REGADD);
uint8_t bits = 1;
uint8_t i = 0;
while (i < length - 1) {
bits = (bits << 1);
++bits;
++i;
}
temp &= ~(bits << startBit);
temp |= (data << startBit);
writeByte(DATA_REGADD, temp);
}
/**
* @function writeByte(uint8_t DATA_REGADD, uint8_t data)
* @param DATA_REGADD Data Register Address.
* @param data Writing data.
* @return void.
*/
void I2cPort::writeByte(uint8_t DATA_REGADD, uint8_t data) {
uint8_t buffer[2];
buffer[0] = DATA_REGADD;
buffer[1] = data;
if (write(this->file_descriptor, buffer, 2) != 2) {
msg_error("Can not write data. Address %d.", device_address);
}
}
/**
* @function writeByteBuffer(uint8_t DATA_REGADD, uint8_t *data, uint8_t length)
* @param DATA_REGADD Data Register Address.
* @param data Data storage array.
* @param length Array length.
* @return void.
*/
void I2cPort::writeByteBuffer(uint8_t DATA_REGADD, uint8_t *data,
uint8_t length) {
uint8_t buffer[1];
buffer[0] = DATA_REGADD;
if (write(this->file_descriptor, buffer, 1) != 1) {
msg_error("Can not write data. Address %d.", device_address);
}
if (write(this->file_descriptor, data, length) != length) {
msg_error("Can not write data. Address %d.", device_address);
}
}
/**
* @function writeByteArduino(int8_t data)
* @param data Writing data.
* @return void.
*/
void I2cPort::writeByteArduino(int8_t data) {
int8_t buffer[1];
buffer[0] = data;
if (write(this->file_descriptor, buffer, 1) != 1) {
msg_error("Can not write data. Address %d.", device_address);
}
}
/**
* @function writeByteBufferArduino(uint8_t *data, uint8_t length)
* @param data Data storage array.
* @param length Array length.
* @return void.
*/
void I2cPort::writeByteBufferArduino(uint8_t *data, uint8_t length) {
if (write(this->file_descriptor, data, length) != length) {
msg_error("Can not write data. Address %d.", device_address);
}
}
/**
* @function readBit(uint8_t DATA_REGADD, uint8_t bitNum)
* @param DATA_REGADD Data Register Address.
* @param bitNum Bit Number for reading.
* @return uint8_t bit value.
*/
uint8_t I2cPort::readBit(uint8_t DATA_REGADD, uint8_t bitNum) {
int8_t temp = readByte(DATA_REGADD);
return (uint8_t) ((temp >> bitNum) % 2);
}
/**
* @function readBits(uint8_t DATA_REGADD, uint8_t length, uint8_t startBit)
* @param DATA_REGADD Data Register Address.
* @param length Bits length.
* @param startBit Starting point of the value.
* @return uint8_t bit value.
*/
uint8_t I2cPort::readMoreBits(uint8_t DATA_REGADD, uint8_t length,
uint8_t startBit) {
int8_t temp = readByte(DATA_REGADD);
return (uint8_t) ((temp >> startBit) % (uint8_t) pow(2, length));
}
/**
* @function readByte(uint8_t DATA_REGADD)
* @param DATA_REGADD Data Register Address.
* @return uint8_t bit value.
*/
uint8_t I2cPort::readByte(uint8_t DATA_REGADD) {
uint8_t buffer[1];
buffer[0] = DATA_REGADD;
if (write(this->file_descriptor, buffer, 1) != 1) {
msg_error("Can not write data. Address %d.", device_address);
}
uint8_t value[1];
if (read(this->file_descriptor, value, 1) != 1) {
msg_error("Can not read data. Address %d.", device_address);
}
return value[0];
}
/**
* @function readByteBuffer(uint8_t DATA_REGADD, uint8_t *data, uint8_t length)
* @param DATA_REGADD Data Register Address.
* @param data Data storage array.
* @param length Array length.
* @return void.
*/
void I2cPort::readByteBuffer(uint8_t DATA_REGADD, uint8_t *data,
uint8_t length) {
uint8_t buffer[1];
buffer[0] = DATA_REGADD;
if (write(this->file_descriptor, buffer, 1) != 1) {
msg_error("Can not write data. Address %d.", device_address);
}
if (read(this->file_descriptor, data, length) != length) {
msg_error("Can not read data. Address %d.", device_address);
}
}
/**
* @function readByteBufferArduino(uint8_t* data, uint8_t length)
* @param data Data storage array.
* @param length Array length.
* @return void.
*/
void I2cPort::readByteBufferArduino(uint8_t *data, uint8_t length) {
if (read(this->file_descriptor, data, length) != length) {
msg_error("Can not read data. Address %d.", device_address);
}
}
/**
* @function readWord(uint8_t MSB, uint8_t LSB)
* @param MSB 16-bit values Most Significant Byte Address.
* @param LSB 16-bit values Less Significant Byte Address..
* @return void.
*/
int16_t I2cPort::readWord(uint8_t MSB, uint8_t LSB) {
uint8_t msb = readByte(MSB);
uint8_t lsb = readByte(LSB);
return ((int16_t) msb << 8) + lsb;
}
} // namespace cacaosd_i2cport