From 86e0bbcd23a59cf64b5f9ee87a5539a0de9b9690 Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 2 Feb 2018 10:19:41 +0100 Subject: [PATCH 1/2] Fix I2C slave issue: Rx buffer allocation Fix #212 Signed-off-by: Frederic.Pillon --- libraries/Wire/src/Wire.cpp | 10 +++++++++- libraries/Wire/src/Wire.h | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index bb0963fda6..0ff1c45b89 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -380,6 +380,14 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) if(rxBufferIndex < rxBufferLength){ return; } + + if(rxBuffer == nullptr){ + allocateRxBuffer(numBytes); + // error if no memory block available to allocate the buffer + if(rxBuffer == nullptr){ + Error_Handler(); + } + } // copy twi rx buffer into local read buffer // this enables new reads to happen in parallel memcpy(rxBuffer, inBytes, numBytes); @@ -423,7 +431,7 @@ void TwoWire::onRequest( void (*function)(void) ) * @note Minimum allocated size is BUFFER_LENGTH) * @param length: number of bytes to allocate */ -inline void TwoWire::allocateRxBuffer(size_t length) +void TwoWire::allocateRxBuffer(size_t length) { if(rxBufferAllocated < length) { // By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer. diff --git a/libraries/Wire/src/Wire.h b/libraries/Wire/src/Wire.h index 7c033d0aac..4ceccb9425 100644 --- a/libraries/Wire/src/Wire.h +++ b/libraries/Wire/src/Wire.h @@ -58,7 +58,7 @@ class TwoWire : public Stream static void onRequestService(void); static void onReceiveService(uint8_t*, int); - void allocateRxBuffer(size_t length); + static void allocateRxBuffer(size_t length); void allocateTxBuffer(size_t length); void resetRxBuffer(void); From b1b12dec2b56f662fa2800624d909b8707e7262a Mon Sep 17 00:00:00 2001 From: "Frederic.Pillon" Date: Fri, 2 Feb 2018 16:27:22 +0100 Subject: [PATCH 2/2] Fix I2C slave issue: return correct size Fix #212 Signed-off-by: Frederic.Pillon --- cores/arduino/stm32/twi.c | 2 +- libraries/Wire/src/Wire.cpp | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cores/arduino/stm32/twi.c b/cores/arduino/stm32/twi.c index d46063db7b..ccd633fb40 100644 --- a/cores/arduino/stm32/twi.c +++ b/cores/arduino/stm32/twi.c @@ -539,7 +539,7 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c) if((obj->i2c_onSlaveReceive != NULL) && (obj->slaveMode == SLAVE_MODE_RECEIVE)) { - nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferCount; + nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferSize; if(nbData != 0) { obj->i2c_onSlaveReceive(obj->i2cTxRxBuffer, nbData); } diff --git a/libraries/Wire/src/Wire.cpp b/libraries/Wire/src/Wire.cpp index 0ff1c45b89..d6ed2becb2 100644 --- a/libraries/Wire/src/Wire.cpp +++ b/libraries/Wire/src/Wire.cpp @@ -381,13 +381,12 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) return; } + allocateRxBuffer(numBytes); + // error if no memory block available to allocate the buffer if(rxBuffer == nullptr){ - allocateRxBuffer(numBytes); - // error if no memory block available to allocate the buffer - if(rxBuffer == nullptr){ - Error_Handler(); - } + Error_Handler(); } + // copy twi rx buffer into local read buffer // this enables new reads to happen in parallel memcpy(rxBuffer, inBytes, numBytes);