Skip to main content
Visitor II
July 5, 2020
Solved

How to properly use HAL I2C functions

  • July 5, 2020
  • 2 replies
  • 5157 views

 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.
    Best answer by Pavel A.

    > 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

    2 replies

    Graduate II
    July 5, 2020

    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.Answer
    Super User
    July 5, 2020

    > 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.1Author
    Visitor II
    July 6, 2020

    I did as you and @Piranha​ suggested.

    Thanks that seems to work