User | Revision | Line number | New contents of line |
garfieldsg |
0:662207e34fba
|
1
|
// ported from arduino library: https://github.com/jrowberg/i2cdevlib
|
garfieldsg |
0:662207e34fba
|
2
|
// written by szymon gaertig (email: szymon@gaertig.com.pl, website: szymongaertig.pl)
|
garfieldsg |
0:662207e34fba
|
3
|
// Changelog:
|
garfieldsg |
0:662207e34fba
|
4
|
// 2013-01-08 - first release
|
garfieldsg |
0:662207e34fba
|
5
|
|
garfieldsg |
0:662207e34fba
|
6
|
#include "I2Cdev.h"
|
garfieldsg |
0:662207e34fba
|
7
|
|
garfieldsg |
0:662207e34fba
|
8
|
|
Yihui Xiong |
2:8c562a8fed36
|
9
|
I2Cdev::I2Cdev(I2C &interface) : i2c(interface)
|
garfieldsg |
0:662207e34fba
|
10
|
{
|
garfieldsg |
0:662207e34fba
|
11
|
|
garfieldsg |
0:662207e34fba
|
12
|
}
|
garfieldsg |
0:662207e34fba
|
13
|
|
garfieldsg |
0:662207e34fba
|
14
|
/** Read a single bit from an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
15
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
16
|
* @param regAddr Register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
17
|
* @param bitNum Bit position to read (0-7)
|
garfieldsg |
0:662207e34fba
|
18
|
* @param data Container for single bit value
|
garfieldsg |
0:662207e34fba
|
19
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
20
|
* @return Status of read operation (true = success)
|
garfieldsg |
0:662207e34fba
|
21
|
*/
|
garfieldsg |
0:662207e34fba
|
22
|
int8_t I2Cdev::readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data, uint16_t timeout) {
|
garfieldsg |
0:662207e34fba
|
23
|
uint8_t b;
|
garfieldsg |
0:662207e34fba
|
24
|
uint8_t count = readByte(devAddr, regAddr, &b, timeout);
|
garfieldsg |
0:662207e34fba
|
25
|
*data = b & (1 << bitNum);
|
garfieldsg |
0:662207e34fba
|
26
|
return count;
|
garfieldsg |
0:662207e34fba
|
27
|
}
|
garfieldsg |
0:662207e34fba
|
28
|
|
garfieldsg |
0:662207e34fba
|
29
|
/** Read a single bit from a 16-bit device register.
|
garfieldsg |
0:662207e34fba
|
30
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
31
|
* @param regAddr Register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
32
|
* @param bitNum Bit position to read (0-15)
|
garfieldsg |
0:662207e34fba
|
33
|
* @param data Container for single bit value
|
garfieldsg |
0:662207e34fba
|
34
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
35
|
* @return Status of read operation (true = success)
|
garfieldsg |
0:662207e34fba
|
36
|
*/
|
garfieldsg |
0:662207e34fba
|
37
|
int8_t I2Cdev::readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data, uint16_t timeout) {
|
garfieldsg |
0:662207e34fba
|
38
|
uint16_t b;
|
garfieldsg |
0:662207e34fba
|
39
|
uint8_t count = readWord(devAddr, regAddr, &b, timeout);
|
garfieldsg |
0:662207e34fba
|
40
|
*data = b & (1 << bitNum);
|
garfieldsg |
0:662207e34fba
|
41
|
return count;
|
garfieldsg |
0:662207e34fba
|
42
|
}
|
garfieldsg |
0:662207e34fba
|
43
|
|
garfieldsg |
0:662207e34fba
|
44
|
/** Read multiple bits from an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
45
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
46
|
* @param regAddr Register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
47
|
* @param bitStart First bit position to read (0-7)
|
garfieldsg |
0:662207e34fba
|
48
|
* @param length Number of bits to read (not more than 8)
|
garfieldsg |
0:662207e34fba
|
49
|
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
|
garfieldsg |
0:662207e34fba
|
50
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
51
|
* @return Status of read operation (true = success)
|
garfieldsg |
0:662207e34fba
|
52
|
*/
|
garfieldsg |
0:662207e34fba
|
53
|
int8_t I2Cdev::readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data, uint16_t timeout) {
|
garfieldsg |
0:662207e34fba
|
54
|
// 01101001 read byte
|
garfieldsg |
0:662207e34fba
|
55
|
// 76543210 bit numbers
|
garfieldsg |
0:662207e34fba
|
56
|
// xxx args: bitStart=4, length=3
|
garfieldsg |
0:662207e34fba
|
57
|
// 010 masked
|
garfieldsg |
0:662207e34fba
|
58
|
// -> 010 shifted
|
garfieldsg |
0:662207e34fba
|
59
|
uint8_t count, b;
|
garfieldsg |
0:662207e34fba
|
60
|
if ((count = readByte(devAddr, regAddr, &b, timeout)) != 0) {
|
garfieldsg |
0:662207e34fba
|
61
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
garfieldsg |
0:662207e34fba
|
62
|
b &= mask;
|
garfieldsg |
0:662207e34fba
|
63
|
b >>= (bitStart - length + 1);
|
garfieldsg |
0:662207e34fba
|
64
|
*data = b;
|
garfieldsg |
0:662207e34fba
|
65
|
}
|
garfieldsg |
0:662207e34fba
|
66
|
return count;
|
garfieldsg |
0:662207e34fba
|
67
|
}
|
garfieldsg |
0:662207e34fba
|
68
|
|
garfieldsg |
0:662207e34fba
|
69
|
/** Read multiple bits from a 16-bit device register.
|
garfieldsg |
0:662207e34fba
|
70
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
71
|
* @param regAddr Register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
72
|
* @param bitStart First bit position to read (0-15)
|
garfieldsg |
0:662207e34fba
|
73
|
* @param length Number of bits to read (not more than 16)
|
garfieldsg |
0:662207e34fba
|
74
|
* @param data Container for right-aligned value (i.e. '101' read from any bitStart position will equal 0x05)
|
garfieldsg |
0:662207e34fba
|
75
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
76
|
* @return Status of read operation (1 = success, 0 = failure, -1 = timeout)
|
garfieldsg |
0:662207e34fba
|
77
|
*/
|
garfieldsg |
0:662207e34fba
|
78
|
int8_t I2Cdev::readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data, uint16_t timeout) {
|
garfieldsg |
0:662207e34fba
|
79
|
// 1101011001101001 read byte
|
garfieldsg |
0:662207e34fba
|
80
|
// fedcba9876543210 bit numbers
|
garfieldsg |
0:662207e34fba
|
81
|
// xxx args: bitStart=12, length=3
|
garfieldsg |
0:662207e34fba
|
82
|
// 010 masked
|
garfieldsg |
0:662207e34fba
|
83
|
// -> 010 shifted
|
garfieldsg |
0:662207e34fba
|
84
|
uint8_t count;
|
garfieldsg |
0:662207e34fba
|
85
|
uint16_t w;
|
garfieldsg |
0:662207e34fba
|
86
|
if ((count = readWord(devAddr, regAddr, &w, timeout)) != 0) {
|
garfieldsg |
0:662207e34fba
|
87
|
uint16_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
garfieldsg |
0:662207e34fba
|
88
|
w &= mask;
|
garfieldsg |
0:662207e34fba
|
89
|
w >>= (bitStart - length + 1);
|
garfieldsg |
0:662207e34fba
|
90
|
*data = w;
|
garfieldsg |
0:662207e34fba
|
91
|
}
|
garfieldsg |
0:662207e34fba
|
92
|
return count;
|
garfieldsg |
0:662207e34fba
|
93
|
}
|
garfieldsg |
0:662207e34fba
|
94
|
/** Read single byte from an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
95
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
96
|
* @param regAddr Register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
97
|
* @param data Container for byte value read from device
|
garfieldsg |
0:662207e34fba
|
98
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
99
|
* @return Status of read operation (true = success)
|
garfieldsg |
0:662207e34fba
|
100
|
*/
|
garfieldsg |
0:662207e34fba
|
101
|
int8_t I2Cdev::readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint16_t timeout) {
|
garfieldsg |
0:662207e34fba
|
102
|
return readBytes(devAddr, regAddr, 1, data, timeout);
|
garfieldsg |
0:662207e34fba
|
103
|
}
|
garfieldsg |
0:662207e34fba
|
104
|
|
garfieldsg |
0:662207e34fba
|
105
|
/** Read single word from a 16-bit device register.
|
garfieldsg |
0:662207e34fba
|
106
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
107
|
* @param regAddr Register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
108
|
* @param data Container for word value read from device
|
garfieldsg |
0:662207e34fba
|
109
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
110
|
* @return Status of read operation (true = success)
|
garfieldsg |
0:662207e34fba
|
111
|
*/
|
garfieldsg |
0:662207e34fba
|
112
|
int8_t I2Cdev::readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data, uint16_t timeout) {
|
garfieldsg |
0:662207e34fba
|
113
|
return readWords(devAddr, regAddr, 1, data, timeout);
|
garfieldsg |
0:662207e34fba
|
114
|
}
|
garfieldsg |
0:662207e34fba
|
115
|
|
garfieldsg |
0:662207e34fba
|
116
|
/** Read multiple bytes from an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
117
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
118
|
* @param regAddr First register regAddr to read from
|
garfieldsg |
0:662207e34fba
|
119
|
* @param length Number of bytes to read
|
garfieldsg |
0:662207e34fba
|
120
|
* @param data Buffer to store read data in
|
garfieldsg |
0:662207e34fba
|
121
|
* @param timeout Optional read timeout in milliseconds (0 to disable, leave off to use default class value in I2Cdev::readTimeout)
|
garfieldsg |
0:662207e34fba
|
122
|
* @return Number of bytes read (-1 indicates failure)
|
garfieldsg |
0:662207e34fba
|
123
|
*/
|
garfieldsg |
0:662207e34fba
|
124
|
int8_t I2Cdev::readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout)
|
garfieldsg |
0:662207e34fba
|
125
|
{
|
garfieldsg |
0:662207e34fba
|
126
|
char command[1];
|
garfieldsg |
0:662207e34fba
|
127
|
command[0] = regAddr;
|
garfieldsg |
0:662207e34fba
|
128
|
char *redData = (char*)malloc(length);
|
garfieldsg |
0:662207e34fba
|
129
|
i2c.write(devAddr<<1, command, 1, true);
|
garfieldsg |
0:662207e34fba
|
130
|
i2c.read(devAddr<<1, redData, length);
|
garfieldsg |
0:662207e34fba
|
131
|
for(int i =0; i < length; i++) {
|
garfieldsg |
0:662207e34fba
|
132
|
data[i] = redData[i];
|
garfieldsg |
0:662207e34fba
|
133
|
}
|
garfieldsg |
1:1e0baaf91e96
|
134
|
free (redData);
|
garfieldsg |
0:662207e34fba
|
135
|
return length;
|
garfieldsg |
0:662207e34fba
|
136
|
}
|
garfieldsg |
0:662207e34fba
|
137
|
|
garfieldsg |
0:662207e34fba
|
138
|
int8_t I2Cdev::readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout)
|
garfieldsg |
0:662207e34fba
|
139
|
{
|
garfieldsg |
0:662207e34fba
|
140
|
return 0;
|
garfieldsg |
0:662207e34fba
|
141
|
}
|
garfieldsg |
0:662207e34fba
|
142
|
|
garfieldsg |
0:662207e34fba
|
143
|
/** write a single bit in an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
144
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
145
|
* @param regAddr Register regAddr to write to
|
garfieldsg |
0:662207e34fba
|
146
|
* @param bitNum Bit position to write (0-7)
|
garfieldsg |
0:662207e34fba
|
147
|
* @param value New bit value to write
|
garfieldsg |
0:662207e34fba
|
148
|
* @return Status of operation (true = success)
|
garfieldsg |
0:662207e34fba
|
149
|
*/
|
garfieldsg |
0:662207e34fba
|
150
|
bool I2Cdev::writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data) {
|
garfieldsg |
0:662207e34fba
|
151
|
uint8_t b;
|
garfieldsg |
0:662207e34fba
|
152
|
readByte(devAddr, regAddr, &b);
|
garfieldsg |
0:662207e34fba
|
153
|
b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
|
garfieldsg |
0:662207e34fba
|
154
|
return writeByte(devAddr, regAddr, b);
|
garfieldsg |
0:662207e34fba
|
155
|
}
|
garfieldsg |
0:662207e34fba
|
156
|
|
garfieldsg |
0:662207e34fba
|
157
|
/** write a single bit in a 16-bit device register.
|
garfieldsg |
0:662207e34fba
|
158
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
159
|
* @param regAddr Register regAddr to write to
|
garfieldsg |
0:662207e34fba
|
160
|
* @param bitNum Bit position to write (0-15)
|
garfieldsg |
0:662207e34fba
|
161
|
* @param value New bit value to write
|
garfieldsg |
0:662207e34fba
|
162
|
* @return Status of operation (true = success)
|
garfieldsg |
0:662207e34fba
|
163
|
*/
|
garfieldsg |
0:662207e34fba
|
164
|
bool I2Cdev::writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data) {
|
garfieldsg |
0:662207e34fba
|
165
|
uint16_t w;
|
garfieldsg |
0:662207e34fba
|
166
|
readWord(devAddr, regAddr, &w);
|
garfieldsg |
0:662207e34fba
|
167
|
w = (data != 0) ? (w | (1 << bitNum)) : (w & ~(1 << bitNum));
|
garfieldsg |
0:662207e34fba
|
168
|
return writeWord(devAddr, regAddr, w);
|
garfieldsg |
0:662207e34fba
|
169
|
}
|
garfieldsg |
0:662207e34fba
|
170
|
|
garfieldsg |
0:662207e34fba
|
171
|
/** Write multiple bits in an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
172
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
173
|
* @param regAddr Register regAddr to write to
|
garfieldsg |
0:662207e34fba
|
174
|
* @param bitStart First bit position to write (0-7)
|
garfieldsg |
0:662207e34fba
|
175
|
* @param length Number of bits to write (not more than 8)
|
garfieldsg |
0:662207e34fba
|
176
|
* @param data Right-aligned value to write
|
garfieldsg |
0:662207e34fba
|
177
|
* @return Status of operation (true = success)
|
garfieldsg |
0:662207e34fba
|
178
|
*/
|
garfieldsg |
0:662207e34fba
|
179
|
bool I2Cdev::writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data) {
|
garfieldsg |
0:662207e34fba
|
180
|
// 010 value to write
|
garfieldsg |
0:662207e34fba
|
181
|
// 76543210 bit numbers
|
garfieldsg |
0:662207e34fba
|
182
|
// xxx args: bitStart=4, length=3
|
garfieldsg |
0:662207e34fba
|
183
|
// 00011100 mask byte
|
garfieldsg |
0:662207e34fba
|
184
|
// 10101111 original value (sample)
|
garfieldsg |
0:662207e34fba
|
185
|
// 10100011 original & ~mask
|
garfieldsg |
0:662207e34fba
|
186
|
// 10101011 masked | value
|
garfieldsg |
0:662207e34fba
|
187
|
uint8_t b;
|
garfieldsg |
0:662207e34fba
|
188
|
if (readByte(devAddr, regAddr, &b) != 0) {
|
garfieldsg |
0:662207e34fba
|
189
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
garfieldsg |
0:662207e34fba
|
190
|
data <<= (bitStart - length + 1); // shift data into correct position
|
garfieldsg |
0:662207e34fba
|
191
|
data &= mask; // zero all non-important bits in data
|
garfieldsg |
0:662207e34fba
|
192
|
b &= ~(mask); // zero all important bits in existing byte
|
garfieldsg |
0:662207e34fba
|
193
|
b |= data; // combine data with existing byte
|
garfieldsg |
0:662207e34fba
|
194
|
return writeByte(devAddr, regAddr, b);
|
garfieldsg |
0:662207e34fba
|
195
|
} else {
|
garfieldsg |
0:662207e34fba
|
196
|
return false;
|
garfieldsg |
0:662207e34fba
|
197
|
}
|
garfieldsg |
0:662207e34fba
|
198
|
}
|
garfieldsg |
0:662207e34fba
|
199
|
|
garfieldsg |
0:662207e34fba
|
200
|
/** Write multiple bits in a 16-bit device register.
|
garfieldsg |
0:662207e34fba
|
201
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
202
|
* @param regAddr Register regAddr to write to
|
garfieldsg |
0:662207e34fba
|
203
|
* @param bitStart First bit position to write (0-15)
|
garfieldsg |
0:662207e34fba
|
204
|
* @param length Number of bits to write (not more than 16)
|
garfieldsg |
0:662207e34fba
|
205
|
* @param data Right-aligned value to write
|
garfieldsg |
0:662207e34fba
|
206
|
* @return Status of operation (true = success)
|
garfieldsg |
0:662207e34fba
|
207
|
*/
|
garfieldsg |
0:662207e34fba
|
208
|
bool I2Cdev::writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data) {
|
garfieldsg |
0:662207e34fba
|
209
|
// 010 value to write
|
garfieldsg |
0:662207e34fba
|
210
|
// fedcba9876543210 bit numbers
|
garfieldsg |
0:662207e34fba
|
211
|
// xxx args: bitStart=12, length=3
|
garfieldsg |
0:662207e34fba
|
212
|
// 0001110000000000 mask byte
|
garfieldsg |
0:662207e34fba
|
213
|
// 1010111110010110 original value (sample)
|
garfieldsg |
0:662207e34fba
|
214
|
// 1010001110010110 original & ~mask
|
garfieldsg |
0:662207e34fba
|
215
|
// 1010101110010110 masked | value
|
garfieldsg |
0:662207e34fba
|
216
|
uint16_t w;
|
garfieldsg |
0:662207e34fba
|
217
|
if (readWord(devAddr, regAddr, &w) != 0) {
|
garfieldsg |
0:662207e34fba
|
218
|
uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
|
garfieldsg |
0:662207e34fba
|
219
|
data <<= (bitStart - length + 1); // shift data into correct position
|
garfieldsg |
0:662207e34fba
|
220
|
data &= mask; // zero all non-important bits in data
|
garfieldsg |
0:662207e34fba
|
221
|
w &= ~(mask); // zero all important bits in existing word
|
garfieldsg |
0:662207e34fba
|
222
|
w |= data; // combine data with existing word
|
garfieldsg |
0:662207e34fba
|
223
|
return writeWord(devAddr, regAddr, w);
|
garfieldsg |
0:662207e34fba
|
224
|
} else {
|
garfieldsg |
0:662207e34fba
|
225
|
return false;
|
garfieldsg |
0:662207e34fba
|
226
|
}
|
garfieldsg |
0:662207e34fba
|
227
|
}
|
garfieldsg |
0:662207e34fba
|
228
|
|
garfieldsg |
0:662207e34fba
|
229
|
/** Write single byte to an 8-bit device register.
|
garfieldsg |
0:662207e34fba
|
230
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
231
|
* @param regAddr Register address to write to
|
garfieldsg |
0:662207e34fba
|
232
|
* @param data New byte value to write
|
garfieldsg |
0:662207e34fba
|
233
|
* @return Status of operation (true = success)
|
garfieldsg |
0:662207e34fba
|
234
|
*/
|
garfieldsg |
0:662207e34fba
|
235
|
bool I2Cdev::writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data) {
|
garfieldsg |
0:662207e34fba
|
236
|
return writeBytes(devAddr, regAddr, 1, &data);
|
garfieldsg |
0:662207e34fba
|
237
|
}
|
garfieldsg |
0:662207e34fba
|
238
|
|
garfieldsg |
0:662207e34fba
|
239
|
/** Write single word to a 16-bit device register.
|
garfieldsg |
0:662207e34fba
|
240
|
* @param devAddr I2C slave device address
|
garfieldsg |
0:662207e34fba
|
241
|
* @param regAddr Register address to write to
|
garfieldsg |
0:662207e34fba
|
242
|
* @param data New word value to write
|
garfieldsg |
0:662207e34fba
|
243
|
* @return Status of operation (true = success)
|
garfieldsg |
0:662207e34fba
|
244
|
*/
|
garfieldsg |
0:662207e34fba
|
245
|
bool I2Cdev::writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data) {
|
garfieldsg |
0:662207e34fba
|
246
|
return writeWords(devAddr, regAddr, 1, &data);
|
garfieldsg |
0:662207e34fba
|
247
|
}
|
garfieldsg |
0:662207e34fba
|
248
|
|
garfieldsg |
0:662207e34fba
|
249
|
bool I2Cdev::writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data)
|
garfieldsg |
0:662207e34fba
|
250
|
{
|
Yihui Xiong |
2:8c562a8fed36
|
251
|
int len = length + 1;
|
Yihui Xiong |
2:8c562a8fed36
|
252
|
char *buf = (char*)malloc(len);
|
Yihui Xiong |
2:8c562a8fed36
|
253
|
buf[0] = regAddr;
|
Yihui Xiong |
2:8c562a8fed36
|
254
|
for (int i = 0; i < length; i++) {
|
Yihui Xiong |
2:8c562a8fed36
|
255
|
buf[i + 1] = data[i];
|
garfieldsg |
0:662207e34fba
|
256
|
}
|
Yihui Xiong |
2:8c562a8fed36
|
257
|
i2c.write(devAddr << 1 , buf, len);
|
Yihui Xiong |
2:8c562a8fed36
|
258
|
|
Yihui Xiong |
2:8c562a8fed36
|
259
|
free (buf);
|
garfieldsg |
0:662207e34fba
|
260
|
return true;
|
garfieldsg |
0:662207e34fba
|
261
|
}
|
garfieldsg |
0:662207e34fba
|
262
|
|
garfieldsg |
0:662207e34fba
|
263
|
bool I2Cdev::writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data)
|
garfieldsg |
0:662207e34fba
|
264
|
{
|
Yihui Xiong |
2:8c562a8fed36
|
265
|
int len = length * 2 + 1;
|
Yihui Xiong |
2:8c562a8fed36
|
266
|
char *buf = (char*)malloc(len);
|
Yihui Xiong |
2:8c562a8fed36
|
267
|
buf[0] = regAddr;
|
Yihui Xiong |
2:8c562a8fed36
|
268
|
for (int i = 0; i < length; i++) {
|
Yihui Xiong |
2:8c562a8fed36
|
269
|
uint16_t word = data[i];
|
Yihui Xiong |
2:8c562a8fed36
|
270
|
buf[2*i + 1] = word >> 8;
|
Yihui Xiong |
2:8c562a8fed36
|
271
|
buf[2*i + 2] = word & 0xFF;
|
Yihui Xiong |
2:8c562a8fed36
|
272
|
}
|
Yihui Xiong |
2:8c562a8fed36
|
273
|
i2c.write(devAddr << 1 , buf, len);
|
Yihui Xiong |
2:8c562a8fed36
|
274
|
|
Yihui Xiong |
2:8c562a8fed36
|
275
|
free (buf);
|
garfieldsg |
0:662207e34fba
|
276
|
return true;
|
garfieldsg |
0:662207e34fba
|
277
|
}
|
garfieldsg |
0:662207e34fba
|
278
|
|
garfieldsg |
0:662207e34fba
|
279
|
uint16_t I2Cdev::readTimeout(void)
|
garfieldsg |
0:662207e34fba
|
280
|
{
|
garfieldsg |
0:662207e34fba
|
281
|
return 0;
|
garfieldsg |
0:662207e34fba
|
282
|
} |