0

How can I read from a 16 bit address using I2C? I know how to write to it, i.e sending the low bit of the address in the data portion but cannot seem to understand how to read from it. I'm using python btw.

Ok so I managed to read the register using the /dev/i2c-1 file. I am using the os.read function in python like this. def func(fd,addr):
os.write(fd,addr) //sets the register to be read. x = os.read(fd,3) //reads 3 bytes from the specified address.

Ok I have check the transaction using Saleae. It works correctlyenter image description here

The problem that I'm having right now is that I cannot store the returned value i.e 0x03 in my variable i.e x. The os.read function documentation says that if the EOF of the file is reached, a null string is returned. So everytime, I read the register, even though I successfully carry out a read, I cannot show it on my terminal by printing out a variable. How can I solve this problem?

1

The datasheet for the device will tell you how to read the device registers.

The method used will depend on the specific device (as documented in its datasheet).

  • To do the read, I have to first do a write operation that specifies the read address, and then read from the address. The problem is that the address is 16 bit whereas the SMBus library only allows 8 bit register addresses. Also i've tried to use ioctl. That doesn't work either. Again it has the same problem. It just gets rid of a whole byte of the address – Muhammad Usman Aug 15 '17 at 14:46
  • Well don't use SMBus, use I2C. My pigpio library allows raw I2C from Python. See i2c_read_device and i2c_write_device. – joan Aug 15 '17 at 15:04
  • @MuhammadUsman SMBus has standardized address and command registers, it is a "flavor" or even protocol on top of i2c. i2c itself is a generic serial bus and has no restrictions on address size. You need to be generating the control bytes yourself and pass them in raw form to i2c driver. Note that i2c devices tend to be big endian so you should be generating byte/char arrays to send, and shifting to read, you cannot use the 16bit address directly. – crasic Aug 15 '17 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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