2020-07-05 07:09 AM
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):
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.
Solved! Go to Solution.
2020-07-05 08:47 AM
> 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
2020-07-05 07:49 AM
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.
2020-07-05 08:47 AM
> 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
2020-07-05 10:31 PM
I did as you and @Piranha suggested.
Thanks that seems to work