From 5089bd169444ed64c48fd2d22719c5839eebe2c7 Mon Sep 17 00:00:00 2001 From: tokiAi <53859590+tokiAi@users.noreply.github.com> Date: Wed, 30 Jun 2021 18:21:28 +0200 Subject: [PATCH 1/3] solve issues #69 I2C init bug Jetson exact description of the error and solution here: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice/issues/69 --- adafruit_bus_device/i2c_device.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 770f4c7..0efbd2f 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -151,16 +151,11 @@ def __probe_for_device(self): while not self.i2c.try_lock(): pass try: - self.i2c.writeto(self.device_address, b"") + result = bytearray(1) + self.i2c.readfrom_into(self.device_address, result) except OSError: - # some OS's dont like writing an empty bytesting... - # Retry by reading a byte - try: - result = bytearray(1) - self.i2c.readfrom_into(self.device_address, result) - except OSError: - # pylint: disable=raise-missing-from - raise ValueError("No I2C device at address: 0x%x" % self.device_address) - # pylint: enable=raise-missing-from + # pylint: disable=raise-missing-from + raise ValueError("No I2C device at address: 0x%x" % self.device_address) + # pylint: enable=raise-missing-from finally: self.i2c.unlock() From ea178187bf860de6286b83c970f15b35c040254c Mon Sep 17 00:00:00 2001 From: tokiAi <53859590+tokiAi@users.noreply.github.com> Date: Wed, 30 Jun 2021 21:59:50 +0200 Subject: [PATCH 2/3] bool switch between write probing and read This enables probing with I2C-read (without write) from the sensor libraries. --- adafruit_bus_device/i2c_device.py | 41 ++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 0efbd2f..4364b19 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -41,13 +41,21 @@ class I2CDevice: device.write(bytes_read) """ - def __init__(self, i2c, device_address, probe=True): - + def __init__(self, i2c, device_address, probe=True, probe_with_write=True): + """ + If probe is true you will probing the device. + With probe_with_write you can switch between: + True - a probing with write (& read if OSError) + False - only probing with read + """ self.i2c = i2c self.device_address = device_address if probe: - self.__probe_for_device() + if probe_with_write: + self.__write_probe_for_device() + else: + self.__only_read_probe_for_device() def readinto(self, buf, *, start=0, end=None): """ @@ -142,7 +150,32 @@ def __exit__(self, exc_type, exc_val, exc_tb): self.i2c.unlock() return False - def __probe_for_device(self): + def __write_probe_for_device(self): + """ + Try to write empty byte string to an address. + If OSError: + Try to read a byte from an address, + if you get an OSError it means the device is not there + or that the device does not support these means of probing + """ + while not self.i2c.try_lock(): + pass + try: + self.i2c.writeto(self.device_address, b"") + except OSError: + # some OS's dont like writing an empty bytesting... + # Retry by reading a byte + try: + result = bytearray(1) + self.i2c.readfrom_into(self.device_address, result) + except OSError: + # pylint: disable=raise-missing-from + raise ValueError("No I2C device at address: 0x%x" % self.device_address) + # pylint: enable=raise-missing-from + finally: + self.i2c.unlock() + + def __only_read_probe_for_device(self): """ Try to read a byte from an address, if you get an OSError it means the device is not there From 8dc5829d0640b29b1a61504b1196e557f9d2e1d1 Mon Sep 17 00:00:00 2001 From: tokiAi <53859590+tokiAi@users.noreply.github.com> Date: Thu, 1 Jul 2021 15:04:14 +0200 Subject: [PATCH 3/3] fix black --- adafruit_bus_device/i2c_device.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 4364b19..a3cfd69 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -44,7 +44,7 @@ class I2CDevice: def __init__(self, i2c, device_address, probe=True, probe_with_write=True): """ If probe is true you will probing the device. - With probe_with_write you can switch between: + With probe_with_write you can switch between: True - a probing with write (& read if OSError) False - only probing with read """