cancel
Showing results for 
Search instead for 
Did you mean: 

Changing I2C reading mode

Fin95
Associate II

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.

8 REPLIES 8
TDK
Super User

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

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

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

 


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

 

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.

Hi @Andrew Neil ,

Thanks for your response!

The issue isn’t with the gas sensor itself, but rather with the way I’m trying to read data via I2C, which doesn’t follow the conventional approach.

Specifically, I’m aiming for a functionality similar to HAL_I2C_Mem_Read, as suggested by @TDK . However, in my case, I need to send multiple EEPROM address bytes, followed by a restart condition without a stop bit, and then read the data buffer.

Is this kind of sequence supported by HAL_I2C_Mem_Read, or should I use a different HAL function to achieve this behavior?

Thanks again for your help!

 


@Fin95 wrote:

The issue isn’t with the gas sensor itself, but rather with the way I’m trying to read data via I2C, which doesn’t follow the conventional approach.


But giving the part number allows people to check out the datasheet to see exactly what that "unconventional approach" is.

Better still, give a direct link to the datasheet.

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.

Ok, the gas sensor is this: https://www.renesas.com/en/products/zmod4410?srsltid=AfmBOopelXCvozcRLAmjhgqs_Rt6-DrQ8_zLMNfFxPtMkawyeqX11P6P

In the Datasheet you are can check the I2C protocol and in the User Manual you can check the example program I am importing (I have to implement the i2c write and read).

I have already tried to use the HAL_I2C_Mem_Read in the read function, but it doesn't work.

Thanks again for the support!

So have you tried putting "ZMOD4410 stm32" into your favourite internet search engine?

eg, Someone here on the Renesas forum says that HAL_I2C_Mem_Write/Read worked for them ...

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.

Yes, I have tried, but it doesn't work.

I will retry with these functions.

Thanks