]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
update reset behavior
authorcaternuson <caternuson@gmail.com>
Sun, 25 Apr 2021 18:07:58 +0000 (11:07 -0700)
committercaternuson <caternuson@gmail.com>
Sun, 25 Apr 2021 18:07:58 +0000 (11:07 -0700)
src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py

index f5f3945ba3403ffca9fe23a13702a7f700fd957b..95cd757bba875ed564831324c71b2d494b590cb8 100644 (file)
@@ -1,9 +1,13 @@
 """Chip Definition for Pico with u2if firmware"""
 # https://github.com/execuc/u2if
 
+import os
 import time
 import hid
 
+# Use to set delay between reset and device reopen. if negative, don't reset at all
+PICO_U2IF_RESET_DELAY = float(os.environ.get("PICO_U2IF_RESET_DELAY", 1))
+
 # pylint: disable=import-outside-toplevel,too-many-branches,too-many-statements
 # pylint: disable=too-many-arguments,too-many-function-args, too-many-public-methods
 
@@ -67,12 +71,15 @@ class Pico_u2if:
     PWM_GET_DUTY_NS = 0x37
 
     def __init__(self):
+        self._hid = hid.device()
+        self._hid.open(Pico_u2if.VID, Pico_u2if.PID)
+        if PICO_U2IF_RESET_DELAY >= 0:
+            self._reset()
         self._i2c_index = None
         self._spi_index = None
         self._serial = None
         self._neopixel_initialized = False
         self._uart_rx_buffer = None
-        self._reset()
 
     def _hid_xfer(self, report, response=True):
         """Perform HID Transfer"""
@@ -86,26 +93,17 @@ class Pico_u2if:
         return None
 
     def _reset(self):
-        # get a HID device
-        self._hid = hid.device()
-        # open and reset
-        self._hid.open(Pico_u2if.VID, Pico_u2if.PID)
-        resp = self._hid_xfer(bytes([self.SYS_RESET]), True)
-        if resp[1] != self.RESP_OK:
-            raise RuntimeError("Reset error.")
-        # reopen
-        max_retry = 10
-        retries = 0
-        while True:
+        self._hid_xfer(bytes([self.SYS_RESET]), False)
+        time.sleep(PICO_U2IF_RESET_DELAY)
+        start = time.monotonic()
+        while time.monotonic() - start < 5:
             try:
                 self._hid.open(Pico_u2if.VID, Pico_u2if.PID)
-                return True
             except OSError:
-                if retries >= max_retry:
-                    break
                 time.sleep(0.1)
-                retries += 1
-        return False
+                continue
+            return
+        raise OSError("Pico open error.")
 
     # ----------------------------------------------------------------
     # GPIO