]> Repositories - hackapet/Adafruit_Blinka.git/blobdiff - src/busio.py
Merge branch 'master' of github.com:adafruit/Adafruit_Blinka
[hackapet/Adafruit_Blinka.git] / src / busio.py
index be09cd08833c79e41a7e378bf2b02ca7578122cf..74cfa89eb75d2dabe4bc68bedeae80aab87701ea 100755 (executable)
@@ -7,6 +7,8 @@ See `CircuitPython:busio` in CircuitPython for more details.
 * Author(s): cefn
 """
 
+import threading
+
 from adafruit_blinka import Enum, Lockable, agnostic
 from adafruit_blinka.agnostic import board_id, detector
 import adafruit_platformdetect.board as ap_board
@@ -31,14 +33,19 @@ class I2C(Lockable):
             from machine import I2C as _I2C
         from microcontroller.pin import i2cPorts
         for portId, portScl, portSda in i2cPorts:
-            if scl == portScl and sda == portSda:
-                self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
-                break
+            try:
+                if scl == portScl and sda == portSda:
+                    self._i2c = _I2C(portId, mode=_I2C.MASTER, baudrate=frequency)
+                    break
+            except RuntimeError:
+                pass
         else:
             raise ValueError(
                 "No Hardware I2C on (scl,sda)={}\nValid I2C ports: {}".format((scl, sda), i2cPorts)
             )
 
+        self._lock = threading.RLock()
+
     def deinit(self):
         try:
             del self._i2c
@@ -46,9 +53,11 @@ class I2C(Lockable):
             pass
 
     def __enter__(self):
+        self._lock.acquire()
         return self
 
     def __exit__(self, exc_type, exc_value, traceback):
+        self._lock.release()
         self.deinit()
 
     def scan(self):