-
Notifications
You must be signed in to change notification settings - Fork 1
/
LocalI2CConnection.cpp
148 lines (129 loc) · 3.57 KB
/
LocalI2CConnection.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
#include "LocalI2CConnection.h"
#include "i2c-dev.h"
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
LocalI2CConnection::LocalI2CConnection(char* devFileName) {
devFile = open(devFileName, O_RDWR);
if (devFile < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
printf("Failed to open the bus.\n");
exit(1);
}
slaveAddr = -1;
}
LocalI2CConnection::~LocalI2CConnection() {
close(devFile);
}
void LocalI2CConnection::setSlaveAddress(uint16 address) {
if (slaveAddr != address) {
slaveAddr = address;
if (ioctl(devFile, I2C_SLAVE, slaveAddr) < 0) {
printf("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
}
}
int32 LocalI2CConnection::getSlaveAddress() {
return slaveAddr;
}
void LocalI2CConnection::writeQuick(uint8 value) {
int32 result = i2c_smbus_write_quick(devFile, value);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
}
uint8 LocalI2CConnection::readByte() {
int32 result = i2c_smbus_read_byte(devFile);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}
void LocalI2CConnection::writeByte(uint8 value) {
int32 result = i2c_smbus_write_byte(devFile, value);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
}
uint8 LocalI2CConnection::readByteData(uint8 command) {
int32 result = i2c_smbus_read_byte_data(devFile, command);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}
void LocalI2CConnection::writeByteData(uint8 command, uint8 value) {
int32 result = i2c_smbus_write_byte_data(devFile, command, value);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
}
uint16 LocalI2CConnection::readWordData(uint8 command) {
int32 result = i2c_smbus_read_word_data(devFile, command);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}
void LocalI2CConnection::writeWordData(uint8 command, uint16 value) {
int32 result = i2c_smbus_write_word_data(devFile, command, value);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
}
uint8 LocalI2CConnection::readBlockData(uint8 command, uint8* values) {
int32 result = i2c_smbus_read_block_data(devFile, command, values);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}
void LocalI2CConnection::writeBlockData(uint8 command, uint8 length, const uint8* values) {
int32 result = i2c_smbus_write_block_data(devFile, command, length, values);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
}
uint8 LocalI2CConnection::readI2CBlockData(uint8 command, uint8 length, uint8* values) {
int32 result = i2c_smbus_read_i2c_block_data(devFile, command, length, values);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}
void LocalI2CConnection::writeI2CBlockData(uint8 command, uint8 length, const uint8* values) {
int32 result = i2c_smbus_write_i2c_block_data(devFile, command, length, values);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
}
uint16 LocalI2CConnection::processCall(uint8 command, uint16 value) {
int32 result = i2c_smbus_process_call(devFile, command, value);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}
uint8 LocalI2CConnection::blockProcessCall(uint8 command, uint8 length, uint8* values) {
int32 result = i2c_smbus_block_process_call(devFile, command, length, values);
if (result < 0) {
printf("i2c transaction failed.\n");
exit(1);
}
return result;
}