2020-04-13 08:16 AM
I am trying to get SMbus communication to work and it works fine most of the time but I have seen that after sometime it gets stuck at I2C event I2C_EVENT_MASTER_BYTE_TRANSMITTING.
I have implemented a timeout function which retries event 255 times but I can still see that the return I get from the I2C retry event is an error. I also tried to do MCU reset and re init I2C if it still gets stuck after timeout but then the mcu is resetting all the time.
Is my I2C_EventRetry function incorrect > Or is there any good way to handle this error?
Below is my Init, Transmit and Event Retry function.
void I2C_Init()
{
/* Initialize I2C peripheral */
I2C_Init(I2C1, I2C_SPEED, 0x00,
I2C_Mode_SMBusHost, I2C_DutyCycle_2,
I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit); // 0x00 is a dummy value. Only used in slave mode
I2C_Cmd(I2C1, ENABLE); // Enable peripheral
}
static void I2C_EventRetry(I2C_Event_TypeDef I2C_Event)
{
uint8_t retryCounter = 0;
do{
Delay_Us(1);
retryCounter++;
}while((!I2C_CheckEvent(I2C1, I2C_Event)) && (retryCounter < RETRY_LIMIT));
}
static void StartTransmit(uint8_t slaveAddress, uint8_t commandCode, uint8_t dataLength, uint8_t regAddress1, uint8_t regAddress2)
{
/* Disable I2C acknowledgement */
I2C_AcknowledgeConfig(I2C1, ENABLE);
/* Send I2C START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on I2C EV5 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_MODE_SELECT);
/* Send slave address for write */
I2C_Send7bitAddress(I2C1, slaveAddress, I2C_Direction_Transmitter);
/* Test on I2C EV6 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);
/* Send the command code */
I2C_SendData(I2C1, commandCode);
/* Test on I2C EV8 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_BYTE_TRANSMITTING);
/* Send the Data length */
I2C_SendData(I2C1, dataLength);
/* Test on I2C EV8 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_BYTE_TRANSMITTING);
/* Send the Data byte */
I2C_SendData(I2C1, regAddress1);
/* Test on I2C EV8 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_BYTE_TRANSMITTING);
/* Send the Data byte */
I2C_SendData(I2C1, regAddress2);
/* Test on I2C EV8 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_BYTE_TRANSMITTED); //EV8_2
/* Send Stop condition */
I2C_GenerateSTOP(I2C1, ENABLE);
Delay_Ms(1);
/* Send I2C START condition */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on I2C EV5 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_MODE_SELECT);
/* Send slave address for write */
I2C_Send7bitAddress(I2C1, slaveAddress, I2C_Direction_Transmitter);
/* Test on I2C EV6 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);
/* Send the command code */
I2C_SendData(I2C1, commandCode);
/* Test on I2C EV8 and clear it */
I2C_EventRetry(I2C_EVENT_MASTER_BYTE_TRANSMITTED); //EV8_2
/* Send Stop condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}