Skip to content

Commit a632631

Browse files
committed
Add initial support for target awareness in mbed CLI.
* `mbed compile -t TOOLCHAIN -m detect` or `mbed compile -t TOOLCHAIN -m auto` will compile for the detected connected target. * `mbed detect` will provide (limited) report for connected targets even executed outside mbed program or mbed-os codebase. Various checks are added: * multiple connected targets (will refuse to compile/export) * no connected targets * no mbed-ls support
1 parent 23afb59 commit a632631

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

mbed/mbed.py

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import shutil
2828
import stat
2929
import errno
30+
import ctypes
3031
from itertools import chain, izip, repeat
3132
from urlparse import urlparse
3233
import urllib2
@@ -1322,17 +1323,29 @@ def get_env(self):
13221323
return env
13231324

13241325
def get_target(self, target=None):
1325-
target_cfg = self.get_cfg('TARGET')
1326-
target = target if target else target_cfg
1326+
if target:
1327+
if target.lower() == 'detect' or target.lower() == 'auto':
1328+
targets = self.get_detected_targets()
1329+
if targets == False:
1330+
error("The target detection requires that the 'mbed-ls' python module is installed.")
1331+
elif len(targets) > 1:
1332+
error("Multiple targets were detected.\nOnly 1 target board should be connected to your system when you use the '-m auto' switch.")
1333+
elif len(targets) == 0:
1334+
error("No targets were detected.\nPlease make sure a target board to this system.")
1335+
else:
1336+
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (targets[0]['name'], targets[0]['mount'], targets[0]['serial']))
1337+
target = targets[0]['name']
1338+
else:
1339+
target = self.get_cfg('TARGET')
13271340
if target is None:
1328-
error('Please specify compile target using the -m switch or set default target using command "target"', 1)
1341+
error("Please specify target using the -m switch or set default target using command 'mbed target'", 1)
13291342
return target
13301343

13311344
def get_toolchain(self, toolchain=None):
13321345
toolchain_cfg = self.get_cfg('TOOLCHAIN')
13331346
tchain = toolchain if toolchain else toolchain_cfg
13341347
if tchain is None:
1335-
error('Please specify compile toolchain using the -t switch or set default toolchain using command "toolchain"', 1)
1348+
error("Please specify toolchain using the -t switch or set default toolchain using command 'mbed toolchain'", 1)
13361349
return tchain
13371350

13381351
def set_defaults(self, target=None, toolchain=None):
@@ -1359,6 +1372,28 @@ def ignore_build_dir(self):
13591372
except IOError:
13601373
error("Unable to write build ignore file in \"%s\"" % os.path.join(build_path, '.mbedignore'), 1)
13611374

1375+
def get_detected_targets(self):
1376+
targets = []
1377+
try:
1378+
import mbed_lstools
1379+
oldError = None
1380+
if os.name == 'nt':
1381+
oldError = ctypes.windll.kernel32.SetErrorMode(1) # Disable Windows error box temporarily. note that SEM_FAILCRITICALERRORS = 1
1382+
mbeds = mbed_lstools.create()
1383+
detect_muts_list = mbeds.list_mbeds()
1384+
if os.name == 'nt':
1385+
ctypes.windll.kernel32.SetErrorMode(oldError)
1386+
1387+
for mut in detect_muts_list:
1388+
targets.append({
1389+
'id': mut['target_id'], 'name': mut['platform_name'],
1390+
'mount': mut['mount_point'], 'serial': mut['serial_port']
1391+
})
1392+
except (IOError, ImportError, OSError):
1393+
return False
1394+
1395+
return targets
1396+
13621397

13631398
# Global class used for global config
13641399
class Global(object):
@@ -2261,18 +2296,25 @@ def detect():
22612296
# Gather remaining arguments
22622297
args = remainder
22632298
# Find the root of the program
2264-
program = Program(os.getcwd(), True)
2299+
program = Program(os.getcwd(), False)
22652300
program.check_requirements(True)
22662301
# Change directories to the program root to use mbed OS tools
22672302
with cd(program.path):
2268-
tools_dir = program.get_tools()
2303+
tools_dir = program.get_tools_dir()
22692304

2270-
# Prepare environment variables
2271-
env = program.get_env()
2305+
if tools_dir:
2306+
# Prepare environment variables
2307+
env = program.get_env()
22722308

2273-
popen(['python', '-u', os.path.join(tools_dir, 'detect_targets.py')]
2274-
+ args,
2275-
env=env)
2309+
popen(['python', '-u', os.path.join(tools_dir, 'detect_targets.py')]
2310+
+ args,
2311+
env=env)
2312+
else:
2313+
warning("The mbed tools were not found in \"%s\". \nLimited information will be shown about connected mbed targets/boards" % program.path)
2314+
targets = program.get_detected_targets()
2315+
if targets:
2316+
for target in targets:
2317+
action("Detected \"%s\" connected to \"%s\" and using com port \"%s\"" % (target['name'], target['mount'], target['serial']))
22762318

22772319

22782320
# Generic config command

0 commit comments

Comments
 (0)