Skip to content

Commit

Permalink
trying to implement main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
o-murphy committed Apr 29, 2024
1 parent 92a13f2 commit 9583069
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
31 changes: 19 additions & 12 deletions pydfuutil/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,6 @@ def le16_to_cpu(data):
return int.from_bytes(data, byteorder='little')



class IntOrBytes:
def __init__(self, value):
if isinstance(value, int):
Expand Down Expand Up @@ -621,7 +620,6 @@ def main() -> None:
mode = Mode.NONE
device_id_filter = None


func_dfu_rt = USB_DFU_FUNC_DESCRIPTOR.parse(bytes(USB_DFU_FUNC_DESCRIPTOR.sizeof()))

if args.verbose:
Expand Down Expand Up @@ -770,6 +768,7 @@ def get_first_dfu_if(dif_: dfu.DfuIf, v: Any = None):
# Transition from run-Time mode to DFU mode

if not (_rt_dif.flags & dfu.Mode.IFF_DFU):

# In the 'first round' during runtime mode, there can only be one
# DFU Interface descriptor according to the DFU Spec.

Expand Down Expand Up @@ -797,22 +796,30 @@ def get_first_dfu_if(dif_: dfu.DfuIf, v: Any = None):
if not quirks & QUIRK_POLLTIMEOUT:
milli_sleep(status.bwPollTimeout)

if status.bState in [dfu.State.APP_IDLE, dfu.State.APP_DETACH]:
if status.bState in (dfu.State.APP_IDLE, dfu.State.APP_DETACH):
print("Device really in Runtime Mode, send DFU "
"detach request...")

ret = dfu.detach(_rt_dif.dev, _rt_dif.interface, 1000)
print(ret)
if IntOrBytes(ret) < 0:
if IntOrBytes(dfu.detach(_rt_dif.dev, _rt_dif.interface, 1000)) < 0:
print("error detaching")
exit(1)

# print(bmAttributes.USB_DFU_WILL_DETACH.__dir__())
# print(func_dfu_rt.bmAttributes)
# # if func_dfu_rt.bmAttributes & bmAttributes.USB_DFU_WILL_DETACH:
# # print('continue')


if func_dfu_rt.bmAttributes & bmAttributes.USB_DFU_WILL_DETACH:
print("Device will detach and reattach...")
else:
print("Resetting USB...\n")
try:
_rt_dif.dev.reset()
except usb.core.USBError as exc:
print("error resetting after detach")
milli_sleep(2000)
elif status.bState == dfu.State.DFU_ERROR:
print("dfuERROR, clearing status")
if IntOrBytes(dfu.clear_status(_rt_dif.dev, _rt_dif.interface)) < 0:
print("error detaching")
exit(1)
else:
print("WARNING: Runtime device already in DFU state ?!?")



Expand Down
27 changes: 14 additions & 13 deletions pydfuutil/usb_dfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@

USB_DT_DFU = 0x21

# class bmAttributes(IntEnum):
# USB_DFU_CAN_DOWNLOAD = 0x1
# USB_DFU_CAN_UPLOAD = 0x2
# USB_DFU_MANIFEST_TOL = 0x3
# USB_DFU_WILL_DETACH = 0x4
class bmAttributes(IntEnum):
USB_DFU_CAN_DOWNLOAD = 0x1
USB_DFU_CAN_UPLOAD = 0x2
USB_DFU_MANIFEST_TOL = 0x3
USB_DFU_WILL_DETACH = 0x4

bmAttributes = FlagsEnum(
Byte,
USB_DFU_CAN_DOWNLOAD=0x1, # is support updates
USB_DFU_CAN_UPLOAD=0x2, # is prog warranty ok
USB_DFU_MANIFEST_TOL=0x4,
USB_DFU_WILL_DETACH=0x8,
)
# bmAttributes = FlagsEnum(
# Byte,
# USB_DFU_CAN_DOWNLOAD=0x1, # is support updates
# USB_DFU_CAN_UPLOAD=0x2, # is prog warranty ok
# USB_DFU_MANIFEST_TOL=0x4,
# USB_DFU_WILL_DETACH=0x8,
# )

USB_DFU_FUNC_DESCRIPTOR = Struct(
bLength=Int8ul,
bDescriptorType=Int8ul,
bmAttributes=bmAttributes,
# bmAttributes=bmAttributes,
bmAttributes=Int8ul,
wDetachTimeOut=Int16ul,
wTransferSize=Int16ul,
bcdDFUVersion=Int16ul,
Expand Down

0 comments on commit 9583069

Please sign in to comment.