util.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # greaseweazle/tools/util.py
  2. #
  3. # Greaseweazle control script: Utility functions.
  4. #
  5. # Written & released by Keir Fraser <keir.xen@gmail.com>
  6. #
  7. # This is free and unencumbered software released into the public domain.
  8. # See the file COPYING for more details, or visit <http://unlicense.org>.
  9. import os, sys, serial, struct, time
  10. from greaseweazle import version
  11. from greaseweazle import usb as USB
  12. from greaseweazle.image.scp import SCP
  13. from greaseweazle.image.hfe import HFE
  14. def get_image_class(name):
  15. image_types = { '.scp': SCP, '.hfe': HFE }
  16. _, ext = os.path.splitext(name)
  17. if not ext.lower() in image_types:
  18. print("**Error: Unrecognised file suffix '%s'" % ext)
  19. return None
  20. return image_types[ext.lower()]
  21. def with_drive_selected(fn, usb, args):
  22. try:
  23. usb.drive_select(True)
  24. usb.drive_motor(True)
  25. fn(usb, args)
  26. except KeyboardInterrupt:
  27. print()
  28. usb.reset()
  29. usb.ser.close()
  30. usb.ser.open()
  31. finally:
  32. usb.drive_motor(False)
  33. usb.drive_select(False)
  34. def usb_reopen(usb, is_update):
  35. mode = { False: 1, True: 0 }
  36. try:
  37. usb.switch_fw_mode(mode[is_update])
  38. except (serial.SerialException, struct.error):
  39. # Mac and Linux raise SerialException ("... returned no data")
  40. # Win10 pyserial returns a short read which fails struct.unpack
  41. pass
  42. usb.ser.close()
  43. for i in range(10):
  44. time.sleep(0.5)
  45. try:
  46. usb.ser.open()
  47. except serial.SerialException:
  48. # Device not found
  49. pass
  50. else:
  51. return USB.Unit(usb.ser)
  52. raise serial.SerialException('Could not reopen port after mode switch')
  53. def usb_open(devicename, is_update=False):
  54. usb = USB.Unit(serial.Serial(devicename))
  55. print("** %s v%u.%u [F%u], Host Tools v%u.%u"
  56. % (("Greaseweazle", "Bootloader")[usb.update_mode],
  57. usb.major, usb.minor, usb.hw_type,
  58. version.major, version.minor))
  59. if usb.update_mode and not is_update:
  60. if usb.hw_type == 7 and not usb.update_jumpered:
  61. usb = usb_reopen(usb, is_update)
  62. if not usb.update_mode:
  63. return usb
  64. print("Greaseweazle is in Firmware Update Mode:")
  65. print(" The only available action is \"update <update_file>\"")
  66. if usb.update_jumpered:
  67. print(" Remove the Update Jumper for normal operation")
  68. else:
  69. print(" Main firmware is erased: You *must* perform an update!")
  70. sys.exit(1)
  71. if is_update and not usb.update_mode:
  72. if usb.hw_type == 7:
  73. return usb_reopen(usb, is_update)
  74. print("Greaseweazle is in Normal Mode:")
  75. print(" To \"update\" you must install the Update Jumper")
  76. sys.exit(1)
  77. if not usb.update_mode and usb.update_needed:
  78. print("Firmware is out of date: Require v%u.%u"
  79. % (version.major, version.minor))
  80. if usb.hw_type == 7:
  81. print("Run \"update <update_file>\"")
  82. else:
  83. print("Install the Update Jumper and \"update <update_file>\"")
  84. sys.exit(1)
  85. return usb
  86. # Local variables:
  87. # python-indent: 4
  88. # End: