Skip to content

Commit

Permalink
Tools/px_uploader: exit if unsuitable board is connected
Browse files Browse the repository at this point in the history
Avoids endless looping, and is mostly useful when used in automated
upload scripts.
  • Loading branch information
bkueng authored and julianoes committed Apr 20, 2020
1 parent 6e18cb8 commit c48c1c4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Tools/px_uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'''
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -828,7 +832,7 @@ def main():
continue

found_bootloader = False
while (True):
while True:
up.open()

# port is open, try talking to it
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit c48c1c4

Please sign in to comment.