cancel
Showing results for 
Search instead for 
Did you mean: 

How to properly use HAL I2C functions

RLosc.1
Associate II

 Hey,

I'm working with TMP175AQDGKRQ1 (temperature sensor) which communicates over I2C.

I've managed to read the temperature register by doing this 

static void Read_Temperature(unsigned char *buffer[2])
{
  HAL_I2C_Master_Transmit(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, /*0x00*/TEMPERATURE_REGISTER, 1, 100);
  HAL_Delay(20);
  HAL_I2C_Master_Receive(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, buffer, 2, 100);
}

Now I'm trying to write a register and then check if the register was actually written by doing this

	  bufferTx[0] = 0x00;
	  bufferTx[1] = 0x02;
	  HAL_I2C_Master_Transmit(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, /*0x03*/THIGH_REGISTER , 1, 100);
	  HAL_Delay(20);
	  HAL_I2C_Master_Transmit(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, bufferTx , 2, 100);
	  HAL_Delay(20);
	  HAL_I2C_Master_Transmit(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR | 0x01, /*0x03*/THIGH_REGISTER , 1, 100);
	  HAL_Delay(20);
	  HAL_I2C_Master_Receive(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, bufferRx, 2, 100);

But it doesn't seem to work.. either I'm writing wrong, reading wrong or both.

 To my understanding it should look like this (From the DS):

0693W000001sAIqQAM.png

but i'm not sure how to control the R/W bit with the "HAL_I2C_Master_Transmit" and "HAL_I2C_Master_Receive" functions.

I would appreciate your help with that.

Rony.

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

> HAL_I2C_Master_Transmit(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, /*0x00*/TEMPERATURE_REGISTER, 1, 100);

The 3rd parameter of HAL_I2C_Master_Transmit is a pointer. Address 0 may be perfectly valid in some STM32's ;)

Also, as @Piranha​ advises, use HAL_I2C_Mem_Read/HAL_I2C_Mem_Write, which conveniently does repeated start.

-- pa

View solution in original post

3 REPLIES 3
Piranha
Chief II

And again someone trapped by using ST's (non-)high-level crap. To do the whole transaction without STOP condition in the middle, one must use HAL_I2C_Mem_Read()/HAL_I2C_Mem_Write() instead. Or even better - drop HAL bloatware and develop a working drivers.

Pavel A.
Evangelist III

> HAL_I2C_Master_Transmit(&hi2c1, /*0x4B<<1*/TEMP_SENSOR_ADDR, /*0x00*/TEMPERATURE_REGISTER, 1, 100);

The 3rd parameter of HAL_I2C_Master_Transmit is a pointer. Address 0 may be perfectly valid in some STM32's ;)

Also, as @Piranha​ advises, use HAL_I2C_Mem_Read/HAL_I2C_Mem_Write, which conveniently does repeated start.

-- pa

I did as you and @Piranha​ suggested.

Thanks that seems to work