Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

I2C problem with READ

Wed Nov 04, 2015 10:06 am

As far as i know,
to be able to read from I2C device, I must first write to a register
and then read values from this register

Example
From a logic analyzer I can see this communication between an I2C device

Address: D0 D1 D2 D3 D4
Write 08 : C8 A1 81 00
Read 08: 21 00 23 04 03


These commands should be a READ command

and the first should write 0xC8A18100 to a device on 08 address and then the second command should
read the value 21 00 23 04 03.
Is my understanding correct?

My problem is that I do not know how to send such big number 0xC8A18100
via Raspberry/Python.

E.g. I tried command
bus.read_i2c_block_data(0x08,0xC8A1)
and I can see( from a logic analyzer)

Address D0 D1 D2 D3 D4
Write 08 A1
Read 08 85 00 00 00 7F

as you can see, only ONE BYTE ( here 0xA1) is sent.
How can I sent more bytes to READ command?

Os is there a different method?
Thank you

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C problem with READ

Fri Nov 06, 2015 6:46 am

Nobody needed to solve a similar problem yet?

User avatar
joan
Posts: 14888
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problem with READ

Fri Nov 06, 2015 8:28 am

You can do what you want with the SMBUS module but you need to understand what each call actually does. You should probably use the read/write i2c block data calls.

However it may be simple if you use my I2C implementation in pigpio. Use the i2c_read_device and the i2c_write_device calls.

Alternatively look at my http://abyz.co.uk/rpi/pigpio/python.htm ... block_data and http://abyz.co.uk/rpi/pigpio/python.htm ... block_data functions for what your calls should do. But note I have the definition of the actual transactions the wrong way around.

Write should be

Code: Select all

S Addr Wr [A] Comm [A] Data [A] Data [A] ... [A] Data [A] P
Read should be

Code: Select all

S Addr Wr [A] Comm [A]
   S Addr Rd [A] [Data] A [Data] A ... A [Data] NA P

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C problem with READ

Fri Nov 06, 2015 7:13 pm

Thank you for your reply. I checked the syntax
i2c_read_i2c_block_data(handle, reg, count)
What is the largest number for reg parameter ?

User avatar
joan
Posts: 14888
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problem with READ

Fri Nov 06, 2015 7:54 pm

Jan1 wrote:Thank you for your reply. I checked the syntax
i2c_read_i2c_block_data(handle, reg, count)
What is the largest number for reg parameter ?
It is a byte, 0-255.

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C problem with READ

Fri Nov 06, 2015 9:03 pm

But then I am not sure it solves my problem.
I need to read from an address/register of address 0xC8 A1 81 00
( see the beginning of this topic)
but your i2c_read_i2c_block_data command
can read only from 0xFF address of registers.

Or am I wrong in understanding?
Thank you again for your help/ explanation

User avatar
joan
Posts: 14888
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problem with READ

Fri Nov 06, 2015 9:21 pm

Code: Select all

Address: D0 D1 D2 D3 D4
Write 08 : C8 A1 81 00
Read 08: 21 00 23 04 03
You write 4 bytes to I2C device 8, e.g. i2c_write_device(handle, [0xc8, 0xa1, 0x81, 0]).

You then read 5 bytes from I2C device 8, e.g. (count, data) = i2c_read_device(handle, 5).

Try from the command line.

pigs i2co 1 8 0 # get handle for device 8 on bus 1 (handle will be 0 for first)
pigs i2cwd 0 0xc8 0xa1 0x81 0
pigs i2crd 0 5

Jan1
Posts: 42
Joined: Tue Nov 13, 2012 7:22 pm

Re: I2C problem with READ

Tue Nov 10, 2015 7:03 pm

Thank you for your help. It works.
But why must I start pigpio daemon first?
SMBus did not need that.
Thanks for the reply

User avatar
joan
Posts: 14888
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C problem with READ

Tue Nov 10, 2015 7:48 pm

Jan1 wrote:Thank you for your help. It works.
But why must I start pigpio daemon first?
SMBus did not need that.
Thanks for the reply
That's a peculiarity of the pigpio Python module. It uses the pigpio daemon to talk to the GPIO.

Now that you know how it works you should be able to make it work with the SMBUS module.

Return to “Interfacing (DSI, CSI, I2C, etc.)”