kplindegaard / smbus2

A drop-in replacement for smbus-cffi/smbus-python in pure Python
MIT License
243 stars 68 forks source link

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

Open satya-prakash-7 opened 2 years ago

satya-prakash-7 commented 2 years ago

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]

kplindegaard commented 2 years ago

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 commented 2 years ago

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!