@@ -35,16 +35,16 @@ static const uint8_t MASTER_ADDRESS = 0x01;
35
35
36
36
TwoWire::TwoWire ()
37
37
{
38
- memset ((void *)&_i2c, 0 , sizeof (_i2c));
39
- _i2c.sda = digitalPinToPinName (SDA);
40
- _i2c.scl = digitalPinToPinName (SCL);
38
+ memset ((void *)&_i2c, 0 , sizeof (_i2c));
39
+ _i2c.sda = digitalPinToPinName (SDA);
40
+ _i2c.scl = digitalPinToPinName (SCL);
41
41
}
42
42
43
43
TwoWire::TwoWire (uint32_t sda, uint32_t scl)
44
44
{
45
- memset ((void *)&_i2c, 0 , sizeof (_i2c));
46
- _i2c.sda = digitalPinToPinName (sda);
47
- _i2c.scl = digitalPinToPinName (scl);
45
+ memset ((void *)&_i2c, 0 , sizeof (_i2c));
46
+ _i2c.sda = digitalPinToPinName (sda);
47
+ _i2c.scl = digitalPinToPinName (scl);
48
48
}
49
49
50
50
/* *
@@ -53,179 +53,179 @@ TwoWire::TwoWire(uint32_t sda, uint32_t scl)
53
53
*/
54
54
TwoWire::~TwoWire ()
55
55
{
56
- end ();
56
+ end ();
57
57
}
58
58
59
59
// Public Methods //////////////////////////////////////////////////////////////
60
60
61
61
void TwoWire::begin (uint32_t sda, uint32_t scl)
62
62
{
63
- _i2c.sda = digitalPinToPinName (sda);
64
- _i2c.scl = digitalPinToPinName (scl);
65
- begin ();
63
+ _i2c.sda = digitalPinToPinName (sda);
64
+ _i2c.scl = digitalPinToPinName (scl);
65
+ begin ();
66
66
}
67
67
68
68
void TwoWire::begin (bool generalCall)
69
69
{
70
- begin (MASTER_ADDRESS, generalCall);
70
+ begin (MASTER_ADDRESS, generalCall);
71
71
}
72
72
73
73
void TwoWire::begin (uint8_t address, bool generalCall, bool NoStretchMode)
74
74
{
75
- rxBufferIndex = 0 ;
76
- rxBufferLength = 0 ;
77
- rxBuffer = nullptr ;
78
- rxBufferAllocated = 0 ;
79
- resetRxBuffer ();
75
+ rxBufferIndex = 0 ;
76
+ rxBufferLength = 0 ;
77
+ rxBuffer = nullptr ;
78
+ rxBufferAllocated = 0 ;
79
+ resetRxBuffer ();
80
80
81
- txDataSize = 0 ;
82
- txAddress = 0 ;
83
- txBuffer = nullptr ;
84
- txBufferAllocated = 0 ;
85
- resetTxBuffer ();
81
+ txDataSize = 0 ;
82
+ txAddress = 0 ;
83
+ txBuffer = nullptr ;
84
+ txBufferAllocated = 0 ;
85
+ resetTxBuffer ();
86
86
87
- _i2c.__this = (void *)this ;
88
- user_onRequest = NULL ;
89
- transmitting = 0 ;
87
+ _i2c.__this = (void *)this ;
88
+ user_onRequest = NULL ;
89
+ transmitting = 0 ;
90
90
91
- ownAddress = address << 1 ;
91
+ ownAddress = address << 1 ;
92
92
93
- _i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0 ;
93
+ _i2c.isMaster = (address == MASTER_ADDRESS) ? 1 : 0 ;
94
94
95
- _i2c.generalCall = (generalCall == true ) ? 1 : 0 ;
95
+ _i2c.generalCall = (generalCall == true ) ? 1 : 0 ;
96
96
97
- _i2c.NoStretchMode = (NoStretchMode == true ) ? 1 : 0 ;
97
+ _i2c.NoStretchMode = (NoStretchMode == true ) ? 1 : 0 ;
98
98
99
- recoverBus (); // in case I2C bus (device) is stuck after a reset for example
99
+ recoverBus (); // in case I2C bus (device) is stuck after a reset for example
100
100
101
- i2c_custom_init (&_i2c, 100000 , I2C_ADDRESSINGMODE_7BIT, ownAddress);
101
+ i2c_custom_init (&_i2c, 100000 , I2C_ADDRESSINGMODE_7BIT, ownAddress);
102
102
103
- if (_i2c.isMaster == 0 ) {
104
- // i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
105
- // i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
103
+ if (_i2c.isMaster == 0 ) {
104
+ // i2c_attachSlaveTxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*)>(&TwoWire::onRequestService));
105
+ // i2c_attachSlaveRxEvent(&_i2c, reinterpret_cast<void(*)(i2c_t*, uint8_t*, int)>(&TwoWire::onReceiveService));
106
106
107
- i2c_attachSlaveTxEvent (&_i2c, onRequestService);
108
- i2c_attachSlaveRxEvent (&_i2c, onReceiveService);
109
- }
107
+ i2c_attachSlaveTxEvent (&_i2c, onRequestService);
108
+ i2c_attachSlaveRxEvent (&_i2c, onReceiveService);
109
+ }
110
110
}
111
111
112
112
void TwoWire::begin (int address, bool generalCall, bool NoStretchMode)
113
113
{
114
- begin ((uint8_t )address, generalCall, NoStretchMode);
114
+ begin ((uint8_t )address, generalCall, NoStretchMode);
115
115
}
116
116
117
117
void TwoWire::end (void )
118
118
{
119
- i2c_deinit (&_i2c);
120
- if (txBuffer != nullptr ) {
121
- free (txBuffer);
122
- txBuffer = nullptr ;
123
- }
124
- txBufferAllocated = 0 ;
125
- if (rxBuffer != nullptr ) {
126
- free (rxBuffer);
127
- rxBuffer = nullptr ;
128
- }
129
- rxBufferAllocated = 0 ;
119
+ i2c_deinit (&_i2c);
120
+ if (txBuffer != nullptr ) {
121
+ free (txBuffer);
122
+ txBuffer = nullptr ;
123
+ }
124
+ txBufferAllocated = 0 ;
125
+ if (rxBuffer != nullptr ) {
126
+ free (rxBuffer);
127
+ rxBuffer = nullptr ;
128
+ }
129
+ rxBufferAllocated = 0 ;
130
130
}
131
131
132
132
void TwoWire::setClock (uint32_t frequency)
133
133
{
134
- i2c_setTiming (&_i2c, frequency);
135
- if (_i2c.isMaster == 0 ) {
136
- i2c_attachSlaveTxEvent (&_i2c, onRequestService);
137
- i2c_attachSlaveRxEvent (&_i2c, onReceiveService);
138
- }
134
+ i2c_setTiming (&_i2c, frequency);
135
+ if (_i2c.isMaster == 0 ) {
136
+ i2c_attachSlaveTxEvent (&_i2c, onRequestService);
137
+ i2c_attachSlaveRxEvent (&_i2c, onReceiveService);
138
+ }
139
139
}
140
140
141
141
uint8_t TwoWire::requestFrom (uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
142
142
{
143
143
#if !defined(I2C_OTHER_FRAME)
144
- UNUSED (sendStop);
144
+ UNUSED (sendStop);
145
145
#endif
146
- uint8_t read = 0 ;
146
+ uint8_t read = 0 ;
147
147
148
- if (_i2c.isMaster == 1 ) {
149
- allocateRxBuffer (quantity);
148
+ if (_i2c.isMaster == 1 ) {
149
+ allocateRxBuffer (quantity);
150
150
151
- if (isize > 0 ) {
152
- // send internal address; this mode allows sending a repeated start to access
153
- // some devices' internal registers. This function is executed by the hardware
154
- // TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
151
+ if (isize > 0 ) {
152
+ // send internal address; this mode allows sending a repeated start to access
153
+ // some devices' internal registers. This function is executed by the hardware
154
+ // TWI module on other processors (for example Due's TWI_IADR and TWI_MMR registers)
155
155
156
- beginTransmission (address);
156
+ beginTransmission (address);
157
157
158
- // the maximum size of internal address is 3 bytes
159
- if (isize > 3 ) {
160
- isize = 3 ;
161
- }
158
+ // the maximum size of internal address is 3 bytes
159
+ if (isize > 3 ) {
160
+ isize = 3 ;
161
+ }
162
162
163
- // write internal register address - most significant byte first
164
- while (isize-- > 0 ) {
165
- write ((uint8_t )(iaddress >> (isize * 8 )));
166
- }
167
- endTransmission (false );
168
- }
163
+ // write internal register address - most significant byte first
164
+ while (isize-- > 0 ) {
165
+ write ((uint8_t )(iaddress >> (isize * 8 )));
166
+ }
167
+ endTransmission (false );
168
+ }
169
169
170
- // perform blocking read into buffer
170
+ // perform blocking read into buffer
171
171
#if defined(I2C_OTHER_FRAME)
172
- if (sendStop == 0 ) {
173
- _i2c.handle .XferOptions = I2C_OTHER_FRAME ;
174
- } else {
175
- _i2c.handle .XferOptions = I2C_OTHER_AND_LAST_FRAME;
176
- }
172
+ if (sendStop == 0 ) {
173
+ _i2c.handle .XferOptions = I2C_OTHER_FRAME ;
174
+ } else {
175
+ _i2c.handle .XferOptions = I2C_OTHER_AND_LAST_FRAME;
176
+ }
177
177
#endif
178
178
179
- if (I2C_OK == i2c_master_read (&_i2c, address << 1 , rxBuffer, quantity)) {
180
- read = quantity;
181
- }
179
+ if (I2C_OK == i2c_master_read (&_i2c, address << 1 , rxBuffer, quantity)) {
180
+ read = quantity;
181
+ }
182
182
183
- // set rx buffer iterator vars
184
- rxBufferIndex = 0 ;
185
- rxBufferLength = read;
183
+ // set rx buffer iterator vars
184
+ rxBufferIndex = 0 ;
185
+ rxBufferLength = read;
186
186
187
- }
188
- return read;
187
+ }
188
+ return read;
189
189
}
190
190
191
191
uint8_t TwoWire::requestFrom (uint8_t address, uint8_t quantity, uint8_t sendStop)
192
192
{
193
- return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint32_t )0 , (uint8_t )0 , (uint8_t )sendStop);
193
+ return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint32_t )0 , (uint8_t )0 , (uint8_t )sendStop);
194
194
}
195
195
196
196
uint8_t TwoWire::requestFrom (uint8_t address, size_t quantity, bool sendStop)
197
197
{
198
- return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )sendStop);
198
+ return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )sendStop);
199
199
}
200
200
201
201
uint8_t TwoWire::requestFrom (uint8_t address, uint8_t quantity)
202
202
{
203
- return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )true );
203
+ return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )true );
204
204
}
205
205
206
206
uint8_t TwoWire::requestFrom (int address, int quantity)
207
207
{
208
- return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )true );
208
+ return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )true );
209
209
}
210
210
211
211
uint8_t TwoWire::requestFrom (int address, int quantity, int sendStop)
212
212
{
213
- return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )sendStop);
213
+ return requestFrom ((uint8_t )address, (uint8_t )quantity, (uint8_t )sendStop);
214
214
}
215
215
216
216
void TwoWire::beginTransmission (uint8_t address)
217
217
{
218
- // indicate that we are transmitting
219
- transmitting = 1 ;
220
- // set address of targeted slave
221
- txAddress = address << 1 ;
222
- // reset tx data size
223
- txDataSize = 0 ;
218
+ // indicate that we are transmitting
219
+ transmitting = 1 ;
220
+ // set address of targeted slave
221
+ txAddress = address << 1 ;
222
+ // reset tx data size
223
+ txDataSize = 0 ;
224
224
}
225
225
226
226
void TwoWire::beginTransmission (int address)
227
227
{
228
- beginTransmission ((uint8_t )address);
228
+ beginTransmission ((uint8_t )address);
229
229
}
230
230
231
231
//
@@ -244,85 +244,85 @@ void TwoWire::beginTransmission(int address)
244
244
uint8_t TwoWire::endTransmission (uint8_t sendStop)
245
245
{
246
246
#if !defined(I2C_OTHER_FRAME)
247
- UNUSED (sendStop);
247
+ UNUSED (sendStop);
248
248
#endif
249
- int8_t ret = 4 ;
250
- // check transfer options and store it in the I2C handle
249
+ int8_t ret = 4 ;
250
+ // check transfer options and store it in the I2C handle
251
251
#if defined(I2C_OTHER_FRAME)
252
- if (sendStop == 0 ) {
253
- _i2c.handle .XferOptions = I2C_OTHER_FRAME ;
254
- } else {
255
- _i2c.handle .XferOptions = I2C_OTHER_AND_LAST_FRAME;
256
- }
252
+ if (sendStop == 0 ) {
253
+ _i2c.handle .XferOptions = I2C_OTHER_FRAME ;
254
+ } else {
255
+ _i2c.handle .XferOptions = I2C_OTHER_AND_LAST_FRAME;
256
+ }
257
257
#endif
258
258
259
- if (_i2c.isMaster == 1 ) {
260
- // transmit buffer (blocking)
261
- switch (i2c_master_write (&_i2c, txAddress, txBuffer, txDataSize)) {
262
- case I2C_OK :
263
- ret = 0 ; // Success
264
- break ;
265
- case I2C_DATA_TOO_LONG :
266
- ret = 1 ;
267
- break ;
268
- case I2C_NACK_ADDR:
269
- ret = 2 ;
270
- break ;
271
- case I2C_NACK_DATA:
272
- ret = 3 ;
273
- break ;
274
- case I2C_TIMEOUT:
275
- case I2C_BUSY:
276
- case I2C_ERROR:
277
- default :
278
- ret = 4 ;
279
- break ;
259
+ if (_i2c.isMaster == 1 ) {
260
+ // transmit buffer (blocking)
261
+ switch (i2c_master_write (&_i2c, txAddress, txBuffer, txDataSize)) {
262
+ case I2C_OK :
263
+ ret = 0 ; // Success
264
+ break ;
265
+ case I2C_DATA_TOO_LONG :
266
+ ret = 1 ;
267
+ break ;
268
+ case I2C_NACK_ADDR:
269
+ ret = 2 ;
270
+ break ;
271
+ case I2C_NACK_DATA:
272
+ ret = 3 ;
273
+ break ;
274
+ case I2C_TIMEOUT:
275
+ case I2C_BUSY:
276
+ case I2C_ERROR:
277
+ default :
278
+ ret = 4 ;
279
+ break ;
280
+ }
281
+
282
+ // reset Tx buffer
283
+ resetTxBuffer ();
284
+
285
+ // reset tx buffer data size
286
+ txDataSize = 0 ;
287
+
288
+ // indicate that we are done transmitting
289
+ transmitting = 0 ;
280
290
}
281
-
282
- // reset Tx buffer
283
- resetTxBuffer ();
284
-
285
- // reset tx buffer data size
286
- txDataSize = 0 ;
287
-
288
- // indicate that we are done transmitting
289
- transmitting = 0 ;
290
- }
291
- return ret;
291
+ return ret;
292
292
}
293
293
294
294
// This provides backwards compatibility with the original
295
295
// definition, and expected behaviour, of endTransmission
296
296
//
297
297
uint8_t TwoWire::endTransmission (void )
298
298
{
299
- return endTransmission ((uint8_t )true );
299
+ return endTransmission ((uint8_t )true );
300
300
}
301
301
302
302
// must be called in:
303
303
// slave tx event callback
304
304
// or after beginTransmission(address)
305
305
size_t TwoWire::write (uint8_t data)
306
306
{
307
- size_t ret = 1 ;
308
- if (transmitting) {
309
- // in master transmitter mode
310
- if (allocateTxBuffer (txDataSize + 1 ) == 0 ) {
311
- ret = 0 ;
307
+ size_t ret = 1 ;
308
+ if (transmitting) {
309
+ // in master transmitter mode
310
+ if (allocateTxBuffer (txDataSize + 1 ) == 0 ) {
311
+ ret = 0 ;
312
+ } else {
313
+ // put byte in tx buffer
314
+ txBuffer[txDataSize] = data;
315
+ // update amount in buffer
316
+ txDataSize++;
317
+ }
312
318
} else {
313
- // put byte in tx buffer
314
- txBuffer[txDataSize] = data;
315
- // update amount in buffer
316
- txDataSize++;
319
+ // in slave send mode
320
+ // reply to master
321
+ if (i2c_slave_write_IT (&_i2c, &data, 1 ) != I2C_OK) {
322
+ ret = 0 ;
323
+ }
317
324
}
318
- } else {
319
- // in slave send mode
320
- // reply to master
321
- if (i2c_slave_write_IT (&_i2c, &data, 1 ) != I2C_OK) {
322
- ret = 0 ;
323
- }
324
- }
325
- return ret;
325
+ return ret;
326
326
}
327
327
328
328
/* *
@@ -334,134 +334,134 @@ size_t TwoWire::write(uint8_t data)
334
334
*/
335
335
size_t TwoWire::write (const uint8_t *data, size_t quantity)
336
336
{
337
- size_t ret = quantity;
337
+ size_t ret = quantity;
338
338
339
- if (transmitting) {
340
- // in master transmitter mode
341
- if (allocateTxBuffer (txDataSize + quantity) == 0 ) {
342
- ret = 0 ;
343
- } else {
344
- // put bytes in tx buffer
345
- memcpy (&(txBuffer[txDataSize]), data, quantity);
339
+ if (transmitting) {
340
+ // in master transmitter mode
341
+ if (allocateTxBuffer (txDataSize + quantity) == 0 ) {
342
+ ret = 0 ;
343
+ } else {
344
+ // put bytes in tx buffer
345
+ memcpy (&(txBuffer[txDataSize]), data, quantity);
346
346
347
- // update amount in buffer
348
- txDataSize += quantity;
349
- }
350
- } else {
351
- // in slave send mode
352
- // reply to master
353
- if (i2c_slave_write_IT (&_i2c, (uint8_t *)data, quantity) != I2C_OK) {
354
- ret = 0 ;
347
+ // update amount in buffer
348
+ txDataSize += quantity;
349
+ }
350
+ } else {
351
+ // in slave send mode
352
+ // reply to master
353
+ if (i2c_slave_write_IT (&_i2c, (uint8_t *)data, quantity) != I2C_OK) {
354
+ ret = 0 ;
355
+ }
355
356
}
356
- }
357
- return ret;
357
+ return ret;
358
358
}
359
359
360
360
// must be called in:
361
361
// slave rx event callback
362
362
// or after requestFrom(address, numBytes)
363
363
int TwoWire::available (void )
364
364
{
365
- return rxBufferLength - rxBufferIndex;
365
+ return rxBufferLength - rxBufferIndex;
366
366
}
367
367
368
368
// must be called in:
369
369
// slave rx event callback
370
370
// or after requestFrom(address, numBytes)
371
371
int TwoWire::read (void )
372
372
{
373
- int value = -1 ;
373
+ int value = -1 ;
374
374
375
- // get each successive byte on each call
376
- if (rxBufferIndex < rxBufferLength) {
377
- value = rxBuffer[rxBufferIndex];
378
- ++rxBufferIndex;
375
+ // get each successive byte on each call
376
+ if (rxBufferIndex < rxBufferLength) {
377
+ value = rxBuffer[rxBufferIndex];
378
+ ++rxBufferIndex;
379
379
380
- /* Commented as not I think it is not useful
381
- * but kept to show that it is possible to
382
- * reset rx buffer when no more data available */
383
- /* if(rxBufferIndex == rxBufferLength) {
384
- resetRxBuffer();
385
- }*/
386
- }
387
- return value;
380
+ /* Commented as not I think it is not useful
381
+ * but kept to show that it is possible to
382
+ * reset rx buffer when no more data available */
383
+ /* if(rxBufferIndex == rxBufferLength) {
384
+ resetRxBuffer();
385
+ }*/
386
+ }
387
+ return value;
388
388
}
389
389
390
390
// must be called in:
391
391
// slave rx event callback
392
392
// or after requestFrom(address, numBytes)
393
393
int TwoWire::peek (void )
394
394
{
395
- int value = -1 ;
395
+ int value = -1 ;
396
396
397
- if (rxBufferIndex < rxBufferLength) {
398
- value = rxBuffer[rxBufferIndex];
399
- }
400
- return value;
397
+ if (rxBufferIndex < rxBufferLength) {
398
+ value = rxBuffer[rxBufferIndex];
399
+ }
400
+ return value;
401
401
}
402
402
403
403
void TwoWire::flush (void )
404
404
{
405
- rxBufferIndex = 0 ;
406
- rxBufferLength = 0 ;
407
- resetRxBuffer ();
408
- txDataSize = 0 ;
409
- resetTxBuffer ();
405
+ rxBufferIndex = 0 ;
406
+ rxBufferLength = 0 ;
407
+ resetRxBuffer ();
408
+ txDataSize = 0 ;
409
+ resetTxBuffer ();
410
410
}
411
411
412
412
// behind the scenes function that is called when data is received
413
413
void TwoWire::onReceiveService (i2c_t *obj)
414
414
{
415
- uint8_t *inBytes = (uint8_t *) obj->i2cTxRxBuffer ;
416
- int numBytes = obj->slaveRxNbData ;
417
-
418
- TwoWire *TW = (TwoWire *)(obj->__this );
419
-
420
- // don't bother if user hasn't registered a callback
421
- if (TW->user_onReceive ) {
422
- // don't bother if rx buffer is in use by a master requestFrom() op
423
- // i know this drops data, but it allows for slight stupidity
424
- // meaning, they may not have read all the master requestFrom() data yet
425
- if (TW->rxBufferIndex >= TW->rxBufferLength ) {
426
- TW->allocateRxBuffer (numBytes);
427
-
428
- // copy twi rx buffer into local read buffer
429
- // this enables new reads to happen in parallel
430
- memcpy (TW->rxBuffer , inBytes, numBytes);
431
- // set rx iterator vars
432
- TW->rxBufferIndex = 0 ;
433
- TW->rxBufferLength = numBytes;
434
- // alert user program
435
- TW->user_onReceive (numBytes);
415
+ uint8_t *inBytes = (uint8_t *) obj->i2cTxRxBuffer ;
416
+ int numBytes = obj->slaveRxNbData ;
417
+
418
+ TwoWire *TW = (TwoWire *)(obj->__this );
419
+
420
+ // don't bother if user hasn't registered a callback
421
+ if (TW->user_onReceive ) {
422
+ // don't bother if rx buffer is in use by a master requestFrom() op
423
+ // i know this drops data, but it allows for slight stupidity
424
+ // meaning, they may not have read all the master requestFrom() data yet
425
+ if (TW->rxBufferIndex >= TW->rxBufferLength ) {
426
+ TW->allocateRxBuffer (numBytes);
427
+
428
+ // copy twi rx buffer into local read buffer
429
+ // this enables new reads to happen in parallel
430
+ memcpy (TW->rxBuffer , inBytes, numBytes);
431
+ // set rx iterator vars
432
+ TW->rxBufferIndex = 0 ;
433
+ TW->rxBufferLength = numBytes;
434
+ // alert user program
435
+ TW->user_onReceive (numBytes);
436
+ }
436
437
}
437
- }
438
438
}
439
439
440
440
// behind the scenes function that is called when data is requested
441
441
void TwoWire::onRequestService (i2c_t *obj)
442
442
{
443
- TwoWire *TW = (TwoWire *)(obj->__this );
443
+ TwoWire *TW = (TwoWire *)(obj->__this );
444
444
445
- // don't bother if user hasn't registered a callback
446
- if (TW->user_onRequest ) {
447
- // reset tx data size
448
- // !!! this will kill any pending pre-master sendTo() activity
449
- TW->txDataSize = 0 ;
450
- // alert user program
451
- TW->user_onRequest ();
452
- }
445
+ // don't bother if user hasn't registered a callback
446
+ if (TW->user_onRequest ) {
447
+ // reset tx data size
448
+ // !!! this will kill any pending pre-master sendTo() activity
449
+ TW->txDataSize = 0 ;
450
+ // alert user program
451
+ TW->user_onRequest ();
452
+ }
453
453
}
454
454
455
455
// sets function called on slave write
456
456
void TwoWire::onReceive (cb_function_receive_t function)
457
457
{
458
- user_onReceive = function;
458
+ user_onReceive = function;
459
459
}
460
460
461
461
// sets function called on slave read
462
462
void TwoWire::onRequest (cb_function_request_t function)
463
463
{
464
- user_onRequest = function;
464
+ user_onRequest = function;
465
465
}
466
466
467
467
/* *
@@ -471,57 +471,57 @@ void TwoWire::onRequest(cb_function_request_t function)
471
471
*/
472
472
void TwoWire::allocateRxBuffer (size_t length)
473
473
{
474
- if (rxBufferAllocated < length) {
475
- // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
476
- if (length < BUFFER_LENGTH) {
477
- length = BUFFER_LENGTH;
474
+ if (rxBufferAllocated < length) {
475
+ // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
476
+ if (length < BUFFER_LENGTH) {
477
+ length = BUFFER_LENGTH;
478
+ }
479
+ uint8_t *tmp = (uint8_t *)realloc (rxBuffer, length * sizeof (uint8_t ));
480
+ if (tmp != nullptr ) {
481
+ rxBuffer = tmp;
482
+ rxBufferAllocated = length;
483
+ } else {
484
+ _Error_Handler (" No enough memory! (%i)\n " , length);
485
+ }
478
486
}
479
- uint8_t *tmp = (uint8_t *)realloc (rxBuffer, length * sizeof (uint8_t ));
480
- if (tmp != nullptr ) {
481
- rxBuffer = tmp;
482
- rxBufferAllocated = length;
483
- } else {
484
- _Error_Handler (" No enough memory! (%i)\n " , length);
485
- }
486
- }
487
487
}
488
488
489
489
inline size_t TwoWire::allocateTxBuffer (size_t length)
490
490
{
491
- size_t ret = length;
492
- if (length > WIRE_MAX_TX_BUFF_LENGTH) {
493
- ret = 0 ;
494
- } else if (txBufferAllocated < length) {
495
- // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
496
- if (length < BUFFER_LENGTH) {
497
- length = BUFFER_LENGTH;
498
- }
499
- uint8_t *tmp = (uint8_t *)realloc (txBuffer, length * sizeof (uint8_t ));
500
- if (tmp != nullptr ) {
501
- txBuffer = tmp;
502
- txBufferAllocated = length;
503
- } else {
504
- _Error_Handler (" No enough memory! (%i)\n " , length);
491
+ size_t ret = length;
492
+ if (length > WIRE_MAX_TX_BUFF_LENGTH) {
493
+ ret = 0 ;
494
+ } else if (txBufferAllocated < length) {
495
+ // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
496
+ if (length < BUFFER_LENGTH) {
497
+ length = BUFFER_LENGTH;
498
+ }
499
+ uint8_t *tmp = (uint8_t *)realloc (txBuffer, length * sizeof (uint8_t ));
500
+ if (tmp != nullptr ) {
501
+ txBuffer = tmp;
502
+ txBufferAllocated = length;
503
+ } else {
504
+ _Error_Handler (" No enough memory! (%i)\n " , length);
505
+ }
505
506
}
506
- }
507
- return ret;
507
+ return ret;
508
508
}
509
509
510
510
/* *
511
511
* @brief Reset Rx/Tx buffer content to 0
512
512
*/
513
513
inline void TwoWire::resetRxBuffer (void )
514
514
{
515
- if (rxBuffer != nullptr ) {
516
- memset (rxBuffer, 0 , rxBufferAllocated);
517
- }
515
+ if (rxBuffer != nullptr ) {
516
+ memset (rxBuffer, 0 , rxBufferAllocated);
517
+ }
518
518
}
519
519
520
520
inline void TwoWire::resetTxBuffer (void )
521
521
{
522
- if (txBuffer != nullptr ) {
523
- memset (txBuffer, 0 , txBufferAllocated);
524
- }
522
+ if (txBuffer != nullptr ) {
523
+ memset (txBuffer, 0 , txBufferAllocated);
524
+ }
525
525
}
526
526
527
527
// Send clear bus (clock pulse) sequence to recover bus.
@@ -531,19 +531,19 @@ inline void TwoWire::resetTxBuffer(void)
531
531
// https://bits4device.wordpress.com/2017/07/28/i2c-bus-recovery/
532
532
void TwoWire::recoverBus (void )
533
533
{
534
- pinMode (pinNametoDigitalPin (_i2c.sda ), INPUT);
534
+ pinMode (pinNametoDigitalPin (_i2c.sda ), INPUT);
535
535
536
- if (digitalReadFast (_i2c.sda ) == LOW) {
537
- pinMode (pinNametoDigitalPin (_i2c.scl ), OUTPUT);
536
+ if (digitalReadFast (_i2c.sda ) == LOW) {
537
+ pinMode (pinNametoDigitalPin (_i2c.scl ), OUTPUT);
538
538
539
- for (int i = 0 ; i < 20 ; i++) {
540
- digitalWriteFast (_i2c.scl , LOW);
541
- delayMicroseconds (10 );
542
- digitalWriteFast (_i2c.scl , HIGH);
543
- delayMicroseconds (10 );
539
+ for (int i = 0 ; i < 20 ; i++) {
540
+ digitalWriteFast (_i2c.scl , LOW);
541
+ delayMicroseconds (10 );
542
+ digitalWriteFast (_i2c.scl , HIGH);
543
+ delayMicroseconds (10 );
544
+ }
545
+ pinMode (pinNametoDigitalPin (_i2c.scl ), INPUT);
544
546
}
545
- pinMode (pinNametoDigitalPin (_i2c.scl ), INPUT);
546
- }
547
547
}
548
548
549
549
// Preinstantiate Objects //////////////////////////////////////////////////////
0 commit comments