Skip to content

Commit 9462a48

Browse files
authored
Merge pull request #1038 from mikeysklar/i2c-scan
i2c scan logic change
2 parents 6a4f208 + 1c34beb commit 9462a48

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

  • src/adafruit_blinka/microcontroller/generic_linux

src/adafruit_blinka/microcontroller/generic_linux/i2c.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,22 @@ def __init__(self, bus_num, mode=MASTER, baudrate=None):
4141
# pylint: enable=unused-argument
4242

4343
def scan(self):
44-
"""Try to read a byte from each address, if you get an OSError
45-
it means the device isnt there"""
44+
"""Scan I2C bus for devices.
45+
46+
On Linux, some devices will NACK SMBus 'read_byte' probes but respond fine
47+
to real I2C transactions. Prefer SMBus 'quick' probe when supported, and
48+
fall back to 'read_byte'. Skip reserved address ranges by scanning only
49+
0x08..0x77.
50+
"""
4651
found = []
47-
for addr in range(0, 0x80):
52+
for addr in range(0x08, 0x78):
4853
try:
49-
self._i2c_bus.read_byte(addr)
54+
self._i2c_bus.write_quick(addr)
5055
except OSError:
51-
continue
56+
try:
57+
self._i2c_bus.read_byte(addr)
58+
except OSError:
59+
continue
5260
found.append(addr)
5361
return found
5462

0 commit comments

Comments
 (0)