2024-11-18 04:46 AM
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.
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:
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
Has anyone experienced similar issues with the INA228 using the STM32 HAL library?
Any help or suggestions would be greatly appreciated! Thanks in advance
2024-11-19 01:11 AM
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..
2024-11-19 01:32 AM
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 ...
2024-11-19 02:46 AM
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.
2024-11-19 12:33 PM
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;
}