]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py
update reset behavior
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / pico_u2if / pico_u2if.py
index 97f699b875f05f83b0fc9f26129922205bec7bf9..95cd757bba875ed564831324c71b2d494b590cb8 100644 (file)
@@ -1,8 +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
 
@@ -15,6 +20,7 @@ class Pico_u2if:
 
     # MISC
     RESP_OK = 0x01
+    SYS_RESET = 0x10
 
     # GPIO
     GPIO_INIT_PIN = 0x20
@@ -64,15 +70,11 @@ class Pico_u2if:
     PWM_SET_DUTY_NS = 0x36
     PWM_GET_DUTY_NS = 0x37
 
-    # UART
-    UART0_INIT = 0x50
-    UART0_DEINIT = 0x51
-    UART0_WRITE = 0x52
-    UART0_READ = 0x53
-
     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
@@ -90,6 +92,19 @@ class Pico_u2if:
             return self._hid.read(64)
         return None
 
+    def _reset(self):
+        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)
+            except OSError:
+                time.sleep(0.1)
+                continue
+            return
+        raise OSError("Pico open error.")
+
     # ----------------------------------------------------------------
     # GPIO
     # ----------------------------------------------------------------