Skip to main content
Visitor II
November 18, 2024
Question

I2C Communication with INA228 from Texas instrument

  • November 18, 2024
  • 3 replies
  • 3403 views

 

Hello everyone,

 I'm interfacing an INA228  with an STM32 microcontroller  via the I2C bus. However, I'm facing a problem with the communication that I can't seem to resolve.

Setup Details:

  • Microcontroller: STM32h7 (using HAL library)
  • INA228 connected via I2C
  • API Used: HAL_I2C_Transmit (blocking mode)
  • I2C Clock Speed: Tested at both 100 kHz and 400 kHz
  • Pull-up Resistors: 2.2 kΩ on SDA and SCL lines
  • Configuration of GPIO in STM32h7: 
    GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;

Issue:

I'm trying to write a pointer register on the INA228 by sending a 1-byte data, in order to perform then a read on this register:

  1. The register address (1 byte)

I'm calling HAL_I2C_Master_Transmit(&hi2c1, INA228_ADDRESS<<1, txBuf, 1,HAL_MAX_DELAY)

txBuf contains address of register.

In most cases, the INA228 responds with a NACK. However, i tried putting my HAL_I2C_Master_Transmit in a loop and at a  random iteration device responds correctly and the transmission is successful.  when its successful i tried reading register content using HAL_I2C_Master_Receive but it fails with error HAL_I2C_ERROR_AF

 
Steps I've Taken:
  1. Verified the I2C address of the INA228 using HAL_I2C_IsDeviceReady --> no detection of INA228, i saw in a post that HAL_I2C_IsDeviceReady had a bug (i'm using V1.11.0), so i stopped relying on it.
  2. Checked the waveform with a logic analyzer; the data being sent appears to be correct.
  3. Reduced the I2C clock speed to 100 kHz — no improvement observed.
  4. Tried adding small delays between the register address byte and the data bytes — issue persists.

Has anyone experienced similar issues with the INA228 using the STM32 HAL library?

  1. Could this be related to timing or specific delays required by the INA228 that are not documented?
  2. Are there any additional I2C settings or configurations in the STM32 HAL that might help with this issue?

Any help or suggestions would be greatly appreciated! Thanks in advance

    This topic has been closed for replies.

    3 replies

    Super User
    November 18, 2024

    Please show your code, and schematic:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

     


    @Mnfz wrote:

    1. Checked the waveform with a logic analyzer; the data being sent appears to be correct.


    That's good - but have you also checked with an oscilloscope?

    An oscilloscope would show if there are any "analogue" issues on your I2C lines - eg, pullups too high or too low ...

    A good diagram here showing the (analogue) effect of too-high and too-low pullup values:

    https://electronics.stackexchange.com/a/473799

     

    Have you contacted TI about this? They should be able to comment whether your LA traces look appropriate for their device...

    https://www.ti.com/product/INA228#support-training 

    (When cross-posting to multiple forums, always give links between threads - so everyone can see the whole story)

    MnfzAuthor
    Visitor II
    November 18, 2024

    Hey Andrew, thanks for your reply, concerning my code:

     

    // INA228_ADDRESS = 0x44
    uint8_t txBuf[1] = {0};
    for (;;;) {
    // Writing register pointer for configuration register to INA228 in blocking mode, in order to read back the content
    HAL_StatusTypeDef state = HAL_I2C_Master_Transmit(&hi2c1, INA228_ADDRESS<<1, txBuf, 1, HAL_MAX_DELAY);
    if (state == HAL_OK) {
    // Can be reached randomly
    uint8_t rxBurf[2] = {0};
    // This call should return rxBuf field with pointed register content (16bits)
    state = HAL_I2C_Master_Receive(&hi2c1, INA228_ADDRESS << 1, rxBuf, 2, HAL_MAX_DELAY);
    if (state == HAL_OK){
    // Never reached
    int32_t conf = rxBuf[0] << 8 | rxBuf[1];
    INFO("conf_reg: %ld", conf);
    return;
    } else {
    ERROR("Receiving register conf content failed with error: %d, flagError: %ld",state, hi2c1.ErrorCode);
    return;
    }
    }}

     

     
    INA228 supply voltage 5V/ GND1 
     +-------------+
    (C = 100nV)5v|VDD1 VDD2|3v3 (C = 100nV)
    + +
    INA228-SDA<---2.2KO--->+ ADUM1250SRZ +<---2.2KO---> I2C-SDA-stm32
    INA228-SCL<---2.2KO----+ +<---2.2KO---I2C-SCL-stm32
    |GND1 GND2|
    +-------------+
    I will see for visualisation on an oscilloscope :) Thank you! 
    As for TI, i left a message on the training and forum
    Super User
    November 18, 2024

    Please see the posting tips (which I linked) for how to properly post source code:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

    and for ASCII Art:

    https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/tac-p/725146/highlight/true#M54

     

    Also what boards are you using - manufacturer boards? 3rd-party? custom?

     


    @Mnfz wrote:
    an overview of my connections

    You didn't previously mention an isolator. Part number?

    Does it work without the isolator?

     


    @Mnfz wrote:
     
    As for TI, i left a message on the training and forum

    Please give a link.

    Also give a link over there back to here.

    Again, so that everyone (both there and here) can see the entire story - eg, they will also want to know about the isolator...

     

    MnfzAuthor
    Visitor II
    November 18, 2024

    This is a visualize simultaneously signals on clock and data lines , I notice a desynchronization between the data
    and the clock, it seems as if its the source of my problem, data frames are not cached due to this  is that correct ?

    As for signals shapes, i think there is no problem related to Pullups, what do you think?

    trace ina sda scl--00000.jpg

    Super User
    November 18, 2024

    Is that before or after the isolator?

    Again, does it work without the isolator?

    MnfzAuthor
    Visitor II
    November 19, 2024

    It's after the isolator, i will join the waves from test points before the isolator.

    As for tests,  i don't have an INA228 that i can connect directly to my stm32 to check if its an isolater issue..

    Graduate II
    November 19, 2024

    You're working with registers so use HAL_I2C_Mem_Read and HAL_I2C_Mem_Write

     

    Example;

    enum RegisterAddress
    {
    	INA228_CONFIG,
    	INA228_ADC_CONFIG,
    	//...
    	INA228_DEVICE_ID = 0x3F,
    };
    
    #define REG_SIZE 1
    #define DATA_BIT_SIZE_16 2 
    #define DATA_BIT_SIZE_24 3 
    #define SLAVE_ADDRESS (0x41 < 1)
    
    typedef union
    {
    	struct
    	{
    		uint8_t data[2];
    	}Bytes;
    	struct
    	{
    		unsigned :3;
    		unsigned ADCRANGE:1; 
    		unsigned TEMPCOMP:1;
    		unsigned CONVDLY:8;
    		unsigned RSTACC:1;
    		unsigned RST:1;
    	}Status;
    }INA228_ConfigRegDef;
    
    
    int main()
    {
     
     INA228_ConfigRegDef configReg =
     {
     .status.CONVDLY = 1,
     .status.ADCRANGE = 1
     }
     
     HAL_I2C_Mem_Write(&hi2c1, SLAVE_ADDRESS, INA228_CONFIG, REG_SIZE, configReg.Bytes.data, DATA_BIT_SIZE_16, 100) != HAL_OK)
     {
     // error routine
     }
    
     return 0;
    }