Question
I2C getting stuck in HAL_I2C_IsDeviceReady
I had this problem on my project, so I created a new one and added only I2C to pins Pb7 and Pb6. I set I2C from disable to I2C and I set them to "pull-up" . That's about it. And the code gets stuck in the HAL_I2C_IsDeviceReady function.
I have also set the sysclk to 80Mhz.
Any ideas what could be the issue?
The code I have and the inside of the HAL_I2C_IsDeviceReady where it enters the never ending loop:
while ((tmp1 == RESET) && (tmp2 == RESET))
{
if (Timeout != HAL_MAX_DELAY)
{
if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
{
/* Update I2C state */
hi2c->State = HAL_I2C_STATE_READY;
/* Update I2C error code */
hi2c->ErrorCode |= HAL_I2C_ERROR_TIMEOUT;
/* Process Unlocked */
__HAL_UNLOCK(hi2c);
return HAL_ERROR;
}
}
tmp1 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_STOPF);
tmp2 = __HAL_I2C_GET_FLAG(hi2c, I2C_FLAG_AF);
}int main(void)
{
/* USER CODE BEGIN 1 */
uint8_t buf[256];
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
HAL_StatusTypeDef ret;
//#ifdef _TEST_
for(uint8_t i=1; i<256; i++)
{
ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 10, 100); //10x proba komunicirat in caka max 100 ms
if (ret != HAL_OK) /* No ACK Received At That Address */
{
//HAL_UART_Transmit(&huart1, Space, sizeof(Space), 10000);
}
else if(ret == HAL_OK)
{
//sprintf(buf, "0x%X", i);
//HAL_UART_Transmit(&huart1, buf, sizeof(buf), 10000);
}
buf[i] = (uint8_t)ret;
}
//#endif
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}