Skip to content

Commit 2415879

Browse files
authored
Merge pull request stm32duino#216 from fpistm/slave_i2c
Fix I2C slave issue
2 parents 6bc0753 + b1b12de commit 2415879

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

cores/arduino/stm32/twi.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ void HAL_I2C_ListenCpltCallback(I2C_HandleTypeDef *hi2c)
539539

540540
if((obj->i2c_onSlaveReceive != NULL) &&
541541
(obj->slaveMode == SLAVE_MODE_RECEIVE)) {
542-
nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferCount;
542+
nbData = I2C_TXRX_BUFFER_SIZE - obj->handle.XferSize;
543543
if(nbData != 0) {
544544
obj->i2c_onSlaveReceive(obj->i2cTxRxBuffer, nbData);
545545
}

libraries/Wire/src/Wire.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,13 @@ void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
380380
if(rxBufferIndex < rxBufferLength){
381381
return;
382382
}
383+
384+
allocateRxBuffer(numBytes);
385+
// error if no memory block available to allocate the buffer
386+
if(rxBuffer == nullptr){
387+
Error_Handler();
388+
}
389+
383390
// copy twi rx buffer into local read buffer
384391
// this enables new reads to happen in parallel
385392
memcpy(rxBuffer, inBytes, numBytes);
@@ -423,7 +430,7 @@ void TwoWire::onRequest( void (*function)(void) )
423430
* @note Minimum allocated size is BUFFER_LENGTH)
424431
* @param length: number of bytes to allocate
425432
*/
426-
inline void TwoWire::allocateRxBuffer(size_t length)
433+
void TwoWire::allocateRxBuffer(size_t length)
427434
{
428435
if(rxBufferAllocated < length) {
429436
// By default we allocate BUFFER_LENGTH bytes. It is the min size of the buffer.

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)