-
Notifications
You must be signed in to change notification settings - Fork 19
/
I2C_PORT.cpp
419 lines (371 loc) · 13 KB
/
I2C_PORT.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "I2C_PORT.h"
/* Port interface */
#ifdef ARDUINO
void I2C_PORT::_I2C_init(TwoWire *wire, uint8_t dev_addr)
{
_wire = wire;
_dev_addr = dev_addr;
//devAddr = dev_addr;
}
bool I2C_PORT::_I2C_checkDevAvl()
{
_wire->beginTransmission(_dev_addr);
return (_wire->endTransmission() ? true : false);
}
void I2C_PORT::_I2C_write1Byte(uint8_t addr, uint8_t data)
{
_wire->beginTransmission(_dev_addr);
_wire->write(addr);
_wire->write(data);
_wire->endTransmission();
}
uint8_t I2C_PORT::_I2C_read8Bit(uint8_t addr)
{
_wire->beginTransmission(_dev_addr);
_wire->write(addr);
_wire->endTransmission();
_wire->requestFrom(_dev_addr, (size_t)1);
return _wire->read();
}
uint16_t I2C_PORT::_I2C_read12Bit(uint8_t addr)
{
uint8_t buff[2];
_I2C_readBuff(addr, 2, buff);
return (buff[0] << 4) + buff[1];
}
uint16_t I2C_PORT::_I2C_read13Bit(uint8_t addr)
{
uint8_t buff[2];
_I2C_readBuff(addr, 2, buff);
return (buff[0] << 5) + buff[1];
}
uint16_t I2C_PORT::_I2C_read16Bit(uint8_t addr)
{
uint8_t buff[2];
_I2C_readBuff(addr, 2, buff);
return (buff[0] << 8) + buff[1];
}
uint32_t I2C_PORT::_I2C_read24Bit(uint8_t addr)
{
uint8_t buff[4];
_I2C_readBuff(addr, 3, buff);
return (buff[0] << 16) + (buff[1] << 8) + buff[2];
}
uint32_t I2C_PORT::_I2C_read32Bit(uint8_t addr)
{
uint8_t buff[4];
_I2C_readBuff(addr, 4, buff);
return (buff[0] << 24) + (buff[1] << 16) + (buff[2] << 8) + buff[3];
}
void I2C_PORT::_I2C_readBuff(uint8_t addr, int size, uint8_t buff[])
{
_wire->beginTransmission(_dev_addr);
_wire->write(addr);
_wire->endTransmission();
_wire->requestFrom(_dev_addr, (size_t)size);
for (int i = 0; i < size; i++)
{
buff[i] = _wire->read();
}
}
uint16_t I2C_PORT::_I2C_read16Bit_lowFirst(uint8_t addr)
{
uint8_t buff[2];
_I2C_readBuff(addr, 2, buff);
return buff[0] + (buff[1] << 8);
}
void I2C_PORT::_I2C_write16Bit(uint8_t addr, uint8_t data_1, uint8_t data_2)
{
_wire->beginTransmission(_dev_addr);
_wire->write(addr);
_wire->write(data_1);
_wire->write(data_2);
_wire->endTransmission();
}
// ! 以下为移植的更好的写法
/** Default timeout value for read operations.
* Set this to 0 to disable timeout detection.
*/
uint16_t I2C_PORT::readTimeout = I2CDEV_DEFAULT_READ_TIMEOUT;
// ^ read function
/** Read a single bit from an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to read from
* @param bitNum Bit position to read (0-7)
* @param data Container for single bit value
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
* @return Status of read operation (true = success)
*/
int8_t I2C_PORT::_I2C_readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout)
{
uint8_t b;
uint8_t count = _I2C_readByte(devAddr, regAddr, &b, timeout);
*data = b & (1 << bitNum);
return count;
}
/** Read single byte from an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to read from
* @param data Container for byte value read from device
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
* @return Status of read operation (true = success)
*/
int8_t I2C_PORT::_I2C_readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout)
{
return _I2C_readBytes(devAddr, regAddr, 1, data, timeout);
}
/** Read multiple bits from an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to read from
* @param bitStart First bit position to read (0-7)
* @param length Number of bits to read (not more than 8)
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
* @return Status of read operation (true = success)
*/
int8_t I2C_PORT::_I2C_readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout)
{
// 01101001 read byte
// 76543210 bit numbers
// xxx args: bitStart=4, length=3
// 010 masked
// -> 010 shifted
uint8_t count, b;
if ((count = _I2C_readByte(devAddr, regAddr, &b, timeout)) != 0)
{
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
b &= mask;
b >>= (bitStart - length + 1);
*data = b;
}
return count;
}
/** Read single word from a 16-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to read from
* @param data Container for word value read from device
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
* @return Status of read operation (true = success)
*/
int8_t I2C_PORT::_I2C_readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout)
{
return _I2C_readWords(devAddr, regAddr, 1, data, timeout);
}
// & read主要移植的这个:readWords
/** Read multiple words from a 16-bit device register.
* @param devAddr I2C slave device address
* @param regAddr First register regAddr to read from
* @param length Number of words to read
* @param data Buffer to store read data in
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
* @return Number of words read (0 indicates failure)
*/
int8_t I2C_PORT::_I2C_readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout)
{
int8_t count = 0;
_wire->beginTransmission(devAddr);
_wire->write(regAddr);
_wire->endTransmission();
_wire->beginTransmission(devAddr);
_wire->requestFrom(devAddr, (uint8_t)(length * 2)); // length=words, this wants bytes
uint32_t t1 = millis();
bool msb = true; // starts with MSB, then LSB
for (; _wire->available() && count < length && (timeout == 0 || millis() - t1 < timeout);)
{
if (msb)
{
// first byte is bits 15-8 (MSb=15)
data[count] = _wire->read() << 8;
}
else
{
// second byte is bits 7-0 (LSb=0)
data[count] |= _wire->read();
count++;
}
msb = !msb;
}
if (timeout > 0 && millis() - t1 >= timeout && count < length)
count = -1; // timeout
_wire->endTransmission();
return count;
}
// & read主要移植的这个:readBytes
/** Read multiple bytes from an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr First register regAddr to read from
* @param length Number of bytes to read
* @param data Buffer to store read data in
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
* @return Number of bytes read (0 indicates failure)
*/
int8_t I2C_PORT::_I2C_readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout)
{
int8_t count = 0;
_wire->beginTransmission(devAddr);
_wire->write(regAddr);
_wire->endTransmission();
_wire->beginTransmission(devAddr);
_wire->requestFrom(devAddr, length);
uint32_t t1 = millis();
for (; _wire->available() && (timeout == 0 || millis() - t1 < timeout); count++)
{
data[count] = _wire->read();
}
if (timeout > 0 && millis() - t1 >= timeout && count < length)
count = -1; // timeout
_wire->endTransmission();
return count;
}
// ^ write function
/** write a single bit in an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to write to
* @param bitNum Bit position to write (0-7)
* @param value New bit value to write
* @return Status of operation (true = success)
*/
bool I2C_PORT::_I2C_writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data)
{
uint8_t b;
_I2C_readByte(devAddr, regAddr, &b);
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
return _I2C_writeByte(devAddr, regAddr, b);
}
/** Write single byte to an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register address to write to
* @param data New byte value to write
* @return Status of operation (true = success)
*/
bool I2C_PORT::_I2C_writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data)
{
return _I2C_writeBytes(devAddr, regAddr, 1, &data);
}
/** Write multiple bits in an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register regAddr to write to
* @param bitStart First bit position to write (0-7)
* @param length Number of bits to write (not more than 8)
* @param data Right-aligned value to write
* @return Status of operation (true = success)
*/
bool I2C_PORT::_I2C_writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data)
{
// 010 value to write
// 76543210 bit numbers
// xxx args: bitStart=4, length=3
// 00011100 mask byte
// 10101111 original value (sample)
// 10100011 original & ~mask
// 10101011 masked | value
uint8_t b;
if (_I2C_readByte(devAddr, regAddr, &b) != 0)
{
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
data <<= (bitStart - length + 1); // shift data into correct position
data &= mask; // zero all non-important bits in data
b &= ~(mask); // zero all important bits in existing byte
b |= data; // combine data with existing byte
return _I2C_writeByte(devAddr, regAddr, b);
}
else
{
return false;
}
}
/** Write single word to a 16-bit device register.
* @param devAddr I2C slave device address
* @param regAddr Register address to write to
* @param data New word value to write
* @return Status of operation (true = success)
*/
bool I2C_PORT::_I2C_writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data)
{
return _I2C_writeWords(devAddr, regAddr, 1, &data);
}
// & write主要移植的这个:writeWords
/** Write multiple words to a 16-bit device register.
* @param devAddr I2C slave device address
* @param regAddr First register address to write to
* @param length Number of words to write
* @param data Buffer to copy new data from
* @return Status of operation (true = success)
*/
bool I2C_PORT::_I2C_writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data)
{
uint8_t status = 0;
_wire->beginTransmission(devAddr);
_wire->write(regAddr); // send address
for (uint8_t i = 0; i < length * 2; i++)
{
_wire->write((uint8_t)(data[i++] >> 8)); // send MSB
_wire->write((uint8_t)data[i]); // send LSB
}
status = _wire->endTransmission();
return status == 0;
}
// & write主要移植的这个:writeBytes
/** Write multiple bytes to an 8-bit device register.
* @param devAddr I2C slave device address
* @param regAddr First register address to write to
* @param length Number of bytes to write
* @param data Buffer to copy new data from
* @return Status of operation (true = success)
*/
bool I2C_PORT::_I2C_writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
{
uint8_t status = 0;
_wire->beginTransmission(devAddr);
_wire->write((uint8_t)regAddr); // send address
for (uint8_t i = 0; i < length; i++)
{
_wire->write((uint8_t)data[i]);
}
status = _wire->endTransmission();
return status == 0;
}
// ! End
// /**
// * @brief I2C device scan
// *
// * @return int I2C device number
// */
// int I2C_PORT::I2C_dev_scan()
// {
// uint8_t error, address;
// int nDevices;
// Serial.println("[I2C_SCAN] device scanning...");
// nDevices = 0;
// for (address = 1; address < 127; address++)
// {
// // The i2c_scanner uses the return value of
// // the Write.endTransmisstion to see if
// // a device did acknowledge to the address.
// Wire.beginTransmission(address);
// error = Wire.endTransmission();
// if (error == 0)
// {
// Serial.print("[I2C_SCAN]: device found at address 0x");
// if (address < 16)
// Serial.print("0");
// Serial.print(address, HEX);
// Serial.println(" !");
// nDevices++;
// }
// else if (error == 4)
// {
// Serial.print("[I2C_SCAN]: unknow error at address 0x");
// if (address < 16)
// Serial.print("0");
// Serial.println(address, HEX);
// }
// }
// Serial.print("[I2C_SCAN]:");
// Serial.printf(" %d devices was found\r\n", nDevices);
// return nDevices;
// }
#else
void I2C_PORT::_I2C_init(uint8_t dev_addr) {}
void I2C_PORT::_I2C_checkDevAvl() {}
#endif