diff --git a/README.md b/README.md index 4e4ae28d..622460d2 100755 --- a/README.md +++ b/README.md @@ -366,6 +366,7 @@ The arguments for *compile* are: * `--library` to compile the code as a [static .a/.ar library](#compiling-static-libraries). * `--config` to inspect the runtime compile configuration (see below). * `-S` or `--supported` shows a matrix of the supported targets and toolchains. +* `-f` or `--flash` to flash/program a connected target after successful compile. * `-c ` to build from scratch, a clean build or rebuild. * `-j ` to control the compile processes on your machine. The default value is 0, which infers the number of processes from the number of cores on your machine. You can use `-j 1` to trigger a sequential compile of source code. * `-v` or `--verbose` for verbose diagnostic output. diff --git a/mbed/mbed.py b/mbed/mbed.py index 20b733da..2ade3b09 100644 --- a/mbed/mbed.py +++ b/mbed/mbed.py @@ -1400,16 +1400,9 @@ def get_target(self, target=None): target = target if target else target_cfg if target and (target.lower() == 'detect' or target.lower() == 'auto'): - targets = self.get_detected_targets() - if targets == False: - error("The target detection requires that the 'mbed-ls' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-ls'.") - elif len(targets) > 1: - error("Multiple targets were detected.\nOnly 1 target board should be connected to your system when you use the '-m auto' switch.") - elif len(targets) == 0: - error("No targets were detected.\nPlease make sure a target board is connected to this system.") - else: - action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial'])) - target = targets[0]['name'] + detected = self.detect_target() + if detected: + target = detected['name'] if target is None: 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): except IOError: error("Unable to write build ignore file in \"%s\"" % os.path.join(build_path, '.mbedignore'), 1) + def detect_target(self, info=None): + targets = self.get_detected_targets() + if targets == False: + 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) + elif len(targets) > 1: + error("Multiple targets were detected.\nOnly 1 target board should be connected to your system.", 1) + elif len(targets) == 0: + error("No targets were detected.\nPlease make sure a target board is connected to this system.", 1) + else: + action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial'])) + info = {'msd': targets[0]['mount'], 'port': targets[0]['serial'], 'name': targets[0]['name']} + + if info is None: + error("The detected target doesn't support Mass Storage Device capability (MSD)", 1) + return info + def get_detected_targets(self): targets = [] try: @@ -2137,12 +2146,13 @@ def status_(ignore=False): dict(name='--source', action='append', help='Source directory. Default: . (current dir)'), dict(name='--build', help='Build directory. Default: build/'), dict(name=['-c', '--clean'], action='store_true', help='Clean the build directory before compiling'), + dict(name=['-f', '--flash'], action='store_true', help='Flash the built firmware onto a connected target.'), dict(name=['-N', '--artifact-name'], help='Name of the built program or library'), dict(name=['-S', '--supported'], dest='supported', action='store_true', help='Shows supported matrix of targets and toolchains'), dict(name='--app-config', dest="app_config", help="Path of an app configuration file (Default is to look for 'mbed_app.json')"), help='Compile code using the mbed build tools', description=("Compile this program using the mbed build tools.")) -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): +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): # Gather remaining arguments args = remainder # Find the root of the program @@ -2221,6 +2231,25 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False, + (['-v'] if verbose else []) + args, env=env) + + if flash: + fw_name = artifact_name if artifact_name else program.name + fw_fbase = os.path.join(build_path, fw_name) + fw_file = fw_fbase + ('.hex' if os.path.exists(fw_fbase+'.hex') else '.bin') + if not os.path.exists(fw_file): + error("Build program file (firmware) not found \"%s\"" % fw_file, 1) + detected = program.detect_target() + + try: + from mbed_host_tests.host_tests_toolbox import flash_dev, reset_dev + except (IOError, ImportError, OSError): + error("The '-f/--flash' option requires that the 'mbed-greentea' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-greentea'.", 1) + + if not flash_dev(detected['msd'], fw_file, program_cycle_s=2): + error("Unable to flash the target board connected to your system.", 1) + + if not reset_dev(detected['port']): + error("Unable to reset the target board connected to your system.\nThis might be caused by an old interface firmware.\nPlease check the board page for new firmware.", 1) program.set_defaults(target=target, toolchain=tchain)