Skip to main content
SKAŞ.1
Associate III
July 27, 2021
Question

Why HAL_I2C_IsDeviceReady return HAL_ERROR

  • July 27, 2021
  • 12 replies
  • 11635 views

Hello folks,

Since my last post, I still couldn't solve my I2C communication problem. I thought maybe return is not HAL_OK for my function so that I couldn't find any I2C addresses. And my guess was correct. I'm getting HAL_ERROR but it doesn't mean anything to me. What could be the reason I am getting this error.

0693W00000D0kZYQAZ.jpg 

Last time I couldn't upload my file, so here it is.

RCC -> HSE -> Ceramic

My clock setting is -> 8Mhz for input freq

HCLK = 84MHz

For pins,

PG13 & PG14 for LEDs

PG13 used for every scan is over,

PG14 used for interrupt (Just check if somethings is wrong but seems not!)

PA0 user button for external interrupt

I2C3 is used since others are not available for external usage. from the datasheet.

0693W00000D0Za6QAF.jpgI connect PCF8574T directly.

I can found the address via Arduino but with STM, I can't

my SPI settings is Standard mode (100MHz) set by CubeMX

most of the things set up by CubeMX.

I am waiting for your kind support,

Kind Regards.

This topic has been closed for replies.

12 replies

Mike_ST
ST Technical Moderator
July 27, 2021

1) Set PA8 and PC9 with GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HIGH or VERY_HIGH setting is not good.

2) Try to roll the counter up to 255. =>

    for(counter = 0 ; counter < 255; counter++)

I think the dev address has to be shifted 1 bit to the left.

3) Check that you have actual (2.7K for example) pull_up resistors on the I2C lines,

GPIO_InitStruct.Pull = GPIO_PULLUP, is often not enough.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. 
SKAŞ.1
SKAŞ.1Author
Associate III
July 27, 2021

I switch the frequencies at "stm32f4xx_hal_msp.c" file

I change my counter to 255

and lastly, I put a 4.7k resistor to SDA and SDC each, one terminal connected to the line another terminal connected to the 5V supply at STM. for both lines.

Still can't get rid of the HAL_ERROR

Mike_ST
ST Technical Moderator
July 27, 2021

Next step for me would be to check with the scope whether there is something on SDA/SCL.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. 
SKAŞ.1
SKAŞ.1Author
Associate III
July 27, 2021

May I ask that what is the return value of IsDeviceReady if there is no device?

Now I am checking for debugging mode with counter increase 1 by 1

for counter 0 1 it turns OK but then, it turns ERROR. And never turn HAL_OK again.

I checked the SCL is clocking, and SDA is alternating (kinda randomly since it's fast and I cannot see clearly but I saw some changes.)

Mike_ST
ST Technical Moderator
July 27, 2021

May I ask that what is the return value of IsDeviceReady if there is no device?

HAL_ERROR.

By chance, try to increase timeout value to something bigger.

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. 
SKAŞ.1
SKAŞ.1Author
Associate III
July 27, 2021

I am getting error in this line

  while (I2C_Trials < Trials);

  hi2c->State = HAL_I2C_STATE_READY;

  /* Process Unlocked */

  __HAL_UNLOCK(hi2c);

  return HAL_ERROR;

TDK
July 27, 2021

What chip are you trying to talk with? The 7-bit slave address should be shifted left one bit. You're only checking for half of the possible slave values.

"If you feel a post has answered your question, please click ""Accept as Solution""."
SKAŞ.1
SKAŞ.1Author
Associate III
July 27, 2021

PCF8574T

SKAŞ.1
SKAŞ.1Author
Associate III
July 27, 2021

Let me correct my self, I got HAL_OK at counter 0 and 1, not in 2.

Also I change my code to more clear debugging, but still doesn't get anything.

 while (1)

 {

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

 for(counter = 0 ; counter < 255; counter++)

 {

 IDR_Result = HAL_I2C_IsDeviceReady(&hi2c3, counter, 3, 10);

 if(IDR_Result == HAL_OK)

 {

 I2C_Address = counter;

 HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_14);

 }

 }

 HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_13);

 HAL_Delay(500);

 }

 /* USER CODE END 3 */

Awindbiker
Visitor II
March 24, 2022

replace your line with this one and give it a try.

HAL_I2C_IsDeviceReady(&hi2c3, (uint16_t)(counter<<1), 3, 10);

SKAŞ.1
SKAŞ.1Author
Associate III
July 27, 2021

OKAY NOW,

I got I2C addresses of

0-1-78-79-130-131

Tomorrow, I'll connect Arduino with known addresses.

So that I could say something, for now, THANKS TO ALL OF YOU!