You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.1 KiB

  1. #!/usr/bin/env python
  2. # Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>
  3. #
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation, either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. from __future__ import print_function
  17. import os
  18. import sys
  19. import time
  20. import usb
  21. if len(sys.argv) < 2:
  22. print('Usage: %s <firmware.hex>' % sys.argv[0])
  23. sys.exit(1)
  24. print('Searching for ps2avrGB... ', end='')
  25. dev = usb.core.find(idVendor=0x20A0, idProduct=0x422D)
  26. if dev is None:
  27. raise ValueError('Device not found')
  28. print('Found', end='\n\n')
  29. print('Device Information:')
  30. print(' idVendor: %d (0x%04x)' % (dev.idVendor, dev.idVendor))
  31. print(' idProduct: %d (0x%04x)' % (dev.idProduct, dev.idProduct))
  32. print('Manufacturer: %s' % (dev.iManufacturer))
  33. print('Serial: %s' % (dev.iSerialNumber))
  34. print('Product: %s' % (dev.iProduct), end='\n\n')
  35. print('Transferring control to bootloader... ', end='')
  36. dev.set_configuration()
  37. request_type = usb.util.build_request_type(
  38. usb.util.CTRL_OUT,
  39. usb.util.CTRL_TYPE_CLASS,
  40. usb.util.CTRL_RECIPIENT_DEVICE)
  41. USBRQ_HID_SET_REPORT = 0x09
  42. HID_REPORT_OPTION = 0x0301
  43. try:
  44. dev.ctrl_transfer(
  45. request_type,
  46. USBRQ_HID_SET_REPORT,
  47. HID_REPORT_OPTION,
  48. 0,
  49. [0, 0, 0xFF] + [0] * 5
  50. )
  51. except usb.core.USBError:
  52. # for some reason I keep getting USBError, but it works!
  53. pass
  54. # wait a bit until bootloader starts up
  55. time.sleep(2)
  56. print('OK')
  57. print('Programming...')
  58. if os.system('bootloadHID -r "%s"' % sys.argv[1]) == 0:
  59. print('\nDone!')