From 38fecfb9ca005c848c982fe8a13233278010d7e2 Mon Sep 17 00:00:00 2001 From: deltaford Date: Sat, 13 Aug 2022 14:32:26 +0200 Subject: [PATCH 1/3] Enable multi slave address handling I2C slave: Enable multi slave address onReceive/onRequest handling. --- libraries/Wire/src/utility/twi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index d349df3c20..e66160d1ce 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -1074,7 +1074,7 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, ui obj->slaveRxNbData = 0; } - if (AddrMatchCode == hi2c->Init.OwnAddress1) { + if (AddrMatchCode) { if (TransferDirection == I2C_DIRECTION_RECEIVE) { obj->slaveMode = SLAVE_MODE_TRANSMIT; From a8a23dfbfa5c21b24335863c22a75a38522d8db1 Mon Sep 17 00:00:00 2001 From: deltaford Date: Fri, 26 Aug 2022 14:52:08 +0200 Subject: [PATCH 2/3] Optimize & Enable handling of multiple I2C slave addresses. Enable handling of multiple I2C slave addresses by removing redundant AddrMatchCode check. --- libraries/Wire/src/utility/twi.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index e66160d1ce..27982429c5 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -1074,24 +1074,22 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, ui obj->slaveRxNbData = 0; } - if (AddrMatchCode) { - if (TransferDirection == I2C_DIRECTION_RECEIVE) { - obj->slaveMode = SLAVE_MODE_TRANSMIT; + if (TransferDirection == I2C_DIRECTION_RECEIVE) { + obj->slaveMode = SLAVE_MODE_TRANSMIT; - if (obj->i2c_onSlaveTransmit != NULL) { - obj->i2cTxRxBufferSize = 0; - obj->i2c_onSlaveTransmit(obj); - } - HAL_I2C_Slave_Seq_Transmit_IT(hi2c, (uint8_t *) obj->i2cTxRxBuffer, - obj->i2cTxRxBufferSize, I2C_LAST_FRAME); - } else { - obj->slaveRxNbData = 0; - obj->slaveMode = SLAVE_MODE_RECEIVE; - /* We don't know in advance how many bytes will be sent by master so - * we'll fetch one by one until master ends the sequence */ - HAL_I2C_Slave_Seq_Receive_IT(hi2c, (uint8_t *) & (obj->i2cTxRxBuffer[obj->slaveRxNbData]), - 1, I2C_NEXT_FRAME); + if (obj->i2c_onSlaveTransmit != NULL) { + obj->i2cTxRxBufferSize = 0; + obj->i2c_onSlaveTransmit(obj); } + HAL_I2C_Slave_Seq_Transmit_IT(hi2c, (uint8_t *) obj->i2cTxRxBuffer, + obj->i2cTxRxBufferSize, I2C_LAST_FRAME); + } else { + obj->slaveRxNbData = 0; + obj->slaveMode = SLAVE_MODE_RECEIVE; + /* We don't know in advance how many bytes will be sent by master so + * we'll fetch one by one until master ends the sequence */ + HAL_I2C_Slave_Seq_Receive_IT(hi2c, (uint8_t *) & (obj->i2cTxRxBuffer[obj->slaveRxNbData]), + 1, I2C_NEXT_FRAME); } } From 49c17d0dca229cf2731191fb17c83152c1b11f8b Mon Sep 17 00:00:00 2001 From: deltaford Date: Fri, 26 Aug 2022 19:30:36 +0200 Subject: [PATCH 3/3] Fixed indentation error --- libraries/Wire/src/utility/twi.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/src/utility/twi.c b/libraries/Wire/src/utility/twi.c index 27982429c5..8364d9dd59 100644 --- a/libraries/Wire/src/utility/twi.c +++ b/libraries/Wire/src/utility/twi.c @@ -1082,7 +1082,7 @@ void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, ui obj->i2c_onSlaveTransmit(obj); } HAL_I2C_Slave_Seq_Transmit_IT(hi2c, (uint8_t *) obj->i2cTxRxBuffer, - obj->i2cTxRxBufferSize, I2C_LAST_FRAME); + obj->i2cTxRxBufferSize, I2C_LAST_FRAME); } else { obj->slaveRxNbData = 0; obj->slaveMode = SLAVE_MODE_RECEIVE;