I2c cannot read from sensor
I am using mlx90614 sensor attached to my nucleo F091RC.
I have a working code with mbed :
double mlx90614::read_temp(uint8_t reg)
{ char cmd[2]; cmd[0] = 0x01; cmd[1] = 0x00;// Send start bit
__i2c->write(__addr, cmd, 2);cmd[0] = reg;
// Write the RAM slave address (register) to read from.. //true argument means a repeated start.. __i2c->write(__addr, cmd, 1, true);// Now read the response..
__i2c->read( __addr, cmd, 2);// Reading pec bit.. we just ignore it.. not useful..
int pec = __i2c->read(true);float temp = (cmd[0] | (cmd[1] << 8));
temp *= 0.02; temp -= 15; return temp; }But due to some issues with mbed-os, I would like to shift to STM cube HAL API's
Following exactly what I did in mbed, I wrote following function
------------------------
static int read_temp()
{ unsigned char cmd[2]; cmd[0] = 0; cmd[1] = 1; HAL_I2C_Master_Transmit(&hi2c2, 0x5A << 1, cmd, 2, 100); cmd[0] = 0x07; HAL_I2C_Master_Transmit(&hi2c2, 0x5A << 1, cmd, 1, 100);HAL_I2C_Master_Receive(&hi2c2, 0x5A << 1, cmd, 2, 100);
float temp = (cmd[0] | (cmd[1] << 8)); temp *= 0.02; temp -= 15; return temp; }------------------------
I am getting a constant value 0xfff for cmd[0] and cmd[1];
I realized it is due to fact that HAL_I2C_Master_Transmit/Receive API's do not support repeat start.
From the data sheet, follow is the algorithm
https://cloud.githubusercontent.com/assets/5645667/26305867/06c1de46-3f2c-11e7-9dad-d3fda7dce60a.png
Therefore in order to support repeat start, I see that I can use HAL_I2C_Master_Transmit/Receive_IT() API's
I wrote the following code..
------------------------
static int read_temp_IT() { unsigned char cmd[2]; cmd[0] = 0; cmd[1] = 1; HAL_I2C_Master_Transmit(&hi2c2, 0x5A << 1, cmd, 2, 100); cmd[0] = 0x07; HAL_I2C_Master_Transmit_IT(&hi2c2, 0x5A << 1, cmd, 1); while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY); HAL_I2C_Master_Receive_IT(&hi2c2, 0x5A << 1, cmd, 2); float temp = (cmd[0] | (cmd[1] << 8)); temp *= 0.02; temp -= 15; return temp; }------------------------
The while() loop for status check - while (HAL_I2C_GetState(&hi2c2) != HAL_I2C_STATE_READY); - hangs indefinitely.
I took reference of following sample project : STM32Cube_FW_F0_V1.8.0/Projects/STM32F091RC-Nucleo/Examples/I2C/I2C_TwoBoards_AdvComIT/Src/main.c
Could some one point out how to get proper I2c values from mlx90614
I saw your post other day. Please share your thoughts. It will greatly help me. Note: this post was migrated and contained many threaded conversations, some content may be missing.