Skip to main content
Graduate II
February 7, 2024
Solved

STM32F302 - I2C Error Code

  • February 7, 2024
  • 5 replies
  • 6232 views

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

    This topic has been closed for replies.
    Best answer by TDK

    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).

    5 replies

    ST Employee
    February 7, 2024

    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 

    Super User
    February 7, 2024

    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.

    Graduate II
    February 7, 2024

    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!

    Graduate II
    February 8, 2024

    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

    Super User
    February 8, 2024

    Hi,

    Just a simple question : 

    >

    I2C configuration:

    • 7 bit data 

    <

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

    Graduate II
    February 13, 2024

    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