Files
panda-meb/python/dfu.py

119 lines
3.9 KiB
Python
Raw Normal View History

2017-08-23 12:49:56 -07:00
import usb1
import struct
import binascii
2017-08-23 12:49:56 -07:00
from .constants import McuType
2017-08-23 12:49:56 -07:00
# *** DFU mode ***
2017-08-23 12:49:56 -07:00
DFU_DNLOAD = 1
DFU_UPLOAD = 2
DFU_GETSTATUS = 3
DFU_CLRSTATUS = 4
DFU_ABORT = 6
class PandaDFU:
2017-08-23 12:49:56 -07:00
def __init__(self, dfu_serial):
self._handle = None
2017-08-23 12:49:56 -07:00
context = usb1.USBContext()
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)
2017-08-23 12:49:56 -07:00
except Exception:
continue
if this_dfu_serial == dfu_serial or dfu_serial is None:
2017-08-23 12:49:56 -07:00
self._handle = device.open()
self._mcu_type = self.get_mcu_type(device)
break
if self._handle is None:
raise Exception(f"failed to open DFU device {dfu_serial}")
2017-08-23 12:49:56 -07:00
@staticmethod
def list():
context = usb1.USBContext()
dfu_serials = []
2017-09-12 20:53:40 -07:00
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))
2017-09-12 20:53:40 -07:00
except Exception:
pass
except Exception:
pass
2017-08-23 12:49:56 -07:00
return dfu_serials
@staticmethod
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 == 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")
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
2017-08-23 12:49:56 -07:00
def status(self):
while 1:
dat = self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
if dat[1] == 0:
2017-08-23 12:49:56 -07:00
break
def clear_status(self):
# Clear status
stat = self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6)
if stat[4] == 0xa:
2017-08-23 12:49:56 -07:00
self._handle.controlRead(0x21, DFU_CLRSTATUS, 0, 0, 0)
elif stat[4] == 0x9:
self._handle.controlWrite(0x21, DFU_ABORT, 0, 0, b"")
2017-08-23 12:49:56 -07:00
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))
2017-08-23 12:49:56 -07:00
self.status()
def program(self, address, dat, block_size=None):
if block_size is None:
2017-08-23 12:49:56 -07:00
block_size = len(dat)
2017-08-23 12:49:56 -07:00
# Set Address Pointer
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", address))
2017-08-23 12:49:56 -07:00
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]
2017-08-23 12:49:56 -07:00
print("programming %d with length %d" % (i, len(ldat)))
self._handle.controlWrite(0x21, DFU_DNLOAD, 2 + i, 0, ldat)
2017-08-23 12:49:56 -07:00
self.status()
2017-08-28 10:42:23 -07:00
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)
2017-08-28 10:42:23 -07:00
self.reset()
def recover(self):
with open(self._mcu_type.config.bootstub_path, "rb") as f:
2017-08-28 10:42:23 -07:00
code = f.read()
self.program_bootstub(code)
2017-08-23 12:49:56 -07:00
def reset(self):
self._handle.controlWrite(0x21, DFU_DNLOAD, 0, 0, b"\x21" + struct.pack("I", self._mcu_type.config.bootstub_address))
2017-08-23 12:49:56 -07:00
self.status()
try:
self._handle.controlWrite(0x21, DFU_DNLOAD, 2, 0, b"")
_ = str(self._handle.controlRead(0x21, DFU_GETSTATUS, 0, 0, 6))
2017-08-23 12:49:56 -07:00
except Exception:
pass