cancel
Showing results for 
Search instead for 
Did you mean: 

I2c cannot read from sensor

Mant Sandy
Associate III
Posted on August 03, 2017 at 06:49

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

https://community.st.com/people/FORTOUNAS.EVANGELOS

‌ 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.
15 REPLIES 15
Posted on August 07, 2017 at 14:38

> Are theese two devices the same?

No the devices are different.

I am attaching the IOC file.. Also, I have enabled only I2C2 in the IOC..

Thank you very much for taking out time and looking into this issue.

________________

Attachments :

i2c2_sample.ioc.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyX0&d=%2Fa%2F0X0000000b96%2FsRc0LFjiS3x1jLkMgWIxlz5pqFXVoyQGv.DaXnKpmRk&asPdf=false
Posted on August 07, 2017 at 15:35

Hello,

Your initialization code is ok.

I2c clocked at 100 khz(maximum value of your device)  try to decrease it.

HAL_I2C_Master_Sequential_Transmit_IT called after initialization activates interrupts.

Check NVIC at run time, to see if you have I2c2 enabled, pending, active interrupt.

Check I2C2 Interrupt and status register (I2C_ISR) at run time. All theese flags inside must produce interrupts.

Is stm32f0xx_it.c linked to your project? 

Do not use compiler optimisations (-O0), to have clear view of your code in debug mode.

Posted on August 08, 2017 at 19:22

What debugger you generally use?

I am using gdb.

in gdb, I can only see r0-r12 and few basic registers like pc, lr, sp, cpsr etc..

How to check for I2c_ISR?

Also. I notice that simple API's like HAL_Delay(1) are never exiting.

and a simple loop like

for(i=0; i<5000; i++);

LMI2
Lead
Posted on August 09, 2017 at 17:20

There is also the Melex (the sensor manufacturer)support address somewhere in their website. They seem to be quite competent in handling this kind of problems, probably because of that SMBbus.

There are also working MLX90614 libraries for Arduino devices.

Posted on August 10, 2017 at 13:58

Thanks mich. With a quick look i could not find the contact. But will search for it thoroughly if I am unable to resolve this issue..

Posted on August 10, 2017 at 17:30

yeah. I remember it was hard to find.

Edit: Or not.

https://www.melexis.com/en/contact