mirror of
https://github.com/infiniteCable2/panda.git
synced 2026-02-19 01:33:52 +08:00
cleanup MCU definitions (#1226)
* cleanup MCU definitions * rename * enum * enum * fix that
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
import usb1
|
||||
import struct
|
||||
import binascii
|
||||
from .config import BOOTSTUB_ADDRESS, APP_ADDRESS_H7, APP_ADDRESS_FX, BLOCK_SIZE_H7, BLOCK_SIZE_FX, DEFAULT_H7_BOOTSTUB_FN, DEFAULT_BOOTSTUB_FN
|
||||
|
||||
from .constants import McuType
|
||||
|
||||
MCU_TYPE_F2 = 0
|
||||
MCU_TYPE_F4 = 1
|
||||
MCU_TYPE_H7 = 2
|
||||
|
||||
# *** DFU mode ***
|
||||
DFU_DNLOAD = 1
|
||||
@@ -15,8 +12,9 @@ DFU_GETSTATUS = 3
|
||||
DFU_CLRSTATUS = 4
|
||||
DFU_ABORT = 6
|
||||
|
||||
class PandaDFU(object):
|
||||
class PandaDFU:
|
||||
def __init__(self, dfu_serial):
|
||||
self._handle = None
|
||||
context = usb1.USBContext()
|
||||
for device in context.getDeviceList(skip_on_error=True):
|
||||
if device.getVendorID() == 0x0483 and device.getProductID() == 0xdf11:
|
||||
@@ -25,10 +23,12 @@ class PandaDFU(object):
|
||||
except Exception:
|
||||
continue
|
||||
if this_dfu_serial == dfu_serial or dfu_serial is None:
|
||||
self._mcu_type = self.get_mcu_type(device)
|
||||
self._handle = device.open()
|
||||
return
|
||||
raise Exception("failed to open " + dfu_serial if dfu_serial is not None else "DFU device")
|
||||
self._mcu_type = self.get_mcu_type(device)
|
||||
break
|
||||
|
||||
if self._handle is None:
|
||||
raise Exception(f"failed to open DFU device {dfu_serial}")
|
||||
|
||||
@staticmethod
|
||||
def list():
|
||||
@@ -46,18 +46,19 @@ class PandaDFU(object):
|
||||
return dfu_serials
|
||||
|
||||
@staticmethod
|
||||
def st_serial_to_dfu_serial(st, mcu_type=MCU_TYPE_F4):
|
||||
def st_serial_to_dfu_serial(st, mcu_type=McuType.F4):
|
||||
if st is None or st == "none":
|
||||
return None
|
||||
uid_base = struct.unpack("H" * 6, bytes.fromhex(st))
|
||||
if mcu_type == MCU_TYPE_H7:
|
||||
if mcu_type == McuType.H7:
|
||||
return binascii.hexlify(struct.pack("!HHH", uid_base[1] + uid_base[5], uid_base[0] + uid_base[4], uid_base[3])).upper().decode("utf-8")
|
||||
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")
|
||||
|
||||
# TODO: Find a way to detect F4 vs F2
|
||||
def get_mcu_type(self, dev):
|
||||
return MCU_TYPE_H7 if dev.getbcdDevice() == 512 else MCU_TYPE_F4
|
||||
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 status(self):
|
||||
while 1:
|
||||
@@ -97,26 +98,18 @@ class PandaDFU(object):
|
||||
|
||||
def program_bootstub(self, code_bootstub):
|
||||
self.clear_status()
|
||||
self.erase(BOOTSTUB_ADDRESS)
|
||||
if self._mcu_type == MCU_TYPE_H7:
|
||||
self.erase(APP_ADDRESS_H7)
|
||||
self.program(BOOTSTUB_ADDRESS, code_bootstub, BLOCK_SIZE_H7)
|
||||
else:
|
||||
self.erase(APP_ADDRESS_FX)
|
||||
self.program(BOOTSTUB_ADDRESS, code_bootstub, BLOCK_SIZE_FX)
|
||||
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.reset()
|
||||
|
||||
def recover(self):
|
||||
fn = DEFAULT_H7_BOOTSTUB_FN if self._mcu_type == MCU_TYPE_H7 else DEFAULT_BOOTSTUB_FN
|
||||
|
||||
with open(fn, "rb") as f:
|
||||
with open(self._mcu_type.config.bootstub_path, "rb") as f:
|
||||
code = f.read()
|
||||
|
||||
self.program_bootstub(code)
|
||||
|
||||
def reset(self):
|
||||
# **** Reset ****
|
||||
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", BOOTSTUB_ADDRESS))
|
||||
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"")
|
||||
|
||||
Reference in New Issue
Block a user