From dd78b2bf6c9d63ef59e81d0c400e85c8b477a8be Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Thu, 20 Jul 2023 13:07:06 -0700 Subject: [PATCH] python lib: allow infinite timeout --- python/base.py | 3 +-- python/spi.py | 13 ++++++------- python/usb.py | 3 +-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/python/base.py b/python/base.py index 5bfa5648..d19a2b86 100644 --- a/python/base.py +++ b/python/base.py @@ -1,5 +1,4 @@ from abc import ABC, abstractmethod -from typing import List from .constants import McuType @@ -24,7 +23,7 @@ class BaseHandle(ABC): ... @abstractmethod - def bulkWrite(self, endpoint: int, data: List[int], timeout: int = TIMEOUT) -> int: + def bulkWrite(self, endpoint: int, data: bytes, timeout: int = TIMEOUT) -> int: ... @abstractmethod diff --git a/python/spi.py b/python/spi.py index abba7940..ad0225b1 100644 --- a/python/spi.py +++ b/python/spi.py @@ -32,7 +32,7 @@ CHECKSUM_START = 0xAB MIN_ACK_TIMEOUT_MS = 100 MAX_XFER_RETRY_COUNT = 5 -XFER_SIZE = 1000 +XFER_SIZE = 0x40*31 DEV_PATH = "/dev/spidev0.0" @@ -164,7 +164,6 @@ class PandaSpiHandle(BaseHandle): logging.debug("- waiting for header ACK") self._wait_for_ack(spi, HACK, MIN_ACK_TIMEOUT_MS, 0x11) - # send data logging.debug("- sending data") packet = bytes([*data, self._calc_checksum(data)]) spi.xfer2(packet) @@ -187,6 +186,7 @@ class PandaSpiHandle(BaseHandle): if remaining > 0: dat += bytes(spi.readbytes(remaining)) + dat = dat[:3 + response_len + 1] if self._calc_checksum(dat) != 0: raise PandaSpiBadChecksum @@ -216,7 +216,7 @@ class PandaSpiHandle(BaseHandle): n = 0 start_time = time.monotonic() exc = PandaSpiException() - while (time.monotonic() - start_time) < timeout*1e-3: + while (timeout == 0) or (time.monotonic() - start_time) < timeout*1e-3: n += 1 logging.debug("\ntry #%d", n) with self.dev.acquire() as spi: @@ -274,20 +274,19 @@ class PandaSpiHandle(BaseHandle): def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = TIMEOUT): return self._transfer(0, struct.pack(" int: + def bulkWrite(self, endpoint: int, data: bytes, timeout: int = TIMEOUT) -> int: for x in range(math.ceil(len(data) / XFER_SIZE)): self._transfer(endpoint, data[XFER_SIZE*x:XFER_SIZE*(x+1)], timeout) return len(data) def bulkRead(self, endpoint: int, length: int, timeout: int = TIMEOUT) -> bytes: - ret: List[int] = [] + ret = b"" for _ in range(math.ceil(length / XFER_SIZE)): d = self._transfer(endpoint, [], timeout, max_rx_len=XFER_SIZE) ret += d if len(d) < XFER_SIZE: break - return bytes(ret) + return ret class STBootloaderSPIHandle(BaseSTBootloaderHandle): diff --git a/python/usb.py b/python/usb.py index 140b4cf9..0f1bff7c 100644 --- a/python/usb.py +++ b/python/usb.py @@ -1,5 +1,4 @@ import struct -from typing import List from .base import BaseHandle, BaseSTBootloaderHandle, TIMEOUT from .constants import McuType @@ -17,7 +16,7 @@ class PandaUsbHandle(BaseHandle): def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = TIMEOUT): return self._libusb_handle.controlRead(request_type, request, value, index, length, timeout) - def bulkWrite(self, endpoint: int, data: List[int], timeout: int = TIMEOUT) -> int: + def bulkWrite(self, endpoint: int, data: bytes, timeout: int = TIMEOUT) -> int: return self._libusb_handle.bulkWrite(endpoint, data, timeout) # type: ignore def bulkRead(self, endpoint: int, length: int, timeout: int = TIMEOUT) -> bytes: