From eccb0b5b2231f7e7814f1b8094769ed26c0dc662 Mon Sep 17 00:00:00 2001 From: Matthew Krause Date: Tue, 2 Jun 2026 15:27:56 -0700 Subject: [PATCH] Address Errata 2.19.4 In I2C HAL Driver As per errata 2.19.4: Description: In master mode, a bus error can be detected spuriously, with the consequence of setting the BERR flag of the I2C_SR register and generating bus error interrupt if such interrupt is enabled. Detection of bus error has no effect on the I2C-bus transfer in master mode and any such transfer continues normally. Workaround: If a bus error interrupt is generated in master mode, the BERR flag must be cleared by software. No other action is required and the ongoing transfer can be handled normally. This code ignores a bus error if in master mode, allowing operations to continue as intented as per the workaround. --- Src/stm32l4xx_hal_i2c.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Src/stm32l4xx_hal_i2c.c b/Src/stm32l4xx_hal_i2c.c index 0f90b14..cf5e3d0 100644 --- a/Src/stm32l4xx_hal_i2c.c +++ b/Src/stm32l4xx_hal_i2c.c @@ -7209,7 +7209,7 @@ static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t T uint32_t error_code = 0; uint32_t tickstart = Tickstart; uint32_t tmp1; - HAL_I2C_ModeTypeDef tmp2; + HAL_I2C_ModeTypeDef tmp2 = hi2c->Mode; if (HAL_IS_BIT_SET(itflag, I2C_FLAG_AF)) { @@ -7226,7 +7226,6 @@ static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t T if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U)) { tmp1 = (uint32_t)(hi2c->Instance->CR2 & I2C_CR2_STOP); - tmp2 = hi2c->Mode; /* In case of I2C still busy, try to regenerate a STOP manually */ if ((__HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_BUSY) != RESET) && \ @@ -7275,7 +7274,7 @@ static HAL_StatusTypeDef I2C_IsErrorOccurred(I2C_HandleTypeDef *hi2c, uint32_t T /* Check if a Bus error occurred */ if (HAL_IS_BIT_SET(itflag, I2C_FLAG_BERR)) { - error_code |= HAL_I2C_ERROR_BERR; + error_code |= tmp2 == HAL_I2C_MODE_MASTER ? RESET : HAL_I2C_ERROR_BERR; /* Clear BERR flag */ __HAL_I2C_CLEAR_FLAG(hi2c, I2C_FLAG_BERR);