@@ -29,11 +29,13 @@ extern "C" {
2929
3030// Initialize Class Variables //////////////////////////////////////////////////
3131uint8_t *TwoWire::rxBuffer = nullptr ;
32+ uint8_t TwoWire::rxBufferAllocated = 0 ;
3233uint8_t TwoWire::rxBufferIndex = 0 ;
3334uint8_t TwoWire::rxBufferLength = 0 ;
3435
3536uint8_t TwoWire::txAddress = 0 ;
3637uint8_t *TwoWire::txBuffer = nullptr ;
38+ uint8_t TwoWire::txBufferAllocated = 0 ;
3739uint8_t TwoWire::txBufferIndex = 0 ;
3840uint8_t TwoWire::txBufferLength = 0 ;
3941
@@ -66,11 +68,11 @@ void TwoWire::begin(uint8_t address)
6668{
6769 rxBufferIndex = 0 ;
6870 rxBufferLength = 0 ;
69- rxBuffer = resetBuffer (rxBuffer );
71+ resetRxBuffer ( );
7072
7173 txBufferIndex = 0 ;
7274 txBufferLength = 0 ;
73- txBuffer = resetBuffer (txBuffer );
75+ resetTxBuffer ( );
7476
7577 transmitting = 0 ;
7678
@@ -101,8 +103,10 @@ void TwoWire::end(void)
101103{
102104 free (txBuffer);
103105 txBuffer = nullptr ;
106+ txBufferAllocated = 0 ;
104107 free (rxBuffer);
105108 rxBuffer = nullptr ;
109+ rxBufferAllocated = 0 ;
106110 i2c_deinit (&_i2c);
107111}
108112
@@ -115,7 +119,7 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddres
115119{
116120 UNUSED (sendStop);
117121 if (master == true ) {
118- rxBuffer = allocateBuffer (rxBuffer, quantity);
122+ allocateRxBuffer ( quantity);
119123 // error if no memory block available to allocate the buffer
120124 if (rxBuffer == nullptr ){
121125 setWriteError ();
@@ -224,10 +228,8 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
224228 break ;
225229 }
226230
227- // Reduce buffer size to free memory in case of large memory use
228- if (txBufferLength > BUFFER_LENGTH) {
229- txBuffer = resetBuffer (txBuffer);
230- }
231+ // reset Tx buffer
232+ resetTxBuffer ();
231233
232234 // reset tx buffer iterator vars
233235 txBufferIndex = 0 ;
@@ -255,7 +257,7 @@ size_t TwoWire::write(uint8_t data)
255257{
256258 if (transmitting){
257259 // in master transmitter mode
258- txBuffer = allocateBuffer (txBuffer, txBufferLength + 1 );
260+ allocateTxBuffer ( txBufferLength + 1 );
259261 // error if no memory block available to allocate the buffer
260262 if (txBuffer == nullptr ){
261263 setWriteError ();
@@ -285,14 +287,20 @@ size_t TwoWire::write(uint8_t data)
285287 */
286288size_t TwoWire::write (const uint8_t *data, size_t quantity)
287289{
288- size_t nb = 0 ;
289-
290290 if (transmitting){
291291 // in master transmitter mode
292- for (size_t i = 0 ; i < quantity; ++i){
293- nb += write (data[i]);
292+ allocateTxBuffer (txBufferLength + quantity);
293+ // error if no memory block available to allocate the buffer
294+ if (txBuffer == nullptr ){
295+ setWriteError ();
296+ return 0 ;
294297 }
295- return nb;
298+ // put bytes in tx buffer
299+ memcpy (&(txBuffer[txBufferIndex]), data, quantity);
300+ txBufferIndex= txBufferIndex + quantity;
301+ // update amount in buffer
302+ txBufferLength = txBufferIndex;
303+ return quantity;
296304 }else {
297305 // in slave send mode
298306 // reply to master
@@ -323,11 +331,10 @@ int TwoWire::read(void)
323331 value = rxBuffer[rxBufferIndex];
324332 ++rxBufferIndex;
325333
326- /* Reduce buffer size to free memory in case of large memory use when no more
327- data available */
328- if ((rxBufferIndex == rxBufferLength) && (rxBufferLength > BUFFER_LENGTH)) {
329- rxBuffer = resetBuffer (rxBuffer);
330- }
334+ /* Reset rx buffer when no more data available */
335+ /* if(rxBufferIndex == rxBufferLength) {
336+ resetRxBuffer();
337+ }*/
331338 }
332339
333340 return value;
@@ -351,10 +358,10 @@ void TwoWire::flush(void)
351358{
352359 rxBufferIndex = 0 ;
353360 rxBufferLength = 0 ;
354- rxBuffer = resetBuffer (rxBuffer );
361+ resetRxBuffer ( );
355362 txBufferIndex = 0 ;
356363 txBufferLength = 0 ;
357- txBuffer = resetBuffer (txBuffer );
364+ resetTxBuffer ( );
358365}
359366
360367// behind the scenes function that is called when data is received
@@ -373,9 +380,7 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
373380 }
374381 // copy twi rx buffer into local read buffer
375382 // this enables new reads to happen in parallel
376- for (uint8_t i = 0 ; i < numBytes; ++i){
377- rxBuffer[i] = inBytes[i];
378- }
383+ memcpy (rxBuffer, inBytes, numBytes);
379384 // set rx iterator vars
380385 rxBufferIndex = 0 ;
381386 rxBufferLength = numBytes;
@@ -411,37 +416,6 @@ void TwoWire::onRequest( void (*function)(void) )
411416 user_onRequest = function;
412417}
413418
414- /* *
415- * @brief Change the size of the buffer.
416- * @param buffer: pointer to the allocated buffer
417- * @param length: number of bytes to allocate
418- * @retval pointer to the new buffer location
419- */
420- uint8_t *TwoWire::allocateBuffer (uint8_t *buffer, size_t length)
421- {
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;
425- }
426-
427- buffer = (uint8_t *)realloc (buffer, length * sizeof (uint8_t ));
428- return buffer;
429- }
430-
431- /* *
432- * @brief Reset the buffer. Reduce its size to BUFFER_LENGTH.
433- * @param buffer: pointer to the allocated buffer
434- * @retval pointer to the new buffer location
435- */
436- uint8_t *TwoWire::resetBuffer (uint8_t *buffer)
437- {
438- buffer = (uint8_t *)realloc (buffer, BUFFER_LENGTH * sizeof (uint8_t ));
439- if (buffer != nullptr ) {
440- memset (buffer, 0 , BUFFER_LENGTH);
441- }
442- return buffer;
443- }
444-
445419// Preinstantiate Objects //////////////////////////////////////////////////////
446420
447421TwoWire Wire = TwoWire(); // D14-D15
0 commit comments