cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F302 - I2C Error Code

Mohamed Aymen
Associate II

Hello Community :),

 

I'm using the I2C of the STM32F302:

I2C configuration:

  • 7 bit data 
  • standard mode "100 KHz"
  • mode polling

When initiating communication between the STM32 and the sensor, the "HAL_I2C_Master_Transmit" function returns "HAL_ERROR."

Upon investigation, I discovered that this error is associated with an error code. Does anyone know the significance of the error code?

I catch the error in line 679 of the HAL function:

MohamedAymen_0-1707321453193.png

 

Thanks,

With Kind Regards,

Aymen

1 ACCEPTED SOLUTION

Accepted Solutions

If HAL_I2C_IsDeviceReady fails, could be a number of issues:

  • Wrong slave address
  • Slave isn't powered or connected correctly
  • Master/Slave are out of sync (reset both, power cycle, or toggle SCL line 9 times).
If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

7 REPLIES 7
Foued_KH
ST Employee

Hello @Mohamed Aymen , 

Make sure that you are using the correct address.

Foued_KH_0-1707322786974.png

 


Use a logic analyzer to check SDA and SCL behavior and identify the issue.

Foued 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

TDK
Guru

AF is an acknowledgement failure--the slave failed to acknowledge the address.

This could be because you're using the wrong address, or the slave isn't hooked up correctly. Note that the slave address should be left-shifted. If the 7-bit slave address is 42, you should be sending 42 << 1 to the HAL_I2C functions.

Use HAL_I2C_IsDeviceReady and ensure the return value is HAL_OK before doing anything else with I2C.

If you feel a post has answered your question, please click "Accept as Solution".
JayDev
Senior II

I wanted to second what @TDK said.  I've never had an I2C application work without left shifting the address by a bit (and I almost always forget to do this initially and run into this same issue).  Best bet with any comms issue is to hook it up to a scope or logic analyzer and make sure your lines look like you expect them to.   It's helped speed up diagnosing these kinds of issues, CS issues in SPI, etc.  Trying to diagnose issues without seeing what the signal is actually doing makes things much, much harder. Good luck!

Mohamed Aymen
Associate II

Hello All, and thanks for the prompt reply,

 

I omitted to mention that the initial communication proceeded smoothly, but after some time, the error occurred. Following a suggestion, I conducted a test using "HAL_I2C_IsDeviceReady"

  • Initially, there were no errors returned, but after a while, the error occurred.

Any other thoughts will be helpful !

 

Thanks,

With Regards,

Aymen

Hi,

Just a simple question : 

>

I2C configuration:

  • 7 bit data 

<

How you can set this ?   afaik I2C always working with bytes , = 8 bit data .

If you feel a post has answered your question, please click "Accept as Solution".

If HAL_I2C_IsDeviceReady fails, could be a number of issues:

  • Wrong slave address
  • Slave isn't powered or connected correctly
  • Master/Slave are out of sync (reset both, power cycle, or toggle SCL line 9 times).
If you feel a post has answered your question, please click "Accept as Solution".
Mohamed Aymen
Associate II

Hello @TDK@Foued_KH,

 

I appreciate your assistance!

Following the adjustment of the SCL lines, it appears that everything is functioning smoothly now.

Additionally, I found the information provided in this link to be beneficial.

 

Aymen