-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C.cpp
215 lines (172 loc) · 5.42 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
///-------------------------------------------------------------------------------------------------
/// @file POC\I2C.cpp.
///
/// @brief Declares the I2C interface
#include "I2C.h"
/// @brief List of 2 c devices
vector<I2C> I2CDeviceList;
///-------------------------------------------------------------------------------------------------
/// @fn I2C::I2C(short slaveID, bool supports10BitAddressing, long fmax)
///
/// @brief Constructor
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param slaveID Identifier for the slave.
/// @param supports10BitAddressing True to supports 10 bit addressing.
/// @param fmax The fmax.
I2C::I2C(short slaveID, bool supports10BitAddressing, long fmax) :
fmax(fmax),
supports10BitAddressing(supports10BitAddressing),
i2cAddress(slaveID) {
}
mutex I2C::i2cMutex;
///-------------------------------------------------------------------------------------------------
/// @fn void I2C::transfere(unsigned char* tx, unsigned char* rx, int ntx, int nrx)
///
/// @brief Transferes
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param [in,out] tx If non-null, the transmit.
/// @param [in,out] rx If non-null, the receive.
/// @param ntx The ntx.
/// @param nrx The nrx.
void I2C::transfere(unsigned char* tx, unsigned char* rx, int ntx, int nrx) {
//TODO
i2cMutex.lock();
int file;
///dev/i2c-0 for OPI
file = open("/dev/i2c-0", O_RDWR);
if (file < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
cout << "could not open I2C file" << endl;
}
if (ioctl(file, I2C_SLAVE, i2cAddress & 0xFF) < 0) {
/* ERROR HANDLING; you can check errno to see what went wrong */
cout << "could not set I2C address" << endl;
}
if (::write(file, tx, ntx) != ntx) {
/* ERROR HANDLING: I2C transaction failed */
cout << "i2c send failed " << hex << i2cAddress << endl;
}
//usleep(100);
//cout << "reading " << (int)nrx << " bytes from I2C" << endl;
if (nrx > 0) {
if (::read(file, rx, nrx) != nrx) {
cout << "i2c receive failed " << hex << i2cAddress << endl;
}
}
close(file);
i2cMutex.unlock();
//usleep(10000);
}
///-------------------------------------------------------------------------------------------------
/// @fn void I2C::write(unsigned char data)
///
/// @brief Writes the given data
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param data The data to write.
void I2C::write(unsigned char data) {
unsigned char readBuffer[0];
unsigned char writeBuffer[1]{ data };
transfere(writeBuffer, readBuffer, 1, 0);
}
///-------------------------------------------------------------------------------------------------
/// @fn unsigned char I2C::read()
///
/// @brief Gets the read
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @returns A char.
unsigned char I2C::read() {
unsigned char readBuffer[1];
unsigned char writeBuffer[0];
transfere(writeBuffer, readBuffer, 0, 1);
return readBuffer[0];
}
///-------------------------------------------------------------------------------------------------
/// @fn unsigned char I2C::readRegister(unsigned char reg)
///
/// @brief Reads a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
///
/// @returns The register.
unsigned char I2C::readRegister(unsigned char reg) {
unsigned char readBuffer[1];
unsigned char writeBuffer[1]{ reg };
transfere(writeBuffer, readBuffer, 1, 1);
return readBuffer[0];
}
///-------------------------------------------------------------------------------------------------
/// @fn void I2C::readRegister(unsigned char reg, unsigned char readBuffer[], unsigned int n)
///
/// @brief Reads a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
/// @param readBuffer Buffer for read data.
/// @param n An int to process.
void I2C::readRegister(unsigned char reg, unsigned char readBuffer[], unsigned int n) {
unsigned char writeBuffer[1]{ reg };
transfere(writeBuffer, readBuffer, 1, n);
}
///-------------------------------------------------------------------------------------------------
/// @fn void I2C::writeRegister(unsigned char reg, unsigned char data)
///
/// @brief Writes a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
/// @param data The data.
void I2C::writeRegister(unsigned char reg, unsigned char data) {
unsigned char readBuffer[0];
unsigned char writeBuffer[2]{ reg , data};
transfere(writeBuffer, readBuffer, 2, 0);
}
///-------------------------------------------------------------------------------------------------
/// @fn void I2C::writeRegister(unsigned char reg, unsigned char writeBuffer[], unsigned int n)
///
/// @brief Writes a register
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @param reg The register.
/// @param writeBuffer Buffer for write data.
/// @param n An int to process.
void I2C::writeRegister(unsigned char reg, unsigned char writeBuffer[], unsigned int n) {
unsigned char writeBufferComplete[1 + n];
writeBufferComplete[0] = reg;
for (unsigned int i = 1; i <= n; i++) {
writeBufferComplete[i] = writeBuffer[i - 1];
}
transfere(writeBufferComplete, nullptr, n + 1, 0);
}
///-------------------------------------------------------------------------------------------------
/// @fn short I2C::getI2CAddress()
///
/// @brief Gets i 2 c address
///
/// @author Benjamin
/// @date 28.09.2020
///
/// @returns The i 2 c address.
short I2C::getI2CAddress() {
return i2cAddress;
}