def usb_open(devicename, is_update=False, mode_check=True): def print_update_instructions(usb): print("To perform an Update:") if not usb.jumperless_update: print(" - Disconnect from USB") print(" - Install the Update Jumper at pins %s" % ("RXI-TXO" if usb.hw_model != 1 else "DCLK-GND")) print(" - Reconnect to USB") print(" - Run \"gw update\" to install firmware v%u.%u" % (version.major, version.minor)) if devicename is None: devicename = find_port() usb = USB.Unit(serial.Serial(devicename)) usb.port_info = port_info(devicename) is_win7 = (platform.system() == 'Windows' and platform.release() == '7') usb.jumperless_update = ((usb.hw_model, usb.hw_submodel) != (1, 0) and not is_win7) if not mode_check: return usb if usb.update_mode and not is_update: if usb.jumperless_update and not usb.update_jumpered: usb = usb_reopen(usb, is_update) if not usb.update_mode: return usb print("ERROR: Greaseweazle is in Firmware Update Mode") print(" - The only available action is \"gw update\"") if usb.update_jumpered: print(" - For normal operation disconnect from USB and remove " "the Update Jumper at pins %s" % ("RXI-TXO" if usb.hw_model != 1 else "DCLK-GND")) else: print(" - Main firmware is erased: You *must* perform an update!") sys.exit(1) if is_update and not usb.update_mode: if usb.jumperless_update: usb = usb_reopen(usb, is_update) error.check( usb.update_mode, """\ Greaseweazle F7 did not change to Firmware Update Mode as requested. If the problem persists, install the Update Jumper at pins RXI-TXO.""") return usb print("ERROR: Greaseweazle is not in Firmware Update Mode") print_update_instructions(usb) sys.exit(1) if not usb.update_mode and usb.update_needed: print("ERROR: Greaseweazle firmware v%u.%u is unsupported" % (usb.major, usb.minor)) print_update_instructions(usb) sys.exit(1) return usb
def usb_open(devicename, is_update=False, mode_check=True): if devicename is None: devicename = find_port() usb = USB.Unit(serial.Serial(devicename)) usb.port_info = port_info(devicename) if not mode_check: return usb print("** %s v%u.%u [F%u], Host Tools v%u.%u" % (("Greaseweazle", "Bootloader")[usb.update_mode], usb.major, usb.minor, usb.hw_model, version.major, version.minor)) if usb.update_mode and not is_update: if usb.hw_model == 7 and not usb.update_jumpered: usb = usb_reopen(usb, is_update) if not usb.update_mode: return usb print("Greaseweazle is in Firmware Update Mode:") print(" The only available action is \"update\" of main firmware") if usb.update_jumpered: print(" Remove the Update Jumper for normal operation") else: print(" Main firmware is erased: You *must* perform an update!") sys.exit(1) if is_update and not usb.update_mode: if usb.hw_model == 7: usb = usb_reopen(usb, is_update) error.check( usb.update_mode, """\ Greaseweazle F7 did not change to Firmware Update Mode as requested. If the problem persists, install the Update Jumper (across RX/TX).""") return usb print("Greaseweazle is in Normal Mode:") print(" To \"update\" you must install the Update Jumper") sys.exit(1) if not usb.update_mode and usb.update_needed: print("Firmware is out of date: Require v%u.%u" % (version.major, version.minor)) if usb.hw_model == 7: print("Run \"update <update_file>\"") else: print("Install the Update Jumper and \"update <update_file>\"") sys.exit(1) return usb
def usb_open(devicename, is_update=False, mode_check=True): if devicename is None: devicename = find_port() usb = USB.Unit(serial.Serial(devicename)) usb.port_info = port_info(devicename) is_win7 = (platform.system() == 'Windows' and platform.release() == '7') usb.jumperless_update = ((usb.hw_model, usb.hw_submodel) != (1, 0) and not is_win7) usb.can_mode_switch = (usb.jumperless_update and not (usb.update_mode and usb.update_jumpered)) if mode_check: usb = usb_mode_check(usb, is_update) return usb
def usb_open(devicename, is_update=False): if devicename == "auto": devicename = find_port() usb = USB.Unit(serial.Serial(devicename)) usb.port_info = port_info(devicename) print("** %s v%u.%u [F%u], Host Tools v%u.%u" % (("Greaseweazle", "Bootloader")[usb.update_mode], usb.major, usb.minor, usb.hw_type, version.major, version.minor)) if usb.update_mode and not is_update: if usb.hw_type == 7 and not usb.update_jumpered: usb = usb_reopen(usb, is_update) if not usb.update_mode: return usb print("Greaseweazle is in Firmware Update Mode:") print(" The only available action is \"update <update_file>\"") if usb.update_jumpered: print(" Remove the Update Jumper for normal operation") else: print(" Main firmware is erased: You *must* perform an update!") sys.exit(1) if is_update and not usb.update_mode: if usb.hw_type == 7: return usb_reopen(usb, is_update) print("Greaseweazle is in Normal Mode:") print(" To \"update\" you must install the Update Jumper") sys.exit(1) if not usb.update_mode and usb.update_needed: print("Firmware is out of date: Require v%u.%u" % (version.major, version.minor)) if usb.hw_type == 7: print("Run \"update <update_file>\"") else: print("Install the Update Jumper and \"update <update_file>\"") sys.exit(1) return usb
def usb_reopen(usb, is_update): mode = {False: 1, True: 0} try: usb.switch_fw_mode(mode[is_update]) except (serial.SerialException, struct.error): # Mac and Linux raise SerialException ("... returned no data") # Win10 pyserial returns a short read which fails struct.unpack pass usb.ser.close() for i in range(10): time.sleep(0.5) try: devicename = find_port(usb.port_info) new_ser = serial.Serial(devicename) except serial.SerialException: # Device not found pass else: new_usb = USB.Unit(new_ser) new_usb.port_info = port_info(devicename) return new_usb raise serial.SerialException('Could not reopen port after mode switch')