mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-07-20 16:52:10 +08:00
openpilot v0.9.2 release
date: 2023-05-18T22:02:42 master commit: c7d3b28b93faa6c955fb24bc64031512ee985ee9
This commit is contained in:
+74
-73
@@ -1,52 +1,96 @@
|
||||
import usb1
|
||||
import struct
|
||||
import binascii
|
||||
from typing import List, Optional
|
||||
|
||||
from .base import BaseSTBootloaderHandle
|
||||
from .spi import STBootloaderSPIHandle, PandaSpiException
|
||||
from .usb import STBootloaderUSBHandle
|
||||
from .constants import McuType
|
||||
|
||||
|
||||
# *** DFU mode ***
|
||||
DFU_DNLOAD = 1
|
||||
DFU_UPLOAD = 2
|
||||
DFU_GETSTATUS = 3
|
||||
DFU_CLRSTATUS = 4
|
||||
DFU_ABORT = 6
|
||||
|
||||
class PandaDFU:
|
||||
def __init__(self, dfu_serial):
|
||||
self._handle = None
|
||||
def __init__(self, dfu_serial: Optional[str]):
|
||||
# try USB, then SPI
|
||||
handle: Optional[BaseSTBootloaderHandle]
|
||||
handle = PandaDFU.usb_connect(dfu_serial)
|
||||
if handle is None:
|
||||
handle = PandaDFU.spi_connect(dfu_serial)
|
||||
|
||||
if handle is None:
|
||||
raise Exception(f"failed to open DFU device {dfu_serial}")
|
||||
|
||||
self._handle: BaseSTBootloaderHandle = handle
|
||||
self._mcu_type: McuType = self._handle.get_mcu_type()
|
||||
|
||||
@staticmethod
|
||||
def usb_connect(dfu_serial: Optional[str]) -> Optional[STBootloaderUSBHandle]:
|
||||
handle = None
|
||||
context = usb1.USBContext()
|
||||
context.open()
|
||||
for device in context.getDeviceList(skip_on_error=True):
|
||||
if device.getVendorID() == 0x0483 and device.getProductID() == 0xdf11:
|
||||
try:
|
||||
this_dfu_serial = device.open().getASCIIStringDescriptor(3)
|
||||
except Exception:
|
||||
continue
|
||||
|
||||
if this_dfu_serial == dfu_serial or dfu_serial is None:
|
||||
self._handle = device.open()
|
||||
self._mcu_type = self.get_mcu_type(device)
|
||||
handle = STBootloaderUSBHandle(device, device.open())
|
||||
break
|
||||
|
||||
if self._handle is None:
|
||||
raise Exception(f"failed to open DFU device {dfu_serial}")
|
||||
return handle
|
||||
|
||||
@staticmethod
|
||||
def list():
|
||||
context = usb1.USBContext()
|
||||
def spi_connect(dfu_serial: Optional[str]) -> Optional[STBootloaderSPIHandle]:
|
||||
handle = None
|
||||
this_dfu_serial = None
|
||||
|
||||
try:
|
||||
handle = STBootloaderSPIHandle()
|
||||
this_dfu_serial = PandaDFU.st_serial_to_dfu_serial(handle.get_uid(), handle.get_mcu_type())
|
||||
except PandaSpiException:
|
||||
handle = None
|
||||
|
||||
if dfu_serial is not None and dfu_serial != this_dfu_serial:
|
||||
handle = None
|
||||
|
||||
return handle
|
||||
|
||||
@staticmethod
|
||||
def list() -> List[str]:
|
||||
ret = PandaDFU.usb_list()
|
||||
ret += PandaDFU.spi_list()
|
||||
return list(set(ret))
|
||||
|
||||
@staticmethod
|
||||
def usb_list() -> List[str]:
|
||||
dfu_serials = []
|
||||
try:
|
||||
for device in context.getDeviceList(skip_on_error=True):
|
||||
if device.getVendorID() == 0x0483 and device.getProductID() == 0xdf11:
|
||||
try:
|
||||
dfu_serials.append(device.open().getASCIIStringDescriptor(3))
|
||||
except Exception:
|
||||
pass
|
||||
with usb1.USBContext() as context:
|
||||
for device in context.getDeviceList(skip_on_error=True):
|
||||
if device.getVendorID() == 0x0483 and device.getProductID() == 0xdf11:
|
||||
try:
|
||||
dfu_serials.append(device.open().getASCIIStringDescriptor(3))
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
return dfu_serials
|
||||
|
||||
@staticmethod
|
||||
def st_serial_to_dfu_serial(st, mcu_type=McuType.F4):
|
||||
def spi_list() -> List[str]:
|
||||
try:
|
||||
h = PandaDFU.spi_connect(None)
|
||||
if h is not None:
|
||||
dfu_serial = PandaDFU.st_serial_to_dfu_serial(h.get_uid(), h.get_mcu_type())
|
||||
return [dfu_serial, ]
|
||||
except PandaSpiException:
|
||||
pass
|
||||
return []
|
||||
|
||||
@staticmethod
|
||||
def st_serial_to_dfu_serial(st: str, mcu_type: McuType = McuType.F4):
|
||||
if st is None or st == "none":
|
||||
return None
|
||||
uid_base = struct.unpack("H" * 6, bytes.fromhex(st))
|
||||
@@ -55,52 +99,17 @@ class PandaDFU:
|
||||
else:
|
||||
return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4] + 0xA, uid_base[3])).upper().decode("utf-8")
|
||||
|
||||
def get_mcu_type(self, dev) -> McuType:
|
||||
# TODO: Find a way to detect F4 vs F2
|
||||
# TODO: also check F4 BCD, don't assume in else
|
||||
return McuType.H7 if dev.getbcdDevice() == 512 else McuType.F4
|
||||
def get_mcu_type(self) -> McuType:
|
||||
return self._mcu_type
|
||||
|
||||
def status(self):
|
||||
while 1:
|
||||
dat = self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
|
||||
if dat[1] == 0:
|
||||
break
|
||||
|
||||
def clear_status(self):
|
||||
# Clear status
|
||||
stat = self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
|
||||
if stat[4] == 0xa:
|
||||
self._handle.controlRead(0x21, DFU_CLRSTATUS, 0, 0, 0)
|
||||
elif stat[4] == 0x9:
|
||||
self._handle.controlWrite(0x21, DFU_ABORT, 0, 0, b"")
|
||||
self.status()
|
||||
stat = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
|
||||
|
||||
def erase(self, address):
|
||||
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x41" + struct.pack("I", address))
|
||||
self.status()
|
||||
|
||||
def program(self, address, dat, block_size=None):
|
||||
if block_size is None:
|
||||
block_size = len(dat)
|
||||
|
||||
# Set Address Pointer
|
||||
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", address))
|
||||
self.status()
|
||||
|
||||
# Program
|
||||
dat += b"\xFF" * ((block_size - len(dat)) % block_size)
|
||||
for i in range(0, len(dat) // block_size):
|
||||
ldat = dat[i * block_size:(i + 1) * block_size]
|
||||
print("programming %d with length %d" % (i, len(ldat)))
|
||||
self._handle.controlWrite(0x21, DFU_DNLOAD, 2 + i, 0, ldat)
|
||||
self.status()
|
||||
def reset(self):
|
||||
self._handle.jump(self._mcu_type.config.bootstub_address)
|
||||
|
||||
def program_bootstub(self, code_bootstub):
|
||||
self.clear_status()
|
||||
self.erase(self._mcu_type.config.bootstub_address)
|
||||
self.erase(self._mcu_type.config.app_address)
|
||||
self.program(self._mcu_type.config.bootstub_address, code_bootstub, self._mcu_type.config.block_size)
|
||||
self._handle.clear_status()
|
||||
self._handle.erase_bootstub()
|
||||
self._handle.erase_app()
|
||||
self._handle.program(self._mcu_type.config.bootstub_address, code_bootstub)
|
||||
self.reset()
|
||||
|
||||
def recover(self):
|
||||
@@ -108,11 +117,3 @@ class PandaDFU:
|
||||
code = f.read()
|
||||
self.program_bootstub(code)
|
||||
|
||||
def reset(self):
|
||||
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", self._mcu_type.config.bootstub_address))
|
||||
self.status()
|
||||
try:
|
||||
self._handle.controlWrite(0x21, DFU_DNLOAD, 2, 0, b"")
|
||||
_ = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user