Skip to main content
Filipx.87
Associate II
March 24, 2023
Solved

HAL I2C timeout flag

I have a simple function that reads one byte from I2C and I need the RxdFlag variable to be cleared in case of unsuccessful transfer (timeout) and I don't know how to get it from the HAL_StatusTypeDef structure. Thanx for your help.

uint8_t GetSlaveInfo (uint8_t *value)
{
 uint8_t RxdFlag = 1;
 HAL_I2C_Master_Receive(&hi2c1, EE_I2C_ADDR << 1, value, 1, 50);
 return RxdFlag;
}

This topic has been closed for replies.
Pavel A.
Pavel A.Best answer
March 25, 2023
uint8_t GetSlaveInfo (uint8_t *value)
{
 uint8_t RxdFlag = 0;
 HAL_StatusTypeDef st =
 HAL_I2C_Master_Receive(&hi2c1, EE_I2C_ADDR << 1, value, 1, 50);
 if (st == HAL_OK)
 RxdFlag = 1;
 return RxdFlag;
}

What should your function return in case of other errors besides of timeout?

2 replies

Pavel A.
Pavel A.Best answer
March 25, 2023
uint8_t GetSlaveInfo (uint8_t *value)
{
 uint8_t RxdFlag = 0;
 HAL_StatusTypeDef st =
 HAL_I2C_Master_Receive(&hi2c1, EE_I2C_ADDR << 1, value, 1, 50);
 if (st == HAL_OK)
 RxdFlag = 1;
 return RxdFlag;
}

What should your function return in case of other errors besides of timeout?

Piranha
Principal III
March 25, 2023
uint8_t GetSlaveInfo(uint8_t *value)
{
	HAL_StatusTypeDef st = HAL_I2C_Master_Receive(&hi2c1, EE_I2C_ADDR << 1, value, 1, 50);
	return (st == HAL_OK);
}
uint8_t GetSlaveInfo(uint8_t *value)
{
	return (HAL_I2C_Master_Receive(&hi2c1, EE_I2C_ADDR << 1, value, 1, 50) == HAL_OK);
}

 Often I don't understand why people write so much useless code...