0

I am trying to interface a MLX90640 Thermal Image camera to a Raspberry Pi5 over I2C using C. The register addresses are 2byte words, so SMBus/ioctl seems unworkable (as it uses 1 byte register addresses only).

I have tried using the standard Unix read() and write() functions and the Logic analyzer trace shows everything working more or less as expected.

The problem I have seems to be that when trying to read a register the initial write sends the correct data BUT adds a STOP bit, which seems to prevent the subsequent read() from reading the correct register. The result is that a read ALWAYS returns the same values.

Is there any way I can prevent a write() before a read() from sending the stop bit?

The open command is: if(0 > (fh = open("/dev/i2c-1", O_RDWR))) {

The initial write: if(2 != (res = write(fh, msg, 2))) {

The subsequent read: if(4 != (res = read(fh, resp, 4))) {

The msg and resp are uint8_t arrays.

Analyzer Trace

Or is there another library I should be using that works on RPi5?

3
  • 1
    Is there any reason you aren't using the dtoverlay?
    – Milliways
    Commented Aug 12 at 11:13
  • I'm not aware of how that would solve the problem. Is there a good reference for me to read?
    – Nic A
    Commented Aug 12 at 11:42
  • I glanced at a data sheet for this camera and I'd bet those address aren't really 16-bit addresses even though they are two bytes (ie., they are actually used as two 8 bit-values). Try using the smbus interface that way; the register address is the high byte, eg., for the "32x24 IR array" the address is 0x24, so to read data from "0x2430" send 0x30 to 0x24 then read from 0x24. The 2nd byte controls what data is fed into the register indicated by the first byte.
    – goldilocks
    Commented Aug 12 at 18:35

1 Answer 1

0

Thank you both for your time, I finally got it figured out.

Should have been using ioctl() with I2C_RDWR. That way the data works out fine. I can set a 16 bit register address for write and chain the read message without the troublesome STOP bit in between.

All seems to be fine now.

Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.