0

I've been using C code based on a convenient linux_i2c-dev.h, but there seems to be no function to read 2 bytes without sending a command byte first. I want the code to work on both PI3s & PI4s.

Is there a similar alternative available if I2C_SMBUS does not support it, without installing packages on many devices to run it?

(I was using Python, but it is too slow and uses too much CPU.)

1 Answer 1

0

It is easier than I thought: The OS read function is exactly what is needed and ioctl() is NOT used for the IO as in other cases.

In summary, without error checking:

int MCP3221= open("/dev/i2c-1", O_RDWR); // open I2C1
ioctl(MCP3221, I2C_SLAVE, 0x4D); // use MCP device address

unsigned char buf[2];
read(MCP3221, buf, 2); // read 2 byte value
int adc = (buf[0] << 8) | buf[1];

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.