Skip to content

Instantly share code, notes, and snippets.

@jhgorse
Created May 29, 2020 21:41
Show Gist options
  • Save jhgorse/5be88151bd71d7bdbfe5171e760ef05c to your computer and use it in GitHub Desktop.
Save jhgorse/5be88151bd71d7bdbfe5171e760ef05c to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# 2016
from hidapi import hidapi
from elftools.elf.elffile import ELFFile
import ctypes, time, sys
# Open device
dev = hidapi.hid_open(0x10C4, 0x63CD, None)
if not dev:
raise RuntimeError('Device not found.')
print 'Found bootloader device, starting flashing.'
# Prepare
if hidapi.hid_send_feature_report(dev, ctypes.c_char_p('\x00\x20'), 2) != 2:
raise RuntimeError('Prepare failed.')
# Flash
while buf:
if hidapi.hid_write(dev, ctypes.c_char_p('\x00' + buf[:64]), 65) != 65:
raise RuntimeError('Writing failed.')
buf = buf[64:]
# Finish
if hidapi.hid_send_feature_report(dev, ctypes.c_char_p('\x00\x21'), 2) != 2:
raise RuntimeError('Finish failed.')
print 'Flashing finished, resetting to runtime.'
# Reset
if hidapi.hid_send_feature_report(dev, ctypes.c_char_p('\x00\x11'), 2) != 2:
raise RuntimeError('Reset failed.')
time.sleep(1)
hidapi.hid_exit()
if hidapi.hid_open(0x10C4, 0x63CD, None):
print 'Done, everything ok.'
else:
print 'Device disappeared.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment