diff --git a/Tools/px_uploader.py b/Tools/px_uploader.py index 88cae2428d12..68159db06837 100755 --- a/Tools/px_uploader.py +++ b/Tools/px_uploader.py @@ -74,6 +74,9 @@ else: runningPython3 = True +class FirmwareNotSuitableException(Exception): + def __init__(self, message): + super(FirmwareNotSuitableException, self).__init__(message) class firmware(object): '''Loads a firmware file''' @@ -567,13 +570,13 @@ def upload(self, fw, force=False, boot_delay=None): # Make sure we are doing the right thing start = time.time() if self.board_type != fw.property('board_id'): - msg = "Firmware not suitable for this board (board_type=%u board_id=%u)" % ( + msg = "Firmware not suitable for this board (Firmware board_type=%u board_id=%u)" % ( self.board_type, fw.property('board_id')) print("WARNING: %s" % msg) if force: print("FORCED WRITE, FLASHING ANYWAY!") else: - raise IOError(msg) + raise FirmwareNotSuitableException(msg) # Prevent uploads where the image would overflow the flash if self.fw_maxsize < fw.property('image_size'): @@ -798,6 +801,7 @@ def main(): baud_flightstack = [int(x) for x in args.baud_flightstack.split(',')] successful = False + unsuitable_board = False for port in portlist: # print("Trying %s" % port) @@ -828,7 +832,7 @@ def main(): continue found_bootloader = False - while (True): + while True: up.open() # port is open, try talking to it @@ -869,6 +873,11 @@ def main(): # print the error print("\nERROR: %s" % ex.args) + except FirmwareNotSuitableException: + unsuitable_board = True + up.close() + continue + except IOError: up.close() continue @@ -883,6 +892,12 @@ def main(): else: sys.exit(1) + if unsuitable_board: + # If we land here, we went through all ports, did not flash any + # board and found at least one unsuitable board. + # Exit with 2, so a caller can distinguish from other errors + sys.exit(2) + # Delay retries to < 20 Hz to prevent spin-lock from hogging the CPU time.sleep(0.05)