From 169f42deab48a2c5ac847567c90c14e02d4c6a24 Mon Sep 17 00:00:00 2001 From: Daniel Liebler Date: Thu, 29 Oct 2020 10:10:06 +0100 Subject: [PATCH 1/2] [Wire] modifying to be able to do multiple .write() calls in i2cRequestEvent() as Slave --- libraries/Wire/src/utility/twi.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 69a5d17fc4..90e1c5a54a 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -846,10 +846,10 @@ i2c_status_e i2c_slave_write_IT(i2c_t *obj, uint8_t *data, uint16_t size) } else { // Check the communication status for (i = 0; i < size; i++) { - obj->i2cTxRxBuffer[i] = *(data + i); + obj->i2cTxRxBuffer[obj->i2cTxRxBufferSize + i] = *(data + i); } - obj->i2cTxRxBufferSize = size; + obj->i2cTxRxBufferSize += size; } return ret; } @@ -986,6 +986,7 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, ui obj->slaveMode = SLAVE_MODE_TRANSMIT; if (obj->i2c_onSlaveTransmit != NULL) { + obj->i2cTxRxBufferSize = 0; obj->i2c_onSlaveTransmit(obj); } #if defined(STM32F0xx) || defined(STM32F1xx) || defined(STM32F2xx) || defined(STM32F3xx) ||\ From 65335d885140cd34784e3a970b61d31f1d17841c Mon Sep 17 00:00:00 2001 From: Daniel Liebler Date: Thu, 29 Oct 2020 10:10:49 +0100 Subject: [PATCH 2/2] [Wire] fix onReceiveEvent() not being triggered when a repeated start/no stop condition is sent in slave mode --- libraries/Wire/src/utility/twi.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 90e1c5a54a..dbf6b99bc8 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -980,6 +980,11 @@ void i2c_attachSlaveTxEvent(i2c_t *obj, void (*function)(i2c_t *)) void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode) { i2c_t *obj = get_i2c_obj(hi2c); + if ((obj->slaveMode == SLAVE_MODE_RECEIVE) && (obj->slaveRxNbData != 0)) { + obj->i2c_onSlaveReceive(obj); + obj->slaveMode = SLAVE_MODE_LISTEN; + obj->slaveRxNbData = 0; + } if (AddrMatchCode == hi2c->Init.OwnAddress1) { if (TransferDirection == I2C_DIRECTION_RECEIVE) {