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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

hid_bootloader_loader.py 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. """
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. """
  7. """
  8. Front-end programmer for the LUFA HID class bootloader.
  9. Usage:
  10. python hid_bootloader_loader.py <Device> <Input>.hex
  11. Example:
  12. python hid_bootloader_loader.py at90usb1287 Mouse.hex
  13. Requires the pywinusb (https://pypi.python.org/pypi/pywinusb/) and
  14. IntelHex (http://bialix.com/intelhex/) libraries.
  15. """
  16. import sys
  17. from pywinusb import hid
  18. from intelhex import IntelHex
  19. # Device information table
  20. device_info_map = dict()
  21. device_info_map['at90usb1287'] = {'page_size': 256, 'flash_kb': 128}
  22. device_info_map['at90usb1286'] = {'page_size': 256, 'flash_kb': 128}
  23. device_info_map['at90usb647'] = {'page_size': 256, 'flash_kb': 64}
  24. device_info_map['at90usb646'] = {'page_size': 256, 'flash_kb': 64}
  25. device_info_map['atmega32u4'] = {'page_size': 128, 'flash_kb': 32}
  26. device_info_map['atmega32u2'] = {'page_size': 128, 'flash_kb': 32}
  27. device_info_map['atmega16u4'] = {'page_size': 128, 'flash_kb': 16}
  28. device_info_map['atmega16u2'] = {'page_size': 128, 'flash_kb': 16}
  29. device_info_map['at90usb162'] = {'page_size': 128, 'flash_kb': 16}
  30. device_info_map['atmega8u2'] = {'page_size': 128, 'flash_kb': 8}
  31. device_info_map['at90usb82'] = {'page_size': 128, 'flash_kb': 8}
  32. def get_hid_device_handle():
  33. hid_device_filter = hid.HidDeviceFilter(vendor_id=0x03EB,
  34. product_id=0x2067)
  35. valid_hid_devices = hid_device_filter.get_devices()
  36. if len(valid_hid_devices) is 0:
  37. return None
  38. else:
  39. return valid_hid_devices[0]
  40. def send_page_data(hid_device, address, data):
  41. # Bootloader page data should be the HID Report ID (always zero) followed
  42. # by the starting address to program, then one device's flash page worth
  43. # of data
  44. output_report_data = [0]
  45. output_report_data.extend([address & 0xFF, address >> 8])
  46. output_report_data.extend(data)
  47. hid_device.send_output_report(output_report_data)
  48. def program_device(hex_data, device_info):
  49. hid_device = get_hid_device_handle()
  50. if hid_device is None:
  51. print("No valid HID device found.")
  52. sys.exit(1)
  53. try:
  54. hid_device.open()
  55. print("Connected to bootloader.")
  56. # Program in all data from the loaded HEX file, in a number of device
  57. # page sized chunks
  58. for addr in range(0, hex_data.maxaddr(), device_info['page_size']):
  59. # Compute the address range of the current page in the device
  60. current_page_range = range(addr, addr+device_info['page_size'])
  61. # Extract the data from the hex file at the specified start page
  62. # address and convert it to a regular list of bytes
  63. page_data = [hex_data[i] for i in current_page_range]
  64. print("Writing address 0x%04X-0x%04X" % (current_page_range[0], current_page_range[-1]))
  65. # Devices with more than 64KB of flash should shift down the page
  66. # address so that it is 16-bit (page size is guaranteed to be
  67. # >= 256 bytes so no non-zero address bits are discarded)
  68. if device_info['flash_kb'] < 64:
  69. send_page_data(hid_device, addr, page_data)
  70. else:
  71. send_page_data(hid_device, addr >> 8, page_data)
  72. # Once programming is complete, start the application via a dummy page
  73. # program to the page address 0xFFFF
  74. print("Programming complete, starting application.")
  75. send_page_data(hid_device, 0xFFFF, [0] * device_info['page_size'])
  76. finally:
  77. hid_device.close()
  78. if __name__ == '__main__':
  79. # Load the specified HEX file
  80. try:
  81. hex_data = IntelHex(sys.argv[2])
  82. except:
  83. print("Could not open the specified HEX file.")
  84. sys.exit(1)
  85. # Retrieve the device information entry for the specified device
  86. try:
  87. device_info = device_info_map[sys.argv[1]]
  88. except:
  89. print("Unknown device name specified.")
  90. sys.exit(1)
  91. program_device(hex_data, device_info)