SPI: connect by serial/UID (#1213)

* SPI: connect by serial/UID

* cleanup

* not everyone can have spi

* fix that

* move that

* mypy fix

Co-authored-by: Comma Device <device@comma.ai>
This commit is contained in:
Adeeb Shihadeh
2023-01-15 23:30:24 -08:00
committed by GitHub
parent b8693da342
commit 3048760737
3 changed files with 42 additions and 29 deletions

View File

@@ -7,7 +7,6 @@ import struct
import hashlib
import binascii
import datetime
import traceback
import warnings
import logging
from functools import wraps
@@ -17,7 +16,7 @@ from itertools import accumulate
from .config import DEFAULT_FW_FN, DEFAULT_H7_FW_FN, SECTOR_SIZES_FX, SECTOR_SIZES_H7
from .dfu import PandaDFU, MCU_TYPE_F2, MCU_TYPE_F4, MCU_TYPE_H7
from .isotp import isotp_send, isotp_recv
from .spi import SpiHandle
from .spi import SpiHandle, PandaSpiException
__version__ = '0.0.10'
@@ -227,7 +226,7 @@ class Panda:
FLAG_GM_HW_CAM = 1
FLAG_GM_HW_CAM_LONG = 2
def __init__(self, serial: Optional[str] = None, claim: bool = True, spi: bool = False, disable_checks: bool = True):
def __init__(self, serial: Optional[str] = None, claim: bool = True, disable_checks: bool = True):
self._serial = serial
self._disable_checks = disable_checks
@@ -237,7 +236,6 @@ class Panda:
self.can_rx_overflow_buffer = b''
# connect and set mcu type
self._spi = spi
self.connect(claim)
# reset comms
@@ -258,15 +256,10 @@ class Panda:
self.close()
self._handle = None
if self._spi:
self._handle = SpiHandle()
# TODO implement
self._serial = "SPIDEV"
self.bootstub = False
else:
self.usb_connect(claim=claim, wait=wait)
# try USB first, then SPI
self.usb_connect(claim=claim, wait=wait)
if self._handle is None:
self.spi_connect()
assert self._handle is not None
self._mcu_type = self.get_mcu_type()
@@ -278,6 +271,23 @@ class Panda:
self.set_heartbeat_disabled()
self.set_power_save(0)
def spi_connect(self):
# get UID to confirm slave is present and up
spi_serial = None
try:
self._handle = SpiHandle()
spi_serial = self.get_uid()
except PandaSpiException:
pass
if spi_serial is not None and ((self._serial is None) or (self._serial == spi_serial)):
self._serial = spi_serial
# TODO: detect this
self.bootstub = False
else:
# failed to connect
self._handle = None
def usb_connect(self, claim=True, wait=False):
context = usb1.USBContext()
while 1:
@@ -288,9 +298,10 @@ class Panda:
this_serial = device.getSerialNumber()
except Exception:
continue
if self._serial is None or this_serial == self._serial:
self._serial = this_serial
print("opening device", self._serial, hex(device.getProductID()))
logging.debug("opening device %s %s", this_serial, hex(device.getProductID()))
self.bootstub = device.getProductID() == 0xddee
self._handle = device.open()
if sys.platform not in ("win32", "cygwin", "msys", "darwin"):
@@ -305,9 +316,8 @@ class Panda:
self._bcd_device = bytearray([bcd >> 8, ])
break
except Exception as e:
print("exception", e)
traceback.print_exc()
except Exception:
logging.exception("USB connect error")
if not wait or self._handle is not None:
break
context = usb1.USBContext() # New context needed so new devices show up
@@ -349,8 +359,6 @@ class Panda:
if not success:
raise Exception("reconnect failed")
@staticmethod
def flash_static(handle, code, mcu_type):
assert mcu_type is not None, "must set valid mcu_type to flash"