diff options
author | tmk <hasu@tmk-kbd.com> | 2015-05-13 11:13:10 +0900 |
---|---|---|
committer | tmk <hasu@tmk-kbd.com> | 2015-05-13 11:13:10 +0900 |
commit | f6d56675f9f981c5464f0ca7a1fbb0162154e8c5 (patch) | |
tree | 57c9d4b3808a26116ae0ee7956fc00b84841aa2b /protocol/lufa/LUFA-git/Bootloaders/HID/HostLoaderApp_Python | |
parent | 4d116a04e94cf0d19317d5b44e4fa9f34a3e5594 (diff) |
Squashed 'tmk_core/' changes from caca2c0..dc0e46e
dc0e46e Rename LUFA to LUFA-git
3bfa7fa Remove LUFA-120730
215b764 Merge commit 'afa0f22a9299686fd88f58ce09c5b521ac917e8f' as 'protocol/lufa/LUFA'
afa0f22 Squashed 'protocol/lufa/LUFA/' content from commit def7fca
c0c42fa Remove submodule of LUFA
30f897d Merge commit '87ced33feb74e79c3281dda36eb6d6d153399b41' as 'protocol/usb_hid/USB_Host_Shield_2.0'
87ced33 Squashed 'protocol/usb_hid/USB_Host_Shield_2.0/' content from commit aab4a69
14f6d49 Remove submodule of USB_Host_Shield_2.0
git-subtree-dir: tmk_core
git-subtree-split: dc0e46eaa4367d4e218f8816e3c117895820f07c
Diffstat (limited to 'protocol/lufa/LUFA-git/Bootloaders/HID/HostLoaderApp_Python')
m--------- | protocol/lufa/LUFA-git | 0 | ||||
-rw-r--r-- | protocol/lufa/LUFA-git/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py | 120 |
2 files changed, 120 insertions, 0 deletions
diff --git a/protocol/lufa/LUFA-git b/protocol/lufa/LUFA-git deleted file mode 160000 -Subproject b6c18b2a7c544653efbe12a1d4e8ba65e7d83c3 diff --git a/protocol/lufa/LUFA-git/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py b/protocol/lufa/LUFA-git/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py new file mode 100644 index 0000000000..2598fde605 --- /dev/null +++ b/protocol/lufa/LUFA-git/Bootloaders/HID/HostLoaderApp_Python/hid_bootloader_loader.py @@ -0,0 +1,120 @@ +""" + LUFA Library + Copyright (C) Dean Camera, 2014. + + dean [at] fourwalledcubicle [dot] com + www.lufa-lib.org +""" + +""" + Front-end programmer for the LUFA HID class bootloader. + + Usage: + python hid_bootloader_loader.py <Device> <Input>.hex + + Example: + python hid_bootloader_loader.py at90usb1287 Mouse.hex + + Requires the pywinusb (https://pypi.python.org/pypi/pywinusb/) and + IntelHex (http://bialix.com/intelhex/) libraries. +""" + +import sys +from pywinusb import hid +from intelhex import IntelHex + + +# Device information table +device_info_map = dict() +device_info_map['at90usb1287'] = {'page_size': 256, 'flash_kb': 128} +device_info_map['at90usb1286'] = {'page_size': 256, 'flash_kb': 128} +device_info_map['at90usb647'] = {'page_size': 256, 'flash_kb': 64} +device_info_map['at90usb646'] = {'page_size': 256, 'flash_kb': 64} +device_info_map['atmega32u4'] = {'page_size': 128, 'flash_kb': 32} +device_info_map['atmega32u2'] = {'page_size': 128, 'flash_kb': 32} +device_info_map['atmega16u4'] = {'page_size': 128, 'flash_kb': 16} +device_info_map['atmega16u2'] = {'page_size': 128, 'flash_kb': 16} +device_info_map['at90usb162'] = {'page_size': 128, 'flash_kb': 16} +device_info_map['atmega8u2'] = {'page_size': 128, 'flash_kb': 8} +device_info_map['at90usb82'] = {'page_size': 128, 'flash_kb': 8} + + +def get_hid_device_handle(): + hid_device_filter = hid.HidDeviceFilter(vendor_id=0x03EB, + product_id=0x2067) + + valid_hid_devices = hid_device_filter.get_devices() + + if len(valid_hid_devices) is 0: + return None + else: + return valid_hid_devices[0] + + +def send_page_data(hid_device, address, data): + # Bootloader page data should be the HID Report ID (always zero) followed + # by the starting address to program, then one device's flash page worth + # of data + output_report_data = [0] + output_report_data.extend([address & 0xFF, address >> 8]) + output_report_data.extend(data) + + hid_device.send_output_report(output_report_data) + + +def program_device(hex_data, device_info): + hid_device = get_hid_device_handle() + + if hid_device is None: + print("No valid HID device found.") + sys.exit(1) + + try: + hid_device.open() + print("Connected to bootloader.") + + # Program in all data from the loaded HEX file, in a number of device + # page sized chunks + for addr in range(0, hex_data.maxaddr(), device_info['page_size']): + # Compute the address range of the current page in the device + current_page_range = range(addr, addr+device_info['page_size']) + + # Extract the data from the hex file at the specified start page + # address and convert it to a regular list of bytes + page_data = [hex_data[i] for i in current_page_range] + + print("Writing address 0x%04X-0x%04X" % (current_page_range[0], current_page_range[-1])) + + # Devices with more than 64KB of flash should shift down the page + # address so that it is 16-bit (page size is guaranteed to be + # >= 256 bytes so no non-zero address bits are discarded) + if device_info['flash_kb'] < 64: + send_page_data(hid_device, addr, page_data) + else: + send_page_data(hid_device, addr >> 8, page_data) + + # Once programming is complete, start the application via a dummy page + # program to the page address 0xFFFF + print("Programming complete, starting application.") + send_page_data(hid_device, 0xFFFF, [0] * device_info['page_size']) + + finally: + hid_device.close() + + +if __name__ == '__main__': + # Load the specified HEX file + try: + hex_data = IntelHex(sys.argv[2]) + except: + print("Could not open the specified HEX file.") + sys.exit(1) + + # Retrieve the device information entry for the specified device + try: + device_info = device_info_map[sys.argv[1]] + except: + print("Unknown device name specified.") + sys.exit(1) + + program_device(hex_data, device_info) |