]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/pico_u2if/pico_u2if.py
add reset
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / pico_u2if / pico_u2if.py
index 0a1b596d7df4e42b8c3b5327bc0e381b3ee6f9bc..f5f3945ba3403ffca9fe23a13702a7f700fd957b 100644 (file)
@@ -1,8 +1,12 @@
 """Chip Definition for Pico with u2if firmware"""
 # https://github.com/execuc/u2if
 
+import time
 import hid
 
+# pylint: disable=import-outside-toplevel,too-many-branches,too-many-statements
+# pylint: disable=too-many-arguments,too-many-function-args, too-many-public-methods
+
 
 class Pico_u2if:
     """MCP2221 Device Class Definition"""
@@ -12,6 +16,7 @@ class Pico_u2if:
 
     # MISC
     RESP_OK = 0x01
+    SYS_RESET = 0x10
 
     # GPIO
     GPIO_INIT_PIN = 0x20
@@ -61,20 +66,13 @@ 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)
         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"""
@@ -87,6 +85,28 @@ class Pico_u2if:
             return self._hid.read(64)
         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:
+            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
+
     # ----------------------------------------------------------------
     # GPIO
     # ----------------------------------------------------------------
@@ -360,6 +380,7 @@ class Pico_u2if:
         # init
         if not self._neopixel_initialized:
             # deinit any current setup
+            # pylint: disable=protected-access
             self._hid_xfer(bytes([self.WS2812B_DEINIT]))
             resp = self._hid_xfer(
                 bytes(
@@ -382,6 +403,7 @@ class Pico_u2if:
             True,
         )
         if resp[1] != self.RESP_OK:
+            # pylint: disable=no-else-raise
             if resp[2] == 0x01:
                 raise RuntimeError(
                     "Neopixel write error : too many pixel for the firmware."
@@ -399,6 +421,7 @@ class Pico_u2if:
     # ----------------------------------------------------------------
     # PWM
     # ----------------------------------------------------------------
+    # pylint: disable=unused-argument
     def pwm_configure(self, pin, frequency=500, duty_cycle=0, variable_frequency=False):
         """Configure PWM."""
         self.pwm_deinit(pin)
@@ -415,11 +438,7 @@ class Pico_u2if:
 
     def pwm_get_frequency(self, pin):
         """PWM get freq."""
-        resp = self._hid_xfer(
-            bytes([self.PWM_GET_FREQ, pin.id])
-            + frequency.to_bytes(4, byteorder="little"),
-            True,
-        )
+        resp = self._hid_xfer(bytes([self.PWM_GET_FREQ, pin.id]), True)
         if resp[1] != self.RESP_OK:
             raise RuntimeError("PWM get frequency error.")
         return int.from_bytes(resp[3 : 3 + 4], byteorder="little")
@@ -432,6 +451,7 @@ class Pico_u2if:
             True,
         )
         if resp[1] != self.RESP_OK:
+            # pylint: disable=no-else-raise
             if resp[3] == 0x01:
                 raise RuntimeError("PWM different frequency on same slice.")
             elif resp[3] == 0x02: