Skip to content

Commit 9630304

Browse files
committed
Added feature support for -f/--flash to mbed compile to flash (and reset) the target after successful compile
1 parent de681c9 commit 9630304

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

mbed/mbed.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1400,16 +1400,9 @@ def get_target(self, target=None):
14001400
target = target if target else target_cfg
14011401

14021402
if target and (target.lower() == 'detect' or target.lower() == 'auto'):
1403-
targets = self.get_detected_targets()
1404-
if targets == False:
1405-
error("The target detection requires that the 'mbed-ls' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-ls'.")
1406-
elif len(targets) > 1:
1407-
error("Multiple targets were detected.\nOnly 1 target board should be connected to your system when you use the '-m auto' switch.")
1408-
elif len(targets) == 0:
1409-
error("No targets were detected.\nPlease make sure a target board is connected to this system.")
1410-
else:
1411-
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1412-
target = targets[0]['name']
1403+
detected = self.detect_target()
1404+
if detected:
1405+
target = detected['name']
14131406

14141407
if target is None:
14151408
error("Please specify target using the -m switch or set default target using command 'mbed target'", 1)
@@ -1447,6 +1440,22 @@ def ignore_build_dir(self):
14471440
except IOError:
14481441
error("Unable to write build ignore file in \"%s\"" % os.path.join(build_path, '.mbedignore'), 1)
14491442

1443+
def detect_target(self, info=None):
1444+
targets = self.get_detected_targets()
1445+
if targets == False:
1446+
error("The target detection requires that the 'mbed-ls' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-ls'.")
1447+
elif len(targets) > 1:
1448+
error("Multiple targets were detected.\nOnly 1 target board should be connected to your system.")
1449+
elif len(targets) == 0:
1450+
error("No targets were detected.\nPlease make sure a target board is connected to this system.")
1451+
else:
1452+
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1453+
info = {'msd': targets[0]['mount'], 'port': targets[0]['serial'], 'name': targets[0]['name']}
1454+
1455+
if info is None:
1456+
error("The detected target doesn't support Mass Storage Device capability (MSD)", 1)
1457+
return info
1458+
14501459
def get_detected_targets(self):
14511460
targets = []
14521461
try:
@@ -2137,12 +2146,13 @@ def status_(ignore=False):
21372146
dict(name='--source', action='append', help='Source directory. Default: . (current dir)'),
21382147
dict(name='--build', help='Build directory. Default: build/'),
21392148
dict(name=['-c', '--clean'], action='store_true', help='Clean the build directory before compiling'),
2149+
dict(name=['-f', '--flash'], action='store_true', help='Flash the built firmware onto a connected target.'),
21402150
dict(name=['-N', '--artifact-name'], help='Name of the built program or library'),
21412151
dict(name=['-S', '--supported'], dest='supported', action='store_true', help='Shows supported matrix of targets and toolchains'),
21422152
dict(name='--app-config', dest="app_config", help="Path of an app configuration file (Default is to look for 'mbed_app.json')"),
21432153
help='Compile code using the mbed build tools',
21442154
description=("Compile this program using the mbed build tools."))
2145-
def compile_(toolchain=None, target=None, profile=False, compile_library=False, compile_config=False, config_prefix=None, source=False, build=False, clean=False, artifact_name=None, supported=False, app_config=None):
2155+
def compile_(toolchain=None, target=None, profile=False, compile_library=False, compile_config=False, config_prefix=None, source=False, build=False, clean=False, flash=False, artifact_name=None, supported=False, app_config=None):
21462156
# Gather remaining arguments
21472157
args = remainder
21482158
# Find the root of the program
@@ -2221,6 +2231,23 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
22212231
+ (['-v'] if verbose else [])
22222232
+ args,
22232233
env=env)
2234+
2235+
if flash:
2236+
fw_name = artifact_name if artifact_name else program.name
2237+
fw_fbase = os.path.join(build_path, fw_name)
2238+
fw_file = fw_fbase + ('.hex' if os.path.exists(fw_fbase+'.hex') else '.bin')
2239+
if not fw_file:
2240+
error("Firmware file not found \"%s\"" % fw_file)
2241+
detected = program.detect_target()
2242+
2243+
try:
2244+
from mbed_host_tests.host_tests_toolbox import flash_dev, reset_dev
2245+
except (IOError, ImportError, OSError):
2246+
error("The target programing requires that the 'mbed-greentea' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-greentea'.")
2247+
return False
2248+
2249+
flash_dev(detected['msd'], fw_file, program_cycle_s=0)
2250+
reset_dev(detected['port'])
22242251

22252252
program.set_defaults(target=target, toolchain=tchain)
22262253

0 commit comments

Comments
 (0)