cancel
Showing results for 
Search instead for 
Did you mean: 

F411: how to handle I2C Errors

HDaji.1
Senior

I am using the I2C code in STM32Cube_FW_F4_V1.26.0\Projects\STM32F411E-Discovery\Examples\I2C\I2C_TwoBoards_ComPolling as base to write my own I2C firmware.

I would say the HAL library in stm32f4xx_hal_i2c.c is quite useful and covers all I2C communication cases. However, there is not much about how to recover from an I2C error.

In the I2C_TwoBoards_ComPolling sample, if there is I2C error, error handler does nothing and MCU just hangs.

So far in my application, I run into two I2C errors: AF and TIMEOUT.

  1. I wonder in those two cases how I can recover I2C communication without software reset I2C.
  2. For the worst case, if I want to reset, what are the proper steps? Disable I2C and do Init again?

BTW, I am using F411.

12 REPLIES 12

This solved an intermittent problem I have struggled with for quite some time. It turns out the IC I'm communicating with would sometimes hold the SDA line low, toggling the clock shows it had a byte left to transmit. This solution is perfect for recovering in software, before I had to power on and off the whole system. Thank you for sharing it!

Hi @TDK ,

I am working with stm32g491re.I am communicating with TCA9537(Ioexpander) from stm32 through I2C. It will control a realy. For my product EFT(Electrical Fast Transient) testing, After starting EFT teat relay getting turned off and not recovering.
So i modified the code.

void ioexpandercheck(){ 

		uint8_t check[1]; 

		HAL_StatusTypeDef status=tca9537_read(&hmcp, TCA9537_REGISTER_INPUT,check); 

		HAL_Delay(10); 

		if(status!=HAL_OK){ 

			HAL_I2C_DeInit(&hi2c3); 

			HAL_Delay(10); 

			HAL_I2C_Init(&hi2c3); 

			HAL_Delay(50); 

			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 0); 

			HAL_Delay(5); 

			HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, 1); 

			HAL_Delay(5); 

			tca9537_init(&hmcp, &hi2c3, TCA9537_I2C_ADDR); 

			tca9537_iodir(&hmcp, TCA9537_CONFIG_IO12_OUTPUT); 



		} 

} 

Here continuously I am reading the expander pin if read is not ok I am resetting both I2C and ioexpander and reinitializing the ioexpander. With this i can able to recover the relay operation in EFT but its not stable.
In above ioexpander reset i done using reset pin of ioexpander.
Instead of doing through reset pin, in datasheet also they mentioned how to do with software.but i am conused about implementation.
Can anyone suggest.

sireevenkat1_0-1724906014649.png

Thanks

Please make a new thread with this question. You cannot issue a software reset if the bus lines are being held, which is what we are discussing in this thread.

If you feel a post has answered your question, please click "Accept as Solution".