1
\$\begingroup\$

I'm working with I2C and I have been using python library to communicate with a magnetic sensor. Now I am working with a different chip and it makes me wonder if I get the right concept or not. The only difference in new chip is that the register values are 2bytes.

Suppose

Chip address is 0x30, register address is 0x11 and data that I want to write is 0x22

This is how i2c works To write to register address: - write(chip address, register address) - write(chip address, data)

This is how i2c works To read from Register: - write(chip address, register address) - Read(chip address, No of bytes)

Please correct me if I'm wrong.

I've been using a python library quick2wire for this. In this library we can use a function called writing_bytes(chip address, register address, data)

How does it decide which is register address and which is data?

If the chip's register address is 2bytes, it will set pointer to address+data in writing_bytes function?

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Which sensor? Register based accesses can't in general be a separate write, the sequence is start - I2C address write - register address write - data write - stop. \$\endgroup\$
    – Justme
    Apr 14, 2020 at 14:17

1 Answer 1

2
\$\begingroup\$

It may depend on the chip. Do you have a datasheet?

Here is a drawing I made recently (I2C HDMI encoder) :

enter image description here

  • A : Chip address
  • R : Register number
  • D : Data read/write

  • Blue : Host signal

  • Red : Target signal

The number of bytes is determined by the stop condition on writes and the ACK level for reads (ACK=1 : Lst byte).

\$\endgroup\$
7
  • \$\begingroup\$ Thanks, consider following function: ' int Write_I2C(int addr, int reg , int data) { int file; file = open(filename, O_RDWR); if (file == -1) { perror("Check I2C Bus"); exit(1); } char buf[10]; ioctl(file, I2C_SLAVE, addr); buf[0] = reg; buf[1] = data; if (write (file, buf, 2) != 2) { perror("Write Failed\n\n"); exit (1); } } ' How will it decide if it's a 2 byte address or one byte is adress and one byte is data? \$\endgroup\$
    – Inam
    Apr 15, 2020 at 13:48
  • \$\begingroup\$ @Inam. You should find in your I2C chip datasheet the format it expects. 7bits or 10bits addresses, whether there is one or several bytes for selecting the register... The host just sends bytes. \$\endgroup\$
    – TEMLIB
    Apr 15, 2020 at 13:59
  • \$\begingroup\$ I am using ATAES132A. My question is, it's adresses are 16 bits. How Can I access them? \$\endgroup\$
    – Inam
    Apr 15, 2020 at 14:08
  • \$\begingroup\$ @Inam. Datasheet page 128 : you must send 3 bytes : For writes : buf[0]=reg/256; buf[1]=reg&255; buf[2]=data; (with reg a 16bits number). For read, first send 2 bytes : buf[0]=reg/256; buf[1]=reg&255; then read 1 byte. You can send/receive more data bytes if you want to optimise. \$\endgroup\$
    – TEMLIB
    Apr 15, 2020 at 14:22
  • \$\begingroup\$ My diagram above won't work with this chip. Yiou need to send two bytes as "register select". \$\endgroup\$
    – TEMLIB
    Apr 15, 2020 at 14:24

Your Answer

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

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