Skip to content

Commit 554a0a8

Browse files
authored
Merge pull request #1221 from DanielLiebler/i2cSlaveFix
Wire fixes for repeated start(slave) and multiple .write in onRequestEvent()
2 parents e50affb + 65335d8 commit 554a0a8

File tree

1 file changed

+8
-2
lines changed
  • libraries/Wire/src/utility

1 file changed

+8
-2
lines changed

libraries/Wire/src/utility/twi.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -846,10 +846,10 @@ i2c_status_e i2c_slave_write_IT(i2c_t *obj, uint8_t *data, uint16_t size)
846846
} else {
847847
// Check the communication status
848848
for (i = 0; i < size; i++) {
849-
obj->i2cTxRxBuffer[i] = *(data + i);
849+
obj->i2cTxRxBuffer[obj->i2cTxRxBufferSize + i] = *(data + i);
850850
}
851851

852-
obj->i2cTxRxBufferSize = size;
852+
obj->i2cTxRxBufferSize += size;
853853
}
854854
return ret;
855855
}
@@ -980,12 +980,18 @@ void i2c_attachSlaveTxEvent(i2c_t *obj, void (*function)(i2c_t *))
980980
void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
981981
{
982982
i2c_t *obj = get_i2c_obj(hi2c);
983+
if ((obj->slaveMode == SLAVE_MODE_RECEIVE) && (obj->slaveRxNbData != 0)) {
984+
obj->i2c_onSlaveReceive(obj);
985+
obj->slaveMode = SLAVE_MODE_LISTEN;
986+
obj->slaveRxNbData = 0;
987+
}
983988

984989
if (AddrMatchCode == hi2c->Init.OwnAddress1) {
985990
if (TransferDirection == I2C_DIRECTION_RECEIVE) {
986991
obj->slaveMode = SLAVE_MODE_TRANSMIT;
987992

988993
if (obj->i2c_onSlaveTransmit != NULL) {
994+
obj->i2cTxRxBufferSize = 0;
989995
obj->i2c_onSlaveTransmit(obj);
990996
}
991997
#if defined(STM32F0xx) || defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F3xx) ||\

0 commit comments

Comments
 (0)