Skip to content

Commit ae86dc3

Browse files
authored
Merge pull request #466 from screamerbg/f/flasher
New Feature: Flash Target Board
2 parents a445a8a + 558a20e commit ae86dc3

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ The arguments for *compile* are:
366366
* `--library` to compile the code as a [static .a/.ar library](#compiling-static-libraries).
367367
* `--config` to inspect the runtime compile configuration (see below).
368368
* `-S` or `--supported` shows a matrix of the supported targets and toolchains.
369+
* `-f` or `--flash` to flash/program a connected target after successful compile.
369370
* `-c ` to build from scratch, a clean build or rebuild.
370371
* `-j <jobs>` 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.
371372
* `-v` or `--verbose` for verbose diagnostic output.

mbed/mbed.py

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,16 +1415,9 @@ def get_target(self, target=None):
14151415
target = target if target else target_cfg
14161416

14171417
if target and (target.lower() == 'detect' or target.lower() == 'auto'):
1418-
targets = self.get_detected_targets()
1419-
if targets == False:
1420-
error("The target detection requires that the 'mbed-ls' python module is installed.\nYou can install mbed-ls by running 'pip install mbed-ls'.")
1421-
elif len(targets) > 1:
1422-
error("Multiple targets were detected.\nOnly 1 target board should be connected to your system when you use the '-m auto' switch.")
1423-
elif len(targets) == 0:
1424-
error("No targets were detected.\nPlease make sure a target board is connected to this system.")
1425-
else:
1426-
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1427-
target = targets[0]['name']
1418+
detected = self.detect_target()
1419+
if detected:
1420+
target = detected['name']
14281421

14291422
if target is None:
14301423
error("Please specify target using the -m switch or set default target using command 'mbed target'", 1)
@@ -1462,6 +1455,22 @@ def ignore_build_dir(self):
14621455
except IOError:
14631456
error("Unable to write build ignore file in \"%s\"" % os.path.join(build_path, '.mbedignore'), 1)
14641457

1458+
def detect_target(self, info=None):
1459+
targets = self.get_detected_targets()
1460+
if targets == False:
1461+
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)
1462+
elif len(targets) > 1:
1463+
error("Multiple targets were detected.\nOnly 1 target board should be connected to your system.", 1)
1464+
elif len(targets) == 0:
1465+
error("No targets were detected.\nPlease make sure a target board is connected to this system.", 1)
1466+
else:
1467+
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1468+
info = {'msd': targets[0]['mount'], 'port': targets[0]['serial'], 'name': targets[0]['name']}
1469+
1470+
if info is None:
1471+
error("The detected target doesn't support Mass Storage Device capability (MSD)", 1)
1472+
return info
1473+
14651474
def get_detected_targets(self):
14661475
targets = []
14671476
try:
@@ -2152,12 +2161,13 @@ def status_(ignore=False):
21522161
dict(name='--source', action='append', help='Source directory. Default: . (current dir)'),
21532162
dict(name='--build', help='Build directory. Default: build/'),
21542163
dict(name=['-c', '--clean'], action='store_true', help='Clean the build directory before compiling'),
2164+
dict(name=['-f', '--flash'], action='store_true', help='Flash the built firmware onto a connected target.'),
21552165
dict(name=['-N', '--artifact-name'], help='Name of the built program or library'),
21562166
dict(name=['-S', '--supported'], dest='supported', action='store_true', help='Shows supported matrix of targets and toolchains'),
21572167
dict(name='--app-config', dest="app_config", help="Path of an app configuration file (Default is to look for 'mbed_app.json')"),
21582168
help='Compile code using the mbed build tools',
21592169
description=("Compile this program using the mbed build tools."))
2160-
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):
2170+
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):
21612171
# Gather remaining arguments
21622172
args = remainder
21632173
# Find the root of the program
@@ -2236,6 +2246,25 @@ def compile_(toolchain=None, target=None, profile=False, compile_library=False,
22362246
+ (['-v'] if verbose else [])
22372247
+ args,
22382248
env=env)
2249+
2250+
if flash:
2251+
fw_name = artifact_name if artifact_name else program.name
2252+
fw_fbase = os.path.join(build_path, fw_name)
2253+
fw_file = fw_fbase + ('.hex' if os.path.exists(fw_fbase+'.hex') else '.bin')
2254+
if not os.path.exists(fw_file):
2255+
error("Build program file (firmware) not found \"%s\"" % fw_file, 1)
2256+
detected = program.detect_target()
2257+
2258+
try:
2259+
from mbed_host_tests.host_tests_toolbox import flash_dev, reset_dev
2260+
except (IOError, ImportError, OSError):
2261+
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)
2262+
2263+
if not flash_dev(detected['msd'], fw_file, program_cycle_s=2):
2264+
error("Unable to flash the target board connected to your system.", 1)
2265+
2266+
if not reset_dev(detected['port']):
2267+
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)
22392268

22402269
program.set_defaults(target=target, toolchain=tchain)
22412270

0 commit comments

Comments
 (0)