Skip to content

Commit a92d7da

Browse files
authored
Merge pull request #717 from screamerbg/f/target-flashing
Fix: Firmware flashing routine
2 parents e955ad0 + 0711f94 commit a92d7da

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

mbed/mbed.py

+28-15
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ def get_target(self, target=None):
15831583
target = target if target else target_cfg
15841584

15851585
if target and (target.lower() == 'detect' or target.lower() == 'auto'):
1586-
detected = self.detect_target()
1586+
detected = self.detect_single_target()
15871587
if detected:
15881588
target = detected['name']
15891589

@@ -1623,7 +1623,7 @@ def ignore_build_dir(self):
16231623
except IOError:
16241624
error("Unable to write build ignore file in \"%s\"" % os.path.join(build_path, '.mbedignore'), 1)
16251625

1626-
def detect_target(self, info=None):
1626+
def detect_single_target(self, info=None):
16271627
targets = self.get_detected_targets()
16281628
if targets == False:
16291629
error("The target detection requires that the 'mbed-ls' python module is installed.\nYou can install mbed-ls by running \"pip install mbed-ls\".", 1)
@@ -1633,7 +1633,7 @@ def detect_target(self, info=None):
16331633
error("No targets were detected.\nPlease make sure a target board is connected to this system.", 1)
16341634
else:
16351635
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1636-
info = {'msd': targets[0]['mount'], 'port': targets[0]['serial'], 'name': targets[0]['name']}
1636+
info = targets[0]
16371637

16381638
if info is None:
16391639
error("The detected target doesn't support Mass Storage Device capability (MSD)", 1)
@@ -2532,23 +2532,36 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
25322532
env=env)
25332533

25342534
if flash or sterm:
2535-
detected = program.detect_target()
25362535
try:
25372536
from mbed_host_tests.host_tests_toolbox import flash_dev
25382537
except (IOError, ImportError, OSError):
25392538
error("The '-f/--flash' option requires that the 'mbed-greentea' python module is installed.\nYou can install mbed-greentea by running \"%s -m pip install mbed-greentea\"." % python_cmd, 1)
25402539

2541-
if flash:
2542-
fw_name = artifact_name if artifact_name else program.name
2543-
fw_fbase = os.path.join(build_path, fw_name)
2544-
fw_file = fw_fbase + ('.hex' if os.path.exists(fw_fbase+'.hex') else '.bin')
2545-
if not os.path.exists(fw_file):
2546-
error("Build program file (firmware) not found \"%s\"" % fw_file, 1)
2547-
if not flash_dev(detected['msd'], fw_file, program_cycle_s=4):
2548-
error("Unable to flash the target board connected to your system.", 1)
2549-
2550-
if flash or sterm:
2551-
mbed_sterm(detected['port'], reset=flash, sterm=sterm)
2540+
connected = False
2541+
targets = program.get_detected_targets()
2542+
if targets:
2543+
for _target in targets:
2544+
if _target['name'] is None:
2545+
continue
2546+
elif _target['name'].upper() == target.upper():
2547+
connected = _target
2548+
2549+
# apply new firmware
2550+
if flash:
2551+
fw_name = artifact_name if artifact_name else program.name
2552+
fw_fbase = os.path.join(build_path, fw_name)
2553+
fw_file = fw_fbase + ('.hex' if os.path.exists(fw_fbase+'.hex') else '.bin')
2554+
if not os.path.exists(fw_file):
2555+
error("Build program file (firmware) not found \"%s\"" % fw_file, 1)
2556+
if not flash_dev(connected['mount'], fw_file, program_cycle_s=4):
2557+
error("Unable to flash the target board connected to your system.", 1)
2558+
2559+
# reset board and/or connect to serial port
2560+
if flash or sterm:
2561+
mbed_sterm(connected['serial'], reset=flash, sterm=sterm)
2562+
2563+
if not connected:
2564+
error("The target board you compiled for is not connected to your system.\nPlease reconnect it and retry the last command.", 1)
25522565

25532566
program.set_defaults(target=target, toolchain=tchain)
25542567

0 commit comments

Comments
 (0)