Repository navigation
Expand file tree
/
Copy pathI2Cdev.c
More file actions
391 lines (309 loc) · 12 KB
/
Copy pathI2Cdev.c
File metadata and controls
391 lines (309 loc) · 12 KB
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
// I2Cdev library collection - Main I2C device class header file
// Abstracts bit and byte I2C R/W functions into a convenient class
// 6/9/2012 by Jeff Rowberg <jeff@rowberg.net>
// 03/28/2017 by Kamnev Yuriy <kamnev.u1969@gmail.com>
//
// Changelog:
// 2017-03-28 - ported to STM32 using Keil MDK Pack
/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2013 Jeff Rowberg
Copyright (c) 2017 Kamnev Yuriy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include "I2Cdev.h"
#include <string.h>
#include <I2C_STM32F10x.h>
extern ARM_DRIVER_I2C I2CDev_Driver;
#define _i2c_transmit(dev_addr, data, len, pending) \
I2CDev_Driver.MasterTransmit(dev_addr, data, len, pending);\
while(I2CDev_Driver.GetStatus().busy)
#define _i2c_receive(dev_addr, data, len, pending) \
I2CDev_Driver.MasterReceive(dev_addr, data, len, pending); \
while(I2CDev_Driver.GetStatus().busy)
#define i2c_transmit_ack(dev_addr, data, len) _i2c_transmit(dev_addr, data, len, true)
#define i2c_transmit_nack(dev_addr, data, len) _i2c_transmit(dev_addr, data, len, false)
#define i2c_receive_ack(dev_addr, data, len) _i2c_receive(dev_addr, data, len, true)
#define i2c_receive_nack(dev_addr, data, len) _i2c_receive(dev_addr, data, len, false)
/** Read several byte from an 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to read from
* @param len How many bytes to read
* @param data Buffer to save data into
* @return Status of read operation (0 = success, <0 = error)
*/
int8_t I2Cdev_readBytes(uint8_t dev_addr, uint8_t reg_addr, uint8_t len, uint8_t *data) {
int8_t err = 0;
uint8_t reg_data[1] = {reg_addr};
err = i2c_transmit_ack(dev_addr, reg_data, 1);
if(err < 0) {
return err;
}
err = i2c_receive_nack(dev_addr, data, len);
return err;
}
/** Read a single byte from a 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register reg_addr to read from
* @param data Buffer to save data into
* @return Status of read operation (0 = success, <0 = error)
*/
int8_t I2Cdev_readByte(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data) {
return I2Cdev_readBytes(dev_addr, reg_addr, 1, data);
}
/** Read a several 16-bit words from a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register reg_addr to read from
* @param len Number of words to read
* @param data Buffer to save data into
* @return Status of read operation (true = success)
*/
int8_t I2Cdev_readWords(uint8_t dev_addr, uint8_t reg_addr, uint8_t len, uint16_t *data) {
int8_t err;
uint16_t bytes_num = len*2;
uint8_t reg_info[1] = {reg_addr};
err = i2c_transmit_ack(dev_addr, reg_info, 1);
if(err < 0) {
return err;
}
uint8_t words_in_bytes[bytes_num];
err = i2c_receive_nack(dev_addr, words_in_bytes, bytes_num);
if(err < 0) {
return err;
}
uint8_t words_cnt = 0;
for(uint16_t i=0; i<bytes_num; i+=2) {
data[words_cnt++] = (words_in_bytes[i] << 8) | words_in_bytes[i+1];
}
return 0;
}
/** Read a single word from a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to read from
* @param data Container for single word
* @return Status of read operation (0 = success, <0 = error)
*/
int8_t I2Cdev_readWord(uint8_t dev_addr, uint8_t reg_addr, uint16_t *data) {
return I2Cdev_readWords(dev_addr, reg_addr, 1, data);
}
/** Read a single bit from a 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to read from
* @param bitn Bit position to read (0-15)
* @param data Container for single bit value
* @return Status of read operation (0 = success, <0 = error)
*/
int8_t I2Cdev_readBit(uint8_t dev_addr, uint8_t reg_addr, uint8_t bitn, uint8_t *data) {
int8_t err;
err = I2Cdev_readByte(dev_addr, reg_addr, data);
*data = (*data >> bitn) & 0x01;
return err;
}
/** Read several bits from a 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to read from
* @param start_bit First bit position to read (0-7)
* @param len Number of bits to read (<= 8)
* @param data Container for right-aligned value
* @return Status of read operation (0 = success, <0 = error)
*/
int8_t I2Cdev_readBits(uint8_t dev_addr, uint8_t reg_addr, uint8_t start_bit,
uint8_t len, uint8_t *data)
{
int8_t err;
uint8_t b;
if ((err = I2Cdev_readByte(dev_addr, reg_addr, &b)) == 0) {
uint8_t mask = ((1 << len) - 1) << (start_bit - len + 1);
b &= mask;
b >>= (start_bit - len + 1);
*data = b;
}
return err;
}
/** Read a single bit from a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to read from
* @param bit_n Bit position to read (0-15)
* @param data Container for single bit value
* @return Status of read operation (true = success)
*/
int8_t I2Cdev_readBitW(uint8_t dev_addr, uint8_t reg_addr, uint8_t bit_n, uint16_t *data) {
int8_t err;
err = I2Cdev_readWord(dev_addr, reg_addr, data);
*data = (*data >> bit_n) & 0x01;
return err;
}
/** Read several bits from a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to read from
* @param start_bit First bit position to read (0-15)
* @param len Number of bits to read (<= 16)
* @param data Container for right-aligned value
* @return Status of read operation (0 = success, <0 = error)
*/
int8_t I2Cdev_readBitsW(uint8_t dev_addr, uint8_t reg_addr, uint8_t start_bit,
uint8_t len, uint16_t *data)
{
int8_t err;
uint16_t w;
if ((err = I2Cdev_readWord(dev_addr, reg_addr, &w)) == 0) {
uint16_t mask = ((1 << len) - 1) << (start_bit - len + 1);
w &= mask;
w >>= (start_bit - len + 1);
*data = w;
}
return err;
}
/** Write multiple bytes to an 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr First register address to write to
* @param len Number of bytes to write
* @param data Buffer to copy new data from
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeBytes(uint8_t dev_addr, uint8_t reg_addr, uint8_t len, uint8_t *data) {
int8_t err;
uint8_t ts_data[len+1];
ts_data[0] = reg_addr;
memcpy(ts_data+1, data, len);
err = i2c_transmit_nack(dev_addr, ts_data, len+1);
return err;
}
/** Write single byte to an 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register address to write to
* @param data New byte value to write
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeByte(uint8_t dev_addr, uint8_t reg_addr, uint8_t data) {
int8_t err;
uint8_t ts_data[2] = {reg_addr, data};
err = i2c_transmit_nack(dev_addr, ts_data, 2);
return err;
}
/** Write single 16-bit word to an 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register address to write to
* @param data New byte value to write
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeWord(uint8_t dev_addr, uint8_t reg_addr, uint16_t data) {
int8_t err;
uint8_t ts_data[3] = {reg_addr, (data >> 8) & 0xFF, data & 0xFF};
err = i2c_transmit_nack(reg_addr, ts_data, 3);
return err;
}
/** Write multiple words to a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr First register address to write to
* @param len Number of words to write
* @param data Buffer to copy new data from
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeWords(uint8_t dev_addr, uint8_t reg_addr, uint8_t len, uint16_t *data) {
uint16_t bytes_num = len*2+1;
uint8_t bytes[bytes_num];
bytes[0] = reg_addr;
uint16_t bytes_pos = 1;
for(uint8_t i=0; i<len; i++) {
bytes[bytes_pos] = (data[i] >> 8) & 0xFF;
bytes[bytes_pos+1] = data[i] & 0xFF;
bytes_pos += 2;
}
return i2c_transmit_nack(dev_addr, bytes, bytes_num);
}
/** write a single bit in an 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to write to
* @param bit_n Bit position to write (0-7)
* @param data New bit value to write
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeBit(uint8_t dev_addr, uint8_t reg_addr, uint8_t bit_n, uint8_t data) {
uint8_t b;
int8_t err;
err = I2Cdev_readByte(dev_addr, reg_addr, &b);
if(err < 0) {
return err;
}
b = (data != 0) ? (b | (1<<bit_n)) : (b &= ~(1<<bit_n));
return I2Cdev_writeByte(dev_addr, reg_addr, b);
}
/** write a single bit in a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to write to
* @param bit_n Bit position to write (0-15)
* @param data New bit value to write
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeBitW(uint8_t dev_addr, uint8_t reg_addr, uint8_t bit_n, uint16_t data) {
uint16_t w;
I2Cdev_readWord(dev_addr, reg_addr, &w);
w = (data != 0) ? (w | (1<<bit_n)) : (w &= ~(1<<bit_n));
return I2Cdev_writeWord(dev_addr, reg_addr, w);
}
/** Write multiple bits in an 8-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to write to
* @param start_bit First bit position to write (0-7)
* @param len Number of bits to write (not more than 8)
* @param data Right-aligned value to write
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeBits(uint8_t dev_addr, uint8_t reg_addr, uint8_t start_bit,
uint8_t len, uint8_t data)
{
uint8_t b;
int8_t err;
if ((err = I2Cdev_readByte(dev_addr, reg_addr, &b)) == 0) {
uint8_t mask = ((1 << len) - 1) << (start_bit - len + 1);
data <<= (start_bit - len + 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 I2Cdev_writeByte(dev_addr, reg_addr, b);
}
else {
return err;
}
}
/** Write multiple bits in a 16-bit device register.
* @param dev_addr I2C slave device address
* @param reg_addr Register regAddr to write to
* @param start_bit First bit position to write (0-15)
* @param len Number of bits to write (not more than 16)
* @param data Right-aligned value to write
* @return Status of operation (0 = success, <0 = error)
*/
int8_t I2Cdev_writeBitsW(uint8_t dev_addr, uint8_t reg_addr, uint8_t start_bit,
uint8_t len, uint16_t data)
{
uint16_t w;
int8_t err;
if ((err = I2Cdev_readWord(dev_addr, reg_addr, &w)) != 0) {
uint16_t mask = ((1 << len) - 1) << (start_bit - len + 1);
data <<= (start_bit - len + 1); // shift data into correct position
data &= mask; // zero all non-important bits in data
w &= ~(mask); // zero all important bits in existing word
w |= data; // combine data with existing word
return I2Cdev_writeWord(dev_addr, reg_addr, w);
}
else {
return err;
}
}