forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWire.cpp
506 lines (433 loc) · 12.7 KB
/
Wire.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/*
TwoWire.cpp - TWI/I2C library for Wiring & Arduino
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 2012 by Todd Krein ([email protected]) to implement repeated starts
*/
extern "C" {
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
}
#include "Wire.h"
// Distinguish master from slave.
// 0x01 is a reserved value, and thus cannot be used by slave devices
static const uint8_t MASTER_ADDRESS = 0x01;
// Constructors ////////////////////////////////////////////////////////////////
TwoWire::TwoWire()
{
_i2c.sda = digitalPinToPinName(SDA);
_i2c.scl = digitalPinToPinName(SCL);
}
TwoWire::TwoWire(uint32_t sda, uint32_t scl)
{
_i2c.sda = digitalPinToPinName(sda);
_i2c.scl = digitalPinToPinName(scl);
}
// Public Methods //////////////////////////////////////////////////////////////
void TwoWire::begin(uint32_t sda, uint32_t scl)
{
_i2c.sda = digitalPinToPinName(sda);
_i2c.scl = digitalPinToPinName(scl);
begin();
}
void TwoWire::begin(bool generalCall)
{
begin(MASTER_ADDRESS, generalCall);
}
void TwoWire::begin(uint8_t address, bool generalCall)
{
rxBufferIndex = 0;
rxBufferLength = 0;
rxBuffer = nullptr;
rxBufferAllocated = 0;
resetRxBuffer();
txDataSize = 0;
txAddress = 0;
txBuffer = nullptr;
txBufferAllocated = 0;
resetTxBuffer();
_i2c.__this = (void *)this;
user_onRequest = NULL;
transmitting = 0;
ownAddress = address << 1;
_i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0;
_i2c.generalCall = (generalCall == true) ? 1 : 0;
i2c_custom_init(&_i2c, 100000, I2C_ADDRESSINGMODE_7BIT, ownAddress);
if (_i2c.isMaster == 0) {
// i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
// i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
i2c_attachSlaveTxEvent(&_i2c, onRequestService);
i2c_attachSlaveRxEvent(&_i2c, onReceiveService);
}
}
void TwoWire::begin(int address, bool generalCall)
{
begin((uint8_t)address, generalCall);
}
void TwoWire::end(void)
{
i2c_deinit(&_i2c);
free(txBuffer);
txBuffer = nullptr;
txBufferAllocated = 0;
free(rxBuffer);
rxBuffer = nullptr;
rxBufferAllocated = 0;
}
void TwoWire::setClock(uint32_t frequency)
{
i2c_setTiming(&_i2c, frequency);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
{
#if !defined(I2C_OTHER_FRAME)
UNUSED(sendStop);
#endif
uint8_t read = 0;
if (_i2c.isMaster == 1) {
allocateRxBuffer(quantity);
if (isize > 0) {
// send internal address; this mode allows sending a repeated start to access
// some devices' internal registers. This function is executed by the hardware
// TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
beginTransmission(address);
// the maximum size of internal address is 3 bytes
if (isize > 3) {
isize = 3;
}
// write internal register address - most significant byte first
while (isize-- > 0) {
write((uint8_t)(iaddress >> (isize * 8)));
}
endTransmission(false);
}
// perform blocking read into buffer
#if defined(I2C_OTHER_FRAME)
if (sendStop == 0) {
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
} else {
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
}
#endif
if (I2C_OK == i2c_master_read(&_i2c, address << 1, rxBuffer, quantity)) {
read = quantity;
}
// set rx buffer iterator vars
rxBufferIndex = 0;
rxBufferLength = read;
}
return read;
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
{
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint32_t)0, (uint8_t)0, (uint8_t)sendStop);
}
uint8_t TwoWire::requestFrom(uint8_t address, size_t quantity, bool sendStop)
{
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
}
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
{
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
}
uint8_t TwoWire::requestFrom(int address, int quantity)
{
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
}
uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
{
return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
}
void TwoWire::beginTransmission(uint8_t address)
{
// indicate that we are transmitting
transmitting = 1;
// set address of targeted slave
txAddress = address << 1;
// reset tx data size
txDataSize = 0;
}
void TwoWire::beginTransmission(int address)
{
beginTransmission((uint8_t)address);
}
//
// Originally, 'endTransmission' was an f(void) function.
// It has been modified to take one parameter indicating
// whether or not a STOP should be performed on the bus.
// Calling endTransmission(false) allows a sketch to
// perform a repeated start.
//
// WARNING: Nothing in the library keeps track of whether
// the bus tenure has been properly ended with a STOP. It
// is very possible to leave the bus in a hung state if
// no call to endTransmission(true) is made. Some I2C
// devices will behave oddly if they do not see a STOP.
//
uint8_t TwoWire::endTransmission(uint8_t sendStop)
{
#if !defined(I2C_OTHER_FRAME)
UNUSED(sendStop);
#endif
int8_t ret = 4;
// check transfer options and store it in the I2C handle
#if defined(I2C_OTHER_FRAME)
if (sendStop == 0) {
_i2c.handle.XferOptions = I2C_OTHER_FRAME ;
} else {
_i2c.handle.XferOptions = I2C_OTHER_AND_LAST_FRAME;
}
#endif
if (_i2c.isMaster == 1) {
// transmit buffer (blocking)
switch (i2c_master_write(&_i2c, txAddress, txBuffer, txDataSize)) {
case I2C_OK :
ret = 0; // Success
break;
case I2C_DATA_TOO_LONG :
ret = 1;
break;
case I2C_NACK_ADDR:
ret = 2;
break;
case I2C_NACK_DATA:
ret = 3;
break;
case I2C_TIMEOUT:
case I2C_BUSY:
case I2C_ERROR:
default:
ret = 4;
break;
}
// reset Tx buffer
resetTxBuffer();
// reset tx buffer data size
txDataSize = 0;
// indicate that we are done transmitting
transmitting = 0;
}
return ret;
}
// This provides backwards compatibility with the original
// definition, and expected behaviour, of endTransmission
//
uint8_t TwoWire::endTransmission(void)
{
return endTransmission((uint8_t)true);
}
// must be called in:
// slave tx event callback
// or after beginTransmission(address)
size_t TwoWire::write(uint8_t data)
{
size_t ret = 1;
if (transmitting) {
// in master transmitter mode
if (allocateTxBuffer(txDataSize + 1) == 0) {
ret = 0;
} else {
// put byte in tx buffer
txBuffer[txDataSize] = data;
// update amount in buffer
txDataSize++;
}
} else {
// in slave send mode
// reply to master
if (i2c_slave_write_IT(&_i2c, &data, 1) != I2C_OK) {
ret = 0;
}
}
return ret;
}
/**
* @brief This function must be called in slave Tx event callback or after
* beginTransmission() and before endTransmission().
* @param pdata: pointer to the buffer data
* @param quantity: number of bytes to write
* @retval number of bytes ready to write.
*/
size_t TwoWire::write(const uint8_t *data, size_t quantity)
{
size_t ret = quantity;
if (transmitting) {
// in master transmitter mode
if (allocateTxBuffer(txDataSize + quantity) == 0) {
ret = 0;
} else {
// put bytes in tx buffer
memcpy(&(txBuffer[txDataSize]), data, quantity);
// update amount in buffer
txDataSize += quantity;
}
} else {
// in slave send mode
// reply to master
if (i2c_slave_write_IT(&_i2c, (uint8_t *)data, quantity) != I2C_OK) {
ret = 0;
}
}
return ret;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::available(void)
{
return rxBufferLength - rxBufferIndex;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::read(void)
{
int value = -1;
// get each successive byte on each call
if (rxBufferIndex < rxBufferLength) {
value = rxBuffer[rxBufferIndex];
++rxBufferIndex;
/* Commented as not I think it is not useful
* but kept to show that it is possible to
* reset rx buffer when no more data available */
/*if(rxBufferIndex == rxBufferLength) {
resetRxBuffer();
}*/
}
return value;
}
// must be called in:
// slave rx event callback
// or after requestFrom(address, numBytes)
int TwoWire::peek(void)
{
int value = -1;
if (rxBufferIndex < rxBufferLength) {
value = rxBuffer[rxBufferIndex];
}
return value;
}
void TwoWire::flush(void)
{
rxBufferIndex = 0;
rxBufferLength = 0;
resetRxBuffer();
txDataSize = 0;
resetTxBuffer();
}
// behind the scenes function that is called when data is received
void TwoWire::onReceiveService(i2c_t *obj)
{
uint8_t *inBytes = (uint8_t *) obj->i2cTxRxBuffer;
int numBytes = obj->slaveRxNbData;
TwoWire *TW = (TwoWire *)(obj->__this);
// don't bother if user hasn't registered a callback
if (TW->user_onReceive) {
// don't bother if rx buffer is in use by a master requestFrom() op
// i know this drops data, but it allows for slight stupidity
// meaning, they may not have read all the master requestFrom() data yet
if (TW->rxBufferIndex >= TW->rxBufferLength) {
TW->allocateRxBuffer(numBytes);
// copy twi rx buffer into local read buffer
// this enables new reads to happen in parallel
memcpy(TW->rxBuffer, inBytes, numBytes);
// set rx iterator vars
TW->rxBufferIndex = 0;
TW->rxBufferLength = numBytes;
// alert user program
TW->user_onReceive(numBytes);
}
}
}
// behind the scenes function that is called when data is requested
void TwoWire::onRequestService(i2c_t *obj)
{
TwoWire *TW = (TwoWire *)(obj->__this);
// don't bother if user hasn't registered a callback
if (TW->user_onRequest) {
// reset tx data size
// !!! this will kill any pending pre-master sendTo() activity
TW->txDataSize = 0;
// alert user program
TW->user_onRequest();
}
}
// sets function called on slave write
void TwoWire::onReceive(void (*function)(int))
{
user_onReceive = function;
}
// sets function called on slave read
void TwoWire::onRequest(void (*function)(void))
{
user_onRequest = function;
}
/**
* @brief Allocate the Rx/Tx buffer to the requested length if needed
* @note Minimum allocated size is BUFFER_LENGTH)
* @param length: number of bytes to allocate
*/
void TwoWire::allocateRxBuffer(size_t length)
{
if (rxBufferAllocated < length) {
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
if (length < BUFFER_LENGTH) {
length = BUFFER_LENGTH;
}
uint8_t *tmp = (uint8_t *)realloc(rxBuffer, length * sizeof(uint8_t));
if (tmp != nullptr) {
rxBuffer = tmp;
rxBufferAllocated = length;
} else {
_Error_Handler("No enough memory! (%i)\n", length);
}
}
}
inline size_t TwoWire::allocateTxBuffer(size_t length)
{
size_t ret = length;
if (length > WIRE_MAX_TX_BUFF_LENGTH) {
ret = 0;
} else if (txBufferAllocated < length) {
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
if (length < BUFFER_LENGTH) {
length = BUFFER_LENGTH;
}
uint8_t *tmp = (uint8_t *)realloc(txBuffer, length * sizeof(uint8_t));
if (tmp != nullptr) {
txBuffer = tmp;
txBufferAllocated = length;
} else {
_Error_Handler("No enough memory! (%i)\n", length);
}
}
return ret;
}
/**
* @brief Reset Rx/Tx buffer content to 0
*/
inline void TwoWire::resetRxBuffer(void)
{
if (rxBuffer != nullptr) {
memset(rxBuffer, 0, rxBufferAllocated);
}
}
inline void TwoWire::resetTxBuffer(void)
{
if (txBuffer != nullptr) {
memset(txBuffer, 0, txBufferAllocated);
}
}
// Preinstantiate Objects //////////////////////////////////////////////////////
TwoWire Wire = TwoWire(); //D14-D15