Skip to main content
Associate
January 16, 2024
Question

stm32 I2c won't send data

  • January 16, 2024
  • 7 replies
  • 4279 views

I am trying to get to use I2C on my stm32L4R5ZIT-P.

i am trying to configure a sensor by sending data but the I2C pins wont give a signal?

i dont ude interups or DMA

these are the pins that i use and the setting for I2C:

fontys_student_2-1705416583285.png

 

fontys_student_1-1705416526079.png

this are the includes i use

fontys_student_3-1705416909107.png

this is my code. i don't have any errors or warnings in the code

fontys_student_4-1705416964693.png

how do i fix it and make shure i can send data out?

 

    This topic has been closed for replies.

    7 replies

    TDK
    January 16, 2024

    Does HAL_I2C_IsDeviceReady return HAL_OK? Do your other functions return HAL_OK?

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

    i am new to the stm platform. where can i see if the functions give a HAL_OK

    Andrew Neil
    Super User
    January 17, 2024

    @fontys_student wrote:

     where can i see if the functions give a HAL_OK


    It will be the return value from the function call; eg, 

    HAL_StatusTypeDef result; // for the return value from HAL function calls
    
    result = HAL_I2C_IsDeviceReady( &hi2c2, 0x40<<1, 10, 1000 );

     

    BTW: rather than writing `0x40<<1` as a Magic Number, it would be better style to give it a meaningful name; eg,

    #define SENSOR_I2C_ADDRESS (0x40<<1)

    then you can write:

     

    result = HAL_I2C_IsDeviceReady( &hi2c2, SENSOR_I2C_ADDRESS, 10, 1000 );

     

    Which is not only clearer & easier to understand, but also easier to maintain; eg, if you find that the address needs changing...

     

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Bob S
    Super User
    January 16, 2024

    What do you mean "won't give a signal"?  Are you looking at the SDA and SCL lines with a scope or logic analyzer?

    "I don't have any errors or warnings in the code" -> means no compiler errors or warning.  You need to check the return values from the HAL calls. Do you call MX_I2C2_Init() somewhere before the code you posted?

    Is this a custom board or one of the ST Nucleo (or Disco?) boards?  Are there external pull-up resistors on the SCL/SDA lines?  Using only the STM32's internal pull-ups MAY work for testing but should not be relied on in a production device - they are too high a resistance.

    Also, when posting code, please don't post images, copy/paste the code into the forum message using the code tags (click on the "..." button then "</>").

    Andrew Neil
    Super User
    January 17, 2024
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    TDK
    January 17, 2024

    Start with blocking functions. Call HAL_I2C_IsDeviceReady and ensure it returns HAL_OK before calling any other I2C function. Don't call HAL_I2C_EnableListen_IT at the start.

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