AnsweredAssumed Answered

I2c cannot read from sensor

Question asked by Mant Sandy on Aug 3, 2017
Latest reply on Aug 28, 2017 by Mant Sandy

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 -= 273.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 -= 273.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

 

image

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 -= 273.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

 

Vangelis Fortounas I saw your post other day. Please share your thoughts. It will greatly help me.

Outcomes