0

Well-versed in C/C++, but new to Python.

I have done a lot of research here and maybe I am missing something, but it seems that Pi/Python does not have primitive functions for I2C, which is sad to be honest. If this is the case, then I don't understand why the foundation would not support bare-bone I2C. There are some work around's for different cases, but they do not solve all, and with some primitive functions, it could.

Here is one case: 16-bit REG_ADDR WRITE, but doesnt solve 16-bit REG_ADDR READ: https://www.raspberrypi.org/forums/viewtopic.php?t=170062

Here is what is not possible:

  • A sensor with 16-bit registers, and 16-bit data. I am not talking about the slaver address. I see a ton of threads where people go back and forth on this subject and mix the 2.

  • 7-bit/8-bit slave address

  • 16-bit register offsets
  • 16-bit data in each offset

How can I communicate with such a device? IS it possible?

  • As soon as you want to read, all of the read functions, take an 8-bit read address, thus even if you could write the offset address before a read, the read will fail.

read_block_data(SLAVE_ADDR, REG_ADDR, DATA_ARRAY[]) the REG_ADDR is always 8-bits, hence you cannot talk to a 16-bit address part.

Is there a module someone has to just have primitive write and read functions?

  • write a byte array: write(SLAVE_ADDR, BYTEARRAY[])
  • read a byte array: Array = read(SLAVE_ADDR)

Why is this not available?

1
  • See this please: Again, never a read example, no explanation of these parameters. raspberry-projects.com/pi/programming-in-python/… long[] read_block_data(int addr,char cmd) #this is in C, but I assume this is available, but what is the cmd parameter? offset? # of bytes? the other functions it's offset, but then how do you know how many read? Where is the official documentation for this? I found something once before but it was horribly incomplete.
    – Pedro
    Jun 14, 2018 at 3:11

2 Answers 2

1

You are confusing a number of issues.

The Pi only really supports 7-bit I2C slave addresses. This isn't normally a problem as you hardly ever come across anything other than 7-bit I2C address devices. I never have.

That has nothing to do with whether the device uses 16-bit or 8-bit (or any other number of bits) for internal register addresses or has 8-bit or 16-bit internal storage size. I2C always transfers in units of 8-bit bytes. So to read/write a 16-bit register you transfer two bytes.

The Python SMBus module only supports SMBus commands (with a couple of irrelevant to this answer exceptions). This is okay for most but not all I2C devices.

My pigpio library does offer I2C as well as SMBus support.

The I2C support is for arbitrary length read and write of bytes.

For Python see i2c_read_device and i2c_write_device.

3
  • Thanks for responding, I never really mentioned anything other than a slave address of 8-bits. This is about using a pi ... with python, and using their smbus lib to write (& READ) multiple bytes, from a device which has 16-bit register addresses. you can use the original smbus lib to fake out the multiple byte read to this type of device, but you cannot read, due to the 8-bit parameter for the reg address.
    – Pedro
    Jun 14, 2018 at 17:20
  • Okay, I made an incorrect assumption about questioning the slave address. However I have answered the only relevant one of your many questions.
    – joan
    Jun 14, 2018 at 20:08
  • What device are you using that uses 16-bit register addresses?
    – NomadMaker
    Jun 15, 2018 at 0:18
0

Hi This may be the answer to use PIGPIO, however it is not being found after installations. I followed the directions to download the zip, unzip, make & install. I think I may have an issue with directories.

** using Python v3.5, on a Pi Zero W.

Thanks, -P

1
  • daemon? why? why hasn't anyone just written a simplified linux i/f for I2c?
    – Pedro
    Jun 15, 2018 at 0:12

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.