hid_bootloader_loader.py 4.3 KB

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