aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Kaim <sebb@sebb767.de>2017-10-10 22:52:53 +0200
committerJack Humbert <jack.humb@gmail.com>2017-10-13 05:37:19 -1000
commit598cb8265538b5950868790b7f1b33603c2e2d3b (patch)
tree44ec1e9a1b1b52b8e373376af0acdbd6070e33e2
parent74f51009a820edb29ba6d3d3154252683736c44c (diff)
downloadqmk_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-xkeyboards/ps2avrGB/program110
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
21import time 21import time
22import usb 22import usb
23 23
24if len(sys.argv) < 2:
25 print('Usage: %s <firmware.hex>' % sys.argv[0])
26 sys.exit(1)
27 24
28print('Searching for ps2avrGB... ', end='') 25def 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
29def 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
33def 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
41def 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
30dev = usb.core.find(idVendor=0x20A0, idProduct=0x422D) 50def sendDeviceToBootloaderMode(dev):
31if 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
34print('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
36print('Device Information:') 60 USBRQ_HID_SET_REPORT = 0x09
37print(' idVendor: %d (0x%04x)' % (dev.idVendor, dev.idVendor)) 61 HID_REPORT_OPTION = 0x0301
38print(' idProduct: %d (0x%04x)' % (dev.idProduct, dev.idProduct)) 62
39print('Manufacturer: %s' % (dev.iManufacturer)) 63 dev.ctrl_transfer(request_type, USBRQ_HID_SET_REPORT, HID_REPORT_OPTION, 0, [0, 0, 0xFF] + [0] * 5)
40print('Serial: %s' % (dev.iSerialNumber)) 64 except usb.core.USBError:
41print('Product: %s' % (dev.iProduct), end='\n\n') 65 # for some reason I keep getting USBError, but it works!
66 pass
67
68
69if len(sys.argv) < 2:
70 print('Usage: %s <firmware.hex>' % sys.argv[0])
71 sys.exit(1)
42 72
43print('Transferring control to bootloader... ', end='') 73kb = checkForKeyboardInNormalMode()
44 74
45dev.set_configuration() 75if 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
47request_type = usb.util.build_request_type( 81attempts = 12 # 60 seconds
48 usb.util.CTRL_OUT, 82found = False
49 usb.util.CTRL_TYPE_CLASS, 83for 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
52USBRQ_HID_SET_REPORT = 0x09 86 if checkForKeyboardInBootloaderMode():
53HID_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
56try: 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 )
64except 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 101if not found:
69time.sleep(2) 102 print("Couldn't find a flashable keyboard. Aborting.")
103 sys.exit(2)
70 104
71print('OK')
72print('Programming...')
73if os.system('bootloadHID -r "%s"' % sys.argv[1]) == 0:
74 print('\nDone!')