Skip to main content
RLosc.1
Associate II
Jul 5, 2020 2:09 PM UTC
Solved

How to properly use HAL I2C functions

 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.

This topic has been closed for replies.
Pavel A.
Pavel A.Best answer
Jul 5, 2020 3:47 PM UTC

> 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

3 replies

Piranha
Principal III
Jul 5, 2020 2:49 PM UTC

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.
Pavel A.Best answer
Jul 5, 2020 3:47 PM UTC

> 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

RLosc.1
RLosc.1Author
Associate II
Jul 6, 2020 5:31 AM UTC

I did as you and @Piranha​ suggested.

Thanks that seems to work