@@ -29,11 +29,13 @@ extern "C" {
29
29
30
30
// Initialize Class Variables //////////////////////////////////////////////////
31
31
uint8_t *TwoWire::rxBuffer = nullptr ;
32
+ uint8_t TwoWire::rxBufferAllocated = 0 ;
32
33
uint8_t TwoWire::rxBufferIndex = 0 ;
33
34
uint8_t TwoWire::rxBufferLength = 0 ;
34
35
35
36
uint8_t TwoWire::txAddress = 0 ;
36
37
uint8_t *TwoWire::txBuffer = nullptr ;
38
+ uint8_t TwoWire::txBufferAllocated = 0 ;
37
39
uint8_t TwoWire::txBufferIndex = 0 ;
38
40
uint8_t TwoWire::txBufferLength = 0 ;
39
41
@@ -66,11 +68,11 @@ void TwoWire::begin(uint8_t address)
66
68
{
67
69
rxBufferIndex = 0 ;
68
70
rxBufferLength = 0 ;
69
- rxBuffer = resetBuffer (rxBuffer );
71
+ resetRxBuffer ( );
70
72
71
73
txBufferIndex = 0 ;
72
74
txBufferLength = 0 ;
73
- txBuffer = resetBuffer (txBuffer );
75
+ resetTxBuffer ( );
74
76
75
77
transmitting = 0 ;
76
78
@@ -101,8 +103,10 @@ void TwoWire::end(void)
101
103
{
102
104
free (txBuffer);
103
105
txBuffer = nullptr ;
106
+ txBufferAllocated = 0 ;
104
107
free (rxBuffer);
105
108
rxBuffer = nullptr ;
109
+ rxBufferAllocated = 0 ;
106
110
i2c_deinit (&_i2c);
107
111
}
108
112
@@ -115,7 +119,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
115
119
{
116
120
UNUSED (sendStop);
117
121
if (master == true ) {
118
- rxBuffer = allocateBuffer (rxBuffer, quantity);
122
+ allocateRxBuffer ( quantity);
119
123
// error if no memory block available to allocate the buffer
120
124
if (rxBuffer == nullptr ){
121
125
setWriteError ();
@@ -225,9 +229,7 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
225
229
}
226
230
227
231
// Reduce buffer size to free memory in case of large memory use
228
- if (txBufferLength > BUFFER_LENGTH) {
229
- txBuffer = resetBuffer (txBuffer);
230
- }
232
+ resetTxBuffer ();
231
233
232
234
// reset tx buffer iterator vars
233
235
txBufferIndex = 0 ;
@@ -255,7 +257,7 @@ size_t TwoWire::write(uint8_t data)
255
257
{
256
258
if (transmitting){
257
259
// in master transmitter mode
258
- txBuffer = allocateBuffer (txBuffer, txBufferLength + 1 );
260
+ allocateTxBuffer ( txBufferLength + 1 );
259
261
// error if no memory block available to allocate the buffer
260
262
if (txBuffer == nullptr ){
261
263
setWriteError ();
@@ -285,14 +287,22 @@ size_t TwoWire::write(uint8_t data)
285
287
*/
286
288
size_t TwoWire::write (const uint8_t *data, size_t quantity)
287
289
{
288
- size_t nb = 0 ;
290
+ // size_t nb = 0;
289
291
290
292
if (transmitting){
291
293
// in master transmitter mode
292
- for (size_t i = 0 ; i < quantity; ++i){
293
- nb += write (data[i]);
294
+ allocateTxBuffer (txBufferLength + quantity);
295
+ // error if no memory block available to allocate the buffer
296
+ if (txBuffer == nullptr ){
297
+ setWriteError ();
298
+ return 0 ;
294
299
}
295
- return nb;
300
+ // put bytes in tx buffer
301
+ memcpy (&(txBuffer[txBufferIndex]), data, quantity);
302
+ txBufferIndex= txBufferIndex + quantity;
303
+ // update amount in buffer
304
+ txBufferLength = txBufferIndex;
305
+ return quantity;
296
306
}else {
297
307
// in slave send mode
298
308
// reply to master
@@ -326,7 +336,7 @@ int TwoWire::read(void)
326
336
/* Reduce buffer size to free memory in case of large memory use when no more
327
337
data available */
328
338
if ((rxBufferIndex == rxBufferLength) && (rxBufferLength > BUFFER_LENGTH)) {
329
- rxBuffer = resetBuffer (rxBuffer );
339
+ resetRxBuffer ( );
330
340
}
331
341
}
332
342
@@ -351,10 +361,10 @@ void TwoWire::flush(void)
351
361
{
352
362
rxBufferIndex = 0 ;
353
363
rxBufferLength = 0 ;
354
- rxBuffer = resetBuffer (rxBuffer );
364
+ resetRxBuffer ( );
355
365
txBufferIndex = 0 ;
356
366
txBufferLength = 0 ;
357
- txBuffer = resetBuffer (txBuffer );
367
+ resetTxBuffer ( );
358
368
}
359
369
360
370
// behind the scenes function that is called when data is received
@@ -417,29 +427,61 @@ void TwoWire::onRequest( void (*function)(void) )
417
427
* @param length: number of bytes to allocate
418
428
* @retval pointer to the new buffer location
419
429
*/
420
- uint8_t * TwoWire::allocateBuffer ( uint8_t *buffer, size_t length)
430
+ void TwoWire::allocateRxBuffer ( size_t length)
421
431
{
422
- // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
423
- if (length < BUFFER_LENGTH) {
424
- length = BUFFER_LENGTH;
432
+ if (rxBufferAllocated < length) {
433
+ // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
434
+ if (length < BUFFER_LENGTH) {
435
+ length = BUFFER_LENGTH;
436
+ }
437
+ rxBuffer = (uint8_t *)realloc (rxBuffer, length * sizeof (uint8_t ));
438
+ rxBufferAllocated = length;
425
439
}
440
+ }
426
441
427
- buffer = (uint8_t *)realloc (buffer, length * sizeof (uint8_t ));
428
- return buffer;
442
+ void TwoWire::allocateTxBuffer (size_t length)
443
+ {
444
+ if (txBufferAllocated < length) {
445
+ // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.
446
+ if (length < BUFFER_LENGTH) {
447
+ length = BUFFER_LENGTH;
448
+ }
449
+ txBuffer = (uint8_t *)realloc (txBuffer, length * sizeof (uint8_t ));
450
+ txBufferAllocated = length;
451
+ }
429
452
}
430
453
431
454
/* *
432
455
* @brief Reset the buffer. Reduce is size if greater than BUFFER_LENGTH.
433
456
* @param buffer: pointer to the allocated buffer
434
457
* @retval pointer to the new buffer location
435
458
*/
436
- uint8_t * TwoWire::resetBuffer ( uint8_t *buffer )
459
+ void TwoWire::resetRxBuffer ( void )
437
460
{
438
- buffer = (uint8_t *)realloc (buffer, BUFFER_LENGTH * sizeof (uint8_t ));
439
- if (buffer != nullptr ) {
440
- memset (buffer, 0 , BUFFER_LENGTH);
461
+ if (rxBufferAllocated != BUFFER_LENGTH) {
462
+ rxBuffer = (uint8_t *)realloc (rxBuffer, BUFFER_LENGTH * sizeof (uint8_t ));
463
+ if (rxBuffer != nullptr ) {
464
+ memset (rxBuffer, 0 , BUFFER_LENGTH);
465
+ rxBufferAllocated = BUFFER_LENGTH;
466
+ }
467
+ else {
468
+ rxBufferAllocated = 0 ;
469
+ }
470
+ }
471
+ }
472
+
473
+ void TwoWire::resetTxBuffer (void )
474
+ {
475
+ if (txBufferAllocated != BUFFER_LENGTH) {
476
+ txBuffer = (uint8_t *)realloc (txBuffer, BUFFER_LENGTH * sizeof (uint8_t ));
477
+ if (txBuffer != nullptr ) {
478
+ memset (txBuffer, 0 , BUFFER_LENGTH);
479
+ txBufferAllocated = BUFFER_LENGTH;
480
+ }
481
+ else {
482
+ txBufferAllocated = 0 ;
483
+ }
441
484
}
442
- return buffer;
443
485
}
444
486
445
487
// Preinstantiate Objects //////////////////////////////////////////////////////
0 commit comments