cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L496 I2C Reading Error

WQ
Associate III

Hello:  This is weird problem. We have a STM32L496 system that handles multiple I2C devices through its three I2C buses. The I2C#1 (1MHz) has 10 NXP devices, #2 (400kHz) has 4 TI TMP117 chips, and #3 (400kHz) miscellaneous devices including other TI devices. They all use the same sub-functions to do basic I2C reading and writing (LL driver based polling).

The problem is with the TMP117 reading on I2C #2. It occasionally get wrong temperatures. We first thought it was from TI TMP117 chip. But the scope showed the correct and clean waveforms. 

We have never observed the error if we slow down the I2C#2 speed to 100kHz, 

We have never seen the similar reading problem on other I2C buses and devices that run with the same I2C device drivers.

All the setup and configuration of all I2C buses are all default with STM32CubeIDE (v1.14.1).

 

Any help is very much appreciated.

 

 

 

 

 

int TMP117_read(I2C_TypeDef *I2Cx, uint16_t devAddrOffset, float* temp_celsius)
{
	int ret = 0;

	uint8_t i2c_buf[2];

	uint16_t tmp117_DevAddr = TMP117_DEV_ADDR0 + (devAddrOffset<<1) ;

	union {
		uint8_t ubyte[2];
		int16_t uword;
	} i2c_data;

	ret += I2C_Master_MemRead(I2Cx, tmp117_DevAddr, TMP117_REG_TEMP, I2C_MEMADD_SIZE_8BIT, i2c_buf, 2);


	i2c_data.ubyte[0] = i2c_buf[1];
	i2c_data.ubyte[1] = i2c_buf[0];

	myprintf(p_uxrx_cli_arg->ux_sel, "i2c_buf=0x%02X,0x%02X, uword=0x%04X,%d, TEMP=%f ", i2c_buf[0], i2c_buf[1], i2c_data.uword, i2c_data.uword, i2c_data.uword * TMP117_LSB_TEMP);

	*temp_celsius = i2c_data.uword * TMP117_LSB_TEMP;

	return ret;
}

int I2C_Master_MemRead(I2C_TypeDef *I2Cx, uint32_t SlaveDevAddr, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint32_t Size) {
	//uint32_t tickstart = HAL_GetTick();
	uint32_t tickstart = time_stick_get();

	LL_I2C_HandleTransfer(I2Cx, SlaveDevAddr, LL_I2C_ADDRSLAVE_7BIT, MemAddSize, LL_I2C_MODE_SOFTEND, LL_I2C_GENERATE_START_WRITE);

	while (!LL_I2C_IsActiveFlag_TXIS(I2Cx)) {
        //if ( (HAL_GetTick()-tickstart) > I2C_DEFAULT_TIMEOUT ) {
        if ( (time_stick_get()-tickstart) > I2C_DEFAULT_TIMEOUT ) {
        	return ERROR;
        }
	}

	if (MemAddSize == I2C_MEMADD_SIZE_16BIT)
	{
		LL_I2C_TransmitData8(I2Cx, I2C_MEM_ADD_MSB(MemAddress));
		while (!LL_I2C_IsActiveFlag_TXIS(I2Cx)) {
		}
	}

	LL_I2C_TransmitData8(I2Cx, I2C_MEM_ADD_LSB(MemAddress));
	while (!LL_I2C_IsActiveFlag_TC(I2Cx)) {
	}

	uint8_t Index = 0;

	LL_I2C_HandleTransfer(I2Cx, SlaveDevAddr, LL_I2C_ADDRSLAVE_7BIT, Size, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_RESTART_7BIT_READ);

	while (!LL_I2C_IsActiveFlag_STOP(I2Cx)) {
		if (LL_I2C_IsActiveFlag_RXNE(I2Cx)) {
			pData[Index++] = LL_I2C_ReceiveData8(I2Cx);
		}
	}

	LL_I2C_ClearFlag_STOP(I2Cx);
	return SUCCESS;
}

 

 

 

 

 

 

0 REPLIES 0