diff options
| author | zvecr <git@zvecr.com> | 2019-02-22 20:50:58 +0000 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-04-12 14:04:40 -0700 |
| commit | 0e88d756f92d1254c58e66f9538c07b68ded489c (patch) | |
| tree | 1fd11473cbf0f4acd288221be45462f87516eda0 /util | |
| parent | a9a5fd754ed30a3d9453c77b5f622898848d3042 (diff) | |
| download | qmk_firmware-0e88d756f92d1254c58e66f9538c07b68ded489c.tar.gz qmk_firmware-0e88d756f92d1254c58e66f9538c07b68ded489c.zip | |
Add vendor and product arguments to atmega32a_program.py
Diffstat (limited to 'util')
| -rwxr-xr-x | util/atmega32a_program.py | 43 |
1 files changed, 24 insertions, 19 deletions
diff --git a/util/atmega32a_program.py b/util/atmega32a_program.py index b777b9110..324614aff 100755 --- a/util/atmega32a_program.py +++ b/util/atmega32a_program.py | |||
| @@ -19,18 +19,19 @@ from __future__ import print_function | |||
| 19 | import os | 19 | import os |
| 20 | import sys | 20 | import sys |
| 21 | import time | 21 | import time |
| 22 | import argparse | ||
| 22 | import usb | 23 | import usb |
| 23 | 24 | ||
| 24 | 25 | ||
| 25 | def checkForKeyboardInNormalMode(): | 26 | def check_keyboard_normal_mode(vendor, product): |
| 26 | """Returns a device if a ps2avrGB device in normal made (that is in keyboard mode) or None if it is not found.""" | 27 | """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 | return usb.core.find(idVendor=vendor, idProduct=product) |
| 28 | 29 | ||
| 29 | def checkForKeyboardInBootloaderMode(): | 30 | def check_keyboard_bootloader_mode(): |
| 30 | """Returns True if a ps2avrGB device in bootloader (flashable) mode is found and False otherwise.""" | 31 | """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 | return (usb.core.find(idVendor=0x16c0, idProduct=0x05df) is not None) |
| 32 | 33 | ||
| 33 | def flashKeyboard(firmware_file): | 34 | def flash_keyboard(firmware_file): |
| 34 | """Calls bootloadHID to flash the given file to the device.""" | 35 | """Calls bootloadHID to flash the given file to the device.""" |
| 35 | print('Flashing firmware to device ...') | 36 | print('Flashing firmware to device ...') |
| 36 | if os.system('bootloadHID -r "%s"' % firmware_file) == 0: | 37 | if os.system('bootloadHID -r "%s"' % firmware_file) == 0: |
| @@ -38,24 +39,24 @@ def flashKeyboard(firmware_file): | |||
| 38 | else: | 39 | else: |
| 39 | print('\nbootloadHID returned an error.') | 40 | print('\nbootloadHID returned an error.') |
| 40 | 41 | ||
| 41 | def printDeviceInfo(dev): | 42 | def print_device_info(dev): |
| 42 | """Prints all infos for a given USB device""" | 43 | """Prints all infos for a given USB device""" |
| 43 | print('Device Information:') | 44 | print('Device Information:') |
| 44 | print(' idVendor: %d (0x%04x)' % (dev.idVendor, dev.idVendor)) | 45 | print(' idVendor: %d (0x%02x)' % (dev.idVendor, dev.idVendor)) |
| 45 | print(' idProduct: %d (0x%04x)' % (dev.idProduct, dev.idProduct)) | 46 | print(' idProduct: %d (0x%02x)' % (dev.idProduct, dev.idProduct)) |
| 46 | print('Manufacturer: %s' % (dev.iManufacturer)) | 47 | print('Manufacturer: %s' % (dev.iManufacturer)) |
| 47 | print('Serial: %s' % (dev.iSerialNumber)) | 48 | print('Serial: %s' % (dev.iSerialNumber)) |
| 48 | print('Product: %s' % (dev.iProduct), end='\n\n') | 49 | print('Product: %s' % (dev.iProduct), end='\n\n') |
| 49 | 50 | ||
| 50 | def sendDeviceToBootloaderMode(dev): | 51 | def send_device_to_bootloader_mode(dev): |
| 51 | """Tries to send a given ps2avrGB keyboard to bootloader mode to allow flashing.""" | 52 | """Tries to send a given ps2avrGB keyboard to bootloader mode to allow flashing.""" |
| 52 | try: | 53 | try: |
| 53 | dev.set_configuration() | 54 | dev.set_configuration() |
| 54 | 55 | ||
| 55 | request_type = usb.util.build_request_type( | 56 | request_type = usb.util.build_request_type( |
| 56 | usb.util.CTRL_OUT, | 57 | usb.util.CTRL_OUT, |
| 57 | usb.util.CTRL_TYPE_CLASS, | 58 | usb.util.CTRL_TYPE_CLASS, |
| 58 | usb.util.CTRL_RECIPIENT_DEVICE) | 59 | usb.util.CTRL_RECIPIENT_DEVICE) |
| 59 | 60 | ||
| 60 | USBRQ_HID_SET_REPORT = 0x09 | 61 | USBRQ_HID_SET_REPORT = 0x09 |
| 61 | HID_REPORT_OPTION = 0x0301 | 62 | HID_REPORT_OPTION = 0x0301 |
| @@ -65,16 +66,21 @@ def sendDeviceToBootloaderMode(dev): | |||
| 65 | # for some reason I keep getting USBError, but it works! | 66 | # for some reason I keep getting USBError, but it works! |
| 66 | pass | 67 | pass |
| 67 | 68 | ||
| 69 | def auto_int(value): | ||
| 70 | """Helper for argparse to enable auto base detection""" | ||
| 71 | return int(value, 0) | ||
| 68 | 72 | ||
| 69 | if len(sys.argv) < 2: | 73 | parser = argparse.ArgumentParser(description='Flash bootloadHID device') |
| 70 | print('Usage: %s <firmware.hex>' % sys.argv[0]) | 74 | parser.add_argument('--vendor', type=auto_int, default=0x20A0, help='Non bootloader idVendor to search for (default: 0x%(default)02x)') |
| 71 | sys.exit(1) | 75 | parser.add_argument('--product', type=auto_int, default=0x422D, help='Non bootloader idProduct to search for (default: 0x%(default)02x)') |
| 76 | parser.add_argument('firmware_hex', type=argparse.FileType('r'), help='Firmware hex file to flash') | ||
| 77 | args = parser.parse_args() | ||
| 72 | 78 | ||
| 73 | kb = checkForKeyboardInNormalMode() | 79 | kb = check_keyboard_normal_mode(args.vendor, args.product) |
| 74 | 80 | ||
| 75 | if kb is not None: | 81 | if kb is not None: |
| 76 | print('Found a keyboard in normal mode. Attempting to send it to bootloader mode ...', end='') | 82 | print('Found a keyboard in normal mode. Attempting to send it to bootloader mode ...', end='') |
| 77 | sendDeviceToBootloaderMode(kb) | 83 | send_device_to_bootloader_mode(kb) |
| 78 | print(' done.') | 84 | print(' done.') |
| 79 | print("Hint: If your keyboard can't be set to bootloader mode automatically, plug it in while pressing the bootloader key to do so manually.") | 85 | print("Hint: If your keyboard can't be set to bootloader mode automatically, plug it in while pressing the bootloader key to do so manually.") |
| 80 | print(" You can find more infos about this here: https://github.com/qmk/qmk_firmware/tree/master/keyboards/ps2avrGB#setting-the-board-to-bootloader-mode") | 86 | print(" You can find more infos about this here: https://github.com/qmk/qmk_firmware/tree/master/keyboards/ps2avrGB#setting-the-board-to-bootloader-mode") |
| @@ -84,9 +90,9 @@ found = False | |||
| 84 | for attempt in range(1, attempts + 1): | 90 | for attempt in range(1, attempts + 1): |
| 85 | print("Searching for keyboard in bootloader mode (%i/%i) ... " % (attempt, attempts), end='') | 91 | print("Searching for keyboard in bootloader mode (%i/%i) ... " % (attempt, attempts), end='') |
| 86 | 92 | ||
| 87 | if checkForKeyboardInBootloaderMode(): | 93 | if check_keyboard_bootloader_mode(): |
| 88 | print('Found', end='\n\n') | 94 | print('Found', end='\n\n') |
| 89 | flashKeyboard(sys.argv[1]) | 95 | flash_keyboard(args.firmware_hex.name) |
| 90 | found = True | 96 | found = True |
| 91 | break | 97 | break |
| 92 | else: | 98 | else: |
| @@ -102,4 +108,3 @@ for attempt in range(1, attempts + 1): | |||
| 102 | if not found: | 108 | if not found: |
| 103 | print("Couldn't find a flashable keyboard. Aborting.") | 109 | print("Couldn't find a flashable keyboard. Aborting.") |
| 104 | sys.exit(2) | 110 | sys.exit(2) |
| 105 | |||
