STM32 i2c communication failure reset method

Recently, I was investigating STM32 F10X, preparing to migrate the company's AVR MCU project to STM32. When investigating the STM32 i2c part, after disconnecting from the i2c slave, then reading / writing the i2c slave requires some processing on the i2c_read / write function on the STM32 side.

Just after i2c read and write timeout, the following code was added:

I2C_AcknowledgeConfig(I2C1, DISABLE);        
/*!< Send STOP Condition */
I2C_GenerateSTOP(I2C1, ENABLE); 

 Then connect the i2c slave and perform i2c read / write and found that it has been stuck in the following two positions:

I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY)

 or

I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)

 That is to say, after the i2c slave is connected again, the i2c bus will not respond after the busy or start signal is sent, and this solution will not work.

 

When browsing the stm32f10x_i2c.c library, I saw the function of void I2C_DeInit (I2C_TypeDef * I2Cx). After i2c read and write timeout, add:

I2C_DeInit (I2C1); 
I2c_Configuration ();

 The problem is solved.

The I2c_Configuration () function is as follows:

void I2c_Configuration(void)
{
	I2C_InitTypeDef  I2C_InitStructure;
	
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed = 100000;
	I2C_Init(I2C1, &I2C_InitStructure);
	
	I2C_Cmd(I2C1, ENABLE);	
}

 

Guess you like

Origin www.cnblogs.com/wanglouxiaozi/p/12714965.html