adafruit / Adafruit_CircuitPython_BusDevice

Two helper classes that handle transaction related state for I2C and SPI including locks.
MIT License
108 stars 76 forks source link

I2C write error I2CDevice #98

Open Jerzeek opened 7 months ago

Jerzeek commented 7 months ago

Hi folks! I have a strange problem with one of my I2C devices. If I use the traditional try_lock() function, I get correct communication with my I2C device, If I use the improved I2CDevice, the communication stops. This is the sample code I wrote to check my sanity:

import busio
import board
import time

#this works
i2c = busio.I2C(board.SCL, board.SDA)
while not i2c.try_lock():
    pass
try:
    i2c.writeto(0x0f, bytearray([0x84,0x02,0x01]))
    print("done one")

finally:
    i2c.unlock()

#this does not work....
from adafruit_bus_device.i2c_device import I2CDevice
device = I2CDevice(i2c, 0x0f)
with device:
    device.write(bytearray([0x84,0x02,0x01]))
    print("done two")

This is what I see on the logic analyser: image

It seems to be specific to the I2C device, it is a grove i2c motor driver I tried the same with a temperature sensor but that worked fine..

caternuson commented 7 months ago

The last write shown is actually coming from this line:

device = I2CDevice(i2c, 0x0f)

since the I2CDevice does a device discovery check (probe) when created.

That is followed by a start that does nothing. That should be the start of the context manager.

Try suppressing the probe check and see if that changes the behavior.

device = I2CDevice(i2c, 0x0f, probe=False)