Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hansfbaier/b75252a5d73a9789478a7b2ae1066516 to your computer and use it in GitHub Desktop.
Save hansfbaier/b75252a5d73a9789478a7b2ae1066516 to your computer and use it in GitHub Desktop.
Attempt to put CH569 into debug mode
#!/usr/bin/env python3
import sys
import usb.core
import usb.util
from time import sleep
# find our device
dev = usb.core.find(idVendor=0x4348, idProduct=0x55e0)
if not dev:
print("Could not find device!")
sys.exit()
dev.set_configuration()
# get an endpoint instance
cfg = dev.get_active_configuration()
intf = cfg[(0,0)]
ep_out = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_OUT)
ep_in = usb.util.find_descriptor(
intf,
# match the first OUT endpoint
custom_match = \
lambda e: \
usb.util.endpoint_direction(e.bEndpointAddress) == \
usb.util.ENDPOINT_IN)
assert ep_out is not None
assert ep_in is not None
def to_bytes(bytestr: str):
return bytes([int('0x' + b, 16) for b in bytestr.split(' ')])
def to_str(bytes):
return " ".join(["{:02x}".format(b) for b in bytes])
initreq1 = b'\xa1\x12\x00B\x10MCU ISP & WCH.CN'
initreq2 = "a7 02 00 1f 00"
req1 = b'\xa1\x12\x00B\x10MCU ISP & W'
req4 = req2 = "a7 02 00 1f 00"
req3 = "a8 0e 00 07 00 11 bf f9 f7 13 bf f9 ec e5 f2 ff 8f"
req5 = "a2 01 00 01"
for i in range(6):
ep_out.write(initreq1)
resp = to_str(ep_in.read(6))
print(resp)
assert resp == "a1 00 02 00 69 10"
device_id = "27 7e 3b 26 3b 38 9d dc"
for i in range(4):
ep_out.write(initreq2)
resp = to_str(ep_in.read(30))
print(resp)
should_be = "a7 00 1a 00 1f 00 11 bf f9 f7 13 bf f9 ec 45 f2 ff 8f 00 02 07 00 " + device_id
if resp == should_be:
break
ep_out.write(req1)
sleep(0.004)
resp1 = to_str(ep_in.read(6))
print(str(resp1))
assert resp1.startswith("a1 00 02 00")
ep_out.write(to_bytes(req2))
resp2 = to_str(ep_in.read(30))
print(resp2)
assert resp2.startswith("a7 00 1a 00 1f 00 11 bf f9 f7 13 bf f9 ec")
assert resp2.endswith("f2 ff 8f 00 02 07 00 27 7e 3b 26 3b 38 9d dc")
ep_out.write(to_bytes(req3))
resp3 = to_str(ep_in.read(6, 0))
print(resp3)
assert resp3 == "a8 00 02 00 00 00"
sleep(0.004)
ep_out.write(to_bytes(req4))
resp4 = to_str(ep_in.read(30))
print(resp4)
assert resp4.startswith("a7 00 1a 00 1f 00 11 bf f9 f7 13 bf f9 ec")
assert resp4.endswith("f2 ff 8f 00 02 07 00 27 7e 3b 26 3b 38 9d dc")
ep_out.write(to_bytes(req5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment