I am creating a NuttX device driver for Melexis MLX90614 and it is a nice adventure.
First I downloaded the adafruit lib and tested it on Arduino Uno just to confirm the sensor was working, but I didn’t look the source code because I always prefer to read the datasheet and implement the driver from scratch.
All things fine, the mlxtest example worked correctly and I got the ambient temperature and the object temperature printed correctly.
Then I created the driver and decided to test it on STM32F103, but discovered that I2C_TRANSFER() for STM32F103 is broken. No problem, let to test it on STM32F4Discovery board:
nsh> dd if=/dev/thermo0 of=/dev/null bs=2 count=1
mlx90614_read_word: value: 0xDF | value: 0x3A | value: 0xCF | ret: 0
Let see in the logic analyzer to confirm:
Very good! But how to check if CRC-8 PEC field (third received byte: 0xCF) is correct?
I found this nice online CRC calculator:
But putting all values as suggested in the datasheet didn’t work:
0x5A 0x06 0x5A 0xDF 0x3A Result CRC value: 0x2F
Then just web searching for: MLX90614 PEC CRC-8 example
Returned me this thread:
The “thb” guy saved me:
“I haven’t checked your CRC implementation but there is a mistake in the MLX datasheet or at least it’s badly written. You have to include all the I2C frame’s data for the PEC’s calculation not just the replied data. For a read word command you have to include [SA_W, Command, SA_R, LSB, MSB] and for a write word command [SA_W, Command, LSB, MSB]. So, for their first example the calculation must be made on [ 0xB4, 0x07, 0xB5, 0xD2, 0x3A ] and not just on [ 0xD2, 0x3A ] and this way you get the expected 0x30.”
What this guy is saying is: we need to include all the bits of the Slave Address, the address needs to be seen as 8-bit including the the less significant bit Write or Read. So I2C Write at address 0x5A is seen as 0xB4 and I2C Read is seen as 0xB5 (pay attention at bits over the “5A” in the logic analyzer image above).
So, let to try his idea:
0xB4 0x06 0xB5 0xDF 0x3A Result CRC value: 0xCF