2023-01-16 15:30:24 +08:00
|
|
|
import os
|
2023-01-15 15:32:17 +08:00
|
|
|
import fcntl
|
2022-11-11 13:34:43 +08:00
|
|
|
import math
|
2022-12-21 14:07:24 +08:00
|
|
|
import time
|
2022-11-04 13:18:12 +08:00
|
|
|
import struct
|
2022-11-11 13:34:43 +08:00
|
|
|
import logging
|
2023-01-28 16:32:07 +08:00
|
|
|
import threading
|
2023-01-15 15:32:17 +08:00
|
|
|
from contextlib import contextmanager
|
2022-11-04 13:18:12 +08:00
|
|
|
from functools import reduce
|
2022-11-11 13:34:43 +08:00
|
|
|
from typing import List
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2023-01-29 08:01:54 +08:00
|
|
|
from .base import BaseHandle
|
|
|
|
|
2023-01-16 04:51:22 +08:00
|
|
|
try:
|
|
|
|
import spidev
|
|
|
|
except ImportError:
|
|
|
|
spidev = None
|
|
|
|
|
2022-11-04 13:18:12 +08:00
|
|
|
# Constants
|
|
|
|
SYNC = 0x5A
|
|
|
|
HACK = 0x79
|
|
|
|
DACK = 0x85
|
|
|
|
NACK = 0x1F
|
|
|
|
CHECKSUM_START = 0xAB
|
|
|
|
|
2022-12-21 15:05:04 +08:00
|
|
|
ACK_TIMEOUT_SECONDS = 0.1
|
2022-12-21 14:07:24 +08:00
|
|
|
MAX_XFER_RETRY_COUNT = 5
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-11-11 13:34:43 +08:00
|
|
|
USB_MAX_SIZE = 0x40
|
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
DEV_PATH = "/dev/spidev0.0"
|
|
|
|
|
2022-12-21 14:07:24 +08:00
|
|
|
|
|
|
|
class PandaSpiException(Exception):
|
|
|
|
pass
|
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
class PandaSpiUnavailable(PandaSpiException):
|
|
|
|
pass
|
|
|
|
|
2022-12-21 14:07:24 +08:00
|
|
|
class PandaSpiNackResponse(PandaSpiException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PandaSpiMissingAck(PandaSpiException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PandaSpiBadChecksum(PandaSpiException):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class PandaSpiTransferFailed(PandaSpiException):
|
|
|
|
pass
|
|
|
|
|
2023-01-15 15:32:17 +08:00
|
|
|
|
2023-01-28 16:32:07 +08:00
|
|
|
SPI_LOCK = threading.Lock()
|
2023-01-15 15:32:17 +08:00
|
|
|
|
2023-01-28 16:32:07 +08:00
|
|
|
class SpiDevice:
|
|
|
|
"""
|
|
|
|
Provides locked, thread-safe access to a panda's SPI interface.
|
|
|
|
"""
|
|
|
|
def __init__(self, speed=30000000):
|
2023-01-16 15:30:24 +08:00
|
|
|
if not os.path.exists(DEV_PATH):
|
|
|
|
raise PandaSpiUnavailable(f"SPI device not found: {DEV_PATH}")
|
2023-01-16 04:51:22 +08:00
|
|
|
if spidev is None:
|
2023-01-16 15:30:24 +08:00
|
|
|
raise PandaSpiUnavailable("spidev is not installed")
|
2023-01-16 04:51:22 +08:00
|
|
|
|
2023-01-28 16:32:07 +08:00
|
|
|
self._spidev = spidev.SpiDev() # pylint: disable=c-extension-no-member
|
|
|
|
self._spidev.open(0, 0)
|
|
|
|
self._spidev.max_speed_hz = speed
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def acquire(self):
|
|
|
|
try:
|
|
|
|
SPI_LOCK.acquire()
|
|
|
|
fcntl.flock(self._spidev, fcntl.LOCK_EX)
|
|
|
|
yield self._spidev
|
|
|
|
finally:
|
|
|
|
fcntl.flock(self._spidev, fcntl.LOCK_UN)
|
|
|
|
SPI_LOCK.release()
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
self._spidev.close()
|
|
|
|
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2023-01-29 08:01:54 +08:00
|
|
|
class PandaSpiHandle(BaseHandle):
|
2023-01-28 16:32:07 +08:00
|
|
|
"""
|
|
|
|
A class that mimics a libusb1 handle for panda SPI communications.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self.dev = SpiDevice()
|
2022-11-04 13:18:12 +08:00
|
|
|
|
|
|
|
# helpers
|
2022-11-11 13:34:43 +08:00
|
|
|
def _calc_checksum(self, data: List[int]) -> int:
|
|
|
|
cksum = CHECKSUM_START
|
|
|
|
for b in data:
|
|
|
|
cksum ^= b
|
|
|
|
return cksum
|
|
|
|
|
2023-01-28 16:32:07 +08:00
|
|
|
def _wait_for_ack(self, spi, ack_val: int) -> None:
|
2022-12-21 14:07:24 +08:00
|
|
|
start = time.monotonic()
|
|
|
|
while (time.monotonic() - start) < ACK_TIMEOUT_SECONDS:
|
2023-01-28 16:32:07 +08:00
|
|
|
dat = spi.xfer2(b"\x12")[0]
|
2022-12-21 14:07:24 +08:00
|
|
|
if dat == NACK:
|
|
|
|
raise PandaSpiNackResponse
|
|
|
|
elif dat == ack_val:
|
|
|
|
return
|
|
|
|
|
|
|
|
raise PandaSpiMissingAck
|
|
|
|
|
2023-01-28 16:32:07 +08:00
|
|
|
def _transfer(self, spi, endpoint: int, data, max_rx_len: int = 1000) -> bytes:
|
2022-11-11 13:34:43 +08:00
|
|
|
logging.debug("starting transfer: endpoint=%d, max_rx_len=%d", endpoint, max_rx_len)
|
|
|
|
logging.debug("==============================================")
|
|
|
|
|
2023-01-16 15:30:24 +08:00
|
|
|
exc = PandaSpiException()
|
2022-12-21 14:07:24 +08:00
|
|
|
for n in range(MAX_XFER_RETRY_COUNT):
|
2022-11-11 13:34:43 +08:00
|
|
|
logging.debug("\ntry #%d", n+1)
|
2022-11-04 13:18:12 +08:00
|
|
|
try:
|
2022-11-11 13:34:43 +08:00
|
|
|
logging.debug("- send header")
|
2022-11-04 13:18:12 +08:00
|
|
|
packet = struct.pack("<BBHH", SYNC, endpoint, len(data), max_rx_len)
|
|
|
|
packet += bytes([reduce(lambda x, y: x^y, packet) ^ CHECKSUM_START])
|
2023-01-28 16:32:07 +08:00
|
|
|
spi.xfer2(packet)
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-12-21 14:07:24 +08:00
|
|
|
logging.debug("- waiting for header ACK")
|
2023-01-28 16:32:07 +08:00
|
|
|
self._wait_for_ack(spi, HACK)
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-11-11 13:34:43 +08:00
|
|
|
# send data
|
|
|
|
logging.debug("- sending data")
|
|
|
|
packet = bytes([*data, self._calc_checksum(data)])
|
2023-01-28 16:32:07 +08:00
|
|
|
spi.xfer2(packet)
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-12-21 14:07:24 +08:00
|
|
|
logging.debug("- waiting for data ACK")
|
2023-01-28 16:32:07 +08:00
|
|
|
self._wait_for_ack(spi, DACK)
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-11-11 13:34:43 +08:00
|
|
|
# get response length, then response
|
2023-01-28 16:32:07 +08:00
|
|
|
response_len_bytes = bytes(spi.xfer2(b"\x00" * 2))
|
2022-11-11 13:34:43 +08:00
|
|
|
response_len = struct.unpack("<H", response_len_bytes)[0]
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-11-11 13:34:43 +08:00
|
|
|
logging.debug("- receiving response")
|
2023-01-28 16:32:07 +08:00
|
|
|
dat = bytes(spi.xfer2(b"\x00" * (response_len + 1)))
|
2022-11-11 13:34:43 +08:00
|
|
|
if self._calc_checksum([DACK, *response_len_bytes, *dat]) != 0:
|
2022-12-21 14:07:24 +08:00
|
|
|
raise PandaSpiBadChecksum
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-11-11 13:34:43 +08:00
|
|
|
return dat[:-1]
|
2023-01-16 15:30:24 +08:00
|
|
|
except PandaSpiException as e:
|
|
|
|
exc = e
|
|
|
|
logging.debug("SPI transfer failed, %d retries left", n, exc_info=True)
|
|
|
|
raise exc
|
2022-11-04 13:18:12 +08:00
|
|
|
|
|
|
|
# libusb1 functions
|
|
|
|
def close(self):
|
2023-01-28 16:32:07 +08:00
|
|
|
self.dev.close()
|
2022-11-04 13:18:12 +08:00
|
|
|
|
2022-11-11 13:34:43 +08:00
|
|
|
def controlWrite(self, request_type: int, request: int, value: int, index: int, data, timeout: int = 0):
|
2023-01-28 16:32:07 +08:00
|
|
|
with self.dev.acquire() as spi:
|
|
|
|
return self._transfer(spi, 0, struct.pack("<BHHH", request, value, index, 0))
|
2022-11-11 13:34:43 +08:00
|
|
|
|
|
|
|
def controlRead(self, request_type: int, request: int, value: int, index: int, length: int, timeout: int = 0):
|
2023-01-28 16:32:07 +08:00
|
|
|
with self.dev.acquire() as spi:
|
|
|
|
return self._transfer(spi, 0, struct.pack("<BHHH", request, value, index, length))
|
2022-11-11 13:34:43 +08:00
|
|
|
|
|
|
|
# TODO: implement these properly
|
|
|
|
def bulkWrite(self, endpoint: int, data: List[int], timeout: int = 0) -> int:
|
2023-01-28 16:32:07 +08:00
|
|
|
with self.dev.acquire() as spi:
|
2023-01-15 15:32:17 +08:00
|
|
|
for x in range(math.ceil(len(data) / USB_MAX_SIZE)):
|
2023-01-28 16:32:07 +08:00
|
|
|
self._transfer(spi, endpoint, data[USB_MAX_SIZE*x:USB_MAX_SIZE*(x+1)])
|
2023-01-15 15:32:17 +08:00
|
|
|
return len(data)
|
2022-11-11 13:34:43 +08:00
|
|
|
|
|
|
|
def bulkRead(self, endpoint: int, length: int, timeout: int = 0) -> bytes:
|
|
|
|
ret: List[int] = []
|
2023-01-28 16:32:07 +08:00
|
|
|
with self.dev.acquire() as spi:
|
2023-01-15 15:32:17 +08:00
|
|
|
for _ in range(math.ceil(length / USB_MAX_SIZE)):
|
2023-01-28 16:32:07 +08:00
|
|
|
d = self._transfer(spi, endpoint, [], max_rx_len=USB_MAX_SIZE)
|
2023-01-15 15:32:17 +08:00
|
|
|
ret += d
|
|
|
|
if len(d) < USB_MAX_SIZE:
|
|
|
|
break
|
2022-11-11 13:34:43 +08:00
|
|
|
return bytes(ret)
|