cancel
Showing results for 
Search instead for 
Did you mean: 

I2C Communication with INA228 from Texas instrument

Mnfz
Associate

 

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

13 REPLIES 13

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

They're readily available; eg, https://www.adafruit.com/product/5832

It's always wise to test first with devkits before committing to a custom PCB ...

 

1. Reduce I2C frequency to 10..20 kHz, check if the problem persists.

2. Use HAL_I2C_Mem_Read to send the register address and read data from it in a single I2C transaction.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Karl Yamashita
Lead III

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;
}

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.