Skip to main content
Graduate
October 23, 2025
Question

Changing I2C reading mode

  • October 23, 2025
  • 1 reply
  • 439 views

Hi,

 

I am trying to create the I2C communication using the format used from the company seller of the device.
I have to follow these steps:

I2CReadFunciton ( uint8_t slAddr,

uint8_t* wrData, int wrLen,

uint8_t* rdData, int rdLen

  1. Initiate the bus by issuing a START condition.

  2. If there are bytes to write (wrLen > 0):

    • Address the target device with its slave address in write mode.
    • Transmit the wrLen bytes contained in wrData.
    • Without releasing the bus (i.e., do not send a STOP), issue a REPEATED START to transition into the read phase.
  3. Begin the read phase:

    • Address the same device with its slave address in read mode.
    • Receive rdLen bytes, storing them sequentially into rdData.
  4. Terminate the transaction with a STOP condition.

Key constraints:

  • If a write phase occurs, it must be immediately followed by a repeated startno STOP is allowed between the write and the subsequent read.
  • The read always occurs after either the initial START (if no write) or the repeated START (if a write was performed).

 

My problem is how to not have the stop condition in the write function using the HAL functions.

    This topic has been closed for replies.

    1 reply

    Super User
    October 23, 2025

    Please include your full chip part number in your posts.

    What device are you interfacing with?

     

    HAL_I2C_Mem_Read will issue a write then a read with a repeated-start condition. Perhaps use that or look there to see how it is done.

    stm32f4xx-hal-driver/Src/stm32f4xx_hal_i2c.c at b0c13b119171025ac8e47d9028032fb7b7f6fd6d · STMicroelectronics/stm32f4xx-hal-driver

    Fin95Author
    Graduate
    October 24, 2025

    Thanks for the answer!

    It's a gas sensor, but it's form the coompetition of ST, so I prefer not to say which is.

    I have already used it, but it didn't work because it can happen that in the write part the wrLen cab be bigger than 1 byte, so the HAL_I2C_Mem_Read doesn't work.

    In principle, it can happen I need to write 3 bytes and read 5 bytes.

    I found the HAL_I2C_Master_Seq_Transmit_IT, but I am not pretty sure how to use it.

    Thans in advance for the help

     

    Super User
    October 27, 2025

    @Fin95 wrote:

    It's a gas sensor, but it's form the competition of ST, so I prefer not to say which is.


    That really isn't a problem: you can come to ST's forums for questions specifically about the STM32 - the way the STM32 behaves and is programmed does not depend on whether it's connected to other ST parts!

    (just remember that you need to go to the sensor  manufacturer for questions specifically about the sensor).

     

    Seeing the sensor datasheet is key to understanding what you're actually trying to achieve.