diff options
| author | Sebastian Kaim <sebb@sebb767.de> | 2017-10-10 22:52:53 +0200 |
|---|---|---|
| committer | Jack Humbert <jack.humb@gmail.com> | 2017-10-13 05:37:19 -1000 |
| commit | 598cb8265538b5950868790b7f1b33603c2e2d3b (patch) | |
| tree | 44ec1e9a1b1b52b8e373376af0acdbd6070e33e2 | |
| parent | 74f51009a820edb29ba6d3d3154252683736c44c (diff) | |
| download | qmk_firmware-598cb8265538b5950868790b7f1b33603c2e2d3b.tar.gz qmk_firmware-598cb8265538b5950868790b7f1b33603c2e2d3b.zip | |
Extended the programming script for the ps2avrGB keyboard series:
- a keyboard already in bootloader mode will now be detected
- if setting the keyboard to bootloader mode doesn't work, a hint will be printed on how to do so
- instead of failing instantly when no keyboard is found, the script will now wait up to 60 seconds (it retries every 5 seconds, up to 12 times)
| -rwxr-xr-x | keyboards/ps2avrGB/program | 110 |
1 files changed, 70 insertions, 40 deletions
diff --git a/keyboards/ps2avrGB/program b/keyboards/ps2avrGB/program index a88d9cd9b..8593ae0bf 100755 --- a/keyboards/ps2avrGB/program +++ b/keyboards/ps2avrGB/program | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | #!/usr/bin/env python | 1 | #!/usr/bin/env python |
| 2 | # Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com> | 2 | # Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>, Sebastian Kaim <sebb@sebb767.de> |
| 3 | # | 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify | 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 | 5 | # it under the terms of the GNU General Public License as published by |
| @@ -21,54 +21,84 @@ import sys | |||
| 21 | import time | 21 | import time |
| 22 | import usb | 22 | import usb |
| 23 | 23 | ||
| 24 | if len(sys.argv) < 2: | ||
| 25 | print('Usage: %s <firmware.hex>' % sys.argv[0]) | ||
| 26 | sys.exit(1) | ||
| 27 | 24 | ||
| 28 | print('Searching for ps2avrGB... ', end='') | 25 | def checkForKeyboardInNormalMode(): |
| 26 | """Returns a device if a ps2avrGB device in normal made (that is in keyboard mode) or None if it is not found.""" | ||
| 27 | return usb.core.find(idVendor=0x20A0, idProduct=0x422D) | ||
| 28 | |||
| 29 | def checkForKeyboardInBootloaderMode(): | ||
| 30 | """Returns True if a ps2avrGB device in bootloader (flashable) mode is found and False otherwise.""" | ||
| 31 | return (usb.core.find(idVendor=0x16c0, idProduct=0x05df) is not None) | ||
| 32 | |||
| 33 | def flashKeyboard(firmware_file): | ||
| 34 | """Calls bootloadHID to flash the given file to the device.""" | ||
| 35 | print('Flashing firmware to device ...') | ||
| 36 | if os.system('bootloadHID -r "%s"' % firmware_file) == 0: | ||
| 37 | print('\nDone!') | ||
| 38 | else: | ||
| 39 | print('\nbootloadHID returned an error.') | ||
| 40 | |||
| 41 | def printDeviceInfo(dev): | ||
| 42 | """Prints all infos for a given USB device""" | ||
| 43 | print('Device Information:') | ||
| 44 | print(' idVendor: %d (0x%04x)' % (dev.idVendor, dev.idVendor)) | ||
| 45 | print(' idProduct: %d (0x%04x)' % (dev.idProduct, dev.idProduct)) | ||
| 46 | print('Manufacturer: %s' % (dev.iManufacturer)) | ||
| 47 | print('Serial: %s' % (dev.iSerialNumber)) | ||
| 48 | print('Product: %s' % (dev.iProduct), end='\n\n') | ||
| 29 | 49 | ||
| 30 | dev = usb.core.find(idVendor=0x20A0, idProduct=0x422D) | 50 | def sendDeviceToBootloaderMode(dev): |
| 31 | if dev is None: | 51 | """Tries to send a given ps2avrGB keyboard to bootloader mode to allow flashing.""" |
| 32 | raise ValueError('Device not found') | 52 | try: |
| 53 | dev.set_configuration() | ||
| 33 | 54 | ||
| 34 | print('Found', end='\n\n') | 55 | request_type = usb.util.build_request_type( |
| 56 | usb.util.CTRL_OUT, | ||
| 57 | usb.util.CTRL_TYPE_CLASS, | ||
| 58 | usb.util.CTRL_RECIPIENT_DEVICE) | ||
| 35 | 59 | ||
| 36 | print('Device Information:') | 60 | USBRQ_HID_SET_REPORT = 0x09 |
| 37 | print(' idVendor: %d (0x%04x)' % (dev.idVendor, dev.idVendor)) | 61 | HID_REPORT_OPTION = 0x0301 |
| 38 | print(' idProduct: %d (0x%04x)' % (dev.idProduct, dev.idProduct)) | 62 | |
| 39 | print('Manufacturer: %s' % (dev.iManufacturer)) | 63 | dev.ctrl_transfer(request_type, USBRQ_HID_SET_REPORT, HID_REPORT_OPTION, 0, [0, 0, 0xFF] + [0] * 5) |
| 40 | print('Serial: %s' % (dev.iSerialNumber)) | 64 | except usb.core.USBError: |
| 41 | print('Product: %s' % (dev.iProduct), end='\n\n') | 65 | # for some reason I keep getting USBError, but it works! |
| 66 | pass | ||
| 67 | |||
| 68 | |||
| 69 | if len(sys.argv) < 2: | ||
| 70 | print('Usage: %s <firmware.hex>' % sys.argv[0]) | ||
| 71 | sys.exit(1) | ||
| 42 | 72 | ||
| 43 | print('Transferring control to bootloader... ', end='') | 73 | kb = checkForKeyboardInNormalMode() |
| 44 | 74 | ||
| 45 | dev.set_configuration() | 75 | if kb is not None: |
| 76 | print('Found a keyboad in normal mode. Attempting to send it to bootloader mode ...', end='') | ||
| 77 | sendDeviceToBootloaderMode(kb) | ||
| 78 | print(' done.') | ||
| 79 | print("Hint: If your keyboard can't be set to bootloader mode automatically, plug it in while pressing left control to do so manually.") | ||
| 46 | 80 | ||
| 47 | request_type = usb.util.build_request_type( | 81 | attempts = 12 # 60 seconds |
| 48 | usb.util.CTRL_OUT, | 82 | found = False |
| 49 | usb.util.CTRL_TYPE_CLASS, | 83 | for attempt in range(1, attempts + 1): |
| 50 | usb.util.CTRL_RECIPIENT_DEVICE) | 84 | print("Searching for keyboard in bootloader mode (%i/%i) ... " % (attempt, attempts), end='') |
| 51 | 85 | ||
| 52 | USBRQ_HID_SET_REPORT = 0x09 | 86 | if checkForKeyboardInBootloaderMode(): |
| 53 | HID_REPORT_OPTION = 0x0301 | 87 | print('Found', end='\n\n') |
| 88 | flashKeyboard(sys.argv[1]) | ||
| 89 | found = True | ||
| 90 | break | ||
| 91 | else: | ||
| 92 | print('Nothing.', end='') | ||
| 54 | 93 | ||
| 94 | if attempt != attempts: # no need to wait on the last attempt | ||
| 95 | print(' Sleeping 5 seconds.', end='') | ||
| 96 | time.sleep(5) | ||
| 55 | 97 | ||
| 56 | try: | 98 | # print a newline |
| 57 | dev.ctrl_transfer( | 99 | print() |
| 58 | request_type, | ||
| 59 | USBRQ_HID_SET_REPORT, | ||
| 60 | HID_REPORT_OPTION, | ||
| 61 | 0, | ||
| 62 | [0, 0, 0xFF] + [0] * 5 | ||
| 63 | ) | ||
| 64 | except usb.core.USBError: | ||
| 65 | # for some reason I keep getting USBError, but it works! | ||
| 66 | pass | ||
| 67 | 100 | ||
| 68 | # wait a bit until bootloader starts up | 101 | if not found: |
| 69 | time.sleep(2) | 102 | print("Couldn't find a flashable keyboard. Aborting.") |
| 103 | sys.exit(2) | ||
| 70 | 104 | ||
| 71 | print('OK') | ||
| 72 | print('Programming...') | ||
| 73 | if os.system('bootloadHID -r "%s"' % sys.argv[1]) == 0: | ||
| 74 | print('\nDone!') | ||
