Skip to content

Commit d5ef001

Browse files
committed
Fix I2C slave issue: Rx buffer allocation
Fix stm32duino#212 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 6d65a71 commit d5ef001

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

Diff for: libraries/Wire/src/Wire.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,14 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
380380
if(rxBufferIndex < rxBufferLength){
381381
return;
382382
}
383+
384+
if(rxBuffer == nullptr){
385+
allocateRxBuffer(numBytes);
386+
// error if no memory block available to allocate the buffer
387+
if(rxBuffer == nullptr){
388+
Error_Handler();
389+
}
390+
}
383391
// copy twi rx buffer into local read buffer
384392
// this enables new reads to happen in parallel
385393
memcpy(rxBuffer, inBytes, numBytes);
@@ -423,7 +431,7 @@ void TwoWire::onRequest( void (*function)(void) )
423431
* @note Minimum allocated size is BUFFER_LENGTH)
424432
* @param length: number of bytes to allocate
425433
*/
426-
inline void TwoWire::allocateRxBuffer(size_t length)
434+
void TwoWire::allocateRxBuffer(size_t length)
427435
{
428436
if(rxBufferAllocated < length) {
429437
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.

Diff for: libraries/Wire/src/Wire.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class TwoWire : public Stream
5858
static void onRequestService(void);
5959
static void onReceiveService(uint8_t*, int);
6060

61-
void allocateRxBuffer(size_t length);
61+
static void allocateRxBuffer(size_t length);
6262
void allocateTxBuffer(size_t length);
6363

6464
void resetRxBuffer(void);

0 commit comments

Comments
 (0)