Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble reading and writing data vial I2C using smbus2 library #85

Open
satya-prakash-7 opened this issue Jul 8, 2022 · 2 comments
Open
Labels

Comments

@satya-prakash-7
Copy link

satya-prakash-7 commented Jul 8, 2022

I'm trying to connect my energy measurement IC with Raspberry Pi 3B using the I2C protocol. I'm using sudo i2cdetect -y 1 to detect the connected devices and it is correctly showing the address(7-bits) of the connected IC. The IC contains various registers of 8,16,24 and 32-bits. The register addresses are 16-bits long and I'm using the following code to access a register 0x021C(24-bits) and write registers 0xFE(8-bit) and 0x0120(16-bit).

Python Code

from smbus2 import SMBus

bus = SMBus(1)

address = 0x38

bus.write_byte_data(address,0xFE,0xAD)  #Writing single byte of data
bus.write_i2c_block_data(address,0x120,[0x00,0x30])  #Writing multiple bytes of data to the register
block = bus.read_i2c_block_data(address,0x21C,3)  #Reading multiple bytes of data from the register
print(block)

bus.close()

The output should be[0x8D,0xXX,0xXX]but the code is producing output as [0,0,0]

  • Question-1: Is giving a 16-bit addressing allowed in this Library or not?
  • Question-2: If allowed then why the code is not working?
@kplindegaard
Copy link
Owner

Answer: smbus2 is just a wrapper for i2c and SMBus support in the kernel. Hence, 16 bit addressing is not possible as per the definition of i2c_smbus_ioctl_data struct in the kernel.

@silver2row
Copy link

silver2row commented Oct 9, 2022

Hello,

Is there a way to use this library to interface a 32-bit armhf machine w/ a PCA9685 at a specific i2c address?

Seth

P.S. If I need to start a new thread/post, please let me know. The reason I am asking is b/c of the PCA9685 being on a i2c controlled daughter card on my particular board. I see on line 302 in the smbus2.py file, there is a call to handle i2c-{} (which is any i2c device in /dev/). I am having a complication b/c of a compatibility layer from the BSP people. /dev/bone/i2c/2 is the file I am currently trying to handle.

Sorry for the interruption in this post. Yes, this PCA9685 chip on the daughter card for the SBC in question works w/ smbus2. Phew!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants